blob: aa640a85bb2169aa14fb8d9875beb0ac9091192c [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);
354}
NeilBrown390ee602010-06-01 19:37:27 +1000355EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100356
NeilBrown3fa841d2009-09-23 18:10:29 +1000357int mddev_congested(mddev_t *mddev, int bits)
358{
359 return mddev->suspended;
360}
361EXPORT_SYMBOL(mddev_congested);
362
NeilBrowna2826aa2009-12-14 12:49:49 +1100363/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200364 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100365 */
366
Tejun Heoe9c74692010-09-03 11:56:18 +0200367static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100368{
369 mdk_rdev_t *rdev = bio->bi_private;
370 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100371
372 rdev_dec_pending(rdev, mddev);
373
374 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200375 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200376 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100377 }
378 bio_put(bio);
379}
380
NeilBrowna7a07e62010-12-09 16:04:25 +1100381static void md_submit_flush_data(struct work_struct *ws);
382
NeilBrowna035fc32010-12-09 16:17:51 +1100383static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100384{
NeilBrowna035fc32010-12-09 16:17:51 +1100385 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100386 mdk_rdev_t *rdev;
387
NeilBrowna7a07e62010-12-09 16:04:25 +1100388 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
389 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100390 rcu_read_lock();
391 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
392 if (rdev->raid_disk >= 0 &&
393 !test_bit(Faulty, &rdev->flags)) {
394 /* Take two references, one is dropped
395 * when request finishes, one after
396 * we reclaim rcu_read_lock
397 */
398 struct bio *bi;
399 atomic_inc(&rdev->nr_pending);
400 atomic_inc(&rdev->nr_pending);
401 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100402 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200403 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100404 bi->bi_private = rdev;
405 bi->bi_bdev = rdev->bdev;
406 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200407 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100408 rcu_read_lock();
409 rdev_dec_pending(rdev, mddev);
410 }
411 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100412 if (atomic_dec_and_test(&mddev->flush_pending))
413 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100414}
415
Tejun Heoe9c74692010-09-03 11:56:18 +0200416static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100417{
Tejun Heoe9c74692010-09-03 11:56:18 +0200418 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
419 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100420
Tejun Heoe9c74692010-09-03 11:56:18 +0200421 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100422 /* an empty barrier - all done */
423 bio_endio(bio, 0);
424 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200425 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100426 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100427 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100428 }
NeilBrown2b74e122010-12-09 15:59:01 +1100429
430 mddev->flush_bio = NULL;
431 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100432}
433
Tejun Heoe9c74692010-09-03 11:56:18 +0200434void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100435{
436 spin_lock_irq(&mddev->write_lock);
437 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200438 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100439 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200440 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100441 spin_unlock_irq(&mddev->write_lock);
442
NeilBrowna035fc32010-12-09 16:17:51 +1100443 INIT_WORK(&mddev->flush_work, submit_flushes);
444 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100445}
Tejun Heoe9c74692010-09-03 11:56:18 +0200446EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100447
NeilBrown97658cd2011-04-18 18:25:42 +1000448/* Support for plugging.
449 * This mirrors the plugging support in request_queue, but does not
450 * require having a whole queue or request structures.
451 * We allocate an md_plug_cb for each md device and each thread it gets
452 * plugged on. This links tot the private plug_handle structure in the
453 * personality data where we keep a count of the number of outstanding
454 * plugs so other code can see if a plug is active.
455 */
456struct md_plug_cb {
457 struct blk_plug_cb cb;
458 mddev_t *mddev;
459};
460
461static void plugger_unplug(struct blk_plug_cb *cb)
462{
463 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
464 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
465 md_wakeup_thread(mdcb->mddev->thread);
466 kfree(mdcb);
467}
468
469/* Check that an unplug wakeup will come shortly.
470 * If not, wakeup the md thread immediately
471 */
472int mddev_check_plugged(mddev_t *mddev)
473{
474 struct blk_plug *plug = current->plug;
475 struct md_plug_cb *mdcb;
476
477 if (!plug)
478 return 0;
479
480 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
481 if (mdcb->cb.callback == plugger_unplug &&
482 mdcb->mddev == mddev) {
483 /* Already on the list, move to top */
484 if (mdcb != list_first_entry(&plug->cb_list,
485 struct md_plug_cb,
486 cb.list))
487 list_move(&mdcb->cb.list, &plug->cb_list);
488 return 1;
489 }
490 }
491 /* Not currently on the callback list */
492 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
493 if (!mdcb)
494 return 0;
495
496 mdcb->mddev = mddev;
497 mdcb->cb.callback = plugger_unplug;
498 atomic_inc(&mddev->plug_cnt);
499 list_add(&mdcb->cb.list, &plug->cb_list);
500 return 1;
501}
502EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static inline mddev_t *mddev_get(mddev_t *mddev)
505{
506 atomic_inc(&mddev->active);
507 return mddev;
508}
509
Dan Williams5fd3a172009-03-04 00:57:25 -0700510static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static void mddev_put(mddev_t *mddev)
513{
NeilBrowna167f662010-10-26 18:31:13 +1100514 struct bio_set *bs = NULL;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
517 return;
NeilBrownd3374822009-01-09 08:31:10 +1100518 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100519 mddev->ctime == 0 && !mddev->hold_active) {
520 /* Array is not configured at all, and not held active,
521 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100523 bs = mddev->bio_set;
524 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100525 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200526 /* We did a probe so need to clean up. Call
527 * queue_work inside the spinlock so that
528 * flush_workqueue() after mddev_find will
529 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100530 */
531 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200532 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100533 } else
534 kfree(mddev);
535 }
536 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100537 if (bs)
538 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
NeilBrown390ee602010-06-01 19:37:27 +1000541void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100542{
543 mutex_init(&mddev->open_mutex);
544 mutex_init(&mddev->reconfig_mutex);
545 mutex_init(&mddev->bitmap_info.mutex);
546 INIT_LIST_HEAD(&mddev->disks);
547 INIT_LIST_HEAD(&mddev->all_mddevs);
548 init_timer(&mddev->safemode_timer);
549 atomic_set(&mddev->active, 1);
550 atomic_set(&mddev->openers, 0);
551 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000552 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100553 spin_lock_init(&mddev->write_lock);
554 atomic_set(&mddev->flush_pending, 0);
555 init_waitqueue_head(&mddev->sb_wait);
556 init_waitqueue_head(&mddev->recovery_wait);
557 mddev->reshape_position = MaxSector;
558 mddev->resync_min = 0;
559 mddev->resync_max = MaxSector;
560 mddev->level = LEVEL_NONE;
561}
NeilBrown390ee602010-06-01 19:37:27 +1000562EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static mddev_t * mddev_find(dev_t unit)
565{
566 mddev_t *mddev, *new = NULL;
567
NeilBrown8f5f02c2011-02-16 13:58:51 +1100568 if (unit && MAJOR(unit) != MD_MAJOR)
569 unit &= ~((1<<MdpMinorShift)-1);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 retry:
572 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
NeilBrownefeb53c2009-01-09 08:31:10 +1100574 if (unit) {
575 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
576 if (mddev->unit == unit) {
577 mddev_get(mddev);
578 spin_unlock(&all_mddevs_lock);
579 kfree(new);
580 return mddev;
581 }
582
583 if (new) {
584 list_add(&new->all_mddevs, &all_mddevs);
585 spin_unlock(&all_mddevs_lock);
586 new->hold_active = UNTIL_IOCTL;
587 return new;
588 }
589 } else if (new) {
590 /* find an unused unit number */
591 static int next_minor = 512;
592 int start = next_minor;
593 int is_free = 0;
594 int dev = 0;
595 while (!is_free) {
596 dev = MKDEV(MD_MAJOR, next_minor);
597 next_minor++;
598 if (next_minor > MINORMASK)
599 next_minor = 0;
600 if (next_minor == start) {
601 /* Oh dear, all in use. */
602 spin_unlock(&all_mddevs_lock);
603 kfree(new);
604 return NULL;
605 }
606
607 is_free = 1;
608 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
609 if (mddev->unit == dev) {
610 is_free = 0;
611 break;
612 }
613 }
614 new->unit = dev;
615 new->md_minor = MINOR(dev);
616 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 list_add(&new->all_mddevs, &all_mddevs);
618 spin_unlock(&all_mddevs_lock);
619 return new;
620 }
621 spin_unlock(&all_mddevs_lock);
622
NeilBrown9ffae0c2006-01-06 00:20:32 -0800623 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!new)
625 return NULL;
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 new->unit = unit;
628 if (MAJOR(unit) == MD_MAJOR)
629 new->md_minor = MINOR(unit);
630 else
631 new->md_minor = MINOR(unit) >> MdpMinorShift;
632
NeilBrownfafd7fb2010-04-01 15:55:30 +1100633 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto retry;
636}
637
638static inline int mddev_lock(mddev_t * mddev)
639{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800640 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Dan Williamsb522adc2009-03-31 15:00:31 +1100643static inline int mddev_is_locked(mddev_t *mddev)
644{
645 return mutex_is_locked(&mddev->reconfig_mutex);
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static inline int mddev_trylock(mddev_t * mddev)
649{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800650 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
NeilBrownb6eb1272010-04-15 10:13:47 +1000653static struct attribute_group md_redundancy_group;
654
NeilBrowna64c8762010-04-14 17:15:37 +1000655static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
NeilBrowna64c8762010-04-14 17:15:37 +1000657 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000658 /* These cannot be removed under reconfig_mutex as
659 * an access to the files will try to take reconfig_mutex
660 * while holding the file unremovable, which leads to
661 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000662 * So hold set sysfs_active while the remove in happeing,
663 * and anything else which might set ->to_remove or my
664 * otherwise change the sysfs namespace will fail with
665 * -EBUSY if sysfs_active is still set.
666 * We set sysfs_active under reconfig_mutex and elsewhere
667 * test it under the same mutex to ensure its correct value
668 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000669 */
NeilBrowna64c8762010-04-14 17:15:37 +1000670 struct attribute_group *to_remove = mddev->to_remove;
671 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000672 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000673 mutex_unlock(&mddev->reconfig_mutex);
674
NeilBrown00bcb4a2010-06-01 19:37:23 +1000675 if (mddev->kobj.sd) {
676 if (to_remove != &md_redundancy_group)
677 sysfs_remove_group(&mddev->kobj, to_remove);
678 if (mddev->pers == NULL ||
679 mddev->pers->sync_request == NULL) {
680 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
681 if (mddev->sysfs_action)
682 sysfs_put(mddev->sysfs_action);
683 mddev->sysfs_action = NULL;
684 }
NeilBrowna64c8762010-04-14 17:15:37 +1000685 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000686 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000687 } else
688 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
NeilBrown005eca52005-08-22 13:11:08 -0700690 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
NeilBrown2989ddb2006-01-06 00:20:43 -0800693static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100695 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100697 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (rdev->desc_nr == nr)
699 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return NULL;
702}
703
704static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
705{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 mdk_rdev_t *rdev;
707
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100708 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (rdev->bdev->bd_dev == dev)
710 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return NULL;
713}
714
NeilBrownd9d166c2006-01-06 00:20:51 -0800715static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800716{
717 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800718 list_for_each_entry(pers, &pers_list, list) {
719 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800720 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800721 if (strcmp(pers->name, clevel)==0)
722 return pers;
723 }
NeilBrown2604b702006-01-06 00:20:36 -0800724 return NULL;
725}
726
Andre Nollb73df2d2008-07-11 22:02:23 +1000727/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100728static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100730 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000731 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734static int alloc_disk_sb(mdk_rdev_t * rdev)
735{
736 if (rdev->sb_page)
737 MD_BUG();
738
739 rdev->sb_page = alloc_page(GFP_KERNEL);
740 if (!rdev->sb_page) {
741 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000742 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
745 return 0;
746}
747
748static void free_disk_sb(mdk_rdev_t * rdev)
749{
750 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800751 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 rdev->sb_loaded = 0;
753 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000754 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100755 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757}
758
759
NeilBrown6712ecf2007-09-27 12:47:43 +0200760static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700761{
762 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800763 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700764
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700765 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
766 printk("md: super_written gets error=%d, uptodate=%d\n",
767 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
768 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800769 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700770 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700771
NeilBrowna9701a32005-11-08 21:39:34 -0800772 if (atomic_dec_and_test(&mddev->pending_writes))
773 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700774 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700775}
776
777void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
778 sector_t sector, int size, struct page *page)
779{
780 /* write first size bytes of page to sector of rdev
781 * Increment mddev->pending_writes before returning
782 * and decrement it on completion, waking up sb_wait
783 * if zero is reached.
784 * If an error occurred, call md_error
785 */
NeilBrowna167f662010-10-26 18:31:13 +1100786 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700787
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100788 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700789 bio->bi_sector = sector;
790 bio_add_page(bio, page, size, 0);
791 bio->bi_private = rdev;
792 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800793
NeilBrown7bfa19f2005-06-21 17:17:28 -0700794 atomic_inc(&mddev->pending_writes);
Jens Axboe721a9602011-03-09 11:56:30 +0100795 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800796}
797
798void md_super_wait(mddev_t *mddev)
799{
Tejun Heoe9c74692010-09-03 11:56:18 +0200800 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800801 DEFINE_WAIT(wq);
802 for(;;) {
803 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
804 if (atomic_read(&mddev->pending_writes)==0)
805 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800806 schedule();
807 }
808 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700809}
810
NeilBrown6712ecf2007-09-27 12:47:43 +0200811static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
NeilBrown2b193362010-10-27 15:16:40 +1100816int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100817 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
NeilBrowna167f662010-10-26 18:31:13 +1100819 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct completion event;
821 int ret;
822
Jens Axboe721a9602011-03-09 11:56:30 +0100823 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100825 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
826 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100827 if (metadata_op)
828 bio->bi_sector = sector + rdev->sb_start;
829 else
830 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 bio_add_page(bio, page, size, 0);
832 init_completion(&event);
833 bio->bi_private = &event;
834 bio->bi_end_io = bi_complete;
835 submit_bio(rw, bio);
836 wait_for_completion(&event);
837
838 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
839 bio_put(bio);
840 return ret;
841}
NeilBrowna8745db2006-01-06 00:20:34 -0800842EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
NeilBrown0002b272005-09-09 16:23:53 -0700844static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 char b[BDEVNAME_SIZE];
847 if (!rdev->sb_page) {
848 MD_BUG();
849 return -EINVAL;
850 }
851 if (rdev->sb_loaded)
852 return 0;
853
854
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100855 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto fail;
857 rdev->sb_loaded = 1;
858 return 0;
859
860fail:
861 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
862 bdevname(rdev->bdev,b));
863 return -EINVAL;
864}
865
866static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
867{
Andre Noll05710462008-07-11 22:02:20 +1000868 return sb1->set_uuid0 == sb2->set_uuid0 &&
869 sb1->set_uuid1 == sb2->set_uuid1 &&
870 sb1->set_uuid2 == sb2->set_uuid2 &&
871 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
875{
876 int ret;
877 mdp_super_t *tmp1, *tmp2;
878
879 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
880 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
881
882 if (!tmp1 || !tmp2) {
883 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100884 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto abort;
886 }
887
888 *tmp1 = *sb1;
889 *tmp2 = *sb2;
890
891 /*
892 * nr_disks is not constant
893 */
894 tmp1->nr_disks = 0;
895 tmp2->nr_disks = 0;
896
Andre Nollce0c8e02008-07-11 22:02:20 +1000897 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700899 kfree(tmp1);
900 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return ret;
902}
903
NeilBrown4d167f02007-05-09 02:35:37 -0700904
905static u32 md_csum_fold(u32 csum)
906{
907 csum = (csum & 0xffff) + (csum >> 16);
908 return (csum & 0xffff) + (csum >> 16);
909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static unsigned int calc_sb_csum(mdp_super_t * sb)
912{
NeilBrown4d167f02007-05-09 02:35:37 -0700913 u64 newcsum = 0;
914 u32 *sb32 = (u32*)sb;
915 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 unsigned int disk_csum, csum;
917
918 disk_csum = sb->sb_csum;
919 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700920
921 for (i = 0; i < MD_SB_BYTES/4 ; i++)
922 newcsum += sb32[i];
923 csum = (newcsum & 0xffffffff) + (newcsum>>32);
924
925
926#ifdef CONFIG_ALPHA
927 /* This used to use csum_partial, which was wrong for several
928 * reasons including that different results are returned on
929 * different architectures. It isn't critical that we get exactly
930 * the same return value as before (we always csum_fold before
931 * testing, and that removes any differences). However as we
932 * know that csum_partial always returned a 16bit value on
933 * alphas, do a fold to maximise conformity to previous behaviour.
934 */
935 sb->sb_csum = md_csum_fold(disk_csum);
936#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700938#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return csum;
940}
941
942
943/*
944 * Handle superblock details.
945 * We want to be able to handle multiple superblock formats
946 * so we have a common interface to them all, and an array of
947 * different handlers.
948 * We rely on user-space to write the initial superblock, and support
949 * reading and updating of superblocks.
950 * Interface methods are:
951 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
952 * loads and validates a superblock on dev.
953 * if refdev != NULL, compare superblocks on both devices
954 * Return:
955 * 0 - dev has a superblock that is compatible with refdev
956 * 1 - dev has a superblock that is compatible and newer than refdev
957 * so dev should be used as the refdev in future
958 * -EINVAL superblock incompatible or invalid
959 * -othererror e.g. -EIO
960 *
961 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
962 * Verify that dev is acceptable into mddev.
963 * The first time, mddev->raid_disks will be 0, and data from
964 * dev should be merged in. Subsequent calls check that dev
965 * is new enough. Return 0 or -EINVAL
966 *
967 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
968 * Update the superblock for rdev with data in mddev
969 * This does not write to disc.
970 *
971 */
972
973struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000974 char *name;
975 struct module *owner;
976 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
977 int minor_version);
978 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
979 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
980 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000981 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982};
983
984/*
Andre Noll0894cc32009-06-18 08:49:23 +1000985 * Check that the given mddev has no bitmap.
986 *
987 * This function is called from the run method of all personalities that do not
988 * support bitmaps. It prints an error message and returns non-zero if mddev
989 * has a bitmap. Otherwise, it returns 0.
990 *
991 */
992int md_check_no_bitmap(mddev_t *mddev)
993{
NeilBrownc3d97142009-12-14 12:49:52 +1100994 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000995 return 0;
996 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
997 mdname(mddev), mddev->pers->name);
998 return 1;
999}
1000EXPORT_SYMBOL(md_check_no_bitmap);
1001
1002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 * load_super for 0.90.0
1004 */
1005static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1006{
1007 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1008 mdp_super_t *sb;
1009 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /*
Andre Noll0f420352008-07-11 22:02:23 +10001012 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 * it's at the end of the disk.
1014 *
1015 * It also happens to be a multiple of 4Kb.
1016 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001017 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
NeilBrown0002b272005-09-09 16:23:53 -07001019 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (ret) return ret;
1021
1022 ret = -EINVAL;
1023
1024 bdevname(rdev->bdev, b);
1025 sb = (mdp_super_t*)page_address(rdev->sb_page);
1026
1027 if (sb->md_magic != MD_SB_MAGIC) {
1028 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1029 b);
1030 goto abort;
1031 }
1032
1033 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001034 sb->minor_version < 90 ||
1035 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1037 sb->major_version, sb->minor_version,
1038 b);
1039 goto abort;
1040 }
1041
1042 if (sb->raid_disks <= 0)
1043 goto abort;
1044
NeilBrown4d167f02007-05-09 02:35:37 -07001045 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1047 b);
1048 goto abort;
1049 }
1050
1051 rdev->preferred_minor = sb->md_minor;
1052 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001053 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (sb->level == LEVEL_MULTIPATH)
1056 rdev->desc_nr = -1;
1057 else
1058 rdev->desc_nr = sb->this_disk.number;
1059
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001060 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001062 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 __u64 ev1, ev2;
1064 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1065 if (!uuid_equal(refsb, sb)) {
1066 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1067 b, bdevname(refdev->bdev,b2));
1068 goto abort;
1069 }
1070 if (!sb_equal(refsb, sb)) {
1071 printk(KERN_WARNING "md: %s has same UUID"
1072 " but different superblock to %s\n",
1073 b, bdevname(refdev->bdev, b2));
1074 goto abort;
1075 }
1076 ev1 = md_event(sb);
1077 ev2 = md_event(refsb);
1078 if (ev1 > ev2)
1079 ret = 1;
1080 else
1081 ret = 0;
1082 }
NeilBrown8190e752009-06-18 08:48:58 +10001083 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Andre Nolldd8ac332009-03-31 14:33:13 +11001085 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001086 /* "this cannot possibly happen" ... */
1087 ret = -EINVAL;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 abort:
1090 return ret;
1091}
1092
1093/*
1094 * validate_super for 0.90.0
1095 */
1096static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1097{
1098 mdp_disk_t *desc;
1099 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001100 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
NeilBrown41158c72005-06-21 17:17:25 -07001102 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001103 clear_bit(Faulty, &rdev->flags);
1104 clear_bit(In_sync, &rdev->flags);
1105 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (mddev->raid_disks == 0) {
1108 mddev->major_version = 0;
1109 mddev->minor_version = sb->minor_version;
1110 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001111 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001112 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 mddev->ctime = sb->ctime;
1114 mddev->utime = sb->utime;
1115 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001116 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 mddev->layout = sb->layout;
1118 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001119 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001120 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001121 mddev->bitmap_info.offset = 0;
1122 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
NeilBrownf6705572006-03-27 01:18:11 -08001124 if (mddev->minor_version >= 91) {
1125 mddev->reshape_position = sb->reshape_position;
1126 mddev->delta_disks = sb->delta_disks;
1127 mddev->new_level = sb->new_level;
1128 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001129 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001130 } else {
1131 mddev->reshape_position = MaxSector;
1132 mddev->delta_disks = 0;
1133 mddev->new_level = mddev->level;
1134 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001135 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001136 }
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (sb->state & (1<<MD_SB_CLEAN))
1139 mddev->recovery_cp = MaxSector;
1140 else {
1141 if (sb->events_hi == sb->cp_events_hi &&
1142 sb->events_lo == sb->cp_events_lo) {
1143 mddev->recovery_cp = sb->recovery_cp;
1144 } else
1145 mddev->recovery_cp = 0;
1146 }
1147
1148 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1149 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1150 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1151 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1152
1153 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001154
1155 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001156 mddev->bitmap_info.file == NULL)
1157 mddev->bitmap_info.offset =
1158 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001159
NeilBrown41158c72005-06-21 17:17:25 -07001160 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001161 /* Insist on good event counter while assembling, except
1162 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001164 if (sb->disks[rdev->desc_nr].state & (
1165 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1166 if (ev1 < mddev->events)
1167 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001168 } else if (mddev->bitmap) {
1169 /* if adding to array with a bitmap, then we can accept an
1170 * older device ... but not too old.
1171 */
NeilBrown41158c72005-06-21 17:17:25 -07001172 if (ev1 < mddev->bitmap->events_cleared)
1173 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001174 } else {
1175 if (ev1 < mddev->events)
1176 /* just a hot-add of a new device, leave raid_disk at -1 */
1177 return 0;
1178 }
NeilBrown41158c72005-06-21 17:17:25 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 desc = sb->disks + rdev->desc_nr;
1182
1183 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001184 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001185 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1186 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001187 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001189 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1190 /* active but not in sync implies recovery up to
1191 * reshape position. We don't know exactly where
1192 * that is, so set to zero for now */
1193 if (mddev->minor_version >= 91) {
1194 rdev->recovery_offset = 0;
1195 rdev->raid_disk = desc->raid_disk;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001198 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1199 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001200 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001201 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203}
1204
1205/*
1206 * sync_super for 0.90.0
1207 */
1208static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1209{
1210 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 mdk_rdev_t *rdev2;
1212 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* make rdev->sb match mddev data..
1216 *
1217 * 1/ zero out disks
1218 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1219 * 3/ any empty disks < next_spare become removed
1220 *
1221 * disks[0] gets initialised to REMOVED because
1222 * we cannot be sure from other fields if it has
1223 * been initialised or not.
1224 */
1225 int i;
1226 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1227
NeilBrown61181562005-09-09 16:24:02 -07001228 rdev->sb_size = MD_SB_BYTES;
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 sb = (mdp_super_t*)page_address(rdev->sb_page);
1231
1232 memset(sb, 0, sizeof(*sb));
1233
1234 sb->md_magic = MD_SB_MAGIC;
1235 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 sb->patch_version = mddev->patch_version;
1237 sb->gvalid_words = 0; /* ignored */
1238 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1239 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1240 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1241 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1242
1243 sb->ctime = mddev->ctime;
1244 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001245 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 sb->raid_disks = mddev->raid_disks;
1247 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001248 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->utime = mddev->utime;
1250 sb->state = 0;
1251 sb->events_hi = (mddev->events>>32);
1252 sb->events_lo = (u32)mddev->events;
1253
NeilBrownf6705572006-03-27 01:18:11 -08001254 if (mddev->reshape_position == MaxSector)
1255 sb->minor_version = 90;
1256 else {
1257 sb->minor_version = 91;
1258 sb->reshape_position = mddev->reshape_position;
1259 sb->new_level = mddev->new_level;
1260 sb->delta_disks = mddev->delta_disks;
1261 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001262 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001263 }
1264 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (mddev->in_sync)
1266 {
1267 sb->recovery_cp = mddev->recovery_cp;
1268 sb->cp_events_hi = (mddev->events>>32);
1269 sb->cp_events_lo = (u32)mddev->events;
1270 if (mddev->recovery_cp == MaxSector)
1271 sb->state = (1<< MD_SB_CLEAN);
1272 } else
1273 sb->recovery_cp = 0;
1274
1275 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001276 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
NeilBrownc3d97142009-12-14 12:49:52 +11001278 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001279 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001282 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001284 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001285 int is_active = test_bit(In_sync, &rdev2->flags);
1286
1287 if (rdev2->raid_disk >= 0 &&
1288 sb->minor_version >= 91)
1289 /* we have nowhere to store the recovery_offset,
1290 * but if it is not below the reshape_position,
1291 * we can piggy-back on that.
1292 */
1293 is_active = 1;
1294 if (rdev2->raid_disk < 0 ||
1295 test_bit(Faulty, &rdev2->flags))
1296 is_active = 0;
1297 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001298 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001300 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001301 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 d = &sb->disks[rdev2->desc_nr];
1303 nr_disks++;
1304 d->number = rdev2->desc_nr;
1305 d->major = MAJOR(rdev2->bdev->bd_dev);
1306 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001307 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 d->raid_disk = rdev2->raid_disk;
1309 else
1310 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001311 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001313 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001315 if (test_bit(In_sync, &rdev2->flags))
1316 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 active++;
1318 working++;
1319 } else {
1320 d->state = 0;
1321 spare++;
1322 working++;
1323 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001324 if (test_bit(WriteMostly, &rdev2->flags))
1325 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* now set the "removed" and "faulty" bits on any missing devices */
1328 for (i=0 ; i < mddev->raid_disks ; i++) {
1329 mdp_disk_t *d = &sb->disks[i];
1330 if (d->state == 0 && d->number == 0) {
1331 d->number = i;
1332 d->raid_disk = i;
1333 d->state = (1<<MD_DISK_REMOVED);
1334 d->state |= (1<<MD_DISK_FAULTY);
1335 failed++;
1336 }
1337 }
1338 sb->nr_disks = nr_disks;
1339 sb->active_disks = active;
1340 sb->working_disks = working;
1341 sb->failed_disks = failed;
1342 sb->spare_disks = spare;
1343
1344 sb->this_disk = sb->disks[rdev->desc_nr];
1345 sb->sb_csum = calc_sb_csum(sb);
1346}
1347
1348/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001349 * rdev_size_change for 0.90.0
1350 */
1351static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001352super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001353{
Andre Noll58c0fed2009-03-31 14:33:13 +11001354 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001355 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001356 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001357 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001358 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001359 if (!num_sectors || num_sectors > rdev->sb_start)
1360 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001361 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001362 rdev->sb_page);
1363 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001364 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365}
1366
1367
1368/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * version 1 superblock
1370 */
1371
NeilBrown1c05b4b2006-10-21 10:24:08 -07001372static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001374 __le32 disk_csum;
1375 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 unsigned long long newcsum;
1377 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001378 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int i;
1380
1381 disk_csum = sb->sb_csum;
1382 sb->sb_csum = 0;
1383 newcsum = 0;
1384 for (i=0; size>=4; size -= 4 )
1385 newcsum += le32_to_cpu(*isuper++);
1386
1387 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001388 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1391 sb->sb_csum = disk_csum;
1392 return cpu_to_le32(csum);
1393}
1394
1395static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1396{
1397 struct mdp_superblock_1 *sb;
1398 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001399 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001401 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /*
Andre Noll0f420352008-07-11 22:02:23 +10001404 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 * It is always aligned to a 4K boundary and
1406 * depeding on minor_version, it can be:
1407 * 0: At least 8K, but less than 12K, from end of device
1408 * 1: At start of device
1409 * 2: 4K from start of device.
1410 */
1411 switch(minor_version) {
1412 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001413 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001414 sb_start -= 8*2;
1415 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 break;
1417 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001418 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001421 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 default:
1424 return -EINVAL;
1425 }
Andre Noll0f420352008-07-11 22:02:23 +10001426 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
NeilBrown0002b272005-09-09 16:23:53 -07001428 /* superblock is rarely larger than 1K, but it can be larger,
1429 * and it is safe to read 4k, so we do that
1430 */
1431 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (ret) return ret;
1433
1434
1435 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1436
1437 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1438 sb->major_version != cpu_to_le32(1) ||
1439 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001440 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001441 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return -EINVAL;
1443
1444 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1445 printk("md: invalid superblock checksum on %s\n",
1446 bdevname(rdev->bdev,b));
1447 return -EINVAL;
1448 }
1449 if (le64_to_cpu(sb->data_size) < 10) {
1450 printk("md: data_size too small on %s\n",
1451 bdevname(rdev->bdev,b));
1452 return -EINVAL;
1453 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rdev->preferred_minor = 0xffff;
1456 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001457 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
NeilBrown0002b272005-09-09 16:23:53 -07001459 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001460 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001461 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001462 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1463
1464 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001465 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001466 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001467
NeilBrown31b65a02006-07-10 04:44:14 -07001468 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1469 rdev->desc_nr = -1;
1470 else
1471 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1472
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001473 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001474 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001475 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 __u64 ev1, ev2;
1477 struct mdp_superblock_1 *refsb =
1478 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1479
1480 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1481 sb->level != refsb->level ||
1482 sb->layout != refsb->layout ||
1483 sb->chunksize != refsb->chunksize) {
1484 printk(KERN_WARNING "md: %s has strangely different"
1485 " superblock to %s\n",
1486 bdevname(rdev->bdev,b),
1487 bdevname(refdev->bdev,b2));
1488 return -EINVAL;
1489 }
1490 ev1 = le64_to_cpu(sb->events);
1491 ev2 = le64_to_cpu(refsb->events);
1492
1493 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001494 ret = 1;
1495 else
1496 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
NeilBrowna1801f82008-03-04 14:29:31 -08001498 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001499 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001500 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001502 rdev->sectors = rdev->sb_start;
1503 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001505 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001506 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001507 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1512{
1513 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001514 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
NeilBrown41158c72005-06-21 17:17:25 -07001516 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001517 clear_bit(Faulty, &rdev->flags);
1518 clear_bit(In_sync, &rdev->flags);
1519 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (mddev->raid_disks == 0) {
1522 mddev->major_version = 1;
1523 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001524 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001525 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1527 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1528 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001529 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 mddev->layout = le32_to_cpu(sb->layout);
1531 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001532 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001533 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001534 mddev->bitmap_info.offset = 0;
1535 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1538 memcpy(mddev->uuid, sb->set_uuid, 16);
1539
1540 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001541
NeilBrown71c08052005-09-09 16:23:51 -07001542 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001543 mddev->bitmap_info.file == NULL )
1544 mddev->bitmap_info.offset =
1545 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001546
NeilBrownf6705572006-03-27 01:18:11 -08001547 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1548 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1549 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1550 mddev->new_level = le32_to_cpu(sb->new_level);
1551 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001552 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001553 } else {
1554 mddev->reshape_position = MaxSector;
1555 mddev->delta_disks = 0;
1556 mddev->new_level = mddev->level;
1557 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001558 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001559 }
1560
NeilBrown41158c72005-06-21 17:17:25 -07001561 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001562 /* Insist of good event counter while assembling, except for
1563 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001565 if (rdev->desc_nr >= 0 &&
1566 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1567 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1568 if (ev1 < mddev->events)
1569 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001570 } else if (mddev->bitmap) {
1571 /* If adding to array with a bitmap, then we can accept an
1572 * older device, but not too old.
1573 */
NeilBrown41158c72005-06-21 17:17:25 -07001574 if (ev1 < mddev->bitmap->events_cleared)
1575 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001576 } else {
1577 if (ev1 < mddev->events)
1578 /* just a hot-add of a new device, leave raid_disk at -1 */
1579 return 0;
1580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (mddev->level != LEVEL_MULTIPATH) {
1582 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001583 if (rdev->desc_nr < 0 ||
1584 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1585 role = 0xffff;
1586 rdev->desc_nr = -1;
1587 } else
1588 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 switch(role) {
1590 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 break;
1592 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001593 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
1595 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001596 if ((le32_to_cpu(sb->feature_map) &
1597 MD_FEATURE_RECOVERY_OFFSET))
1598 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1599 else
1600 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 rdev->raid_disk = role;
1602 break;
1603 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001604 if (sb->devflags & WriteMostly1)
1605 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001606 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001607 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return 0;
1610}
1611
1612static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1613{
1614 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 mdk_rdev_t *rdev2;
1616 int max_dev, i;
1617 /* make rdev->sb match mddev and rdev data. */
1618
1619 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1620
1621 sb->feature_map = 0;
1622 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001623 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 memset(sb->pad1, 0, sizeof(sb->pad1));
1625 memset(sb->pad2, 0, sizeof(sb->pad2));
1626 memset(sb->pad3, 0, sizeof(sb->pad3));
1627
1628 sb->utime = cpu_to_le64((__u64)mddev->utime);
1629 sb->events = cpu_to_le64(mddev->events);
1630 if (mddev->in_sync)
1631 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1632 else
1633 sb->resync_offset = cpu_to_le64(0);
1634
NeilBrown1c05b4b2006-10-21 10:24:08 -07001635 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001636
NeilBrownf0ca3402006-02-02 14:28:04 -08001637 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001638 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001639 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001640 sb->level = cpu_to_le32(mddev->level);
1641 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001642
NeilBrownc3d97142009-12-14 12:49:52 +11001643 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1644 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001645 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001646 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001647
1648 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001649 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001650 sb->feature_map |=
1651 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1652 sb->recovery_offset =
1653 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001654 }
1655
NeilBrownf6705572006-03-27 01:18:11 -08001656 if (mddev->reshape_position != MaxSector) {
1657 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1658 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1659 sb->new_layout = cpu_to_le32(mddev->new_layout);
1660 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1661 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001662 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001663 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001666 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (rdev2->desc_nr+1 > max_dev)
1668 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001669
NeilBrown70471da2009-08-03 10:59:57 +10001670 if (max_dev > le32_to_cpu(sb->max_dev)) {
1671 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001672 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001673 rdev->sb_size = max_dev * 2 + 256;
1674 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1675 if (rdev->sb_size & bmask)
1676 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001677 } else
1678 max_dev = le32_to_cpu(sb->max_dev);
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 for (i=0; i<max_dev;i++)
1681 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1682
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001683 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001685 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001687 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001689 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001690 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 else
1692 sb->dev_roles[i] = cpu_to_le16(0xffff);
1693 }
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 sb->sb_csum = calc_sb_1_csum(sb);
1696}
1697
Chris Webb0cd17fe2008-06-28 08:31:46 +10001698static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001699super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001700{
1701 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001702 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001703 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001704 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001705 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001706 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001707 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001708 max_sectors -= rdev->data_offset;
1709 if (!num_sectors || num_sectors > max_sectors)
1710 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001711 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001712 /* minor version 0 with bitmap we can't move */
1713 return 0;
1714 } else {
1715 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001716 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001717 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001718 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001719 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001720 if (!num_sectors || num_sectors > max_sectors)
1721 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001722 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001723 }
1724 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001725 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001726 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001727 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001728 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001729 rdev->sb_page);
1730 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001731 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Adrian Bunk75c96f82005-05-05 16:16:09 -07001734static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 [0] = {
1736 .name = "0.90.0",
1737 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001738 .load_super = super_90_load,
1739 .validate_super = super_90_validate,
1740 .sync_super = super_90_sync,
1741 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 },
1743 [1] = {
1744 .name = "md-1",
1745 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001746 .load_super = super_1_load,
1747 .validate_super = super_1_validate,
1748 .sync_super = super_1_sync,
1749 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 },
1751};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1754{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001755 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
NeilBrown4b809912008-07-21 17:05:25 +10001757 rcu_read_lock();
1758 rdev_for_each_rcu(rdev, mddev1)
1759 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001760 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001761 rdev2->bdev->bd_contains) {
1762 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001763 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001764 }
1765 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return 0;
1767}
1768
1769static LIST_HEAD(pending_raid_disks);
1770
Andre Nollac5e7112009-08-03 10:59:47 +10001771/*
1772 * Try to register data integrity profile for an mddev
1773 *
1774 * This is called when an array is started and after a disk has been kicked
1775 * from the array. It only succeeds if all working and active component devices
1776 * are integrity capable with matching profiles.
1777 */
1778int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001779{
Andre Nollac5e7112009-08-03 10:59:47 +10001780 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001781
Andre Nollac5e7112009-08-03 10:59:47 +10001782 if (list_empty(&mddev->disks))
1783 return 0; /* nothing to do */
1784 if (blk_get_integrity(mddev->gendisk))
1785 return 0; /* already registered */
1786 list_for_each_entry(rdev, &mddev->disks, same_set) {
1787 /* skip spares and non-functional disks */
1788 if (test_bit(Faulty, &rdev->flags))
1789 continue;
1790 if (rdev->raid_disk < 0)
1791 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001792 if (!reference) {
1793 /* Use the first rdev as the reference */
1794 reference = rdev;
1795 continue;
1796 }
1797 /* does this rdev's profile match the reference profile? */
1798 if (blk_integrity_compare(reference->bdev->bd_disk,
1799 rdev->bdev->bd_disk) < 0)
1800 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001801 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001802 if (!reference || !bdev_get_integrity(reference->bdev))
1803 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001804 /*
1805 * All component devices are integrity capable and have matching
1806 * profiles, register the common profile for the md device.
1807 */
1808 if (blk_integrity_register(mddev->gendisk,
1809 bdev_get_integrity(reference->bdev)) != 0) {
1810 printk(KERN_ERR "md: failed to register integrity for %s\n",
1811 mdname(mddev));
1812 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001813 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001814 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1815 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1816 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1817 mdname(mddev));
1818 return -EINVAL;
1819 }
Andre Nollac5e7112009-08-03 10:59:47 +10001820 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001821}
Andre Nollac5e7112009-08-03 10:59:47 +10001822EXPORT_SYMBOL(md_integrity_register);
1823
1824/* Disable data integrity if non-capable/non-matching disk is being added */
1825void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1826{
1827 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1828 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1829
1830 if (!bi_mddev) /* nothing to do */
1831 return;
1832 if (rdev->raid_disk < 0) /* skip spares */
1833 return;
1834 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1835 rdev->bdev->bd_disk) >= 0)
1836 return;
1837 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1838 blk_integrity_unregister(mddev->gendisk);
1839}
1840EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1843{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001844 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001845 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001846 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001847 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 if (rdev->mddev) {
1850 MD_BUG();
1851 return -EINVAL;
1852 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001853
1854 /* prevent duplicates */
1855 if (find_rdev(mddev, rdev->bdev->bd_dev))
1856 return -EEXIST;
1857
Andre Nolldd8ac332009-03-31 14:33:13 +11001858 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1859 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1860 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001861 if (mddev->pers) {
1862 /* Cannot change size, so fail
1863 * If mddev->level <= 0, then we don't care
1864 * about aligning sizes (e.g. linear)
1865 */
1866 if (mddev->level > 0)
1867 return -ENOSPC;
1868 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001869 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 /* Verify rdev->desc_nr is unique.
1873 * If it is -1, assign a free number, else
1874 * check number is not in use
1875 */
1876 if (rdev->desc_nr < 0) {
1877 int choice = 0;
1878 if (mddev->pers) choice = mddev->raid_disks;
1879 while (find_rdev_nr(mddev, choice))
1880 choice++;
1881 rdev->desc_nr = choice;
1882 } else {
1883 if (find_rdev_nr(mddev, rdev->desc_nr))
1884 return -EBUSY;
1885 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001886 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1887 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1888 mdname(mddev), mddev->max_disks);
1889 return -EBUSY;
1890 }
NeilBrown19133a42005-11-08 21:39:35 -08001891 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001892 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001893 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001896 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001897
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001898 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001899 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001900
Tejun Heo0762b8b2008-08-25 19:56:12 +09001901 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001902 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1903 /* failure here is OK */;
1904 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001905
NeilBrown4b809912008-07-21 17:05:25 +10001906 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01001907 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001908
1909 /* May as well allow recovery to be retried once */
1910 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001913
1914 fail:
1915 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1916 b, mdname(mddev));
1917 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
NeilBrown177a99b2008-02-06 01:39:56 -08001920static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001921{
1922 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1923 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001924 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001925}
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1928{
1929 char b[BDEVNAME_SIZE];
1930 if (!rdev->mddev) {
1931 MD_BUG();
1932 return;
1933 }
Tejun Heo49731ba2011-01-14 18:43:57 +01001934 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001935 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1937 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001938 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001939 sysfs_put(rdev->sysfs_state);
1940 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001941 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001942 * writing to 'remove' to "dev/state". We also need
1943 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001944 */
NeilBrown4b809912008-07-21 17:05:25 +10001945 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001946 INIT_WORK(&rdev->del_work, md_delayed_delete);
1947 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001948 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951/*
1952 * prevent the device from being mounted, repartitioned or
1953 * otherwise reused by a RAID array (or any other kernel
1954 * subsystem), by bd_claiming the device.
1955 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001956static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 int err = 0;
1959 struct block_device *bdev;
1960 char b[BDEVNAME_SIZE];
1961
Tejun Heod4d77622010-11-13 11:55:18 +01001962 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1963 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (IS_ERR(bdev)) {
1965 printk(KERN_ERR "md: could not open %s.\n",
1966 __bdevname(dev, b));
1967 return PTR_ERR(bdev);
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rdev->bdev = bdev;
1970 return err;
1971}
1972
1973static void unlock_rdev(mdk_rdev_t *rdev)
1974{
1975 struct block_device *bdev = rdev->bdev;
1976 rdev->bdev = NULL;
1977 if (!bdev)
1978 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01001979 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982void md_autodetect_dev(dev_t dev);
1983
1984static void export_rdev(mdk_rdev_t * rdev)
1985{
1986 char b[BDEVNAME_SIZE];
1987 printk(KERN_INFO "md: export_rdev(%s)\n",
1988 bdevname(rdev->bdev,b));
1989 if (rdev->mddev)
1990 MD_BUG();
1991 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001993 if (test_bit(AutoDetected, &rdev->flags))
1994 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995#endif
1996 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001997 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000static void kick_rdev_from_array(mdk_rdev_t * rdev)
2001{
2002 unbind_rdev_from_array(rdev);
2003 export_rdev(rdev);
2004}
2005
2006static void export_array(mddev_t *mddev)
2007{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002008 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
NeilBrownd089c6a2008-02-06 01:39:59 -08002010 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (!rdev->mddev) {
2012 MD_BUG();
2013 continue;
2014 }
2015 kick_rdev_from_array(rdev);
2016 }
2017 if (!list_empty(&mddev->disks))
2018 MD_BUG();
2019 mddev->raid_disks = 0;
2020 mddev->major_version = 0;
2021}
2022
2023static void print_desc(mdp_disk_t *desc)
2024{
2025 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2026 desc->major,desc->minor,desc->raid_disk,desc->state);
2027}
2028
Cheng Renquancd2ac932009-01-09 08:31:08 +11002029static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 int i;
2032
2033 printk(KERN_INFO
2034 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2035 sb->major_version, sb->minor_version, sb->patch_version,
2036 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2037 sb->ctime);
2038 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2039 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2040 sb->md_minor, sb->layout, sb->chunk_size);
2041 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2042 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2043 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2044 sb->failed_disks, sb->spare_disks,
2045 sb->sb_csum, (unsigned long)sb->events_lo);
2046
2047 printk(KERN_INFO);
2048 for (i = 0; i < MD_SB_DISKS; i++) {
2049 mdp_disk_t *desc;
2050
2051 desc = sb->disks + i;
2052 if (desc->number || desc->major || desc->minor ||
2053 desc->raid_disk || (desc->state && (desc->state != 4))) {
2054 printk(" D %2d: ", i);
2055 print_desc(desc);
2056 }
2057 }
2058 printk(KERN_INFO "md: THIS: ");
2059 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Cheng Renquancd2ac932009-01-09 08:31:08 +11002062static void print_sb_1(struct mdp_superblock_1 *sb)
2063{
2064 __u8 *uuid;
2065
2066 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002067 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002068 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002069 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002070 le32_to_cpu(sb->major_version),
2071 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002072 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002073 sb->set_name,
2074 (unsigned long long)le64_to_cpu(sb->ctime)
2075 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2076
2077 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002078 printk(KERN_INFO
2079 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002080 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002081 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002082 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2083 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002084 le32_to_cpu(sb->level),
2085 (unsigned long long)le64_to_cpu(sb->size),
2086 le32_to_cpu(sb->raid_disks),
2087 le32_to_cpu(sb->layout),
2088 le32_to_cpu(sb->chunksize),
2089 (unsigned long long)le64_to_cpu(sb->data_offset),
2090 (unsigned long long)le64_to_cpu(sb->data_size),
2091 (unsigned long long)le64_to_cpu(sb->super_offset),
2092 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2093 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002094 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002095 sb->devflags,
2096 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2097 (unsigned long long)le64_to_cpu(sb->events),
2098 (unsigned long long)le64_to_cpu(sb->resync_offset),
2099 le32_to_cpu(sb->sb_csum),
2100 le32_to_cpu(sb->max_dev)
2101 );
2102}
2103
2104static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002107 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2108 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002109 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2110 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002112 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2113 switch (major_version) {
2114 case 0:
2115 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2116 break;
2117 case 1:
2118 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2119 break;
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 } else
2122 printk(KERN_INFO "md: no rdev superblock!\n");
2123}
2124
Adrian Bunk5e563412006-06-26 00:27:42 -07002125static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002127 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 mdk_rdev_t *rdev;
2129 mddev_t *mddev;
2130 char b[BDEVNAME_SIZE];
2131
2132 printk("\n");
2133 printk("md: **********************************\n");
2134 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2135 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002136 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
NeilBrown32a76272005-06-21 17:17:14 -07002138 if (mddev->bitmap)
2139 bitmap_print_sb(mddev->bitmap);
2140 else
2141 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002142 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 printk("<%s>", bdevname(rdev->bdev,b));
2144 printk("\n");
2145
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002146 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002147 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
2149 printk("md: **********************************\n");
2150 printk("\n");
2151}
2152
2153
NeilBrown42543762006-06-26 00:27:57 -07002154static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
NeilBrown42543762006-06-26 00:27:57 -07002156 /* Update each superblock (in-memory image), but
2157 * if we are allowed to, skip spares which already
2158 * have the right event counter, or have one earlier
2159 * (which would mean they aren't being marked as dirty
2160 * with the rest of the array)
2161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002163 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002164 if (rdev->sb_events == mddev->events ||
2165 (nospares &&
2166 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002167 rdev->sb_events+1 == mddev->events)) {
2168 /* Don't update this superblock */
2169 rdev->sb_loaded = 2;
2170 } else {
2171 super_types[mddev->major_version].
2172 sync_super(mddev, rdev);
2173 rdev->sb_loaded = 1;
2174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176}
2177
NeilBrown850b2b42006-10-03 01:15:46 -07002178static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002181 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002182 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002185 /* First make sure individual recovery_offsets are correct */
2186 list_for_each_entry(rdev, &mddev->disks, same_set) {
2187 if (rdev->raid_disk >= 0 &&
2188 mddev->delta_disks >= 0 &&
2189 !test_bit(In_sync, &rdev->flags) &&
2190 mddev->curr_resync_completed > rdev->recovery_offset)
2191 rdev->recovery_offset = mddev->curr_resync_completed;
2192
2193 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002194 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002195 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002196 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002197 if (!mddev->external)
2198 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002199 wake_up(&mddev->sb_wait);
2200 return;
2201 }
2202
NeilBrowna9701a32005-11-08 21:39:34 -08002203 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002204
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002205 mddev->utime = get_seconds();
2206
NeilBrown850b2b42006-10-03 01:15:46 -07002207 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2208 force_change = 1;
2209 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2210 /* just a clean<-> dirty transition, possibly leave spares alone,
2211 * though if events isn't the right even/odd, we will have to do
2212 * spares after all
2213 */
2214 nospares = 1;
2215 if (force_change)
2216 nospares = 0;
2217 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002218 /* If the array is degraded, then skipping spares is both
2219 * dangerous and fairly pointless.
2220 * Dangerous because a device that was removed from the array
2221 * might have a event_count that still looks up-to-date,
2222 * so it can be re-added without a resync.
2223 * Pointless because if there are any spares to skip,
2224 * then a recovery will happen and soon that array won't
2225 * be degraded any more and the spare can go back to sleep then.
2226 */
NeilBrown850b2b42006-10-03 01:15:46 -07002227 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002228
NeilBrown06d91a52005-06-21 17:17:12 -07002229 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002230
2231 /* If this is just a dirty<->clean transition, and the array is clean
2232 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002233 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002234 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002235 && mddev->can_decrease_events
2236 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002237 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002238 mddev->can_decrease_events = 0;
2239 } else {
NeilBrown42543762006-06-26 00:27:57 -07002240 /* otherwise we have to go forward and ... */
2241 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002242 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 if (!mddev->events) {
2246 /*
2247 * oops, this 64-bit counter should never wrap.
2248 * Either we are in around ~1 trillion A.C., assuming
2249 * 1 reboot per second, or we have a bug:
2250 */
2251 MD_BUG();
2252 mddev->events --;
2253 }
NeilBrowne6910632008-02-06 01:39:51 -08002254 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002255 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 dprintk(KERN_INFO
2258 "md: updating %s RAID superblock on device (in sync %d)\n",
2259 mdname(mddev),mddev->in_sync);
2260
NeilBrown4ad13662007-07-17 04:06:13 -07002261 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002262 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 char b[BDEVNAME_SIZE];
2264 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002265 if (rdev->sb_loaded != 1)
2266 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002267 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 dprintk("(skipping faulty ");
2269
2270 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002271 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002272 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002273 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002274 rdev->sb_page);
2275 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2276 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002277 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002278 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 } else
2281 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002282 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* only need to write one superblock... */
2284 break;
2285 }
NeilBrowna9701a32005-11-08 21:39:34 -08002286 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002287 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002288
NeilBrowna9701a32005-11-08 21:39:34 -08002289 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002290 if (mddev->in_sync != sync_req ||
2291 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002292 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002293 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002294 goto repeat;
2295 }
NeilBrown850b2b42006-10-03 01:15:46 -07002296 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002297 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002298 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002299 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2300 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
Andre Noll7f6ce762008-03-23 18:34:54 +01002304/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002305 * We want to accept with case. For this we use cmd_match.
2306 */
2307static int cmd_match(const char *cmd, const char *str)
2308{
2309 /* See if cmd, written into a sysfs file, matches
2310 * str. They must either be the same, or cmd can
2311 * have a trailing newline
2312 */
2313 while (*cmd && *str && *cmd == *str) {
2314 cmd++;
2315 str++;
2316 }
2317 if (*cmd == '\n')
2318 cmd++;
2319 if (*str || *cmd)
2320 return 0;
2321 return 1;
2322}
2323
NeilBrown86e6ffd2005-11-08 21:39:24 -08002324struct rdev_sysfs_entry {
2325 struct attribute attr;
2326 ssize_t (*show)(mdk_rdev_t *, char *);
2327 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2328};
2329
2330static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002331state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002332{
2333 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002334 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002335
NeilBrownb2d444d2005-11-08 21:39:31 -08002336 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002337 len+= sprintf(page+len, "%sfaulty",sep);
2338 sep = ",";
2339 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002340 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002341 len += sprintf(page+len, "%sin_sync",sep);
2342 sep = ",";
2343 }
NeilBrownf6556752006-06-26 00:28:01 -07002344 if (test_bit(WriteMostly, &rdev->flags)) {
2345 len += sprintf(page+len, "%swrite_mostly",sep);
2346 sep = ",";
2347 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002348 if (test_bit(Blocked, &rdev->flags)) {
2349 len += sprintf(page+len, "%sblocked", sep);
2350 sep = ",";
2351 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002352 if (!test_bit(Faulty, &rdev->flags) &&
2353 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002354 len += sprintf(page+len, "%sspare", sep);
2355 sep = ",";
2356 }
2357 return len+sprintf(page+len, "\n");
2358}
2359
NeilBrown45dc2de2006-06-26 00:27:58 -07002360static ssize_t
2361state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2362{
2363 /* can write
2364 * faulty - simulates and error
2365 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002366 * writemostly - sets write_mostly
2367 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002368 * blocked - sets the Blocked flag
2369 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002370 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002371 */
2372 int err = -EINVAL;
2373 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2374 md_error(rdev->mddev, rdev);
2375 err = 0;
2376 } else if (cmd_match(buf, "remove")) {
2377 if (rdev->raid_disk >= 0)
2378 err = -EBUSY;
2379 else {
2380 mddev_t *mddev = rdev->mddev;
2381 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002382 if (mddev->pers)
2383 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002384 md_new_event(mddev);
2385 err = 0;
2386 }
NeilBrownf6556752006-06-26 00:28:01 -07002387 } else if (cmd_match(buf, "writemostly")) {
2388 set_bit(WriteMostly, &rdev->flags);
2389 err = 0;
2390 } else if (cmd_match(buf, "-writemostly")) {
2391 clear_bit(WriteMostly, &rdev->flags);
2392 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002393 } else if (cmd_match(buf, "blocked")) {
2394 set_bit(Blocked, &rdev->flags);
2395 err = 0;
2396 } else if (cmd_match(buf, "-blocked")) {
2397 clear_bit(Blocked, &rdev->flags);
2398 wake_up(&rdev->blocked_wait);
2399 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2400 md_wakeup_thread(rdev->mddev->thread);
2401
2402 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002403 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2404 set_bit(In_sync, &rdev->flags);
2405 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002406 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002407 if (!err)
2408 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002409 return err ? err : len;
2410}
NeilBrown80ca3a42006-07-10 04:44:18 -07002411static struct rdev_sysfs_entry rdev_state =
2412__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002413
2414static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002415errors_show(mdk_rdev_t *rdev, char *page)
2416{
2417 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2418}
2419
2420static ssize_t
2421errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2422{
2423 char *e;
2424 unsigned long n = simple_strtoul(buf, &e, 10);
2425 if (*buf && (*e == 0 || *e == '\n')) {
2426 atomic_set(&rdev->corrected_errors, n);
2427 return len;
2428 }
2429 return -EINVAL;
2430}
2431static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002432__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002433
NeilBrown014236d2006-01-06 00:20:55 -08002434static ssize_t
2435slot_show(mdk_rdev_t *rdev, char *page)
2436{
2437 if (rdev->raid_disk < 0)
2438 return sprintf(page, "none\n");
2439 else
2440 return sprintf(page, "%d\n", rdev->raid_disk);
2441}
2442
2443static ssize_t
2444slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2445{
2446 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002447 int err;
2448 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002449 int slot = simple_strtoul(buf, &e, 10);
2450 if (strncmp(buf, "none", 4)==0)
2451 slot = -1;
2452 else if (e==buf || (*e && *e!= '\n'))
2453 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002454 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002455 /* Setting 'slot' on an active array requires also
2456 * updating the 'rd%d' link, and communicating
2457 * with the personality with ->hot_*_disk.
2458 * For now we only support removing
2459 * failed/spare devices. This normally happens automatically,
2460 * but not when the metadata is externally managed.
2461 */
NeilBrownc303da62008-02-06 01:39:51 -08002462 if (rdev->raid_disk == -1)
2463 return -EEXIST;
2464 /* personality does all needed checks */
2465 if (rdev->mddev->pers->hot_add_disk == NULL)
2466 return -EINVAL;
2467 err = rdev->mddev->pers->
2468 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2469 if (err)
2470 return err;
2471 sprintf(nm, "rd%d", rdev->raid_disk);
2472 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002473 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002474 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2475 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002476 } else if (rdev->mddev->pers) {
2477 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002478 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002479 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002480 */
2481
2482 if (rdev->raid_disk != -1)
2483 return -EBUSY;
2484
NeilBrownc6751b22011-02-02 11:57:13 +11002485 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2486 return -EBUSY;
2487
Neil Brown6c2fce22008-06-28 08:31:31 +10002488 if (rdev->mddev->pers->hot_add_disk == NULL)
2489 return -EINVAL;
2490
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002491 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002492 if (rdev2->raid_disk == slot)
2493 return -EEXIST;
2494
NeilBrownba1b41b2011-01-14 09:14:34 +11002495 if (slot >= rdev->mddev->raid_disks &&
2496 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2497 return -ENOSPC;
2498
Neil Brown6c2fce22008-06-28 08:31:31 +10002499 rdev->raid_disk = slot;
2500 if (test_bit(In_sync, &rdev->flags))
2501 rdev->saved_raid_disk = slot;
2502 else
2503 rdev->saved_raid_disk = -1;
2504 err = rdev->mddev->pers->
2505 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002506 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002507 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002508 return err;
Neil Brown52664732008-06-28 08:31:44 +10002509 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002510 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002511 sprintf(nm, "rd%d", rdev->raid_disk);
2512 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002513 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002514 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002515 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002516 if (slot >= rdev->mddev->raid_disks &&
2517 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002518 return -ENOSPC;
2519 rdev->raid_disk = slot;
2520 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002521 clear_bit(Faulty, &rdev->flags);
2522 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002523 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002524 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002525 }
NeilBrown014236d2006-01-06 00:20:55 -08002526 return len;
2527}
2528
2529
2530static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002531__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002532
NeilBrown93c8cad2006-01-06 00:20:56 -08002533static ssize_t
2534offset_show(mdk_rdev_t *rdev, char *page)
2535{
Andrew Morton6961ece2006-01-06 00:20:59 -08002536 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002537}
2538
2539static ssize_t
2540offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2541{
2542 char *e;
2543 unsigned long long offset = simple_strtoull(buf, &e, 10);
2544 if (e==buf || (*e && *e != '\n'))
2545 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002546 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002547 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002548 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002549 /* Must set offset before size, so overlap checks
2550 * can be sane */
2551 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002552 rdev->data_offset = offset;
2553 return len;
2554}
2555
2556static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002557__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002558
NeilBrown83303b62006-01-06 00:21:06 -08002559static ssize_t
2560rdev_size_show(mdk_rdev_t *rdev, char *page)
2561{
Andre Nolldd8ac332009-03-31 14:33:13 +11002562 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002563}
2564
NeilBrownc5d79ad2008-02-06 01:39:54 -08002565static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2566{
2567 /* check if two start/length pairs overlap */
2568 if (s1+l1 <= s2)
2569 return 0;
2570 if (s2+l2 <= s1)
2571 return 0;
2572 return 1;
2573}
2574
Dan Williamsb522adc2009-03-31 15:00:31 +11002575static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2576{
2577 unsigned long long blocks;
2578 sector_t new;
2579
2580 if (strict_strtoull(buf, 10, &blocks) < 0)
2581 return -EINVAL;
2582
2583 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2584 return -EINVAL; /* sector conversion overflow */
2585
2586 new = blocks * 2;
2587 if (new != blocks * 2)
2588 return -EINVAL; /* unsigned long long to sector_t overflow */
2589
2590 *sectors = new;
2591 return 0;
2592}
2593
NeilBrown83303b62006-01-06 00:21:06 -08002594static ssize_t
2595rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2596{
NeilBrown27c529b2008-03-04 14:29:33 -08002597 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002598 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002599 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002600
Dan Williamsb522adc2009-03-31 15:00:31 +11002601 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002602 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002603 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002604 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002605 sectors = super_types[my_mddev->major_version].
2606 rdev_size_change(rdev, sectors);
2607 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002608 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002609 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002610 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002611 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002612 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002613 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002614 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002615
Andre Nolldd8ac332009-03-31 14:33:13 +11002616 rdev->sectors = sectors;
2617 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002618 /* need to check that all other rdevs with the same ->bdev
2619 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002620 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002621 * we have to change it back, we will have the lock again.
2622 */
2623 mddev_t *mddev;
2624 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002625 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002626
NeilBrown27c529b2008-03-04 14:29:33 -08002627 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002628 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002629 mdk_rdev_t *rdev2;
2630
2631 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002632 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002633 if (rdev->bdev == rdev2->bdev &&
2634 rdev != rdev2 &&
2635 overlaps(rdev->data_offset, rdev->sectors,
2636 rdev2->data_offset,
2637 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002638 overlap = 1;
2639 break;
2640 }
2641 mddev_unlock(mddev);
2642 if (overlap) {
2643 mddev_put(mddev);
2644 break;
2645 }
2646 }
NeilBrown27c529b2008-03-04 14:29:33 -08002647 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002648 if (overlap) {
2649 /* Someone else could have slipped in a size
2650 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002651 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002652 * safe, and trust userspace not to race with
2653 * itself
2654 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002655 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002656 return -EBUSY;
2657 }
2658 }
NeilBrown83303b62006-01-06 00:21:06 -08002659 return len;
2660}
2661
2662static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002663__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002664
Dan Williams06e3c812009-12-12 21:17:12 -07002665
2666static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2667{
2668 unsigned long long recovery_start = rdev->recovery_offset;
2669
2670 if (test_bit(In_sync, &rdev->flags) ||
2671 recovery_start == MaxSector)
2672 return sprintf(page, "none\n");
2673
2674 return sprintf(page, "%llu\n", recovery_start);
2675}
2676
2677static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2678{
2679 unsigned long long recovery_start;
2680
2681 if (cmd_match(buf, "none"))
2682 recovery_start = MaxSector;
2683 else if (strict_strtoull(buf, 10, &recovery_start))
2684 return -EINVAL;
2685
2686 if (rdev->mddev->pers &&
2687 rdev->raid_disk >= 0)
2688 return -EBUSY;
2689
2690 rdev->recovery_offset = recovery_start;
2691 if (recovery_start == MaxSector)
2692 set_bit(In_sync, &rdev->flags);
2693 else
2694 clear_bit(In_sync, &rdev->flags);
2695 return len;
2696}
2697
2698static struct rdev_sysfs_entry rdev_recovery_start =
2699__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2700
NeilBrown86e6ffd2005-11-08 21:39:24 -08002701static struct attribute *rdev_default_attrs[] = {
2702 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002703 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002704 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002705 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002706 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002707 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002708 NULL,
2709};
2710static ssize_t
2711rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2712{
2713 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2714 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002715 mddev_t *mddev = rdev->mddev;
2716 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002717
2718 if (!entry->show)
2719 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002720
2721 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2722 if (!rv) {
2723 if (rdev->mddev == NULL)
2724 rv = -EBUSY;
2725 else
2726 rv = entry->show(rdev, page);
2727 mddev_unlock(mddev);
2728 }
2729 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002730}
2731
2732static ssize_t
2733rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2734 const char *page, size_t length)
2735{
2736 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2737 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002738 ssize_t rv;
2739 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002740
2741 if (!entry->store)
2742 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002743 if (!capable(CAP_SYS_ADMIN))
2744 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002745 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002746 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002747 if (rdev->mddev == NULL)
2748 rv = -EBUSY;
2749 else
2750 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002751 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002752 }
2753 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002754}
2755
2756static void rdev_free(struct kobject *ko)
2757{
2758 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2759 kfree(rdev);
2760}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002761static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002762 .show = rdev_attr_show,
2763 .store = rdev_attr_store,
2764};
2765static struct kobj_type rdev_ktype = {
2766 .release = rdev_free,
2767 .sysfs_ops = &rdev_sysfs_ops,
2768 .default_attrs = rdev_default_attrs,
2769};
2770
NeilBrowne8bb9a82010-06-01 19:37:26 +10002771void md_rdev_init(mdk_rdev_t *rdev)
2772{
2773 rdev->desc_nr = -1;
2774 rdev->saved_raid_disk = -1;
2775 rdev->raid_disk = -1;
2776 rdev->flags = 0;
2777 rdev->data_offset = 0;
2778 rdev->sb_events = 0;
2779 rdev->last_read_error.tv_sec = 0;
2780 rdev->last_read_error.tv_nsec = 0;
2781 atomic_set(&rdev->nr_pending, 0);
2782 atomic_set(&rdev->read_errors, 0);
2783 atomic_set(&rdev->corrected_errors, 0);
2784
2785 INIT_LIST_HEAD(&rdev->same_set);
2786 init_waitqueue_head(&rdev->blocked_wait);
2787}
2788EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789/*
2790 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2791 *
2792 * mark the device faulty if:
2793 *
2794 * - the device is nonexistent (zero size)
2795 * - the device has no valid superblock
2796 *
2797 * a faulty rdev _never_ has rdev->sb set.
2798 */
2799static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2800{
2801 char b[BDEVNAME_SIZE];
2802 int err;
2803 mdk_rdev_t *rdev;
2804 sector_t size;
2805
NeilBrown9ffae0c2006-01-06 00:20:32 -08002806 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (!rdev) {
2808 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2809 return ERR_PTR(-ENOMEM);
2810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
NeilBrowne8bb9a82010-06-01 19:37:26 +10002812 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if ((err = alloc_disk_sb(rdev)))
2814 goto abort_free;
2815
NeilBrownc5d79ad2008-02-06 01:39:54 -08002816 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (err)
2818 goto abort_free;
2819
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002820 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002821
Mike Snitzer77304d22010-11-08 14:39:12 +01002822 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (!size) {
2824 printk(KERN_WARNING
2825 "md: %s has zero or unknown size, marking faulty!\n",
2826 bdevname(rdev->bdev,b));
2827 err = -EINVAL;
2828 goto abort_free;
2829 }
2830
2831 if (super_format >= 0) {
2832 err = super_types[super_format].
2833 load_super(rdev, NULL, super_minor);
2834 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002835 printk(KERN_WARNING
2836 "md: %s does not have a valid v%d.%d "
2837 "superblock, not importing!\n",
2838 bdevname(rdev->bdev,b),
2839 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 goto abort_free;
2841 }
2842 if (err < 0) {
2843 printk(KERN_WARNING
2844 "md: could not read %s's sb, not importing!\n",
2845 bdevname(rdev->bdev,b));
2846 goto abort_free;
2847 }
2848 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return rdev;
2851
2852abort_free:
2853 if (rdev->sb_page) {
2854 if (rdev->bdev)
2855 unlock_rdev(rdev);
2856 free_disk_sb(rdev);
2857 }
2858 kfree(rdev);
2859 return ERR_PTR(err);
2860}
2861
2862/*
2863 * Check a full RAID array for plausibility
2864 */
2865
2866
NeilBrowna757e642005-04-16 15:26:42 -07002867static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002870 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 char b[BDEVNAME_SIZE];
2872
2873 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002874 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 switch (super_types[mddev->major_version].
2876 load_super(rdev, freshest, mddev->minor_version)) {
2877 case 1:
2878 freshest = rdev;
2879 break;
2880 case 0:
2881 break;
2882 default:
2883 printk( KERN_ERR \
2884 "md: fatal superblock inconsistency in %s"
2885 " -- removing from array\n",
2886 bdevname(rdev->bdev,b));
2887 kick_rdev_from_array(rdev);
2888 }
2889
2890
2891 super_types[mddev->major_version].
2892 validate_super(mddev, freshest);
2893
2894 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002895 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002896 if (mddev->max_disks &&
2897 (rdev->desc_nr >= mddev->max_disks ||
2898 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002899 printk(KERN_WARNING
2900 "md: %s: %s: only %d devices permitted\n",
2901 mdname(mddev), bdevname(rdev->bdev, b),
2902 mddev->max_disks);
2903 kick_rdev_from_array(rdev);
2904 continue;
2905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (rdev != freshest)
2907 if (super_types[mddev->major_version].
2908 validate_super(mddev, rdev)) {
2909 printk(KERN_WARNING "md: kicking non-fresh %s"
2910 " from array!\n",
2911 bdevname(rdev->bdev,b));
2912 kick_rdev_from_array(rdev);
2913 continue;
2914 }
2915 if (mddev->level == LEVEL_MULTIPATH) {
2916 rdev->desc_nr = i++;
2917 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002918 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002919 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002920 rdev->raid_disk = -1;
2921 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
2923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
NeilBrown72e02072009-12-14 12:49:55 +11002926/* Read a fixed-point number.
2927 * Numbers in sysfs attributes should be in "standard" units where
2928 * possible, so time should be in seconds.
2929 * However we internally use a a much smaller unit such as
2930 * milliseconds or jiffies.
2931 * This function takes a decimal number with a possible fractional
2932 * component, and produces an integer which is the result of
2933 * multiplying that number by 10^'scale'.
2934 * all without any floating-point arithmetic.
2935 */
2936int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2937{
2938 unsigned long result = 0;
2939 long decimals = -1;
2940 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2941 if (*cp == '.')
2942 decimals = 0;
2943 else if (decimals < scale) {
2944 unsigned int value;
2945 value = *cp - '0';
2946 result = result * 10 + value;
2947 if (decimals >= 0)
2948 decimals++;
2949 }
2950 cp++;
2951 }
2952 if (*cp == '\n')
2953 cp++;
2954 if (*cp)
2955 return -EINVAL;
2956 if (decimals < 0)
2957 decimals = 0;
2958 while (decimals < scale) {
2959 result *= 10;
2960 decimals ++;
2961 }
2962 *res = result;
2963 return 0;
2964}
2965
2966
NeilBrown19052c02008-08-05 15:54:13 +10002967static void md_safemode_timeout(unsigned long data);
2968
NeilBrowneae17012005-11-08 21:39:23 -08002969static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002970safe_delay_show(mddev_t *mddev, char *page)
2971{
2972 int msec = (mddev->safemode_delay*1000)/HZ;
2973 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2974}
2975static ssize_t
2976safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2977{
NeilBrown16f17b32006-06-26 00:27:37 -07002978 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002979
NeilBrown72e02072009-12-14 12:49:55 +11002980 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002981 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002982 if (msec == 0)
2983 mddev->safemode_delay = 0;
2984 else {
NeilBrown19052c02008-08-05 15:54:13 +10002985 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002986 mddev->safemode_delay = (msec*HZ)/1000;
2987 if (mddev->safemode_delay == 0)
2988 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002989 if (mddev->safemode_delay < old_delay)
2990 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002991 }
2992 return len;
2993}
2994static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002995__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002996
2997static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002998level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002999{
NeilBrown2604b702006-01-06 00:20:36 -08003000 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003001 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003002 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003003 else if (mddev->clevel[0])
3004 return sprintf(page, "%s\n", mddev->clevel);
3005 else if (mddev->level != LEVEL_NONE)
3006 return sprintf(page, "%d\n", mddev->level);
3007 else
3008 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003009}
3010
NeilBrownd9d166c2006-01-06 00:20:51 -08003011static ssize_t
3012level_store(mddev_t *mddev, const char *buf, size_t len)
3013{
Dan Williamsf2859af2010-05-02 10:04:16 -07003014 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003015 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003016 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003017 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003018 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003019 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003020
3021 if (mddev->pers == NULL) {
3022 if (len == 0)
3023 return 0;
3024 if (len >= sizeof(mddev->clevel))
3025 return -ENOSPC;
3026 strncpy(mddev->clevel, buf, len);
3027 if (mddev->clevel[len-1] == '\n')
3028 len--;
3029 mddev->clevel[len] = 0;
3030 mddev->level = LEVEL_NONE;
3031 return rv;
3032 }
3033
3034 /* request to change the personality. Need to ensure:
3035 * - array is not engaged in resync/recovery/reshape
3036 * - old personality can be suspended
3037 * - new personality will access other array.
3038 */
3039
NeilBrownbb4f1e92010-08-08 21:18:03 +10003040 if (mddev->sync_thread ||
3041 mddev->reshape_position != MaxSector ||
3042 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003043 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003044
3045 if (!mddev->pers->quiesce) {
3046 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3047 mdname(mddev), mddev->pers->name);
3048 return -EINVAL;
3049 }
3050
3051 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003052 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003053 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003054 strncpy(clevel, buf, len);
3055 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003056 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003057 clevel[len] = 0;
3058 if (strict_strtol(clevel, 10, &level))
3059 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003060
Dan Williamsf2859af2010-05-02 10:04:16 -07003061 if (request_module("md-%s", clevel) != 0)
3062 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003063 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003064 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003065 if (!pers || !try_module_get(pers->owner)) {
3066 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003067 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003068 return -EINVAL;
3069 }
3070 spin_unlock(&pers_lock);
3071
3072 if (pers == mddev->pers) {
3073 /* Nothing to do! */
3074 module_put(pers->owner);
3075 return rv;
3076 }
3077 if (!pers->takeover) {
3078 module_put(pers->owner);
3079 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003080 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003081 return -EINVAL;
3082 }
3083
NeilBrowne93f68a2010-06-15 09:36:03 +01003084 list_for_each_entry(rdev, &mddev->disks, same_set)
3085 rdev->new_raid_disk = rdev->raid_disk;
3086
NeilBrown245f46c2009-03-31 14:39:39 +11003087 /* ->takeover must set new_* and/or delta_disks
3088 * if it succeeds, and may set them when it fails.
3089 */
3090 priv = pers->takeover(mddev);
3091 if (IS_ERR(priv)) {
3092 mddev->new_level = mddev->level;
3093 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003094 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003095 mddev->raid_disks -= mddev->delta_disks;
3096 mddev->delta_disks = 0;
3097 module_put(pers->owner);
3098 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003099 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003100 return PTR_ERR(priv);
3101 }
3102
3103 /* Looks like we have a winner */
3104 mddev_suspend(mddev);
3105 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003106
3107 if (mddev->pers->sync_request == NULL &&
3108 pers->sync_request != NULL) {
3109 /* need to add the md_redundancy_group */
3110 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3111 printk(KERN_WARNING
3112 "md: cannot register extra attributes for %s\n",
3113 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003114 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003115 }
3116 if (mddev->pers->sync_request != NULL &&
3117 pers->sync_request == NULL) {
3118 /* need to remove the md_redundancy_group */
3119 if (mddev->to_remove == NULL)
3120 mddev->to_remove = &md_redundancy_group;
3121 }
3122
Trela Maciej54071b32010-03-08 16:02:42 +11003123 if (mddev->pers->sync_request == NULL &&
3124 mddev->external) {
3125 /* We are converting from a no-redundancy array
3126 * to a redundancy array and metadata is managed
3127 * externally so we need to be sure that writes
3128 * won't block due to a need to transition
3129 * clean->dirty
3130 * until external management is started.
3131 */
3132 mddev->in_sync = 0;
3133 mddev->safemode_delay = 0;
3134 mddev->safemode = 0;
3135 }
3136
NeilBrowne93f68a2010-06-15 09:36:03 +01003137 list_for_each_entry(rdev, &mddev->disks, same_set) {
3138 char nm[20];
3139 if (rdev->raid_disk < 0)
3140 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003141 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003142 rdev->new_raid_disk = -1;
3143 if (rdev->new_raid_disk == rdev->raid_disk)
3144 continue;
3145 sprintf(nm, "rd%d", rdev->raid_disk);
3146 sysfs_remove_link(&mddev->kobj, nm);
3147 }
3148 list_for_each_entry(rdev, &mddev->disks, same_set) {
3149 if (rdev->raid_disk < 0)
3150 continue;
3151 if (rdev->new_raid_disk == rdev->raid_disk)
3152 continue;
3153 rdev->raid_disk = rdev->new_raid_disk;
3154 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003155 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003156 else {
3157 char nm[20];
3158 sprintf(nm, "rd%d", rdev->raid_disk);
3159 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3160 printk("md: cannot register %s for %s after level change\n",
3161 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003162 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003163 }
3164
3165 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003166 mddev->pers = pers;
3167 mddev->private = priv;
3168 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3169 mddev->level = mddev->new_level;
3170 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003171 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003172 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003173 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003174 if (mddev->pers->sync_request == NULL) {
3175 /* this is now an array without redundancy, so
3176 * it must always be in_sync
3177 */
3178 mddev->in_sync = 1;
3179 del_timer_sync(&mddev->safemode_timer);
3180 }
NeilBrown245f46c2009-03-31 14:39:39 +11003181 pers->run(mddev);
3182 mddev_resume(mddev);
3183 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3184 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3185 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003186 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003187 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003188 return rv;
3189}
3190
3191static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003192__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003193
NeilBrownd4dbd022006-06-26 00:27:59 -07003194
3195static ssize_t
3196layout_show(mddev_t *mddev, char *page)
3197{
3198 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003199 if (mddev->reshape_position != MaxSector &&
3200 mddev->layout != mddev->new_layout)
3201 return sprintf(page, "%d (%d)\n",
3202 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003203 return sprintf(page, "%d\n", mddev->layout);
3204}
3205
3206static ssize_t
3207layout_store(mddev_t *mddev, const char *buf, size_t len)
3208{
3209 char *e;
3210 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003211
3212 if (!*buf || (*e && *e != '\n'))
3213 return -EINVAL;
3214
NeilBrownb3546032009-03-31 14:56:41 +11003215 if (mddev->pers) {
3216 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003217 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003218 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003219 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003220 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003221 if (err) {
3222 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003223 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003224 }
NeilBrownb3546032009-03-31 14:56:41 +11003225 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003226 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003227 if (mddev->reshape_position == MaxSector)
3228 mddev->layout = n;
3229 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003230 return len;
3231}
3232static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003233__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003234
3235
NeilBrowneae17012005-11-08 21:39:23 -08003236static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003237raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003238{
NeilBrownbb636542005-11-08 21:39:45 -08003239 if (mddev->raid_disks == 0)
3240 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003241 if (mddev->reshape_position != MaxSector &&
3242 mddev->delta_disks != 0)
3243 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3244 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003245 return sprintf(page, "%d\n", mddev->raid_disks);
3246}
3247
NeilBrownda943b92006-01-06 00:20:54 -08003248static int update_raid_disks(mddev_t *mddev, int raid_disks);
3249
3250static ssize_t
3251raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3252{
NeilBrownda943b92006-01-06 00:20:54 -08003253 char *e;
3254 int rv = 0;
3255 unsigned long n = simple_strtoul(buf, &e, 10);
3256
3257 if (!*buf || (*e && *e != '\n'))
3258 return -EINVAL;
3259
3260 if (mddev->pers)
3261 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003262 else if (mddev->reshape_position != MaxSector) {
3263 int olddisks = mddev->raid_disks - mddev->delta_disks;
3264 mddev->delta_disks = n - olddisks;
3265 mddev->raid_disks = n;
3266 } else
NeilBrownda943b92006-01-06 00:20:54 -08003267 mddev->raid_disks = n;
3268 return rv ? rv : len;
3269}
3270static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003271__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003272
NeilBrown24dd4692005-11-08 21:39:26 -08003273static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003274chunk_size_show(mddev_t *mddev, char *page)
3275{
NeilBrown08a02ec2007-05-09 02:35:38 -07003276 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003277 mddev->chunk_sectors != mddev->new_chunk_sectors)
3278 return sprintf(page, "%d (%d)\n",
3279 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003280 mddev->chunk_sectors << 9);
3281 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003282}
3283
3284static ssize_t
3285chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3286{
NeilBrown3b343802006-01-06 00:20:47 -08003287 char *e;
3288 unsigned long n = simple_strtoul(buf, &e, 10);
3289
NeilBrown3b343802006-01-06 00:20:47 -08003290 if (!*buf || (*e && *e != '\n'))
3291 return -EINVAL;
3292
NeilBrownb3546032009-03-31 14:56:41 +11003293 if (mddev->pers) {
3294 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003295 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003296 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003297 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003298 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003299 if (err) {
3300 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003301 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003302 }
NeilBrownb3546032009-03-31 14:56:41 +11003303 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003304 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003305 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003306 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003307 }
NeilBrown3b343802006-01-06 00:20:47 -08003308 return len;
3309}
3310static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003311__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003312
NeilBrowna94213b2006-06-26 00:28:00 -07003313static ssize_t
3314resync_start_show(mddev_t *mddev, char *page)
3315{
NeilBrownd1a7c502009-03-31 15:24:32 +11003316 if (mddev->recovery_cp == MaxSector)
3317 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003318 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3319}
3320
3321static ssize_t
3322resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3323{
NeilBrowna94213b2006-06-26 00:28:00 -07003324 char *e;
3325 unsigned long long n = simple_strtoull(buf, &e, 10);
3326
NeilBrownb0986362011-05-11 15:52:21 +10003327 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003328 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003329 if (cmd_match(buf, "none"))
3330 n = MaxSector;
3331 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003332 return -EINVAL;
3333
3334 mddev->recovery_cp = n;
3335 return len;
3336}
3337static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003338__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003339
NeilBrown9e653b62006-06-26 00:27:58 -07003340/*
3341 * The array state can be:
3342 *
3343 * clear
3344 * No devices, no size, no level
3345 * Equivalent to STOP_ARRAY ioctl
3346 * inactive
3347 * May have some settings, but array is not active
3348 * all IO results in error
3349 * When written, doesn't tear down array, but just stops it
3350 * suspended (not supported yet)
3351 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003352 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003353 * readonly
3354 * no resync can happen. no superblocks get written.
3355 * write requests fail
3356 * read-auto
3357 * like readonly, but behaves like 'clean' on a write request.
3358 *
3359 * clean - no pending writes, but otherwise active.
3360 * When written to inactive array, starts without resync
3361 * If a write request arrives then
3362 * if metadata is known, mark 'dirty' and switch to 'active'.
3363 * if not known, block and switch to write-pending
3364 * If written to an active array that has pending writes, then fails.
3365 * active
3366 * fully active: IO and resync can be happening.
3367 * When written to inactive array, starts with resync
3368 *
3369 * write-pending
3370 * clean, but writes are blocked waiting for 'active' to be written.
3371 *
3372 * active-idle
3373 * like active, but no writes have been seen for a while (100msec).
3374 *
3375 */
3376enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3377 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003378static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003379 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3380 "write-pending", "active-idle", NULL };
3381
3382static int match_word(const char *word, char **list)
3383{
3384 int n;
3385 for (n=0; list[n]; n++)
3386 if (cmd_match(word, list[n]))
3387 break;
3388 return n;
3389}
3390
3391static ssize_t
3392array_state_show(mddev_t *mddev, char *page)
3393{
3394 enum array_state st = inactive;
3395
3396 if (mddev->pers)
3397 switch(mddev->ro) {
3398 case 1:
3399 st = readonly;
3400 break;
3401 case 2:
3402 st = read_auto;
3403 break;
3404 case 0:
3405 if (mddev->in_sync)
3406 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003407 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003408 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003409 else if (mddev->safemode)
3410 st = active_idle;
3411 else
3412 st = active;
3413 }
3414 else {
3415 if (list_empty(&mddev->disks) &&
3416 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003417 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003418 st = clear;
3419 else
3420 st = inactive;
3421 }
3422 return sprintf(page, "%s\n", array_states[st]);
3423}
3424
Neil Browndf5b20c2008-07-11 22:02:22 +10003425static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003426static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003427static int do_md_run(mddev_t * mddev);
3428static int restart_array(mddev_t *mddev);
3429
3430static ssize_t
3431array_state_store(mddev_t *mddev, const char *buf, size_t len)
3432{
3433 int err = -EINVAL;
3434 enum array_state st = match_word(buf, array_states);
3435 switch(st) {
3436 case bad_word:
3437 break;
3438 case clear:
3439 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003440 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003441 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003442 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003443 break;
3444 case inactive:
3445 /* stopping an active array */
3446 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003447 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003448 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003449 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003450 } else
3451 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003452 break;
3453 case suspended:
3454 break; /* not supported yet */
3455 case readonly:
3456 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003457 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003458 else {
3459 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003460 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003461 err = do_md_run(mddev);
3462 }
3463 break;
3464 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003465 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003466 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003467 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003468 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003469 err = restart_array(mddev);
3470 if (err == 0) {
3471 mddev->ro = 2;
3472 set_disk_ro(mddev->gendisk, 0);
3473 }
NeilBrown9e653b62006-06-26 00:27:58 -07003474 } else {
3475 mddev->ro = 2;
3476 err = do_md_run(mddev);
3477 }
3478 break;
3479 case clean:
3480 if (mddev->pers) {
3481 restart_array(mddev);
3482 spin_lock_irq(&mddev->write_lock);
3483 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003484 if (mddev->in_sync == 0) {
3485 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003486 if (mddev->safemode == 1)
3487 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003488 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003489 }
3490 err = 0;
3491 } else
3492 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003493 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003494 } else
3495 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003496 break;
3497 case active:
3498 if (mddev->pers) {
3499 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003500 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003501 wake_up(&mddev->sb_wait);
3502 err = 0;
3503 } else {
3504 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003505 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003506 err = do_md_run(mddev);
3507 }
3508 break;
3509 case write_pending:
3510 case active_idle:
3511 /* these cannot be set */
3512 break;
3513 }
3514 if (err)
3515 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003516 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003517 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003518 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003519 }
NeilBrown9e653b62006-06-26 00:27:58 -07003520}
NeilBrown80ca3a42006-07-10 04:44:18 -07003521static struct md_sysfs_entry md_array_state =
3522__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003523
NeilBrown6d7ff732006-01-06 00:21:16 -08003524static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003525max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3526 return sprintf(page, "%d\n",
3527 atomic_read(&mddev->max_corr_read_errors));
3528}
3529
3530static ssize_t
3531max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3532{
3533 char *e;
3534 unsigned long n = simple_strtoul(buf, &e, 10);
3535
3536 if (*buf && (*e == 0 || *e == '\n')) {
3537 atomic_set(&mddev->max_corr_read_errors, n);
3538 return len;
3539 }
3540 return -EINVAL;
3541}
3542
3543static struct md_sysfs_entry max_corr_read_errors =
3544__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3545 max_corrected_read_errors_store);
3546
3547static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003548null_show(mddev_t *mddev, char *page)
3549{
3550 return -EINVAL;
3551}
3552
3553static ssize_t
3554new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3555{
3556 /* buf must be %d:%d\n? giving major and minor numbers */
3557 /* The new device is added to the array.
3558 * If the array has a persistent superblock, we read the
3559 * superblock to initialise info and check validity.
3560 * Otherwise, only checking done is that in bind_rdev_to_array,
3561 * which mainly checks size.
3562 */
3563 char *e;
3564 int major = simple_strtoul(buf, &e, 10);
3565 int minor;
3566 dev_t dev;
3567 mdk_rdev_t *rdev;
3568 int err;
3569
3570 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3571 return -EINVAL;
3572 minor = simple_strtoul(e+1, &e, 10);
3573 if (*e && *e != '\n')
3574 return -EINVAL;
3575 dev = MKDEV(major, minor);
3576 if (major != MAJOR(dev) ||
3577 minor != MINOR(dev))
3578 return -EOVERFLOW;
3579
3580
3581 if (mddev->persistent) {
3582 rdev = md_import_device(dev, mddev->major_version,
3583 mddev->minor_version);
3584 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3585 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3586 mdk_rdev_t, same_set);
3587 err = super_types[mddev->major_version]
3588 .load_super(rdev, rdev0, mddev->minor_version);
3589 if (err < 0)
3590 goto out;
3591 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003592 } else if (mddev->external)
3593 rdev = md_import_device(dev, -2, -1);
3594 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003595 rdev = md_import_device(dev, -1, -1);
3596
3597 if (IS_ERR(rdev))
3598 return PTR_ERR(rdev);
3599 err = bind_rdev_to_array(rdev, mddev);
3600 out:
3601 if (err)
3602 export_rdev(rdev);
3603 return err ? err : len;
3604}
3605
3606static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003607__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003608
3609static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003610bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3611{
3612 char *end;
3613 unsigned long chunk, end_chunk;
3614
3615 if (!mddev->bitmap)
3616 goto out;
3617 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3618 while (*buf) {
3619 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3620 if (buf == end) break;
3621 if (*end == '-') { /* range */
3622 buf = end + 1;
3623 end_chunk = simple_strtoul(buf, &end, 0);
3624 if (buf == end) break;
3625 }
3626 if (*end && !isspace(*end)) break;
3627 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003628 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003629 }
3630 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3631out:
3632 return len;
3633}
3634
3635static struct md_sysfs_entry md_bitmap =
3636__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3637
3638static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003639size_show(mddev_t *mddev, char *page)
3640{
Andre Noll58c0fed2009-03-31 14:33:13 +11003641 return sprintf(page, "%llu\n",
3642 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003643}
3644
Andre Nolld71f9f82008-07-11 22:02:22 +10003645static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003646
3647static ssize_t
3648size_store(mddev_t *mddev, const char *buf, size_t len)
3649{
3650 /* If array is inactive, we can reduce the component size, but
3651 * not increase it (except from 0).
3652 * If array is active, we can try an on-line resize
3653 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003654 sector_t sectors;
3655 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003656
Andre Noll58c0fed2009-03-31 14:33:13 +11003657 if (err < 0)
3658 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003659 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003660 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003661 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003662 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003663 if (mddev->dev_sectors == 0 ||
3664 mddev->dev_sectors > sectors)
3665 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003666 else
3667 err = -ENOSPC;
3668 }
3669 return err ? err : len;
3670}
3671
3672static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003673__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003674
NeilBrown8bb93aa2006-01-06 00:20:50 -08003675
3676/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003677 * This is one of
3678 * 'none' for arrays with no metadata (good luck...)
3679 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003680 * or N.M for internally known formats
3681 */
3682static ssize_t
3683metadata_show(mddev_t *mddev, char *page)
3684{
3685 if (mddev->persistent)
3686 return sprintf(page, "%d.%d\n",
3687 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003688 else if (mddev->external)
3689 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003690 else
3691 return sprintf(page, "none\n");
3692}
3693
3694static ssize_t
3695metadata_store(mddev_t *mddev, const char *buf, size_t len)
3696{
3697 int major, minor;
3698 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003699 /* Changing the details of 'external' metadata is
3700 * always permitted. Otherwise there must be
3701 * no devices attached to the array.
3702 */
3703 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3704 ;
3705 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003706 return -EBUSY;
3707
3708 if (cmd_match(buf, "none")) {
3709 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003710 mddev->external = 0;
3711 mddev->major_version = 0;
3712 mddev->minor_version = 90;
3713 return len;
3714 }
3715 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003716 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003717 if (namelen >= sizeof(mddev->metadata_type))
3718 namelen = sizeof(mddev->metadata_type)-1;
3719 strncpy(mddev->metadata_type, buf+9, namelen);
3720 mddev->metadata_type[namelen] = 0;
3721 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3722 mddev->metadata_type[--namelen] = 0;
3723 mddev->persistent = 0;
3724 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003725 mddev->major_version = 0;
3726 mddev->minor_version = 90;
3727 return len;
3728 }
3729 major = simple_strtoul(buf, &e, 10);
3730 if (e==buf || *e != '.')
3731 return -EINVAL;
3732 buf = e+1;
3733 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003734 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003735 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003736 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003737 return -ENOENT;
3738 mddev->major_version = major;
3739 mddev->minor_version = minor;
3740 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003741 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003742 return len;
3743}
3744
3745static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003746__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003747
NeilBrowna35b0d62006-01-06 00:20:49 -08003748static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003749action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003750{
NeilBrown7eec3142005-11-08 21:39:44 -08003751 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003752 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3753 type = "frozen";
3754 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003755 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003756 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3757 type = "reshape";
3758 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003759 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3760 type = "resync";
3761 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3762 type = "check";
3763 else
3764 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003765 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003766 type = "recover";
3767 }
3768 return sprintf(page, "%s\n", type);
3769}
3770
NeilBrown7ebc0be2011-01-14 09:14:33 +11003771static void reap_sync_thread(mddev_t *mddev);
3772
NeilBrown24dd4692005-11-08 21:39:26 -08003773static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003774action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003775{
NeilBrown7eec3142005-11-08 21:39:44 -08003776 if (!mddev->pers || !mddev->pers->sync_request)
3777 return -EINVAL;
3778
NeilBrownb6a9ce62009-05-26 09:41:17 +10003779 if (cmd_match(page, "frozen"))
3780 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3781 else
3782 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3783
3784 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003785 if (mddev->sync_thread) {
3786 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003787 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003788 }
NeilBrown03c902e2006-01-06 00:20:46 -08003789 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3790 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003791 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003792 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003793 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003794 else if (cmd_match(page, "recover")) {
3795 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3796 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3797 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003798 int err;
3799 if (mddev->pers->start_reshape == NULL)
3800 return -EINVAL;
3801 err = mddev->pers->start_reshape(mddev);
3802 if (err)
3803 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003804 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003805 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003806 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003807 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003808 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003809 return -EINVAL;
3810 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3811 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003812 }
NeilBrown03c902e2006-01-06 00:20:46 -08003813 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003814 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003815 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003816 return len;
3817}
3818
NeilBrown9d888832005-11-08 21:39:26 -08003819static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003820mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003821{
3822 return sprintf(page, "%llu\n",
3823 (unsigned long long) mddev->resync_mismatches);
3824}
3825
NeilBrown80ca3a42006-07-10 04:44:18 -07003826static struct md_sysfs_entry md_scan_mode =
3827__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003828
NeilBrown96de1e62005-11-08 21:39:39 -08003829
NeilBrown80ca3a42006-07-10 04:44:18 -07003830static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003831
NeilBrown88202a02006-01-06 00:21:36 -08003832static ssize_t
3833sync_min_show(mddev_t *mddev, char *page)
3834{
3835 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3836 mddev->sync_speed_min ? "local": "system");
3837}
3838
3839static ssize_t
3840sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3841{
3842 int min;
3843 char *e;
3844 if (strncmp(buf, "system", 6)==0) {
3845 mddev->sync_speed_min = 0;
3846 return len;
3847 }
3848 min = simple_strtoul(buf, &e, 10);
3849 if (buf == e || (*e && *e != '\n') || min <= 0)
3850 return -EINVAL;
3851 mddev->sync_speed_min = min;
3852 return len;
3853}
3854
3855static struct md_sysfs_entry md_sync_min =
3856__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3857
3858static ssize_t
3859sync_max_show(mddev_t *mddev, char *page)
3860{
3861 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3862 mddev->sync_speed_max ? "local": "system");
3863}
3864
3865static ssize_t
3866sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3867{
3868 int max;
3869 char *e;
3870 if (strncmp(buf, "system", 6)==0) {
3871 mddev->sync_speed_max = 0;
3872 return len;
3873 }
3874 max = simple_strtoul(buf, &e, 10);
3875 if (buf == e || (*e && *e != '\n') || max <= 0)
3876 return -EINVAL;
3877 mddev->sync_speed_max = max;
3878 return len;
3879}
3880
3881static struct md_sysfs_entry md_sync_max =
3882__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3883
Iustin Popd7f3d292007-10-16 23:30:54 -07003884static ssize_t
3885degraded_show(mddev_t *mddev, char *page)
3886{
3887 return sprintf(page, "%d\n", mddev->degraded);
3888}
3889static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003890
3891static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003892sync_force_parallel_show(mddev_t *mddev, char *page)
3893{
3894 return sprintf(page, "%d\n", mddev->parallel_resync);
3895}
3896
3897static ssize_t
3898sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3899{
3900 long n;
3901
3902 if (strict_strtol(buf, 10, &n))
3903 return -EINVAL;
3904
3905 if (n != 0 && n != 1)
3906 return -EINVAL;
3907
3908 mddev->parallel_resync = n;
3909
3910 if (mddev->sync_thread)
3911 wake_up(&resync_wait);
3912
3913 return len;
3914}
3915
3916/* force parallel resync, even with shared block devices */
3917static struct md_sysfs_entry md_sync_force_parallel =
3918__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3919 sync_force_parallel_show, sync_force_parallel_store);
3920
3921static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003922sync_speed_show(mddev_t *mddev, char *page)
3923{
3924 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003925 if (mddev->curr_resync == 0)
3926 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003927 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3928 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003929 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003930 db = resync - mddev->resync_mark_cnt;
3931 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003932}
3933
NeilBrown80ca3a42006-07-10 04:44:18 -07003934static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003935
3936static ssize_t
3937sync_completed_show(mddev_t *mddev, char *page)
3938{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003939 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003940
NeilBrownacb180b2009-04-14 16:28:34 +10003941 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3942 return sprintf(page, "none\n");
3943
NeilBrown88202a02006-01-06 00:21:36 -08003944 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003945 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003946 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003947 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003948
NeilBrownacb180b2009-04-14 16:28:34 +10003949 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003950 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003951}
3952
NeilBrown80ca3a42006-07-10 04:44:18 -07003953static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003954
NeilBrowne464eaf2006-03-27 01:18:14 -08003955static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003956min_sync_show(mddev_t *mddev, char *page)
3957{
3958 return sprintf(page, "%llu\n",
3959 (unsigned long long)mddev->resync_min);
3960}
3961static ssize_t
3962min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3963{
3964 unsigned long long min;
3965 if (strict_strtoull(buf, 10, &min))
3966 return -EINVAL;
3967 if (min > mddev->resync_max)
3968 return -EINVAL;
3969 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3970 return -EBUSY;
3971
3972 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003973 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003974 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003975 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003976 return -EINVAL;
3977 }
3978 mddev->resync_min = min;
3979
3980 return len;
3981}
3982
3983static struct md_sysfs_entry md_min_sync =
3984__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3985
3986static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003987max_sync_show(mddev_t *mddev, char *page)
3988{
3989 if (mddev->resync_max == MaxSector)
3990 return sprintf(page, "max\n");
3991 else
3992 return sprintf(page, "%llu\n",
3993 (unsigned long long)mddev->resync_max);
3994}
3995static ssize_t
3996max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3997{
3998 if (strncmp(buf, "max", 3) == 0)
3999 mddev->resync_max = MaxSector;
4000 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004001 unsigned long long max;
4002 if (strict_strtoull(buf, 10, &max))
4003 return -EINVAL;
4004 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004005 return -EINVAL;
4006 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004007 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004008 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4009 return -EBUSY;
4010
4011 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004012 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004013 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004014 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004015 return -EINVAL;
4016 }
4017 mddev->resync_max = max;
4018 }
4019 wake_up(&mddev->recovery_wait);
4020 return len;
4021}
4022
4023static struct md_sysfs_entry md_max_sync =
4024__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4025
4026static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004027suspend_lo_show(mddev_t *mddev, char *page)
4028{
4029 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4030}
4031
4032static ssize_t
4033suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4034{
4035 char *e;
4036 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004037 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004038
NeilBrownb8d966e2009-07-01 11:14:04 +10004039 if (mddev->pers == NULL ||
4040 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004041 return -EINVAL;
4042 if (buf == e || (*e && *e != '\n'))
4043 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004044
4045 mddev->suspend_lo = new;
4046 if (new >= old)
4047 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004048 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004049 else {
4050 /* Expanding suspended region - need to wait */
4051 mddev->pers->quiesce(mddev, 1);
4052 mddev->pers->quiesce(mddev, 0);
4053 }
4054 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004055}
4056static struct md_sysfs_entry md_suspend_lo =
4057__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4058
4059
4060static ssize_t
4061suspend_hi_show(mddev_t *mddev, char *page)
4062{
4063 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4064}
4065
4066static ssize_t
4067suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4068{
4069 char *e;
4070 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004071 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004072
NeilBrownb8d966e2009-07-01 11:14:04 +10004073 if (mddev->pers == NULL ||
4074 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004075 return -EINVAL;
4076 if (buf == e || (*e && *e != '\n'))
4077 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004078
4079 mddev->suspend_hi = new;
4080 if (new <= old)
4081 /* Shrinking suspended region */
4082 mddev->pers->quiesce(mddev, 2);
4083 else {
4084 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004085 mddev->pers->quiesce(mddev, 1);
4086 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004087 }
4088 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004089}
4090static struct md_sysfs_entry md_suspend_hi =
4091__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4092
NeilBrown08a02ec2007-05-09 02:35:38 -07004093static ssize_t
4094reshape_position_show(mddev_t *mddev, char *page)
4095{
4096 if (mddev->reshape_position != MaxSector)
4097 return sprintf(page, "%llu\n",
4098 (unsigned long long)mddev->reshape_position);
4099 strcpy(page, "none\n");
4100 return 5;
4101}
4102
4103static ssize_t
4104reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4105{
4106 char *e;
4107 unsigned long long new = simple_strtoull(buf, &e, 10);
4108 if (mddev->pers)
4109 return -EBUSY;
4110 if (buf == e || (*e && *e != '\n'))
4111 return -EINVAL;
4112 mddev->reshape_position = new;
4113 mddev->delta_disks = 0;
4114 mddev->new_level = mddev->level;
4115 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004116 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004117 return len;
4118}
4119
4120static struct md_sysfs_entry md_reshape_position =
4121__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4122 reshape_position_store);
4123
Dan Williamsb522adc2009-03-31 15:00:31 +11004124static ssize_t
4125array_size_show(mddev_t *mddev, char *page)
4126{
4127 if (mddev->external_size)
4128 return sprintf(page, "%llu\n",
4129 (unsigned long long)mddev->array_sectors/2);
4130 else
4131 return sprintf(page, "default\n");
4132}
4133
4134static ssize_t
4135array_size_store(mddev_t *mddev, const char *buf, size_t len)
4136{
4137 sector_t sectors;
4138
4139 if (strncmp(buf, "default", 7) == 0) {
4140 if (mddev->pers)
4141 sectors = mddev->pers->size(mddev, 0, 0);
4142 else
4143 sectors = mddev->array_sectors;
4144
4145 mddev->external_size = 0;
4146 } else {
4147 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4148 return -EINVAL;
4149 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004150 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004151
4152 mddev->external_size = 1;
4153 }
4154
4155 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004156 if (mddev->pers) {
4157 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004158 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004159 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004160 return len;
4161}
4162
4163static struct md_sysfs_entry md_array_size =
4164__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4165 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004166
NeilBrowneae17012005-11-08 21:39:23 -08004167static struct attribute *md_default_attrs[] = {
4168 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004169 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004170 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004171 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004172 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004173 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004174 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004175 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004176 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004177 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004178 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004179 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004180 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004181 NULL,
4182};
4183
4184static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004185 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004186 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004187 &md_sync_min.attr,
4188 &md_sync_max.attr,
4189 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004190 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004191 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004192 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004193 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004194 &md_suspend_lo.attr,
4195 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004196 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004197 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004198 NULL,
4199};
NeilBrown411036f2005-11-08 21:39:40 -08004200static struct attribute_group md_redundancy_group = {
4201 .name = NULL,
4202 .attrs = md_redundancy_attrs,
4203};
4204
NeilBrowneae17012005-11-08 21:39:23 -08004205
4206static ssize_t
4207md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4208{
4209 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4210 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004211 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004212
4213 if (!entry->show)
4214 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004215 rv = mddev_lock(mddev);
4216 if (!rv) {
4217 rv = entry->show(mddev, page);
4218 mddev_unlock(mddev);
4219 }
NeilBrown96de1e62005-11-08 21:39:39 -08004220 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004221}
4222
4223static ssize_t
4224md_attr_store(struct kobject *kobj, struct attribute *attr,
4225 const char *page, size_t length)
4226{
4227 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4228 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004229 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004230
4231 if (!entry->store)
4232 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004233 if (!capable(CAP_SYS_ADMIN))
4234 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004235 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004236 if (mddev->hold_active == UNTIL_IOCTL)
4237 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004238 if (!rv) {
4239 rv = entry->store(mddev, page, length);
4240 mddev_unlock(mddev);
4241 }
NeilBrown96de1e62005-11-08 21:39:39 -08004242 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004243}
4244
4245static void md_free(struct kobject *ko)
4246{
4247 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004248
4249 if (mddev->sysfs_state)
4250 sysfs_put(mddev->sysfs_state);
4251
4252 if (mddev->gendisk) {
4253 del_gendisk(mddev->gendisk);
4254 put_disk(mddev->gendisk);
4255 }
4256 if (mddev->queue)
4257 blk_cleanup_queue(mddev->queue);
4258
NeilBrowneae17012005-11-08 21:39:23 -08004259 kfree(mddev);
4260}
4261
Emese Revfy52cf25d2010-01-19 02:58:23 +01004262static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004263 .show = md_attr_show,
4264 .store = md_attr_store,
4265};
4266static struct kobj_type md_ktype = {
4267 .release = md_free,
4268 .sysfs_ops = &md_sysfs_ops,
4269 .default_attrs = md_default_attrs,
4270};
4271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272int mdp_major = 0;
4273
Dan Williams5fd3a172009-03-04 00:57:25 -07004274static void mddev_delayed_delete(struct work_struct *ws)
4275{
4276 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4277
NeilBrown43a70502009-12-14 12:49:55 +11004278 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004279 kobject_del(&mddev->kobj);
4280 kobject_put(&mddev->kobj);
4281}
4282
NeilBrownefeb53c2009-01-09 08:31:10 +11004283static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004285 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 mddev_t *mddev = mddev_find(dev);
4287 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004288 int partitioned;
4289 int shift;
4290 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004291 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
4293 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004294 return -ENODEV;
4295
4296 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4297 shift = partitioned ? MdpMinorShift : 0;
4298 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Tejun Heoe804ac72010-10-15 15:36:08 +02004300 /* wait for any previous instance of this device to be
4301 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004302 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004303 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004304
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004305 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004306 error = -EEXIST;
4307 if (mddev->gendisk)
4308 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004309
4310 if (name) {
4311 /* Need to ensure that 'name' is not a duplicate.
4312 */
4313 mddev_t *mddev2;
4314 spin_lock(&all_mddevs_lock);
4315
4316 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4317 if (mddev2->gendisk &&
4318 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4319 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004320 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004321 }
4322 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 }
NeilBrown8b765392009-01-09 08:31:08 +11004324
NeilBrown0909dc42009-07-01 12:27:21 +10004325 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004326 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004327 if (!mddev->queue)
4328 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004329 mddev->queue->queuedata = mddev;
4330
NeilBrown409c57f2009-03-31 14:39:39 +11004331 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 disk = alloc_disk(1 << shift);
4334 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004335 blk_cleanup_queue(mddev->queue);
4336 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004337 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004339 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004341 if (name)
4342 strcpy(disk->disk_name, name);
4343 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004345 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 disk->fops = &md_fops;
4348 disk->private_data = mddev;
4349 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004350 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004351 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004352 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004353 * remove it now.
4354 */
4355 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004357 /* As soon as we call add_disk(), another thread could get
4358 * through to md_open, so make sure it doesn't get too far
4359 */
4360 mutex_lock(&mddev->open_mutex);
4361 add_disk(disk);
4362
Tejun Heoed9e1982008-08-25 19:56:05 +09004363 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4364 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004365 if (error) {
4366 /* This isn't possible, but as kobject_init_and_add is marked
4367 * __must_check, we must do something with the result
4368 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004369 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4370 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004371 error = 0;
4372 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004373 if (mddev->kobj.sd &&
4374 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004375 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004376 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004377 abort:
4378 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004379 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004380 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004381 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004382 }
NeilBrownd3374822009-01-09 08:31:10 +11004383 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004384 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004385}
4386
4387static struct kobject *md_probe(dev_t dev, int *part, void *data)
4388{
4389 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return NULL;
4391}
4392
NeilBrownefeb53c2009-01-09 08:31:10 +11004393static int add_named_array(const char *val, struct kernel_param *kp)
4394{
4395 /* val must be "md_*" where * is not all digits.
4396 * We allocate an array with a large free minor number, and
4397 * set the name to val. val must not already be an active name.
4398 */
4399 int len = strlen(val);
4400 char buf[DISK_NAME_LEN];
4401
4402 while (len && val[len-1] == '\n')
4403 len--;
4404 if (len >= DISK_NAME_LEN)
4405 return -E2BIG;
4406 strlcpy(buf, val, len+1);
4407 if (strncmp(buf, "md_", 3) != 0)
4408 return -EINVAL;
4409 return md_alloc(0, buf);
4410}
4411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412static void md_safemode_timeout(unsigned long data)
4413{
4414 mddev_t *mddev = (mddev_t *) data;
4415
Neil Brown0fd62b82008-06-28 08:31:36 +10004416 if (!atomic_read(&mddev->writes_pending)) {
4417 mddev->safemode = 1;
4418 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004419 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 md_wakeup_thread(mddev->thread);
4422}
4423
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004424static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
NeilBrown390ee602010-06-01 19:37:27 +10004426int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
NeilBrown2604b702006-01-06 00:20:36 -08004428 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004430 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
NeilBrowna757e642005-04-16 15:26:42 -07004432 if (list_empty(&mddev->disks))
4433 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 if (mddev->pers)
4437 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004438 /* Cannot run until previous stop completes properly */
4439 if (mddev->sysfs_active)
4440 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 /*
4443 * Analyze all RAID superblock(s)
4444 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004445 if (!mddev->raid_disks) {
4446 if (!mddev->persistent)
4447 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004448 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
NeilBrownd9d166c2006-01-06 00:20:51 -08004451 if (mddev->level != LEVEL_NONE)
4452 request_module("md-level-%d", mddev->level);
4453 else if (mddev->clevel[0])
4454 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
4456 /*
4457 * Drop all container device buffers, from now on
4458 * the only valid external interface is through the md
4459 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004461 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004462 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 continue;
4464 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004465 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004466
4467 /* perform some consistency tests on the device.
4468 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004469 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004470 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004471 if (rdev->meta_bdev) {
4472 /* Nothing to check */;
4473 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004474 if (mddev->dev_sectors &&
4475 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004476 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004477 printk("md: %s: data overlaps metadata\n",
4478 mdname(mddev));
4479 return -EINVAL;
4480 }
4481 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004482 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004483 > rdev->data_offset) {
4484 printk("md: %s: metadata overlaps data\n",
4485 mdname(mddev));
4486 return -EINVAL;
4487 }
4488 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004489 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
4491
NeilBrowna167f662010-10-26 18:31:13 +11004492 if (mddev->bio_set == NULL)
4493 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004496 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004497 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004499 if (mddev->level != LEVEL_NONE)
4500 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4501 mddev->level);
4502 else
4503 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4504 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return -EINVAL;
4506 }
NeilBrown2604b702006-01-06 00:20:36 -08004507 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004509 if (mddev->level != pers->level) {
4510 mddev->level = pers->level;
4511 mddev->new_level = pers->level;
4512 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004513 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
NeilBrownf6705572006-03-27 01:18:11 -08004515 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004516 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004517 /* This personality cannot handle reshaping... */
4518 mddev->pers = NULL;
4519 module_put(pers->owner);
4520 return -EINVAL;
4521 }
4522
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004523 if (pers->sync_request) {
4524 /* Warn if this is a potentially silly
4525 * configuration.
4526 */
4527 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4528 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004529 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004530
4531 list_for_each_entry(rdev, &mddev->disks, same_set)
4532 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004533 if (rdev < rdev2 &&
4534 rdev->bdev->bd_contains ==
4535 rdev2->bdev->bd_contains) {
4536 printk(KERN_WARNING
4537 "%s: WARNING: %s appears to be"
4538 " on the same physical disk as"
4539 " %s.\n",
4540 mdname(mddev),
4541 bdevname(rdev->bdev,b),
4542 bdevname(rdev2->bdev,b2));
4543 warned = 1;
4544 }
4545 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004546
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004547 if (warned)
4548 printk(KERN_WARNING
4549 "True protection against single-disk"
4550 " failure might be compromised.\n");
4551 }
4552
NeilBrown657390d2005-08-26 18:34:16 -07004553 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004554 /* may be over-ridden by personality */
4555 mddev->resync_max_sectors = mddev->dev_sectors;
4556
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004557 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
NeilBrown0f9552b2009-12-30 12:08:50 +11004559 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004560 mddev->ro = 2; /* read-only, but switch on first write */
4561
NeilBrownb15c2e52006-01-06 00:20:16 -08004562 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004563 if (err)
4564 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004565 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4566 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4567 " but 'external_size' not in effect?\n", __func__);
4568 printk(KERN_ERR
4569 "md: invalid array_size %llu > default size %llu\n",
4570 (unsigned long long)mddev->array_sectors / 2,
4571 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4572 err = -EINVAL;
4573 mddev->pers->stop(mddev);
4574 }
4575 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004576 err = bitmap_create(mddev);
4577 if (err) {
4578 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4579 mdname(mddev), err);
4580 mddev->pers->stop(mddev);
4581 }
4582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 module_put(mddev->pers->owner);
4585 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004586 bitmap_destroy(mddev);
4587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004589 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004590 if (mddev->kobj.sd &&
4591 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004592 printk(KERN_WARNING
4593 "md: cannot register extra attributes for %s\n",
4594 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004595 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004596 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004597 mddev->ro = 0;
4598
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004600 atomic_set(&mddev->max_corr_read_errors,
4601 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 mddev->safemode = 0;
4603 mddev->safemode_timer.function = md_safemode_timeout;
4604 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004605 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004607 smp_wmb();
4608 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004609 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004610 if (rdev->raid_disk >= 0) {
4611 char nm[20];
4612 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004613 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004614 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4618
NeilBrown850b2b42006-10-03 01:15:46 -07004619 if (mddev->flags)
4620 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
NeilBrown0b8c9de2006-07-10 04:44:15 -07004622 md_wakeup_thread(mddev->thread);
4623 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004624
NeilBrownd7603b72006-01-06 00:20:30 -08004625 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004626 sysfs_notify_dirent_safe(mddev->sysfs_state);
4627 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004628 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 return 0;
4630}
NeilBrown390ee602010-06-01 19:37:27 +10004631EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
NeilBrownfe60b012010-03-29 11:10:42 +11004633static int do_md_run(mddev_t *mddev)
4634{
4635 int err;
4636
4637 err = md_run(mddev);
4638 if (err)
4639 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004640 err = bitmap_load(mddev);
4641 if (err) {
4642 bitmap_destroy(mddev);
4643 goto out;
4644 }
NeilBrownfe60b012010-03-29 11:10:42 +11004645 set_capacity(mddev->gendisk, mddev->array_sectors);
4646 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004647 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004648 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4649out:
4650 return err;
4651}
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653static int restart_array(mddev_t *mddev)
4654{
4655 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Andre Noll80fab1d2008-07-11 22:02:21 +10004657 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004659 return -ENXIO;
4660 if (!mddev->pers)
4661 return -EINVAL;
4662 if (!mddev->ro)
4663 return -EBUSY;
4664 mddev->safemode = 0;
4665 mddev->ro = 0;
4666 set_disk_ro(disk, 0);
4667 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4668 mdname(mddev));
4669 /* Kick recovery or resync if necessary */
4670 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4671 md_wakeup_thread(mddev->thread);
4672 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004673 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004674 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
NeilBrownacc55e22006-06-26 00:27:47 -07004677/* similar to deny_write_access, but accounts for our holding a reference
4678 * to the file ourselves */
4679static int deny_bitmap_write_access(struct file * file)
4680{
4681 struct inode *inode = file->f_mapping->host;
4682
4683 spin_lock(&inode->i_lock);
4684 if (atomic_read(&inode->i_writecount) > 1) {
4685 spin_unlock(&inode->i_lock);
4686 return -ETXTBSY;
4687 }
4688 atomic_set(&inode->i_writecount, -1);
4689 spin_unlock(&inode->i_lock);
4690
4691 return 0;
4692}
4693
NeilBrown43a70502009-12-14 12:49:55 +11004694void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004695{
4696 struct inode *inode = file->f_mapping->host;
4697
4698 spin_lock(&inode->i_lock);
4699 atomic_set(&inode->i_writecount, 1);
4700 spin_unlock(&inode->i_lock);
4701}
4702
NeilBrown6177b472010-03-29 11:37:13 +11004703static void md_clean(mddev_t *mddev)
4704{
4705 mddev->array_sectors = 0;
4706 mddev->external_size = 0;
4707 mddev->dev_sectors = 0;
4708 mddev->raid_disks = 0;
4709 mddev->recovery_cp = 0;
4710 mddev->resync_min = 0;
4711 mddev->resync_max = MaxSector;
4712 mddev->reshape_position = MaxSector;
4713 mddev->external = 0;
4714 mddev->persistent = 0;
4715 mddev->level = LEVEL_NONE;
4716 mddev->clevel[0] = 0;
4717 mddev->flags = 0;
4718 mddev->ro = 0;
4719 mddev->metadata_type[0] = 0;
4720 mddev->chunk_sectors = 0;
4721 mddev->ctime = mddev->utime = 0;
4722 mddev->layout = 0;
4723 mddev->max_disks = 0;
4724 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004725 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004726 mddev->delta_disks = 0;
4727 mddev->new_level = LEVEL_NONE;
4728 mddev->new_layout = 0;
4729 mddev->new_chunk_sectors = 0;
4730 mddev->curr_resync = 0;
4731 mddev->resync_mismatches = 0;
4732 mddev->suspend_lo = mddev->suspend_hi = 0;
4733 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4734 mddev->recovery = 0;
4735 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004736 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004737 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004738 mddev->safemode = 0;
4739 mddev->bitmap_info.offset = 0;
4740 mddev->bitmap_info.default_offset = 0;
4741 mddev->bitmap_info.chunksize = 0;
4742 mddev->bitmap_info.daemon_sleep = 0;
4743 mddev->bitmap_info.max_write_behind = 0;
4744}
4745
NeilBrowndefad612011-01-14 09:14:33 +11004746static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004747{
4748 if (mddev->sync_thread) {
4749 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4750 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004751 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004752 }
4753
4754 del_timer_sync(&mddev->safemode_timer);
4755
4756 bitmap_flush(mddev);
4757 md_super_wait(mddev);
4758
4759 if (!mddev->in_sync || mddev->flags) {
4760 /* mark array as shutdown cleanly */
4761 mddev->in_sync = 1;
4762 md_update_sb(mddev, 1);
4763 }
4764}
NeilBrowndefad612011-01-14 09:14:33 +11004765
4766void md_stop_writes(mddev_t *mddev)
4767{
4768 mddev_lock(mddev);
4769 __md_stop_writes(mddev);
4770 mddev_unlock(mddev);
4771}
NeilBrown390ee602010-06-01 19:37:27 +10004772EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004773
NeilBrown390ee602010-06-01 19:37:27 +10004774void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004775{
NeilBrown0ca69882011-01-14 09:14:33 +11004776 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004777 mddev->pers->stop(mddev);
4778 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4779 mddev->to_remove = &md_redundancy_group;
4780 module_put(mddev->pers->owner);
4781 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004782 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004783}
NeilBrown390ee602010-06-01 19:37:27 +10004784EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004785
NeilBrowna4bd82d2010-03-29 13:23:10 +11004786static int md_set_readonly(mddev_t *mddev, int is_open)
4787{
4788 int err = 0;
4789 mutex_lock(&mddev->open_mutex);
4790 if (atomic_read(&mddev->openers) > is_open) {
4791 printk("md: %s still in use.\n",mdname(mddev));
4792 err = -EBUSY;
4793 goto out;
4794 }
4795 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004796 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004797
4798 err = -ENXIO;
4799 if (mddev->ro==1)
4800 goto out;
4801 mddev->ro = 1;
4802 set_disk_ro(mddev->gendisk, 1);
4803 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004804 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004805 err = 0;
4806 }
4807out:
4808 mutex_unlock(&mddev->open_mutex);
4809 return err;
4810}
4811
NeilBrown9e653b62006-06-26 00:27:58 -07004812/* mode:
4813 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004814 * 2 - stop but do not disassemble array
4815 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004816static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004819 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
NeilBrownc8c00a62009-08-10 12:50:52 +10004821 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004822 if (atomic_read(&mddev->openers) > is_open ||
4823 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004824 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004825 mutex_unlock(&mddev->open_mutex);
4826 return -EBUSY;
4827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
NeilBrown6e17b022010-08-07 21:41:19 +10004829 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004830 if (mddev->ro)
4831 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004832
NeilBrowndefad612011-01-14 09:14:33 +11004833 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004834 md_stop(mddev);
4835 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004836 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004837
NeilBrowna4bd82d2010-03-29 13:23:10 +11004838 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004839 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004840
NeilBrowna4bd82d2010-03-29 13:23:10 +11004841 list_for_each_entry(rdev, &mddev->disks, same_set)
4842 if (rdev->raid_disk >= 0) {
4843 char nm[20];
4844 sprintf(nm, "rd%d", rdev->raid_disk);
4845 sysfs_remove_link(&mddev->kobj, nm);
4846 }
NeilBrownc4647292009-05-07 12:51:06 +10004847
NeilBrowna4bd82d2010-03-29 13:23:10 +11004848 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004849 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004850 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004851 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004852
NeilBrowna4bd82d2010-03-29 13:23:10 +11004853 if (mddev->ro)
4854 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004855 } else
4856 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 /*
4858 * Free resources if final stop
4859 */
NeilBrown9e653b62006-06-26 00:27:58 -07004860 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4862
NeilBrown978f9462006-02-02 14:28:05 -08004863 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004864 if (mddev->bitmap_info.file) {
4865 restore_bitmap_write_access(mddev->bitmap_info.file);
4866 fput(mddev->bitmap_info.file);
4867 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004868 }
NeilBrownc3d97142009-12-14 12:49:52 +11004869 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 export_array(mddev);
4872
NeilBrown6177b472010-03-29 11:37:13 +11004873 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004874 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004875 if (mddev->hold_active == UNTIL_STOP)
4876 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004877 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004878 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004879 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004880 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882}
4883
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004884#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885static void autorun_array(mddev_t *mddev)
4886{
4887 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 int err;
4889
NeilBrowna757e642005-04-16 15:26:42 -07004890 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 printk(KERN_INFO "md: running: ");
4894
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004895 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 char b[BDEVNAME_SIZE];
4897 printk("<%s>", bdevname(rdev->bdev,b));
4898 }
4899 printk("\n");
4900
NeilBrownd710e132008-10-13 11:55:12 +11004901 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 if (err) {
4903 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004904 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 }
4906}
4907
4908/*
4909 * lets try to run arrays based on all disks that have arrived
4910 * until now. (those are in pending_raid_disks)
4911 *
4912 * the method: pick the first pending disk, collect all disks with
4913 * the same UUID, remove all from the pending list and put them into
4914 * the 'same_array' list. Then order this list based on superblock
4915 * update time (freshest comes first), kick out 'old' disks and
4916 * compare superblocks. If everything's fine then run it.
4917 *
4918 * If "unit" is allocated, then bump its reference count
4919 */
4920static void autorun_devices(int part)
4921{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004922 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 mddev_t *mddev;
4924 char b[BDEVNAME_SIZE];
4925
4926 printk(KERN_INFO "md: autorun ...\n");
4927 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004928 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004930 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 rdev0 = list_entry(pending_raid_disks.next,
4932 mdk_rdev_t, same_set);
4933
4934 printk(KERN_INFO "md: considering %s ...\n",
4935 bdevname(rdev0->bdev,b));
4936 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004937 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 if (super_90_load(rdev, rdev0, 0) >= 0) {
4939 printk(KERN_INFO "md: adding %s ...\n",
4940 bdevname(rdev->bdev,b));
4941 list_move(&rdev->same_set, &candidates);
4942 }
4943 /*
4944 * now we have a set of devices, with all of them having
4945 * mostly sane superblocks. It's time to allocate the
4946 * mddev.
4947 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004948 if (part) {
4949 dev = MKDEV(mdp_major,
4950 rdev0->preferred_minor << MdpMinorShift);
4951 unit = MINOR(dev) >> MdpMinorShift;
4952 } else {
4953 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4954 unit = MINOR(dev);
4955 }
4956 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4958 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4959 break;
4960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 md_probe(dev, NULL, NULL);
4963 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004964 if (!mddev || !mddev->gendisk) {
4965 if (mddev)
4966 mddev_put(mddev);
4967 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 "md: cannot allocate memory for md drive.\n");
4969 break;
4970 }
4971 if (mddev_lock(mddev))
4972 printk(KERN_WARNING "md: %s locked, cannot run\n",
4973 mdname(mddev));
4974 else if (mddev->raid_disks || mddev->major_version
4975 || !list_empty(&mddev->disks)) {
4976 printk(KERN_WARNING
4977 "md: %s already running, cannot run %s\n",
4978 mdname(mddev), bdevname(rdev0->bdev,b));
4979 mddev_unlock(mddev);
4980 } else {
4981 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004982 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004983 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 list_del_init(&rdev->same_set);
4985 if (bind_rdev_to_array(rdev, mddev))
4986 export_rdev(rdev);
4987 }
4988 autorun_array(mddev);
4989 mddev_unlock(mddev);
4990 }
4991 /* on success, candidates will be empty, on error
4992 * it won't...
4993 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004994 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004995 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 mddev_put(mddev);
4999 }
5000 printk(KERN_INFO "md: ... autorun DONE.\n");
5001}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005002#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004static int get_version(void __user * arg)
5005{
5006 mdu_version_t ver;
5007
5008 ver.major = MD_MAJOR_VERSION;
5009 ver.minor = MD_MINOR_VERSION;
5010 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5011
5012 if (copy_to_user(arg, &ver, sizeof(ver)))
5013 return -EFAULT;
5014
5015 return 0;
5016}
5017
5018static int get_array_info(mddev_t * mddev, void __user * arg)
5019{
5020 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005021 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
NeilBrowna9f326e2009-09-23 18:06:41 +10005024 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005025 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005027 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 failed++;
5029 else {
5030 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005031 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005032 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 else
5034 spare++;
5035 }
5036 }
5037
5038 info.major_version = mddev->major_version;
5039 info.minor_version = mddev->minor_version;
5040 info.patch_version = MD_PATCHLEVEL_VERSION;
5041 info.ctime = mddev->ctime;
5042 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005043 info.size = mddev->dev_sectors / 2;
5044 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005045 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 info.nr_disks = nr;
5047 info.raid_disks = mddev->raid_disks;
5048 info.md_minor = mddev->md_minor;
5049 info.not_persistent= !mddev->persistent;
5050
5051 info.utime = mddev->utime;
5052 info.state = 0;
5053 if (mddev->in_sync)
5054 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005055 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005056 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005057 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 info.working_disks = working;
5059 info.failed_disks = failed;
5060 info.spare_disks = spare;
5061
5062 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005063 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 if (copy_to_user(arg, &info, sizeof(info)))
5066 return -EFAULT;
5067
5068 return 0;
5069}
5070
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005071static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005072{
5073 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5074 char *ptr, *buf = NULL;
5075 int err = -ENOMEM;
5076
Dan Williamsb5470dc2008-06-27 21:44:04 -07005077 if (md_allow_write(mddev))
5078 file = kmalloc(sizeof(*file), GFP_NOIO);
5079 else
5080 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005081
NeilBrown32a76272005-06-21 17:17:14 -07005082 if (!file)
5083 goto out;
5084
5085 /* bitmap disabled, zero the first byte and copy out */
5086 if (!mddev->bitmap || !mddev->bitmap->file) {
5087 file->pathname[0] = '\0';
5088 goto copy_out;
5089 }
5090
5091 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5092 if (!buf)
5093 goto out;
5094
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005095 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5096 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005097 goto out;
5098
5099 strcpy(file->pathname, ptr);
5100
5101copy_out:
5102 err = 0;
5103 if (copy_to_user(arg, file, sizeof(*file)))
5104 err = -EFAULT;
5105out:
5106 kfree(buf);
5107 kfree(file);
5108 return err;
5109}
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111static int get_disk_info(mddev_t * mddev, void __user * arg)
5112{
5113 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 mdk_rdev_t *rdev;
5115
5116 if (copy_from_user(&info, arg, sizeof(info)))
5117 return -EFAULT;
5118
Andre Noll26ef3792008-07-11 22:02:21 +10005119 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 if (rdev) {
5121 info.major = MAJOR(rdev->bdev->bd_dev);
5122 info.minor = MINOR(rdev->bdev->bd_dev);
5123 info.raid_disk = rdev->raid_disk;
5124 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005125 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005127 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 info.state |= (1<<MD_DISK_ACTIVE);
5129 info.state |= (1<<MD_DISK_SYNC);
5130 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005131 if (test_bit(WriteMostly, &rdev->flags))
5132 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 } else {
5134 info.major = info.minor = 0;
5135 info.raid_disk = -1;
5136 info.state = (1<<MD_DISK_REMOVED);
5137 }
5138
5139 if (copy_to_user(arg, &info, sizeof(info)))
5140 return -EFAULT;
5141
5142 return 0;
5143}
5144
5145static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5146{
5147 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5148 mdk_rdev_t *rdev;
5149 dev_t dev = MKDEV(info->major,info->minor);
5150
5151 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5152 return -EOVERFLOW;
5153
5154 if (!mddev->raid_disks) {
5155 int err;
5156 /* expecting a device which has a superblock */
5157 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5158 if (IS_ERR(rdev)) {
5159 printk(KERN_WARNING
5160 "md: md_import_device returned %ld\n",
5161 PTR_ERR(rdev));
5162 return PTR_ERR(rdev);
5163 }
5164 if (!list_empty(&mddev->disks)) {
5165 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5166 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005167 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 .load_super(rdev, rdev0, mddev->minor_version);
5169 if (err < 0) {
5170 printk(KERN_WARNING
5171 "md: %s has different UUID to %s\n",
5172 bdevname(rdev->bdev,b),
5173 bdevname(rdev0->bdev,b2));
5174 export_rdev(rdev);
5175 return -EINVAL;
5176 }
5177 }
5178 err = bind_rdev_to_array(rdev, mddev);
5179 if (err)
5180 export_rdev(rdev);
5181 return err;
5182 }
5183
5184 /*
5185 * add_new_disk can be used once the array is assembled
5186 * to add "hot spares". They must already have a superblock
5187 * written
5188 */
5189 if (mddev->pers) {
5190 int err;
5191 if (!mddev->pers->hot_add_disk) {
5192 printk(KERN_WARNING
5193 "%s: personality does not support diskops!\n",
5194 mdname(mddev));
5195 return -EINVAL;
5196 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005197 if (mddev->persistent)
5198 rdev = md_import_device(dev, mddev->major_version,
5199 mddev->minor_version);
5200 else
5201 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 if (IS_ERR(rdev)) {
5203 printk(KERN_WARNING
5204 "md: md_import_device returned %ld\n",
5205 PTR_ERR(rdev));
5206 return PTR_ERR(rdev);
5207 }
NeilBrown1a855a02010-12-09 16:36:28 +11005208 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005209 if (!mddev->persistent) {
5210 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005211 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005212 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005213 set_bit(In_sync, &rdev->flags);
5214 } else
NeilBrown41158c72005-06-21 17:17:25 -07005215 rdev->raid_disk = -1;
5216 } else
5217 super_types[mddev->major_version].
5218 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005219 if ((info->state & (1<<MD_DISK_SYNC)) &&
5220 (!test_bit(In_sync, &rdev->flags) ||
5221 rdev->raid_disk != info->raid_disk)) {
5222 /* This was a hot-add request, but events doesn't
5223 * match, so reject it.
5224 */
5225 export_rdev(rdev);
5226 return -EINVAL;
5227 }
5228
NeilBrown1a855a02010-12-09 16:36:28 +11005229 if (test_bit(In_sync, &rdev->flags))
5230 rdev->saved_raid_disk = rdev->raid_disk;
5231 else
5232 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005233
NeilBrownb2d444d2005-11-08 21:39:31 -08005234 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005235 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5236 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005237 else
5238 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 rdev->raid_disk = -1;
5241 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005242 if (!err && !mddev->pers->hot_remove_disk) {
5243 /* If there is hot_add_disk but no hot_remove_disk
5244 * then added disks for geometry changes,
5245 * and should be added immediately.
5246 */
5247 super_types[mddev->major_version].
5248 validate_super(mddev, rdev);
5249 err = mddev->pers->hot_add_disk(mddev, rdev);
5250 if (err)
5251 unbind_rdev_from_array(rdev);
5252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 if (err)
5254 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005255 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005256 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005257
NeilBrown17571282006-12-10 02:20:52 -08005258 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005259 if (mddev->degraded)
5260 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005261 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005262 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 return err;
5264 }
5265
5266 /* otherwise, add_new_disk is only allowed
5267 * for major_version==0 superblocks
5268 */
5269 if (mddev->major_version != 0) {
5270 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5271 mdname(mddev));
5272 return -EINVAL;
5273 }
5274
5275 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5276 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005277 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 if (IS_ERR(rdev)) {
5279 printk(KERN_WARNING
5280 "md: error, md_import_device() returned %ld\n",
5281 PTR_ERR(rdev));
5282 return PTR_ERR(rdev);
5283 }
5284 rdev->desc_nr = info->number;
5285 if (info->raid_disk < mddev->raid_disks)
5286 rdev->raid_disk = info->raid_disk;
5287 else
5288 rdev->raid_disk = -1;
5289
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005291 if (info->state & (1<<MD_DISK_SYNC))
5292 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005294 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5295 set_bit(WriteMostly, &rdev->flags);
5296
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 if (!mddev->persistent) {
5298 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005299 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5300 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005301 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005302 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303
NeilBrown2bf071b2006-01-06 00:20:55 -08005304 err = bind_rdev_to_array(rdev, mddev);
5305 if (err) {
5306 export_rdev(rdev);
5307 return err;
5308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 }
5310
5311 return 0;
5312}
5313
5314static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5315{
5316 char b[BDEVNAME_SIZE];
5317 mdk_rdev_t *rdev;
5318
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 rdev = find_rdev(mddev, dev);
5320 if (!rdev)
5321 return -ENXIO;
5322
5323 if (rdev->raid_disk >= 0)
5324 goto busy;
5325
5326 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005327 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005328 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
5330 return 0;
5331busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005332 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 bdevname(rdev->bdev,b), mdname(mddev));
5334 return -EBUSY;
5335}
5336
5337static int hot_add_disk(mddev_t * mddev, dev_t dev)
5338{
5339 char b[BDEVNAME_SIZE];
5340 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 mdk_rdev_t *rdev;
5342
5343 if (!mddev->pers)
5344 return -ENODEV;
5345
5346 if (mddev->major_version != 0) {
5347 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5348 " version-0 superblocks.\n",
5349 mdname(mddev));
5350 return -EINVAL;
5351 }
5352 if (!mddev->pers->hot_add_disk) {
5353 printk(KERN_WARNING
5354 "%s: personality does not support diskops!\n",
5355 mdname(mddev));
5356 return -EINVAL;
5357 }
5358
NeilBrownd710e132008-10-13 11:55:12 +11005359 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 if (IS_ERR(rdev)) {
5361 printk(KERN_WARNING
5362 "md: error, md_import_device() returned %ld\n",
5363 PTR_ERR(rdev));
5364 return -EINVAL;
5365 }
5366
5367 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005368 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005370 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
NeilBrown8190e752009-06-18 08:48:58 +10005372 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373
NeilBrownb2d444d2005-11-08 21:39:31 -08005374 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 printk(KERN_WARNING
5376 "md: can not hot-add faulty %s disk to %s!\n",
5377 bdevname(rdev->bdev,b), mdname(mddev));
5378 err = -EINVAL;
5379 goto abort_export;
5380 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005381 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005383 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005384 err = bind_rdev_to_array(rdev, mddev);
5385 if (err)
5386 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
5388 /*
5389 * The rest should better be atomic, we can have disk failures
5390 * noticed in interrupt contexts ...
5391 */
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 rdev->raid_disk = -1;
5394
NeilBrown850b2b42006-10-03 01:15:46 -07005395 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
5397 /*
5398 * Kick recovery, maybe this spare has to be added to the
5399 * array immediately.
5400 */
5401 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5402 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005403 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 return 0;
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406abort_export:
5407 export_rdev(rdev);
5408 return err;
5409}
5410
NeilBrown32a76272005-06-21 17:17:14 -07005411static int set_bitmap_file(mddev_t *mddev, int fd)
5412{
5413 int err;
5414
NeilBrown36fa3062005-09-09 16:23:45 -07005415 if (mddev->pers) {
5416 if (!mddev->pers->quiesce)
5417 return -EBUSY;
5418 if (mddev->recovery || mddev->sync_thread)
5419 return -EBUSY;
5420 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005421 }
5422
NeilBrown36fa3062005-09-09 16:23:45 -07005423
5424 if (fd >= 0) {
5425 if (mddev->bitmap)
5426 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005427 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005428
NeilBrownc3d97142009-12-14 12:49:52 +11005429 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005430 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5431 mdname(mddev));
5432 return -EBADF;
5433 }
5434
NeilBrownc3d97142009-12-14 12:49:52 +11005435 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005436 if (err) {
5437 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5438 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005439 fput(mddev->bitmap_info.file);
5440 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005441 return err;
5442 }
NeilBrownc3d97142009-12-14 12:49:52 +11005443 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005444 } else if (mddev->bitmap == NULL)
5445 return -ENOENT; /* cannot remove what isn't there */
5446 err = 0;
5447 if (mddev->pers) {
5448 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005449 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005450 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005451 if (!err)
5452 err = bitmap_load(mddev);
5453 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005454 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005455 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005456 fd = -1; /* make sure to put the file */
5457 }
NeilBrown36fa3062005-09-09 16:23:45 -07005458 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005459 }
5460 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005461 if (mddev->bitmap_info.file) {
5462 restore_bitmap_write_access(mddev->bitmap_info.file);
5463 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005464 }
NeilBrownc3d97142009-12-14 12:49:52 +11005465 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005466 }
5467
NeilBrown32a76272005-06-21 17:17:14 -07005468 return err;
5469}
5470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471/*
5472 * set_array_info is used two different ways
5473 * The original usage is when creating a new array.
5474 * In this usage, raid_disks is > 0 and it together with
5475 * level, size, not_persistent,layout,chunksize determine the
5476 * shape of the array.
5477 * This will always create an array with a type-0.90.0 superblock.
5478 * The newer usage is when assembling an array.
5479 * In this case raid_disks will be 0, and the major_version field is
5480 * use to determine which style super-blocks are to be found on the devices.
5481 * The minor and patch _version numbers are also kept incase the
5482 * super_block handler wishes to interpret them.
5483 */
5484static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5485{
5486
5487 if (info->raid_disks == 0) {
5488 /* just setting version number for superblock loading */
5489 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005490 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 super_types[info->major_version].name == NULL) {
5492 /* maybe try to auto-load a module? */
5493 printk(KERN_INFO
5494 "md: superblock version %d not known\n",
5495 info->major_version);
5496 return -EINVAL;
5497 }
5498 mddev->major_version = info->major_version;
5499 mddev->minor_version = info->minor_version;
5500 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005501 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005502 /* ensure mddev_put doesn't delete this now that there
5503 * is some minimal configuration.
5504 */
5505 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 return 0;
5507 }
5508 mddev->major_version = MD_MAJOR_VERSION;
5509 mddev->minor_version = MD_MINOR_VERSION;
5510 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5511 mddev->ctime = get_seconds();
5512
5513 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005514 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005515 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 mddev->raid_disks = info->raid_disks;
5517 /* don't set md_minor, it is determined by which /dev/md* was
5518 * openned
5519 */
5520 if (info->state & (1<<MD_SB_CLEAN))
5521 mddev->recovery_cp = MaxSector;
5522 else
5523 mddev->recovery_cp = 0;
5524 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005525 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
5527 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005528 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530 mddev->max_disks = MD_SB_DISKS;
5531
NeilBrowne6910632008-02-06 01:39:51 -08005532 if (mddev->persistent)
5533 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005534 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
NeilBrownc3d97142009-12-14 12:49:52 +11005536 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5537 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005538
NeilBrownf6705572006-03-27 01:18:11 -08005539 mddev->reshape_position = MaxSector;
5540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 /*
5542 * Generate a 128 bit UUID
5543 */
5544 get_random_bytes(mddev->uuid, 16);
5545
NeilBrownf6705572006-03-27 01:18:11 -08005546 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005547 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005548 mddev->new_layout = mddev->layout;
5549 mddev->delta_disks = 0;
5550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 return 0;
5552}
5553
Dan Williams1f403622009-03-31 14:59:03 +11005554void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5555{
Dan Williamsb522adc2009-03-31 15:00:31 +11005556 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5557
5558 if (mddev->external_size)
5559 return;
5560
Dan Williams1f403622009-03-31 14:59:03 +11005561 mddev->array_sectors = array_sectors;
5562}
5563EXPORT_SYMBOL(md_set_array_sectors);
5564
Andre Nolld71f9f82008-07-11 22:02:22 +10005565static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005566{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005567 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005568 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005569 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005570
5571 if (mddev->pers->resize == NULL)
5572 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005573 /* The "num_sectors" is the number of sectors of each device that
5574 * is used. This can only make sense for arrays with redundancy.
5575 * linear and raid0 always use whatever space is available. We can only
5576 * consider changing this number if no resync or reconstruction is
5577 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005578 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005579 * of each device. If num_sectors is zero, we find the largest size
5580 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005581 */
5582 if (mddev->sync_thread)
5583 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005584 if (mddev->bitmap)
5585 /* Sorry, cannot grow a bitmap yet, just remove it,
5586 * grow, and re-add.
5587 */
5588 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005589 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005590 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005591
Andre Nolld71f9f82008-07-11 22:02:22 +10005592 if (fit && (num_sectors == 0 || num_sectors > avail))
5593 num_sectors = avail;
5594 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005595 return -ENOSPC;
5596 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005597 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005598 if (!rv)
5599 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005600 return rv;
5601}
5602
NeilBrownda943b92006-01-06 00:20:54 -08005603static int update_raid_disks(mddev_t *mddev, int raid_disks)
5604{
5605 int rv;
5606 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005607 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005608 return -EINVAL;
5609 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005610 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005611 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005612 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005613 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005614 mddev->delta_disks = raid_disks - mddev->raid_disks;
5615
5616 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005617 if (rv < 0)
5618 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005619 return rv;
5620}
5621
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623/*
5624 * update_array_info is used to change the configuration of an
5625 * on-line array.
5626 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5627 * fields in the info are checked against the array.
5628 * Any differences that cannot be handled will cause an error.
5629 * Normally, only one change can be managed at a time.
5630 */
5631static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5632{
5633 int rv = 0;
5634 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005635 int state = 0;
5636
5637 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005638 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005639 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
5641 if (mddev->major_version != info->major_version ||
5642 mddev->minor_version != info->minor_version ||
5643/* mddev->patch_version != info->patch_version || */
5644 mddev->ctime != info->ctime ||
5645 mddev->level != info->level ||
5646/* mddev->layout != info->layout || */
5647 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005648 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005649 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5650 ((state^info->state) & 0xfffffe00)
5651 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return -EINVAL;
5653 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005654 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5655 cnt++;
5656 if (mddev->raid_disks != info->raid_disks)
5657 cnt++;
5658 if (mddev->layout != info->layout)
5659 cnt++;
5660 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5661 cnt++;
5662 if (cnt == 0)
5663 return 0;
5664 if (cnt > 1)
5665 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
5667 if (mddev->layout != info->layout) {
5668 /* Change layout
5669 * we don't need to do anything at the md level, the
5670 * personality will take care of it all.
5671 */
NeilBrown50ac1682009-06-18 08:47:55 +10005672 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005674 else {
5675 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005676 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005677 if (rv)
5678 mddev->new_layout = mddev->layout;
5679 return rv;
5680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005682 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005683 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
NeilBrownda943b92006-01-06 00:20:54 -08005685 if (mddev->raid_disks != info->raid_disks)
5686 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
NeilBrown36fa3062005-09-09 16:23:45 -07005688 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5689 if (mddev->pers->quiesce == NULL)
5690 return -EINVAL;
5691 if (mddev->recovery || mddev->sync_thread)
5692 return -EBUSY;
5693 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5694 /* add the bitmap */
5695 if (mddev->bitmap)
5696 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005697 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005698 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005699 mddev->bitmap_info.offset =
5700 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005701 mddev->pers->quiesce(mddev, 1);
5702 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005703 if (!rv)
5704 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005705 if (rv)
5706 bitmap_destroy(mddev);
5707 mddev->pers->quiesce(mddev, 0);
5708 } else {
5709 /* remove the bitmap */
5710 if (!mddev->bitmap)
5711 return -ENOENT;
5712 if (mddev->bitmap->file)
5713 return -EINVAL;
5714 mddev->pers->quiesce(mddev, 1);
5715 bitmap_destroy(mddev);
5716 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005717 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005718 }
5719 }
NeilBrown850b2b42006-10-03 01:15:46 -07005720 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 return rv;
5722}
5723
5724static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5725{
5726 mdk_rdev_t *rdev;
5727
5728 if (mddev->pers == NULL)
5729 return -ENODEV;
5730
5731 rdev = find_rdev(mddev, dev);
5732 if (!rdev)
5733 return -ENODEV;
5734
5735 md_error(mddev, rdev);
5736 return 0;
5737}
5738
Andre Noll2f9618c2008-04-25 18:57:58 +02005739/*
5740 * We have a problem here : there is no easy way to give a CHS
5741 * virtual geometry. We currently pretend that we have a 2 heads
5742 * 4 sectors (with a BIG number of cylinders...). This drives
5743 * dosfs just mad... ;-)
5744 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005745static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5746{
5747 mddev_t *mddev = bdev->bd_disk->private_data;
5748
5749 geo->heads = 2;
5750 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005751 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005752 return 0;
5753}
5754
Al Viroa39907f2008-03-02 10:31:15 -05005755static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 unsigned int cmd, unsigned long arg)
5757{
5758 int err = 0;
5759 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005761 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 if (!capable(CAP_SYS_ADMIN))
5764 return -EACCES;
5765
5766 /*
5767 * Commands dealing with the RAID driver but not any
5768 * particular array:
5769 */
5770 switch (cmd)
5771 {
5772 case RAID_VERSION:
5773 err = get_version(argp);
5774 goto done;
5775
5776 case PRINT_RAID_DEBUG:
5777 err = 0;
5778 md_print_devices();
5779 goto done;
5780
5781#ifndef MODULE
5782 case RAID_AUTORUN:
5783 err = 0;
5784 autostart_arrays(arg);
5785 goto done;
5786#endif
5787 default:;
5788 }
5789
5790 /*
5791 * Commands creating/starting a new array:
5792 */
5793
Al Viroa39907f2008-03-02 10:31:15 -05005794 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
5796 if (!mddev) {
5797 BUG();
5798 goto abort;
5799 }
5800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 err = mddev_lock(mddev);
5802 if (err) {
5803 printk(KERN_INFO
5804 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5805 err, cmd);
5806 goto abort;
5807 }
5808
5809 switch (cmd)
5810 {
5811 case SET_ARRAY_INFO:
5812 {
5813 mdu_array_info_t info;
5814 if (!arg)
5815 memset(&info, 0, sizeof(info));
5816 else if (copy_from_user(&info, argp, sizeof(info))) {
5817 err = -EFAULT;
5818 goto abort_unlock;
5819 }
5820 if (mddev->pers) {
5821 err = update_array_info(mddev, &info);
5822 if (err) {
5823 printk(KERN_WARNING "md: couldn't update"
5824 " array info. %d\n", err);
5825 goto abort_unlock;
5826 }
5827 goto done_unlock;
5828 }
5829 if (!list_empty(&mddev->disks)) {
5830 printk(KERN_WARNING
5831 "md: array %s already has disks!\n",
5832 mdname(mddev));
5833 err = -EBUSY;
5834 goto abort_unlock;
5835 }
5836 if (mddev->raid_disks) {
5837 printk(KERN_WARNING
5838 "md: array %s already initialised!\n",
5839 mdname(mddev));
5840 err = -EBUSY;
5841 goto abort_unlock;
5842 }
5843 err = set_array_info(mddev, &info);
5844 if (err) {
5845 printk(KERN_WARNING "md: couldn't set"
5846 " array info. %d\n", err);
5847 goto abort_unlock;
5848 }
5849 }
5850 goto done_unlock;
5851
5852 default:;
5853 }
5854
5855 /*
5856 * Commands querying/configuring an existing array:
5857 */
NeilBrown32a76272005-06-21 17:17:14 -07005858 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005859 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005860 if ((!mddev->raid_disks && !mddev->external)
5861 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5862 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5863 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 err = -ENODEV;
5865 goto abort_unlock;
5866 }
5867
5868 /*
5869 * Commands even a read-only array can execute:
5870 */
5871 switch (cmd)
5872 {
5873 case GET_ARRAY_INFO:
5874 err = get_array_info(mddev, argp);
5875 goto done_unlock;
5876
NeilBrown32a76272005-06-21 17:17:14 -07005877 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005878 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005879 goto done_unlock;
5880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 case GET_DISK_INFO:
5882 err = get_disk_info(mddev, argp);
5883 goto done_unlock;
5884
5885 case RESTART_ARRAY_RW:
5886 err = restart_array(mddev);
5887 goto done_unlock;
5888
5889 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005890 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 goto done_unlock;
5892
5893 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005894 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 goto done_unlock;
5896
Dan Williamse2218352010-05-12 08:25:37 +10005897 case BLKROSET:
5898 if (get_user(ro, (int __user *)(arg))) {
5899 err = -EFAULT;
5900 goto done_unlock;
5901 }
5902 err = -EINVAL;
5903
5904 /* if the bdev is going readonly the value of mddev->ro
5905 * does not matter, no writes are coming
5906 */
5907 if (ro)
5908 goto done_unlock;
5909
5910 /* are we are already prepared for writes? */
5911 if (mddev->ro != 1)
5912 goto done_unlock;
5913
5914 /* transitioning to readauto need only happen for
5915 * arrays that call md_write_start
5916 */
5917 if (mddev->pers) {
5918 err = restart_array(mddev);
5919 if (err == 0) {
5920 mddev->ro = 2;
5921 set_disk_ro(mddev->gendisk, 0);
5922 }
5923 }
5924 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 }
5926
5927 /*
5928 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005929 * superblock, so we do not allow them on read-only arrays.
5930 * However non-MD ioctls (e.g. get-size) will still come through
5931 * here and hit the 'default' below, so only disallow
5932 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005934 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005935 if (mddev->ro == 2) {
5936 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005937 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005938 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5939 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005940 } else {
5941 err = -EROFS;
5942 goto abort_unlock;
5943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 }
5945
5946 switch (cmd)
5947 {
5948 case ADD_NEW_DISK:
5949 {
5950 mdu_disk_info_t info;
5951 if (copy_from_user(&info, argp, sizeof(info)))
5952 err = -EFAULT;
5953 else
5954 err = add_new_disk(mddev, &info);
5955 goto done_unlock;
5956 }
5957
5958 case HOT_REMOVE_DISK:
5959 err = hot_remove_disk(mddev, new_decode_dev(arg));
5960 goto done_unlock;
5961
5962 case HOT_ADD_DISK:
5963 err = hot_add_disk(mddev, new_decode_dev(arg));
5964 goto done_unlock;
5965
5966 case SET_DISK_FAULTY:
5967 err = set_disk_faulty(mddev, new_decode_dev(arg));
5968 goto done_unlock;
5969
5970 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005971 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 goto done_unlock;
5973
NeilBrown32a76272005-06-21 17:17:14 -07005974 case SET_BITMAP_FILE:
5975 err = set_bitmap_file(mddev, (int)arg);
5976 goto done_unlock;
5977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 err = -EINVAL;
5980 goto abort_unlock;
5981 }
5982
5983done_unlock:
5984abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005985 if (mddev->hold_active == UNTIL_IOCTL &&
5986 err != -EINVAL)
5987 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 mddev_unlock(mddev);
5989
5990 return err;
5991done:
5992 if (err)
5993 MD_BUG();
5994abort:
5995 return err;
5996}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005997#ifdef CONFIG_COMPAT
5998static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5999 unsigned int cmd, unsigned long arg)
6000{
6001 switch (cmd) {
6002 case HOT_REMOVE_DISK:
6003 case HOT_ADD_DISK:
6004 case SET_DISK_FAULTY:
6005 case SET_BITMAP_FILE:
6006 /* These take in integer arg, do not convert */
6007 break;
6008 default:
6009 arg = (unsigned long)compat_ptr(arg);
6010 break;
6011 }
6012
6013 return md_ioctl(bdev, mode, cmd, arg);
6014}
6015#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
Al Viroa39907f2008-03-02 10:31:15 -05006017static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
6019 /*
6020 * Succeed if we can lock the mddev, which confirms that
6021 * it isn't being stopped right now.
6022 */
NeilBrownd3374822009-01-09 08:31:10 +11006023 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 int err;
6025
NeilBrownd3374822009-01-09 08:31:10 +11006026 if (mddev->gendisk != bdev->bd_disk) {
6027 /* we are racing with mddev_put which is discarding this
6028 * bd_disk.
6029 */
6030 mddev_put(mddev);
6031 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006032 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006033 /* Then retry the open from the top */
6034 return -ERESTARTSYS;
6035 }
6036 BUG_ON(mddev != bdev->bd_disk->private_data);
6037
NeilBrownc8c00a62009-08-10 12:50:52 +10006038 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 goto out;
6040
6041 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006042 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006043 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006045 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 out:
6047 return err;
6048}
6049
Al Viroa39907f2008-03-02 10:31:15 -05006050static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051{
Al Viroa39907f2008-03-02 10:31:15 -05006052 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006054 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006055 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 mddev_put(mddev);
6057
6058 return 0;
6059}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006060
6061static int md_media_changed(struct gendisk *disk)
6062{
6063 mddev_t *mddev = disk->private_data;
6064
6065 return mddev->changed;
6066}
6067
6068static int md_revalidate(struct gendisk *disk)
6069{
6070 mddev_t *mddev = disk->private_data;
6071
6072 mddev->changed = 0;
6073 return 0;
6074}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006075static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076{
6077 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006078 .open = md_open,
6079 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006080 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006081#ifdef CONFIG_COMPAT
6082 .compat_ioctl = md_compat_ioctl,
6083#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006084 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006085 .media_changed = md_media_changed,
6086 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087};
6088
Adrian Bunk75c96f82005-05-05 16:16:09 -07006089static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090{
6091 mdk_thread_t *thread = arg;
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 /*
6094 * md_thread is a 'system-thread', it's priority should be very
6095 * high. We avoid resource deadlocks individually in each
6096 * raid personality. (RAID5 does preallocation) We also use RR and
6097 * the very same RT priority as kswapd, thus we will never get
6098 * into a priority inversion deadlock.
6099 *
6100 * we definitely have to have equal or higher priority than
6101 * bdflush, otherwise bdflush will deadlock if there are too
6102 * many dirty RAID5 blocks.
6103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
NeilBrown6985c432005-10-19 21:23:47 -07006105 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006106 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
NeilBrown93588e22005-11-15 00:09:12 -08006108 /* We need to wait INTERRUPTIBLE so that
6109 * we don't add to the load-average.
6110 * That means we need to be sure no signals are
6111 * pending
6112 */
6113 if (signal_pending(current))
6114 flush_signals(current);
6115
6116 wait_event_interruptible_timeout
6117 (thread->wqueue,
6118 test_bit(THREAD_WAKEUP, &thread->flags)
6119 || kthread_should_stop(),
6120 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
NeilBrown6c987912011-01-14 09:13:53 +11006122 clear_bit(THREAD_WAKEUP, &thread->flags);
6123 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006124 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006126
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 return 0;
6128}
6129
6130void md_wakeup_thread(mdk_thread_t *thread)
6131{
6132 if (thread) {
6133 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6134 set_bit(THREAD_WAKEUP, &thread->flags);
6135 wake_up(&thread->wqueue);
6136 }
6137}
6138
6139mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6140 const char *name)
6141{
6142 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
NeilBrown9ffae0c2006-01-06 00:20:32 -08006144 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 if (!thread)
6146 return NULL;
6147
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 init_waitqueue_head(&thread->wqueue);
6149
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 thread->run = run;
6151 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006152 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006153 thread->tsk = kthread_run(md_thread, thread,
6154 "%s_%s",
6155 mdname(thread->mddev),
6156 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006157 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 kfree(thread);
6159 return NULL;
6160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 return thread;
6162}
6163
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164void md_unregister_thread(mdk_thread_t *thread)
6165{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006166 if (!thread)
6167 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006168 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006169
6170 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 kfree(thread);
6172}
6173
6174void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6175{
6176 if (!mddev) {
6177 MD_BUG();
6178 return;
6179 }
6180
NeilBrownb2d444d2005-11-08 21:39:31 -08006181 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006183
6184 if (mddev->external)
6185 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6188 mdname(mddev),
6189 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6190 __builtin_return_address(0),__builtin_return_address(1),
6191 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006192*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006193 if (!mddev->pers)
6194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 if (!mddev->pers->error_handler)
6196 return;
6197 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006198 if (mddev->degraded)
6199 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006200 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6202 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6203 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006204 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006205 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006206 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207}
6208
6209/* seq_file implementation /proc/mdstat */
6210
6211static void status_unused(struct seq_file *seq)
6212{
6213 int i = 0;
6214 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
6216 seq_printf(seq, "unused devices: ");
6217
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006218 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 char b[BDEVNAME_SIZE];
6220 i++;
6221 seq_printf(seq, "%s ",
6222 bdevname(rdev->bdev,b));
6223 }
6224 if (!i)
6225 seq_printf(seq, "<none>");
6226
6227 seq_printf(seq, "\n");
6228}
6229
6230
6231static void status_resync(struct seq_file *seq, mddev_t * mddev)
6232{
NeilBrowndd71cf62009-05-07 12:49:35 +10006233 sector_t max_sectors, resync, res;
6234 unsigned long dt, db;
6235 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006236 int scale;
6237 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
NeilBrowndd71cf62009-05-07 12:49:35 +10006239 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006242 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006244 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
6246 /*
6247 * Should not happen.
6248 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006249 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 MD_BUG();
6251 return;
6252 }
NeilBrown4588b422006-03-27 01:18:04 -08006253 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006254 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006255 * u32, as those are the requirements for sector_div.
6256 * Thus 'scale' must be at least 10
6257 */
6258 scale = 10;
6259 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006260 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006261 scale++;
6262 }
6263 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006264 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006265
6266 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 {
NeilBrown4588b422006-03-27 01:18:04 -08006268 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 seq_printf(seq, "[");
6270 for (i = 0; i < x; i++)
6271 seq_printf(seq, "=");
6272 seq_printf(seq, ">");
6273 for (i = 0; i < y; i++)
6274 seq_printf(seq, ".");
6275 seq_printf(seq, "] ");
6276 }
NeilBrown4588b422006-03-27 01:18:04 -08006277 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006278 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6279 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006280 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6281 "check" :
6282 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6283 "resync" : "recovery"))),
6284 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006285 (unsigned long long) resync/2,
6286 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
6288 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 * dt: time from mark until now
6290 * db: blocks written from mark until now
6291 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006292 *
6293 * rt is a sector_t, so could be 32bit or 64bit.
6294 * So we divide before multiply in case it is 32bit and close
6295 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006296 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006297 * near the end of resync when the number of remaining sectors
6298 * is close to 'db'.
6299 * We then divide rt by 32 after multiplying by db to compensate.
6300 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 */
6302 dt = ((jiffies - mddev->resync_mark) / HZ);
6303 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006304 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6305 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
NeilBrowndd71cf62009-05-07 12:49:35 +10006307 rt = max_sectors - resync; /* number of remaining sectors */
6308 sector_div(rt, db/32+1);
6309 rt *= dt;
6310 rt >>= 5;
6311
6312 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6313 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
NeilBrownff4e8d92006-07-10 04:44:16 -07006315 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316}
6317
6318static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6319{
6320 struct list_head *tmp;
6321 loff_t l = *pos;
6322 mddev_t *mddev;
6323
6324 if (l >= 0x10000)
6325 return NULL;
6326 if (!l--)
6327 /* header */
6328 return (void*)1;
6329
6330 spin_lock(&all_mddevs_lock);
6331 list_for_each(tmp,&all_mddevs)
6332 if (!l--) {
6333 mddev = list_entry(tmp, mddev_t, all_mddevs);
6334 mddev_get(mddev);
6335 spin_unlock(&all_mddevs_lock);
6336 return mddev;
6337 }
6338 spin_unlock(&all_mddevs_lock);
6339 if (!l--)
6340 return (void*)2;/* tail */
6341 return NULL;
6342}
6343
6344static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6345{
6346 struct list_head *tmp;
6347 mddev_t *next_mddev, *mddev = v;
6348
6349 ++*pos;
6350 if (v == (void*)2)
6351 return NULL;
6352
6353 spin_lock(&all_mddevs_lock);
6354 if (v == (void*)1)
6355 tmp = all_mddevs.next;
6356 else
6357 tmp = mddev->all_mddevs.next;
6358 if (tmp != &all_mddevs)
6359 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6360 else {
6361 next_mddev = (void*)2;
6362 *pos = 0x10000;
6363 }
6364 spin_unlock(&all_mddevs_lock);
6365
6366 if (v != (void*)1)
6367 mddev_put(mddev);
6368 return next_mddev;
6369
6370}
6371
6372static void md_seq_stop(struct seq_file *seq, void *v)
6373{
6374 mddev_t *mddev = v;
6375
6376 if (mddev && v != (void*)1 && v != (void*)2)
6377 mddev_put(mddev);
6378}
6379
NeilBrownd7603b72006-01-06 00:20:30 -08006380struct mdstat_info {
6381 int event;
6382};
6383
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384static int md_seq_show(struct seq_file *seq, void *v)
6385{
6386 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006387 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006389 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006390 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
6392 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006393 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 seq_printf(seq, "Personalities : ");
6395 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006396 list_for_each_entry(pers, &pers_list, list)
6397 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398
6399 spin_unlock(&pers_lock);
6400 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006401 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 return 0;
6403 }
6404 if (v == (void*)2) {
6405 status_unused(seq);
6406 return 0;
6407 }
6408
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006409 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006411
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6413 seq_printf(seq, "%s : %sactive", mdname(mddev),
6414 mddev->pers ? "" : "in");
6415 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006416 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006418 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006419 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 seq_printf(seq, " %s", mddev->pers->name);
6421 }
6422
Andre Nolldd8ac332009-03-31 14:33:13 +11006423 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006424 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 char b[BDEVNAME_SIZE];
6426 seq_printf(seq, " %s[%d]",
6427 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006428 if (test_bit(WriteMostly, &rdev->flags))
6429 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006430 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 seq_printf(seq, "(F)");
6432 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006433 } else if (rdev->raid_disk < 0)
6434 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006435 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 }
6437
6438 if (!list_empty(&mddev->disks)) {
6439 if (mddev->pers)
6440 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006441 (unsigned long long)
6442 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 else
6444 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006445 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006447 if (mddev->persistent) {
6448 if (mddev->major_version != 0 ||
6449 mddev->minor_version != 90) {
6450 seq_printf(seq," super %d.%d",
6451 mddev->major_version,
6452 mddev->minor_version);
6453 }
NeilBrowne6910632008-02-06 01:39:51 -08006454 } else if (mddev->external)
6455 seq_printf(seq, " super external:%s",
6456 mddev->metadata_type);
6457 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006458 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459
6460 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006461 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006463 if (mddev->pers->sync_request) {
6464 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006465 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006466 seq_printf(seq, "\n ");
6467 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6468 seq_printf(seq, "\tresync=DELAYED\n ");
6469 else if (mddev->recovery_cp < MaxSector)
6470 seq_printf(seq, "\tresync=PENDING\n ");
6471 }
NeilBrown32a76272005-06-21 17:17:14 -07006472 } else
6473 seq_printf(seq, "\n ");
6474
6475 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006476 unsigned long chunk_kb;
6477 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006478 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006479 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006480 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6481 "%lu%s chunk",
6482 bitmap->pages - bitmap->missing_pages,
6483 bitmap->pages,
6484 (bitmap->pages - bitmap->missing_pages)
6485 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006486 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006487 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006488 if (bitmap->file) {
6489 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006490 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006491 }
NeilBrown78d742d2005-06-21 17:17:15 -07006492
NeilBrown32a76272005-06-21 17:17:14 -07006493 seq_printf(seq, "\n");
6494 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496
6497 seq_printf(seq, "\n");
6498 }
6499 mddev_unlock(mddev);
6500
6501 return 0;
6502}
6503
Jan Engelhardt110518b2009-05-07 12:49:37 +10006504static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 .start = md_seq_start,
6506 .next = md_seq_next,
6507 .stop = md_seq_stop,
6508 .show = md_seq_show,
6509};
6510
6511static int md_seq_open(struct inode *inode, struct file *file)
6512{
6513 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006514 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6515 if (mi == NULL)
6516 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
6518 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006519 if (error)
6520 kfree(mi);
6521 else {
6522 struct seq_file *p = file->private_data;
6523 p->private = mi;
6524 mi->event = atomic_read(&md_event_count);
6525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 return error;
6527}
6528
NeilBrownd7603b72006-01-06 00:20:30 -08006529static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6530{
6531 struct seq_file *m = filp->private_data;
6532 struct mdstat_info *mi = m->private;
6533 int mask;
6534
6535 poll_wait(filp, &md_event_waiters, wait);
6536
6537 /* always allow read */
6538 mask = POLLIN | POLLRDNORM;
6539
6540 if (mi->event != atomic_read(&md_event_count))
6541 mask |= POLLERR | POLLPRI;
6542 return mask;
6543}
6544
Arjan van de Venfa027c22007-02-12 00:55:33 -08006545static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006546 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 .open = md_seq_open,
6548 .read = seq_read,
6549 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006550 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006551 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552};
6553
NeilBrown2604b702006-01-06 00:20:36 -08006554int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006557 list_add_tail(&p->list, &pers_list);
6558 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 spin_unlock(&pers_lock);
6560 return 0;
6561}
6562
NeilBrown2604b702006-01-06 00:20:36 -08006563int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564{
NeilBrown2604b702006-01-06 00:20:36 -08006565 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006567 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 spin_unlock(&pers_lock);
6569 return 0;
6570}
6571
NeilBrowneea1bf32009-03-31 14:27:02 +11006572static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573{
6574 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006576 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
6578 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006579 rcu_read_lock();
6580 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006582 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6583 (int)part_stat_read(&disk->part0, sectors[1]) -
6584 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006585 /* sync IO will cause sync_io to increase before the disk_stats
6586 * as sync_io is counted when a request starts, and
6587 * disk_stats is counted when it completes.
6588 * So resync activity will cause curr_events to be smaller than
6589 * when there was no such activity.
6590 * non-sync IO will cause disk_stat to increase without
6591 * increasing sync_io so curr_events will (eventually)
6592 * be larger than it was before. Once it becomes
6593 * substantially larger, the test below will cause
6594 * the array to appear non-idle, and resync will slow
6595 * down.
6596 * If there is a lot of outstanding resync activity when
6597 * we set last_event to curr_events, then all that activity
6598 * completing might cause the array to appear non-idle
6599 * and resync will be slowed down even though there might
6600 * not have been non-resync activity. This will only
6601 * happen once though. 'last_events' will soon reflect
6602 * the state where there is little or no outstanding
6603 * resync requests, and further resync activity will
6604 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006605 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006607 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 rdev->last_events = curr_events;
6609 idle = 0;
6610 }
6611 }
NeilBrown4b809912008-07-21 17:05:25 +10006612 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 return idle;
6614}
6615
6616void md_done_sync(mddev_t *mddev, int blocks, int ok)
6617{
6618 /* another "blocks" (512byte) blocks have been synced */
6619 atomic_sub(blocks, &mddev->recovery_active);
6620 wake_up(&mddev->recovery_wait);
6621 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006622 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 md_wakeup_thread(mddev->thread);
6624 // stop recovery, signal do_sync ....
6625 }
6626}
6627
6628
NeilBrown06d91a52005-06-21 17:17:12 -07006629/* md_write_start(mddev, bi)
6630 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006631 * in superblock) before writing, schedule a superblock update
6632 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006633 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006634void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635{
Neil Brown0fd62b82008-06-28 08:31:36 +10006636 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006637 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006638 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006639
NeilBrownf91de922005-11-08 21:39:36 -08006640 BUG_ON(mddev->ro == 1);
6641 if (mddev->ro == 2) {
6642 /* need to switch to read/write */
6643 mddev->ro = 0;
6644 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6645 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006646 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006647 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006648 }
NeilBrown06d91a52005-06-21 17:17:12 -07006649 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006650 if (mddev->safemode == 1)
6651 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006652 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006653 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006654 if (mddev->in_sync) {
6655 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006656 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006657 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006658 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006659 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006660 }
NeilBrowna9701a32005-11-08 21:39:34 -08006661 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006662 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006663 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006664 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006665 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006666 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667}
6668
6669void md_write_end(mddev_t *mddev)
6670{
6671 if (atomic_dec_and_test(&mddev->writes_pending)) {
6672 if (mddev->safemode == 2)
6673 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006674 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6676 }
6677}
6678
NeilBrown2a2275d2007-01-26 00:57:11 -08006679/* md_allow_write(mddev)
6680 * Calling this ensures that the array is marked 'active' so that writes
6681 * may proceed without blocking. It is important to call this before
6682 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6683 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006684 *
6685 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6686 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006687 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006688int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006689{
6690 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006691 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006692 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006693 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006694 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006695 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006696
6697 spin_lock_irq(&mddev->write_lock);
6698 if (mddev->in_sync) {
6699 mddev->in_sync = 0;
6700 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006701 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006702 if (mddev->safemode_delay &&
6703 mddev->safemode == 0)
6704 mddev->safemode = 1;
6705 spin_unlock_irq(&mddev->write_lock);
6706 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006707 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006708 } else
6709 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006710
NeilBrown070dc6d2010-08-30 17:33:34 +10006711 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006712 return -EAGAIN;
6713 else
6714 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006715}
6716EXPORT_SYMBOL_GPL(md_allow_write);
6717
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718#define SYNC_MARKS 10
6719#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006720void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721{
6722 mddev_t *mddev2;
6723 unsigned int currspeed = 0,
6724 window;
NeilBrown57afd892005-06-21 17:17:13 -07006725 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 unsigned long mark[SYNC_MARKS];
6727 sector_t mark_cnt[SYNC_MARKS];
6728 int last_mark,m;
6729 struct list_head *tmp;
6730 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006731 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006732 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006733 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
6735 /* just incase thread restarts... */
6736 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6737 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006738 if (mddev->ro) /* never try to sync a read-only array */
6739 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740
NeilBrown61df9d92006-10-03 01:15:57 -07006741 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6742 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6743 desc = "data-check";
6744 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6745 desc = "requested-resync";
6746 else
6747 desc = "resync";
6748 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6749 desc = "reshape";
6750 else
6751 desc = "recovery";
6752
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 /* we overload curr_resync somewhat here.
6754 * 0 == not engaged in resync at all
6755 * 2 == checking that there is no conflict with another sync
6756 * 1 == like 2, but have yielded to allow conflicting resync to
6757 * commense
6758 * other == active in resync - this many blocks
6759 *
6760 * Before starting a resync we must have set curr_resync to
6761 * 2, and then checked that every "conflicting" array has curr_resync
6762 * less than ours. When we find one that is the same or higher
6763 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6764 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6765 * This will mean we have to start checking from the beginning again.
6766 *
6767 */
6768
6769 do {
6770 mddev->curr_resync = 2;
6771
6772 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006773 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006774 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006775
6776 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006778 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 if (mddev2 == mddev)
6780 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006781 if (!mddev->parallel_resync
6782 && mddev2->curr_resync
6783 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 DEFINE_WAIT(wq);
6785 if (mddev < mddev2 && mddev->curr_resync == 2) {
6786 /* arbitrarily yield */
6787 mddev->curr_resync = 1;
6788 wake_up(&resync_wait);
6789 }
6790 if (mddev > mddev2 && mddev->curr_resync == 1)
6791 /* no need to wait here, we can wait the next
6792 * time 'round when curr_resync == 2
6793 */
6794 continue;
NeilBrown97441972008-09-19 11:49:54 +10006795 /* We need to wait 'interruptible' so as not to
6796 * contribute to the load average, and not to
6797 * be caught by 'softlockup'
6798 */
6799 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006800 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006801 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006802 printk(KERN_INFO "md: delaying %s of %s"
6803 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006805 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006807 if (signal_pending(current))
6808 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 schedule();
6810 finish_wait(&resync_wait, &wq);
6811 goto try_again;
6812 }
6813 finish_wait(&resync_wait, &wq);
6814 }
6815 }
6816 } while (mddev->curr_resync < 2);
6817
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006818 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006819 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006821 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 */
6823 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006824 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006825 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006826 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6827 j = mddev->resync_min;
6828 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006829 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006830
NeilBrownccfcc3c2006-03-27 01:18:09 -08006831 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006832 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006833 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006835 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006836 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006837 rcu_read_lock();
6838 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006839 if (rdev->raid_disk >= 0 &&
6840 !test_bit(Faulty, &rdev->flags) &&
6841 !test_bit(In_sync, &rdev->flags) &&
6842 rdev->recovery_offset < j)
6843 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006844 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
NeilBrown61df9d92006-10-03 01:15:57 -07006847 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6848 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6849 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006850 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006851 "(but not more than %d KB/sec) for %s.\n",
6852 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853
NeilBrowneea1bf32009-03-31 14:27:02 +11006854 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006855
NeilBrown57afd892005-06-21 17:17:13 -07006856 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 for (m = 0; m < SYNC_MARKS; m++) {
6858 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006859 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 }
6861 last_mark = 0;
6862 mddev->resync_mark = mark[last_mark];
6863 mddev->resync_mark_cnt = mark_cnt[last_mark];
6864
6865 /*
6866 * Tune reconstruction:
6867 */
6868 window = 32*(PAGE_SIZE/512);
6869 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6870 window/2,(unsigned long long) max_sectors/2);
6871
6872 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 last_check = 0;
6874
6875 if (j>2) {
6876 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006877 "md: resuming %s of %s from checkpoint.\n",
6878 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 mddev->curr_resync = j;
6880 }
NeilBrown75d3da42011-01-14 09:14:34 +11006881 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
6883 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006884 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
NeilBrown57afd892005-06-21 17:17:13 -07006886 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006887
NeilBrown7a91ee12009-05-26 12:57:21 +10006888 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6889 ((mddev->curr_resync > mddev->curr_resync_completed &&
6890 (mddev->curr_resync - mddev->curr_resync_completed)
6891 > (max_sectors >> 4)) ||
6892 (j - mddev->curr_resync_completed)*2
6893 >= mddev->resync_max - mddev->curr_resync_completed
6894 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006895 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11006896 wait_event(mddev->recovery_wait,
6897 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11006898 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10006899 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006900 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006901 }
NeilBrownacb180b2009-04-14 16:28:34 +10006902
NeilBrowne62e58a2009-07-01 13:15:35 +10006903 while (j >= mddev->resync_max && !kthread_should_stop()) {
6904 /* As this condition is controlled by user-space,
6905 * we can block indefinitely, so use '_interruptible'
6906 * to avoid triggering warnings.
6907 */
6908 flush_signals(current); /* just in case */
6909 wait_event_interruptible(mddev->recovery_wait,
6910 mddev->resync_max > j
6911 || kthread_should_stop());
6912 }
NeilBrownacb180b2009-04-14 16:28:34 +10006913
6914 if (kthread_should_stop())
6915 goto interrupted;
6916
NeilBrown57afd892005-06-21 17:17:13 -07006917 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006918 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006919 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006920 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 goto out;
6922 }
NeilBrown57afd892005-06-21 17:17:13 -07006923
6924 if (!skipped) { /* actual IO requested */
6925 io_sectors += sectors;
6926 atomic_add(sectors, &mddev->recovery_active);
6927 }
6928
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 j += sectors;
6930 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006931 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006932 if (last_check == 0)
6933 /* this is the earliers that rebuilt will be
6934 * visible in /proc/mdstat
6935 */
6936 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006937
6938 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 continue;
6940
NeilBrown57afd892005-06-21 17:17:13 -07006941 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942
NeilBrowndfc70642008-05-23 13:04:39 -07006943 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 break;
6945
6946 repeat:
6947 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6948 /* step marks */
6949 int next = (last_mark+1) % SYNC_MARKS;
6950
6951 mddev->resync_mark = mark[next];
6952 mddev->resync_mark_cnt = mark_cnt[next];
6953 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006954 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 last_mark = next;
6956 }
6957
6958
NeilBrownc6207272008-02-06 01:39:52 -08006959 if (kthread_should_stop())
6960 goto interrupted;
6961
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
6963 /*
6964 * this loop exits only if either when we are slower than
6965 * the 'hard' speed limit, or the system was IO-idle for
6966 * a jiffy.
6967 * the system might be non-idle CPU-wise, but we only care
6968 * about not overloading the IO subsystem. (things like an
6969 * e2fsck being done on the RAID array should execute fast)
6970 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 cond_resched();
6972
NeilBrown57afd892005-06-21 17:17:13 -07006973 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6974 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
NeilBrown88202a02006-01-06 00:21:36 -08006976 if (currspeed > speed_min(mddev)) {
6977 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006978 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006979 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 goto repeat;
6981 }
6982 }
6983 }
NeilBrown61df9d92006-10-03 01:15:57 -07006984 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 /*
6986 * this also signals 'finished resyncing' to md_stop
6987 */
6988 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6990
6991 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006992 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
NeilBrowndfc70642008-05-23 13:04:39 -07006994 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006995 mddev->curr_resync > 2) {
6996 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6997 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6998 if (mddev->curr_resync >= mddev->recovery_cp) {
6999 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007000 "md: checkpointing %s of %s.\n",
7001 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007002 mddev->recovery_cp = mddev->curr_resync;
7003 }
7004 } else
7005 mddev->recovery_cp = MaxSector;
7006 } else {
7007 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7008 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007009 rcu_read_lock();
7010 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007011 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007012 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007013 !test_bit(Faulty, &rdev->flags) &&
7014 !test_bit(In_sync, &rdev->flags) &&
7015 rdev->recovery_offset < mddev->curr_resync)
7016 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007017 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 }
NeilBrown17571282006-12-10 02:20:52 -08007020 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007023 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7024 /* We completed so min/max setting can be forgotten if used. */
7025 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7026 mddev->resync_min = 0;
7027 mddev->resync_max = MaxSector;
7028 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7029 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 mddev->curr_resync = 0;
7031 wake_up(&resync_wait);
7032 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7033 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007034 return;
7035
7036 interrupted:
7037 /*
7038 * got a signal, exit.
7039 */
7040 printk(KERN_INFO
7041 "md: md_do_sync() got signal ... exiting\n");
7042 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7043 goto out;
7044
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045}
NeilBrown29269552006-03-27 01:18:10 -08007046EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
7048
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007049static int remove_and_add_spares(mddev_t *mddev)
7050{
7051 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007052 int spares = 0;
7053
NeilBrown97e4f422009-03-31 14:33:13 +11007054 mddev->curr_resync_completed = 0;
7055
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007056 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007057 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007058 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007059 (test_bit(Faulty, &rdev->flags) ||
7060 ! test_bit(In_sync, &rdev->flags)) &&
7061 atomic_read(&rdev->nr_pending)==0) {
7062 if (mddev->pers->hot_remove_disk(
7063 mddev, rdev->raid_disk)==0) {
7064 char nm[20];
7065 sprintf(nm,"rd%d", rdev->raid_disk);
7066 sysfs_remove_link(&mddev->kobj, nm);
7067 rdev->raid_disk = -1;
7068 }
7069 }
7070
NeilBrowna8c42c72011-01-31 13:47:13 +11007071 if (mddev->degraded && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007072 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007073 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007074 !test_bit(In_sync, &rdev->flags) &&
7075 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007076 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007077 if (rdev->raid_disk < 0
7078 && !test_bit(Faulty, &rdev->flags)) {
7079 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007080 if (mddev->pers->
7081 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007082 char nm[20];
7083 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007084 if (sysfs_create_link(&mddev->kobj,
7085 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007086 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007087 spares++;
7088 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007089 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007090 } else
7091 break;
7092 }
NeilBrowndfc70642008-05-23 13:04:39 -07007093 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007094 }
7095 return spares;
7096}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007097
7098static void reap_sync_thread(mddev_t *mddev)
7099{
7100 mdk_rdev_t *rdev;
7101
7102 /* resync has finished, collect result */
7103 md_unregister_thread(mddev->sync_thread);
7104 mddev->sync_thread = NULL;
7105 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7106 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7107 /* success...*/
7108 /* activate any spares */
7109 if (mddev->pers->spare_active(mddev))
7110 sysfs_notify(&mddev->kobj, NULL,
7111 "degraded");
7112 }
7113 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7114 mddev->pers->finish_reshape)
7115 mddev->pers->finish_reshape(mddev);
7116 md_update_sb(mddev, 1);
7117
7118 /* if array is no-longer degraded, then any saved_raid_disk
7119 * information must be scrapped
7120 */
7121 if (!mddev->degraded)
7122 list_for_each_entry(rdev, &mddev->disks, same_set)
7123 rdev->saved_raid_disk = -1;
7124
7125 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7126 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7127 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7128 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7129 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7130 /* flag recovery needed just to double check */
7131 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7132 sysfs_notify_dirent_safe(mddev->sysfs_action);
7133 md_new_event(mddev);
7134}
7135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136/*
7137 * This routine is regularly called by all per-raid-array threads to
7138 * deal with generic issues like resync and super-block update.
7139 * Raid personalities that don't have a thread (linear/raid0) do not
7140 * need this as they never do any recovery or update the superblock.
7141 *
7142 * It does not do any resync itself, but rather "forks" off other threads
7143 * to do that as needed.
7144 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7145 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007146 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 * and wakeups up this thread which will reap the thread and finish up.
7148 * This thread also removes any faulty devices (with nr_pending == 0).
7149 *
7150 * The overall approach is:
7151 * 1/ if the superblock needs updating, update it.
7152 * 2/ If a recovery thread is running, don't do anything else.
7153 * 3/ If recovery has finished, clean up, possibly marking spares active.
7154 * 4/ If there are any faulty devices, remove them.
7155 * 5/ If array is degraded, try to add spares devices
7156 * 6/ If array has spares or is not in-sync, start a resync thread.
7157 */
7158void md_check_recovery(mddev_t *mddev)
7159{
NeilBrown5f404022005-06-21 17:17:16 -07007160 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007161 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
7163 if (mddev->ro)
7164 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007165
7166 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007167 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007168 printk(KERN_INFO "md: %s in immediate safe mode\n",
7169 mdname(mddev));
7170 mddev->safemode = 2;
7171 }
7172 flush_signals(current);
7173 }
7174
NeilBrownc89a8ee2008-08-05 15:54:13 +10007175 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7176 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007178 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007180 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007181 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007182 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7183 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 ))
7185 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007186
NeilBrowndf5b89b2006-03-27 01:18:20 -08007187 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007188 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007189
NeilBrownc89a8ee2008-08-05 15:54:13 +10007190 if (mddev->ro) {
7191 /* Only thing we do on a ro array is remove
7192 * failed devices.
7193 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007194 mdk_rdev_t *rdev;
7195 list_for_each_entry(rdev, &mddev->disks, same_set)
7196 if (rdev->raid_disk >= 0 &&
7197 !test_bit(Blocked, &rdev->flags) &&
7198 test_bit(Faulty, &rdev->flags) &&
7199 atomic_read(&rdev->nr_pending)==0) {
7200 if (mddev->pers->hot_remove_disk(
7201 mddev, rdev->raid_disk)==0) {
7202 char nm[20];
7203 sprintf(nm,"rd%d", rdev->raid_disk);
7204 sysfs_remove_link(&mddev->kobj, nm);
7205 rdev->raid_disk = -1;
7206 }
7207 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007208 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7209 goto unlock;
7210 }
7211
NeilBrown31a59e32008-04-30 00:52:30 -07007212 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007213 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007214 spin_lock_irq(&mddev->write_lock);
7215 if (mddev->safemode &&
7216 !atomic_read(&mddev->writes_pending) &&
7217 !mddev->in_sync &&
7218 mddev->recovery_cp == MaxSector) {
7219 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007220 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007221 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007222 }
7223 if (mddev->safemode == 1)
7224 mddev->safemode = 0;
7225 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007226 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007227 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007228 }
NeilBrownfca4d842005-06-21 17:17:11 -07007229
NeilBrown850b2b42006-10-03 01:15:46 -07007230 if (mddev->flags)
7231 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007232
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7234 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7235 /* resync/recovery still happening */
7236 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7237 goto unlock;
7238 }
7239 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007240 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241 goto unlock;
7242 }
Neil Brown72a23c22008-06-28 08:31:41 +10007243 /* Set RUNNING before clearing NEEDED to avoid
7244 * any transients in the value of "sync_action".
7245 */
7246 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7247 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007248 /* Clear some bits that don't mean anything, but
7249 * might be left set
7250 */
NeilBrown24dd4692005-11-08 21:39:26 -08007251 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7252 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007254 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7255 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 /* no recovery is running.
7257 * remove any failed drives, then
7258 * add spares if possible.
7259 * Spare are also removed and re-added, to allow
7260 * the personality to fail the re-add.
7261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007263 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007264 if (mddev->pers->check_reshape == NULL ||
7265 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007266 /* Cannot proceed */
7267 goto unlock;
7268 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007269 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007270 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007271 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7272 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007273 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007274 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007275 } else if (mddev->recovery_cp < MaxSector) {
7276 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007277 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007278 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7279 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007281
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007283 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7284 /* We are adding a device or devices to an array
7285 * which has the bitmap stored on all devices.
7286 * So make sure all bitmap pages get written
7287 */
7288 bitmap_write_all(mddev->bitmap);
7289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 mddev->sync_thread = md_register_thread(md_do_sync,
7291 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007292 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 if (!mddev->sync_thread) {
7294 printk(KERN_ERR "%s: could not start resync"
7295 " thread...\n",
7296 mdname(mddev));
7297 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007298 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7299 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7300 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7301 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7302 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007303 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007305 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007306 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 }
7308 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007309 if (!mddev->sync_thread) {
7310 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7311 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7312 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007313 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007314 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 mddev_unlock(mddev);
7317 }
7318}
7319
Dan Williams6bfe0b42008-04-30 00:52:32 -07007320void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7321{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007322 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007323 wait_event_timeout(rdev->blocked_wait,
7324 !test_bit(Blocked, &rdev->flags),
7325 msecs_to_jiffies(5000));
7326 rdev_dec_pending(rdev, mddev);
7327}
7328EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7329
Adrian Bunk75c96f82005-05-05 16:16:09 -07007330static int md_notify_reboot(struct notifier_block *this,
7331 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332{
7333 struct list_head *tmp;
7334 mddev_t *mddev;
7335
7336 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7337
7338 printk(KERN_INFO "md: stopping all md devices.\n");
7339
NeilBrown29ac4aa2008-02-06 01:39:58 -08007340 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007341 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007342 /* Force a switch to readonly even array
7343 * appears to still be in use. Hence
7344 * the '100'.
7345 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007346 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007347 mddev_unlock(mddev);
7348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 /*
7350 * certain more exotic SCSI devices are known to be
7351 * volatile wrt too early system reboots. While the
7352 * right place to handle this issue is the given
7353 * driver, we do want to have a safe RAID driver ...
7354 */
7355 mdelay(1000*1);
7356 }
7357 return NOTIFY_DONE;
7358}
7359
Adrian Bunk75c96f82005-05-05 16:16:09 -07007360static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 .notifier_call = md_notify_reboot,
7362 .next = NULL,
7363 .priority = INT_MAX, /* before any real devices */
7364};
7365
7366static void md_geninit(void)
7367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7369
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007370 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371}
7372
Adrian Bunk75c96f82005-05-05 16:16:09 -07007373static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374{
Tejun Heoe804ac72010-10-15 15:36:08 +02007375 int ret = -ENOMEM;
7376
Tejun Heoada609e2011-01-25 14:35:54 +01007377 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02007378 if (!md_wq)
7379 goto err_wq;
7380
7381 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7382 if (!md_misc_wq)
7383 goto err_misc_wq;
7384
7385 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7386 goto err_md;
7387
7388 if ((ret = register_blkdev(0, "mdp")) < 0)
7389 goto err_mdp;
7390 mdp_major = ret;
7391
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007392 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007393 md_probe, NULL, NULL);
7394 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395 md_probe, NULL, NULL);
7396
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007398 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
7400 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402
Tejun Heoe804ac72010-10-15 15:36:08 +02007403err_mdp:
7404 unregister_blkdev(MD_MAJOR, "md");
7405err_md:
7406 destroy_workqueue(md_misc_wq);
7407err_misc_wq:
7408 destroy_workqueue(md_wq);
7409err_wq:
7410 return ret;
7411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
7413#ifndef MODULE
7414
7415/*
7416 * Searches all registered partitions for autorun RAID arrays
7417 * at boot time.
7418 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007419
7420static LIST_HEAD(all_detected_devices);
7421struct detected_devices_node {
7422 struct list_head list;
7423 dev_t dev;
7424};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
7426void md_autodetect_dev(dev_t dev)
7427{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007428 struct detected_devices_node *node_detected_dev;
7429
7430 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7431 if (node_detected_dev) {
7432 node_detected_dev->dev = dev;
7433 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7434 } else {
7435 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7436 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438}
7439
7440
7441static void autostart_arrays(int part)
7442{
7443 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007444 struct detected_devices_node *node_detected_dev;
7445 dev_t dev;
7446 int i_scanned, i_passed;
7447
7448 i_scanned = 0;
7449 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
7451 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7452
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007453 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7454 i_scanned++;
7455 node_detected_dev = list_entry(all_detected_devices.next,
7456 struct detected_devices_node, list);
7457 list_del(&node_detected_dev->list);
7458 dev = node_detected_dev->dev;
7459 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007460 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 if (IS_ERR(rdev))
7462 continue;
7463
NeilBrownb2d444d2005-11-08 21:39:31 -08007464 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 MD_BUG();
7466 continue;
7467 }
NeilBrownd0fae182008-03-04 14:29:31 -08007468 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007470 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007472
7473 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7474 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
7476 autorun_devices(part);
7477}
7478
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007479#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480
7481static __exit void md_exit(void)
7482{
7483 mddev_t *mddev;
7484 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007485
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007486 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007487 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007489 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 unregister_blkdev(mdp_major, "mdp");
7491 unregister_reboot_notifier(&md_notifier);
7492 unregister_sysctl_table(raid_table_header);
7493 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007494 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007496 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007498 destroy_workqueue(md_misc_wq);
7499 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500}
7501
Dan Williams685784a2007-07-09 11:56:42 -07007502subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503module_exit(md_exit)
7504
NeilBrownf91de922005-11-08 21:39:36 -08007505static int get_ro(char *buffer, struct kernel_param *kp)
7506{
7507 return sprintf(buffer, "%d", start_readonly);
7508}
7509static int set_ro(const char *val, struct kernel_param *kp)
7510{
7511 char *e;
7512 int num = simple_strtoul(val, &e, 10);
7513 if (*val && (*e == '\0' || *e == '\n')) {
7514 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007515 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007516 }
7517 return -EINVAL;
7518}
7519
NeilBrown80ca3a42006-07-10 04:44:18 -07007520module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7521module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08007522
NeilBrownefeb53c2009-01-09 08:31:10 +11007523module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007524
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525EXPORT_SYMBOL(register_md_personality);
7526EXPORT_SYMBOL(unregister_md_personality);
7527EXPORT_SYMBOL(md_error);
7528EXPORT_SYMBOL(md_done_sync);
7529EXPORT_SYMBOL(md_write_start);
7530EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531EXPORT_SYMBOL(md_register_thread);
7532EXPORT_SYMBOL(md_unregister_thread);
7533EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534EXPORT_SYMBOL(md_check_recovery);
7535MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007536MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007537MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007538MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);