blob: 28f9c1ee4e3a1a256ea8d55b5e062f3e71d6b8e9 [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
NeilBrown2ac87402010-06-01 19:37:29 +1000448/* Support for plugging.
449 * This mirrors the plugging support in request_queue, but does not
450 * require having a whole queue
451 */
452static void plugger_work(struct work_struct *work)
453{
454 struct plug_handle *plug =
455 container_of(work, struct plug_handle, unplug_work);
456 plug->unplug_fn(plug);
457}
458static void plugger_timeout(unsigned long data)
459{
460 struct plug_handle *plug = (void *)data;
461 kblockd_schedule_work(NULL, &plug->unplug_work);
462}
463void plugger_init(struct plug_handle *plug,
464 void (*unplug_fn)(struct plug_handle *))
465{
466 plug->unplug_flag = 0;
467 plug->unplug_fn = unplug_fn;
468 init_timer(&plug->unplug_timer);
469 plug->unplug_timer.function = plugger_timeout;
470 plug->unplug_timer.data = (unsigned long)plug;
471 INIT_WORK(&plug->unplug_work, plugger_work);
472}
473EXPORT_SYMBOL_GPL(plugger_init);
474
475void plugger_set_plug(struct plug_handle *plug)
476{
477 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
478 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
479}
480EXPORT_SYMBOL_GPL(plugger_set_plug);
481
482int plugger_remove_plug(struct plug_handle *plug)
483{
484 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
485 del_timer(&plug->unplug_timer);
486 return 1;
487 } else
488 return 0;
489}
490EXPORT_SYMBOL_GPL(plugger_remove_plug);
491
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static inline mddev_t *mddev_get(mddev_t *mddev)
494{
495 atomic_inc(&mddev->active);
496 return mddev;
497}
498
Dan Williams5fd3a172009-03-04 00:57:25 -0700499static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static void mddev_put(mddev_t *mddev)
502{
NeilBrowna167f662010-10-26 18:31:13 +1100503 struct bio_set *bs = NULL;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
506 return;
NeilBrownd3374822009-01-09 08:31:10 +1100507 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100508 mddev->ctime == 0 && !mddev->hold_active) {
509 /* Array is not configured at all, and not held active,
510 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100512 bs = mddev->bio_set;
513 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100514 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200515 /* We did a probe so need to clean up. Call
516 * queue_work inside the spinlock so that
517 * flush_workqueue() after mddev_find will
518 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100519 */
520 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200521 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100522 } else
523 kfree(mddev);
524 }
525 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100526 if (bs)
527 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
NeilBrown390ee602010-06-01 19:37:27 +1000530void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100531{
532 mutex_init(&mddev->open_mutex);
533 mutex_init(&mddev->reconfig_mutex);
534 mutex_init(&mddev->bitmap_info.mutex);
535 INIT_LIST_HEAD(&mddev->disks);
536 INIT_LIST_HEAD(&mddev->all_mddevs);
537 init_timer(&mddev->safemode_timer);
538 atomic_set(&mddev->active, 1);
539 atomic_set(&mddev->openers, 0);
540 atomic_set(&mddev->active_io, 0);
541 spin_lock_init(&mddev->write_lock);
542 atomic_set(&mddev->flush_pending, 0);
543 init_waitqueue_head(&mddev->sb_wait);
544 init_waitqueue_head(&mddev->recovery_wait);
545 mddev->reshape_position = MaxSector;
546 mddev->resync_min = 0;
547 mddev->resync_max = MaxSector;
548 mddev->level = LEVEL_NONE;
549}
NeilBrown390ee602010-06-01 19:37:27 +1000550EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static mddev_t * mddev_find(dev_t unit)
553{
554 mddev_t *mddev, *new = NULL;
555
556 retry:
557 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
NeilBrownefeb53c2009-01-09 08:31:10 +1100559 if (unit) {
560 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
561 if (mddev->unit == unit) {
562 mddev_get(mddev);
563 spin_unlock(&all_mddevs_lock);
564 kfree(new);
565 return mddev;
566 }
567
568 if (new) {
569 list_add(&new->all_mddevs, &all_mddevs);
570 spin_unlock(&all_mddevs_lock);
571 new->hold_active = UNTIL_IOCTL;
572 return new;
573 }
574 } else if (new) {
575 /* find an unused unit number */
576 static int next_minor = 512;
577 int start = next_minor;
578 int is_free = 0;
579 int dev = 0;
580 while (!is_free) {
581 dev = MKDEV(MD_MAJOR, next_minor);
582 next_minor++;
583 if (next_minor > MINORMASK)
584 next_minor = 0;
585 if (next_minor == start) {
586 /* Oh dear, all in use. */
587 spin_unlock(&all_mddevs_lock);
588 kfree(new);
589 return NULL;
590 }
591
592 is_free = 1;
593 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
594 if (mddev->unit == dev) {
595 is_free = 0;
596 break;
597 }
598 }
599 new->unit = dev;
600 new->md_minor = MINOR(dev);
601 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 list_add(&new->all_mddevs, &all_mddevs);
603 spin_unlock(&all_mddevs_lock);
604 return new;
605 }
606 spin_unlock(&all_mddevs_lock);
607
NeilBrown9ffae0c2006-01-06 00:20:32 -0800608 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!new)
610 return NULL;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 new->unit = unit;
613 if (MAJOR(unit) == MD_MAJOR)
614 new->md_minor = MINOR(unit);
615 else
616 new->md_minor = MINOR(unit) >> MdpMinorShift;
617
NeilBrownfafd7fb2010-04-01 15:55:30 +1100618 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 goto retry;
621}
622
623static inline int mddev_lock(mddev_t * mddev)
624{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800625 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Dan Williamsb522adc2009-03-31 15:00:31 +1100628static inline int mddev_is_locked(mddev_t *mddev)
629{
630 return mutex_is_locked(&mddev->reconfig_mutex);
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static inline int mddev_trylock(mddev_t * mddev)
634{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800635 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
NeilBrownb6eb1272010-04-15 10:13:47 +1000638static struct attribute_group md_redundancy_group;
639
NeilBrowna64c8762010-04-14 17:15:37 +1000640static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
NeilBrowna64c8762010-04-14 17:15:37 +1000642 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000643 /* These cannot be removed under reconfig_mutex as
644 * an access to the files will try to take reconfig_mutex
645 * while holding the file unremovable, which leads to
646 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000647 * So hold set sysfs_active while the remove in happeing,
648 * and anything else which might set ->to_remove or my
649 * otherwise change the sysfs namespace will fail with
650 * -EBUSY if sysfs_active is still set.
651 * We set sysfs_active under reconfig_mutex and elsewhere
652 * test it under the same mutex to ensure its correct value
653 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000654 */
NeilBrowna64c8762010-04-14 17:15:37 +1000655 struct attribute_group *to_remove = mddev->to_remove;
656 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000657 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000658 mutex_unlock(&mddev->reconfig_mutex);
659
NeilBrown00bcb4a2010-06-01 19:37:23 +1000660 if (mddev->kobj.sd) {
661 if (to_remove != &md_redundancy_group)
662 sysfs_remove_group(&mddev->kobj, to_remove);
663 if (mddev->pers == NULL ||
664 mddev->pers->sync_request == NULL) {
665 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
666 if (mddev->sysfs_action)
667 sysfs_put(mddev->sysfs_action);
668 mddev->sysfs_action = NULL;
669 }
NeilBrowna64c8762010-04-14 17:15:37 +1000670 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000671 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000672 } else
673 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
NeilBrown005eca52005-08-22 13:11:08 -0700675 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
NeilBrown2989ddb2006-01-06 00:20:43 -0800678static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100680 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100682 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (rdev->desc_nr == nr)
684 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return NULL;
687}
688
689static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
690{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 mdk_rdev_t *rdev;
692
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100693 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (rdev->bdev->bd_dev == dev)
695 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return NULL;
698}
699
NeilBrownd9d166c2006-01-06 00:20:51 -0800700static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800701{
702 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800703 list_for_each_entry(pers, &pers_list, list) {
704 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800705 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800706 if (strcmp(pers->name, clevel)==0)
707 return pers;
708 }
NeilBrown2604b702006-01-06 00:20:36 -0800709 return NULL;
710}
711
Andre Nollb73df2d2008-07-11 22:02:23 +1000712/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100713static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100715 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000716 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static int alloc_disk_sb(mdk_rdev_t * rdev)
720{
721 if (rdev->sb_page)
722 MD_BUG();
723
724 rdev->sb_page = alloc_page(GFP_KERNEL);
725 if (!rdev->sb_page) {
726 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000727 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
730 return 0;
731}
732
733static void free_disk_sb(mdk_rdev_t * rdev)
734{
735 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800736 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 rdev->sb_loaded = 0;
738 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000739 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100740 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742}
743
744
NeilBrown6712ecf2007-09-27 12:47:43 +0200745static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746{
747 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800748 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700749
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700750 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
751 printk("md: super_written gets error=%d, uptodate=%d\n",
752 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
753 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800754 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700755 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700756
NeilBrowna9701a32005-11-08 21:39:34 -0800757 if (atomic_dec_and_test(&mddev->pending_writes))
758 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700759 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700760}
761
762void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
763 sector_t sector, int size, struct page *page)
764{
765 /* write first size bytes of page to sector of rdev
766 * Increment mddev->pending_writes before returning
767 * and decrement it on completion, waking up sb_wait
768 * if zero is reached.
769 * If an error occurred, call md_error
770 */
NeilBrowna167f662010-10-26 18:31:13 +1100771 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700772
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100773 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700774 bio->bi_sector = sector;
775 bio_add_page(bio, page, size, 0);
776 bio->bi_private = rdev;
777 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800778
NeilBrown7bfa19f2005-06-21 17:17:28 -0700779 atomic_inc(&mddev->pending_writes);
Jens Axboe721a9602011-03-09 11:56:30 +0100780 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800781}
782
783void md_super_wait(mddev_t *mddev)
784{
Tejun Heoe9c74692010-09-03 11:56:18 +0200785 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800786 DEFINE_WAIT(wq);
787 for(;;) {
788 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
789 if (atomic_read(&mddev->pending_writes)==0)
790 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800791 schedule();
792 }
793 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700794}
795
NeilBrown6712ecf2007-09-27 12:47:43 +0200796static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
NeilBrown2b193362010-10-27 15:16:40 +1100801int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100802 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
NeilBrowna167f662010-10-26 18:31:13 +1100804 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct completion event;
806 int ret;
807
Jens Axboe721a9602011-03-09 11:56:30 +0100808 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100810 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
811 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100812 if (metadata_op)
813 bio->bi_sector = sector + rdev->sb_start;
814 else
815 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 bio_add_page(bio, page, size, 0);
817 init_completion(&event);
818 bio->bi_private = &event;
819 bio->bi_end_io = bi_complete;
820 submit_bio(rw, bio);
821 wait_for_completion(&event);
822
823 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
824 bio_put(bio);
825 return ret;
826}
NeilBrowna8745db2006-01-06 00:20:34 -0800827EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
NeilBrown0002b272005-09-09 16:23:53 -0700829static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 char b[BDEVNAME_SIZE];
832 if (!rdev->sb_page) {
833 MD_BUG();
834 return -EINVAL;
835 }
836 if (rdev->sb_loaded)
837 return 0;
838
839
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100840 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto fail;
842 rdev->sb_loaded = 1;
843 return 0;
844
845fail:
846 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
847 bdevname(rdev->bdev,b));
848 return -EINVAL;
849}
850
851static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
852{
Andre Noll05710462008-07-11 22:02:20 +1000853 return sb1->set_uuid0 == sb2->set_uuid0 &&
854 sb1->set_uuid1 == sb2->set_uuid1 &&
855 sb1->set_uuid2 == sb2->set_uuid2 &&
856 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
860{
861 int ret;
862 mdp_super_t *tmp1, *tmp2;
863
864 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
865 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
866
867 if (!tmp1 || !tmp2) {
868 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100869 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto abort;
871 }
872
873 *tmp1 = *sb1;
874 *tmp2 = *sb2;
875
876 /*
877 * nr_disks is not constant
878 */
879 tmp1->nr_disks = 0;
880 tmp2->nr_disks = 0;
881
Andre Nollce0c8e02008-07-11 22:02:20 +1000882 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700884 kfree(tmp1);
885 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return ret;
887}
888
NeilBrown4d167f02007-05-09 02:35:37 -0700889
890static u32 md_csum_fold(u32 csum)
891{
892 csum = (csum & 0xffff) + (csum >> 16);
893 return (csum & 0xffff) + (csum >> 16);
894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static unsigned int calc_sb_csum(mdp_super_t * sb)
897{
NeilBrown4d167f02007-05-09 02:35:37 -0700898 u64 newcsum = 0;
899 u32 *sb32 = (u32*)sb;
900 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 unsigned int disk_csum, csum;
902
903 disk_csum = sb->sb_csum;
904 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700905
906 for (i = 0; i < MD_SB_BYTES/4 ; i++)
907 newcsum += sb32[i];
908 csum = (newcsum & 0xffffffff) + (newcsum>>32);
909
910
911#ifdef CONFIG_ALPHA
912 /* This used to use csum_partial, which was wrong for several
913 * reasons including that different results are returned on
914 * different architectures. It isn't critical that we get exactly
915 * the same return value as before (we always csum_fold before
916 * testing, and that removes any differences). However as we
917 * know that csum_partial always returned a 16bit value on
918 * alphas, do a fold to maximise conformity to previous behaviour.
919 */
920 sb->sb_csum = md_csum_fold(disk_csum);
921#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700923#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return csum;
925}
926
927
928/*
929 * Handle superblock details.
930 * We want to be able to handle multiple superblock formats
931 * so we have a common interface to them all, and an array of
932 * different handlers.
933 * We rely on user-space to write the initial superblock, and support
934 * reading and updating of superblocks.
935 * Interface methods are:
936 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
937 * loads and validates a superblock on dev.
938 * if refdev != NULL, compare superblocks on both devices
939 * Return:
940 * 0 - dev has a superblock that is compatible with refdev
941 * 1 - dev has a superblock that is compatible and newer than refdev
942 * so dev should be used as the refdev in future
943 * -EINVAL superblock incompatible or invalid
944 * -othererror e.g. -EIO
945 *
946 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
947 * Verify that dev is acceptable into mddev.
948 * The first time, mddev->raid_disks will be 0, and data from
949 * dev should be merged in. Subsequent calls check that dev
950 * is new enough. Return 0 or -EINVAL
951 *
952 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
953 * Update the superblock for rdev with data in mddev
954 * This does not write to disc.
955 *
956 */
957
958struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000959 char *name;
960 struct module *owner;
961 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
962 int minor_version);
963 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
964 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
965 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000966 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967};
968
969/*
Andre Noll0894cc32009-06-18 08:49:23 +1000970 * Check that the given mddev has no bitmap.
971 *
972 * This function is called from the run method of all personalities that do not
973 * support bitmaps. It prints an error message and returns non-zero if mddev
974 * has a bitmap. Otherwise, it returns 0.
975 *
976 */
977int md_check_no_bitmap(mddev_t *mddev)
978{
NeilBrownc3d97142009-12-14 12:49:52 +1100979 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000980 return 0;
981 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
982 mdname(mddev), mddev->pers->name);
983 return 1;
984}
985EXPORT_SYMBOL(md_check_no_bitmap);
986
987/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * load_super for 0.90.0
989 */
990static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
991{
992 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
993 mdp_super_t *sb;
994 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /*
Andre Noll0f420352008-07-11 22:02:23 +1000997 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 * it's at the end of the disk.
999 *
1000 * It also happens to be a multiple of 4Kb.
1001 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001002 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
NeilBrown0002b272005-09-09 16:23:53 -07001004 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (ret) return ret;
1006
1007 ret = -EINVAL;
1008
1009 bdevname(rdev->bdev, b);
1010 sb = (mdp_super_t*)page_address(rdev->sb_page);
1011
1012 if (sb->md_magic != MD_SB_MAGIC) {
1013 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1014 b);
1015 goto abort;
1016 }
1017
1018 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001019 sb->minor_version < 90 ||
1020 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1022 sb->major_version, sb->minor_version,
1023 b);
1024 goto abort;
1025 }
1026
1027 if (sb->raid_disks <= 0)
1028 goto abort;
1029
NeilBrown4d167f02007-05-09 02:35:37 -07001030 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1032 b);
1033 goto abort;
1034 }
1035
1036 rdev->preferred_minor = sb->md_minor;
1037 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001038 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (sb->level == LEVEL_MULTIPATH)
1041 rdev->desc_nr = -1;
1042 else
1043 rdev->desc_nr = sb->this_disk.number;
1044
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001045 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001047 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 __u64 ev1, ev2;
1049 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1050 if (!uuid_equal(refsb, sb)) {
1051 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1052 b, bdevname(refdev->bdev,b2));
1053 goto abort;
1054 }
1055 if (!sb_equal(refsb, sb)) {
1056 printk(KERN_WARNING "md: %s has same UUID"
1057 " but different superblock to %s\n",
1058 b, bdevname(refdev->bdev, b2));
1059 goto abort;
1060 }
1061 ev1 = md_event(sb);
1062 ev2 = md_event(refsb);
1063 if (ev1 > ev2)
1064 ret = 1;
1065 else
1066 ret = 0;
1067 }
NeilBrown8190e752009-06-18 08:48:58 +10001068 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Andre Nolldd8ac332009-03-31 14:33:13 +11001070 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001071 /* "this cannot possibly happen" ... */
1072 ret = -EINVAL;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 abort:
1075 return ret;
1076}
1077
1078/*
1079 * validate_super for 0.90.0
1080 */
1081static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1082{
1083 mdp_disk_t *desc;
1084 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001085 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
NeilBrown41158c72005-06-21 17:17:25 -07001087 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001088 clear_bit(Faulty, &rdev->flags);
1089 clear_bit(In_sync, &rdev->flags);
1090 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (mddev->raid_disks == 0) {
1093 mddev->major_version = 0;
1094 mddev->minor_version = sb->minor_version;
1095 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001096 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001097 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 mddev->ctime = sb->ctime;
1099 mddev->utime = sb->utime;
1100 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001101 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 mddev->layout = sb->layout;
1103 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001104 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001105 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001106 mddev->bitmap_info.offset = 0;
1107 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
NeilBrownf6705572006-03-27 01:18:11 -08001109 if (mddev->minor_version >= 91) {
1110 mddev->reshape_position = sb->reshape_position;
1111 mddev->delta_disks = sb->delta_disks;
1112 mddev->new_level = sb->new_level;
1113 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001114 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001115 } else {
1116 mddev->reshape_position = MaxSector;
1117 mddev->delta_disks = 0;
1118 mddev->new_level = mddev->level;
1119 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001120 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001121 }
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (sb->state & (1<<MD_SB_CLEAN))
1124 mddev->recovery_cp = MaxSector;
1125 else {
1126 if (sb->events_hi == sb->cp_events_hi &&
1127 sb->events_lo == sb->cp_events_lo) {
1128 mddev->recovery_cp = sb->recovery_cp;
1129 } else
1130 mddev->recovery_cp = 0;
1131 }
1132
1133 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1134 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1135 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1136 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1137
1138 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001139
1140 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001141 mddev->bitmap_info.file == NULL)
1142 mddev->bitmap_info.offset =
1143 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001144
NeilBrown41158c72005-06-21 17:17:25 -07001145 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001146 /* Insist on good event counter while assembling, except
1147 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001149 if (sb->disks[rdev->desc_nr].state & (
1150 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1151 if (ev1 < mddev->events)
1152 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001153 } else if (mddev->bitmap) {
1154 /* if adding to array with a bitmap, then we can accept an
1155 * older device ... but not too old.
1156 */
NeilBrown41158c72005-06-21 17:17:25 -07001157 if (ev1 < mddev->bitmap->events_cleared)
1158 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001159 } else {
1160 if (ev1 < mddev->events)
1161 /* just a hot-add of a new device, leave raid_disk at -1 */
1162 return 0;
1163 }
NeilBrown41158c72005-06-21 17:17:25 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 desc = sb->disks + rdev->desc_nr;
1167
1168 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001170 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1171 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001172 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001174 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1175 /* active but not in sync implies recovery up to
1176 * reshape position. We don't know exactly where
1177 * that is, so set to zero for now */
1178 if (mddev->minor_version >= 91) {
1179 rdev->recovery_offset = 0;
1180 rdev->raid_disk = desc->raid_disk;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001183 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1184 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001185 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001186 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188}
1189
1190/*
1191 * sync_super for 0.90.0
1192 */
1193static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1194{
1195 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 mdk_rdev_t *rdev2;
1197 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /* make rdev->sb match mddev data..
1201 *
1202 * 1/ zero out disks
1203 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1204 * 3/ any empty disks < next_spare become removed
1205 *
1206 * disks[0] gets initialised to REMOVED because
1207 * we cannot be sure from other fields if it has
1208 * been initialised or not.
1209 */
1210 int i;
1211 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1212
NeilBrown61181562005-09-09 16:24:02 -07001213 rdev->sb_size = MD_SB_BYTES;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 sb = (mdp_super_t*)page_address(rdev->sb_page);
1216
1217 memset(sb, 0, sizeof(*sb));
1218
1219 sb->md_magic = MD_SB_MAGIC;
1220 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 sb->patch_version = mddev->patch_version;
1222 sb->gvalid_words = 0; /* ignored */
1223 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1224 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1225 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1226 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1227
1228 sb->ctime = mddev->ctime;
1229 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001230 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 sb->raid_disks = mddev->raid_disks;
1232 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001233 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sb->utime = mddev->utime;
1235 sb->state = 0;
1236 sb->events_hi = (mddev->events>>32);
1237 sb->events_lo = (u32)mddev->events;
1238
NeilBrownf6705572006-03-27 01:18:11 -08001239 if (mddev->reshape_position == MaxSector)
1240 sb->minor_version = 90;
1241 else {
1242 sb->minor_version = 91;
1243 sb->reshape_position = mddev->reshape_position;
1244 sb->new_level = mddev->new_level;
1245 sb->delta_disks = mddev->delta_disks;
1246 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001247 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001248 }
1249 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (mddev->in_sync)
1251 {
1252 sb->recovery_cp = mddev->recovery_cp;
1253 sb->cp_events_hi = (mddev->events>>32);
1254 sb->cp_events_lo = (u32)mddev->events;
1255 if (mddev->recovery_cp == MaxSector)
1256 sb->state = (1<< MD_SB_CLEAN);
1257 } else
1258 sb->recovery_cp = 0;
1259
1260 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001261 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
NeilBrownc3d97142009-12-14 12:49:52 +11001263 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001264 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001267 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001269 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001270 int is_active = test_bit(In_sync, &rdev2->flags);
1271
1272 if (rdev2->raid_disk >= 0 &&
1273 sb->minor_version >= 91)
1274 /* we have nowhere to store the recovery_offset,
1275 * but if it is not below the reshape_position,
1276 * we can piggy-back on that.
1277 */
1278 is_active = 1;
1279 if (rdev2->raid_disk < 0 ||
1280 test_bit(Faulty, &rdev2->flags))
1281 is_active = 0;
1282 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001283 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001285 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001286 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 d = &sb->disks[rdev2->desc_nr];
1288 nr_disks++;
1289 d->number = rdev2->desc_nr;
1290 d->major = MAJOR(rdev2->bdev->bd_dev);
1291 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001292 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 d->raid_disk = rdev2->raid_disk;
1294 else
1295 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001296 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001298 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001300 if (test_bit(In_sync, &rdev2->flags))
1301 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 active++;
1303 working++;
1304 } else {
1305 d->state = 0;
1306 spare++;
1307 working++;
1308 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001309 if (test_bit(WriteMostly, &rdev2->flags))
1310 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /* now set the "removed" and "faulty" bits on any missing devices */
1313 for (i=0 ; i < mddev->raid_disks ; i++) {
1314 mdp_disk_t *d = &sb->disks[i];
1315 if (d->state == 0 && d->number == 0) {
1316 d->number = i;
1317 d->raid_disk = i;
1318 d->state = (1<<MD_DISK_REMOVED);
1319 d->state |= (1<<MD_DISK_FAULTY);
1320 failed++;
1321 }
1322 }
1323 sb->nr_disks = nr_disks;
1324 sb->active_disks = active;
1325 sb->working_disks = working;
1326 sb->failed_disks = failed;
1327 sb->spare_disks = spare;
1328
1329 sb->this_disk = sb->disks[rdev->desc_nr];
1330 sb->sb_csum = calc_sb_csum(sb);
1331}
1332
1333/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001334 * rdev_size_change for 0.90.0
1335 */
1336static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001337super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338{
Andre Noll58c0fed2009-03-31 14:33:13 +11001339 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001340 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001341 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001343 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001344 if (!num_sectors || num_sectors > rdev->sb_start)
1345 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001346 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001347 rdev->sb_page);
1348 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001349 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001350}
1351
1352
1353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 * version 1 superblock
1355 */
1356
NeilBrown1c05b4b2006-10-21 10:24:08 -07001357static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001359 __le32 disk_csum;
1360 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 unsigned long long newcsum;
1362 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001363 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 int i;
1365
1366 disk_csum = sb->sb_csum;
1367 sb->sb_csum = 0;
1368 newcsum = 0;
1369 for (i=0; size>=4; size -= 4 )
1370 newcsum += le32_to_cpu(*isuper++);
1371
1372 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001373 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1376 sb->sb_csum = disk_csum;
1377 return cpu_to_le32(csum);
1378}
1379
1380static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1381{
1382 struct mdp_superblock_1 *sb;
1383 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001384 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001386 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /*
Andre Noll0f420352008-07-11 22:02:23 +10001389 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 * It is always aligned to a 4K boundary and
1391 * depeding on minor_version, it can be:
1392 * 0: At least 8K, but less than 12K, from end of device
1393 * 1: At start of device
1394 * 2: 4K from start of device.
1395 */
1396 switch(minor_version) {
1397 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001398 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001399 sb_start -= 8*2;
1400 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 break;
1402 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001403 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001406 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
1408 default:
1409 return -EINVAL;
1410 }
Andre Noll0f420352008-07-11 22:02:23 +10001411 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
NeilBrown0002b272005-09-09 16:23:53 -07001413 /* superblock is rarely larger than 1K, but it can be larger,
1414 * and it is safe to read 4k, so we do that
1415 */
1416 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (ret) return ret;
1418
1419
1420 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1421
1422 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1423 sb->major_version != cpu_to_le32(1) ||
1424 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001425 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001426 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return -EINVAL;
1428
1429 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1430 printk("md: invalid superblock checksum on %s\n",
1431 bdevname(rdev->bdev,b));
1432 return -EINVAL;
1433 }
1434 if (le64_to_cpu(sb->data_size) < 10) {
1435 printk("md: data_size too small on %s\n",
1436 bdevname(rdev->bdev,b));
1437 return -EINVAL;
1438 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 rdev->preferred_minor = 0xffff;
1441 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001442 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
NeilBrown0002b272005-09-09 16:23:53 -07001444 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001445 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001446 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001447 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1448
1449 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001450 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001451 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001452
NeilBrown31b65a02006-07-10 04:44:14 -07001453 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1454 rdev->desc_nr = -1;
1455 else
1456 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1457
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001458 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001459 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001460 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 __u64 ev1, ev2;
1462 struct mdp_superblock_1 *refsb =
1463 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1464
1465 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1466 sb->level != refsb->level ||
1467 sb->layout != refsb->layout ||
1468 sb->chunksize != refsb->chunksize) {
1469 printk(KERN_WARNING "md: %s has strangely different"
1470 " superblock to %s\n",
1471 bdevname(rdev->bdev,b),
1472 bdevname(refdev->bdev,b2));
1473 return -EINVAL;
1474 }
1475 ev1 = le64_to_cpu(sb->events);
1476 ev2 = le64_to_cpu(refsb->events);
1477
1478 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001479 ret = 1;
1480 else
1481 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
NeilBrowna1801f82008-03-04 14:29:31 -08001483 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001484 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001485 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001487 rdev->sectors = rdev->sb_start;
1488 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001490 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001491 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001492 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1497{
1498 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001499 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
NeilBrown41158c72005-06-21 17:17:25 -07001501 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001502 clear_bit(Faulty, &rdev->flags);
1503 clear_bit(In_sync, &rdev->flags);
1504 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (mddev->raid_disks == 0) {
1507 mddev->major_version = 1;
1508 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001509 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001510 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1512 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1513 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001514 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 mddev->layout = le32_to_cpu(sb->layout);
1516 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001517 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001518 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001519 mddev->bitmap_info.offset = 0;
1520 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1523 memcpy(mddev->uuid, sb->set_uuid, 16);
1524
1525 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001526
NeilBrown71c08052005-09-09 16:23:51 -07001527 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001528 mddev->bitmap_info.file == NULL )
1529 mddev->bitmap_info.offset =
1530 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001531
NeilBrownf6705572006-03-27 01:18:11 -08001532 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1533 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1534 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1535 mddev->new_level = le32_to_cpu(sb->new_level);
1536 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001537 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001538 } else {
1539 mddev->reshape_position = MaxSector;
1540 mddev->delta_disks = 0;
1541 mddev->new_level = mddev->level;
1542 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001543 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001544 }
1545
NeilBrown41158c72005-06-21 17:17:25 -07001546 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001547 /* Insist of good event counter while assembling, except for
1548 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001550 if (rdev->desc_nr >= 0 &&
1551 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1552 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1553 if (ev1 < mddev->events)
1554 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001555 } else if (mddev->bitmap) {
1556 /* If adding to array with a bitmap, then we can accept an
1557 * older device, but not too old.
1558 */
NeilBrown41158c72005-06-21 17:17:25 -07001559 if (ev1 < mddev->bitmap->events_cleared)
1560 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001561 } else {
1562 if (ev1 < mddev->events)
1563 /* just a hot-add of a new device, leave raid_disk at -1 */
1564 return 0;
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (mddev->level != LEVEL_MULTIPATH) {
1567 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001568 if (rdev->desc_nr < 0 ||
1569 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1570 role = 0xffff;
1571 rdev->desc_nr = -1;
1572 } else
1573 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 switch(role) {
1575 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 break;
1577 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001578 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 break;
1580 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001581 if ((le32_to_cpu(sb->feature_map) &
1582 MD_FEATURE_RECOVERY_OFFSET))
1583 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1584 else
1585 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 rdev->raid_disk = role;
1587 break;
1588 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001589 if (sb->devflags & WriteMostly1)
1590 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001591 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001592 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return 0;
1595}
1596
1597static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1598{
1599 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 mdk_rdev_t *rdev2;
1601 int max_dev, i;
1602 /* make rdev->sb match mddev and rdev data. */
1603
1604 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1605
1606 sb->feature_map = 0;
1607 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001608 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 memset(sb->pad1, 0, sizeof(sb->pad1));
1610 memset(sb->pad2, 0, sizeof(sb->pad2));
1611 memset(sb->pad3, 0, sizeof(sb->pad3));
1612
1613 sb->utime = cpu_to_le64((__u64)mddev->utime);
1614 sb->events = cpu_to_le64(mddev->events);
1615 if (mddev->in_sync)
1616 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1617 else
1618 sb->resync_offset = cpu_to_le64(0);
1619
NeilBrown1c05b4b2006-10-21 10:24:08 -07001620 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001621
NeilBrownf0ca3402006-02-02 14:28:04 -08001622 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001623 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001624 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001625 sb->level = cpu_to_le32(mddev->level);
1626 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001627
NeilBrownc3d97142009-12-14 12:49:52 +11001628 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1629 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001630 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001631 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001632
1633 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001634 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001635 sb->feature_map |=
1636 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1637 sb->recovery_offset =
1638 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001639 }
1640
NeilBrownf6705572006-03-27 01:18:11 -08001641 if (mddev->reshape_position != MaxSector) {
1642 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1643 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1644 sb->new_layout = cpu_to_le32(mddev->new_layout);
1645 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1646 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001647 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001648 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001651 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (rdev2->desc_nr+1 > max_dev)
1653 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001654
NeilBrown70471da2009-08-03 10:59:57 +10001655 if (max_dev > le32_to_cpu(sb->max_dev)) {
1656 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001657 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001658 rdev->sb_size = max_dev * 2 + 256;
1659 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1660 if (rdev->sb_size & bmask)
1661 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001662 } else
1663 max_dev = le32_to_cpu(sb->max_dev);
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 for (i=0; i<max_dev;i++)
1666 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1667
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001668 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001670 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001672 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001674 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001675 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 else
1677 sb->dev_roles[i] = cpu_to_le16(0xffff);
1678 }
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 sb->sb_csum = calc_sb_1_csum(sb);
1681}
1682
Chris Webb0cd17fe2008-06-28 08:31:46 +10001683static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001684super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001685{
1686 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001687 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001688 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001689 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001690 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001691 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001692 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001693 max_sectors -= rdev->data_offset;
1694 if (!num_sectors || num_sectors > max_sectors)
1695 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001696 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001697 /* minor version 0 with bitmap we can't move */
1698 return 0;
1699 } else {
1700 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001701 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001702 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001703 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001704 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001705 if (!num_sectors || num_sectors > max_sectors)
1706 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001707 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001708 }
1709 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001710 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001711 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001712 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001713 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001714 rdev->sb_page);
1715 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001716 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001717}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Adrian Bunk75c96f82005-05-05 16:16:09 -07001719static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 [0] = {
1721 .name = "0.90.0",
1722 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001723 .load_super = super_90_load,
1724 .validate_super = super_90_validate,
1725 .sync_super = super_90_sync,
1726 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 },
1728 [1] = {
1729 .name = "md-1",
1730 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001731 .load_super = super_1_load,
1732 .validate_super = super_1_validate,
1733 .sync_super = super_1_sync,
1734 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 },
1736};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1739{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001740 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
NeilBrown4b809912008-07-21 17:05:25 +10001742 rcu_read_lock();
1743 rdev_for_each_rcu(rdev, mddev1)
1744 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001745 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001746 rdev2->bdev->bd_contains) {
1747 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001748 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001749 }
1750 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return 0;
1752}
1753
1754static LIST_HEAD(pending_raid_disks);
1755
Andre Nollac5e7112009-08-03 10:59:47 +10001756/*
1757 * Try to register data integrity profile for an mddev
1758 *
1759 * This is called when an array is started and after a disk has been kicked
1760 * from the array. It only succeeds if all working and active component devices
1761 * are integrity capable with matching profiles.
1762 */
1763int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001764{
Andre Nollac5e7112009-08-03 10:59:47 +10001765 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001766
Andre Nollac5e7112009-08-03 10:59:47 +10001767 if (list_empty(&mddev->disks))
1768 return 0; /* nothing to do */
1769 if (blk_get_integrity(mddev->gendisk))
1770 return 0; /* already registered */
1771 list_for_each_entry(rdev, &mddev->disks, same_set) {
1772 /* skip spares and non-functional disks */
1773 if (test_bit(Faulty, &rdev->flags))
1774 continue;
1775 if (rdev->raid_disk < 0)
1776 continue;
1777 /*
1778 * If at least one rdev is not integrity capable, we can not
1779 * enable data integrity for the md device.
1780 */
1781 if (!bdev_get_integrity(rdev->bdev))
1782 return -EINVAL;
1783 if (!reference) {
1784 /* Use the first rdev as the reference */
1785 reference = rdev;
1786 continue;
1787 }
1788 /* does this rdev's profile match the reference profile? */
1789 if (blk_integrity_compare(reference->bdev->bd_disk,
1790 rdev->bdev->bd_disk) < 0)
1791 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001792 }
Andre Nollac5e7112009-08-03 10:59:47 +10001793 /*
1794 * All component devices are integrity capable and have matching
1795 * profiles, register the common profile for the md device.
1796 */
1797 if (blk_integrity_register(mddev->gendisk,
1798 bdev_get_integrity(reference->bdev)) != 0) {
1799 printk(KERN_ERR "md: failed to register integrity for %s\n",
1800 mdname(mddev));
1801 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001802 }
Andre Nollac5e7112009-08-03 10:59:47 +10001803 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1804 mdname(mddev));
1805 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001806}
Andre Nollac5e7112009-08-03 10:59:47 +10001807EXPORT_SYMBOL(md_integrity_register);
1808
1809/* Disable data integrity if non-capable/non-matching disk is being added */
1810void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1811{
1812 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1813 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1814
1815 if (!bi_mddev) /* nothing to do */
1816 return;
1817 if (rdev->raid_disk < 0) /* skip spares */
1818 return;
1819 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1820 rdev->bdev->bd_disk) >= 0)
1821 return;
1822 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1823 blk_integrity_unregister(mddev->gendisk);
1824}
1825EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1828{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001829 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001830 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001831 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 if (rdev->mddev) {
1835 MD_BUG();
1836 return -EINVAL;
1837 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001838
1839 /* prevent duplicates */
1840 if (find_rdev(mddev, rdev->bdev->bd_dev))
1841 return -EEXIST;
1842
Andre Nolldd8ac332009-03-31 14:33:13 +11001843 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1844 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1845 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001846 if (mddev->pers) {
1847 /* Cannot change size, so fail
1848 * If mddev->level <= 0, then we don't care
1849 * about aligning sizes (e.g. linear)
1850 */
1851 if (mddev->level > 0)
1852 return -ENOSPC;
1853 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001854 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* Verify rdev->desc_nr is unique.
1858 * If it is -1, assign a free number, else
1859 * check number is not in use
1860 */
1861 if (rdev->desc_nr < 0) {
1862 int choice = 0;
1863 if (mddev->pers) choice = mddev->raid_disks;
1864 while (find_rdev_nr(mddev, choice))
1865 choice++;
1866 rdev->desc_nr = choice;
1867 } else {
1868 if (find_rdev_nr(mddev, rdev->desc_nr))
1869 return -EBUSY;
1870 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001871 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1872 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1873 mdname(mddev), mddev->max_disks);
1874 return -EBUSY;
1875 }
NeilBrown19133a42005-11-08 21:39:35 -08001876 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001877 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001878 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001881 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001882
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001883 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001884 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001885
Tejun Heo0762b8b2008-08-25 19:56:12 +09001886 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001887 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1888 /* failure here is OK */;
1889 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001890
NeilBrown4b809912008-07-21 17:05:25 +10001891 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01001892 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001893
1894 /* May as well allow recovery to be retried once */
1895 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001898
1899 fail:
1900 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1901 b, mdname(mddev));
1902 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
NeilBrown177a99b2008-02-06 01:39:56 -08001905static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001906{
1907 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1908 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001909 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001910}
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1913{
1914 char b[BDEVNAME_SIZE];
1915 if (!rdev->mddev) {
1916 MD_BUG();
1917 return;
1918 }
Tejun Heo49731ba2011-01-14 18:43:57 +01001919 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001920 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1922 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001923 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001924 sysfs_put(rdev->sysfs_state);
1925 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001926 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001927 * writing to 'remove' to "dev/state". We also need
1928 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001929 */
NeilBrown4b809912008-07-21 17:05:25 +10001930 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001931 INIT_WORK(&rdev->del_work, md_delayed_delete);
1932 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001933 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
1936/*
1937 * prevent the device from being mounted, repartitioned or
1938 * otherwise reused by a RAID array (or any other kernel
1939 * subsystem), by bd_claiming the device.
1940 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001941static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 int err = 0;
1944 struct block_device *bdev;
1945 char b[BDEVNAME_SIZE];
1946
Tejun Heod4d77622010-11-13 11:55:18 +01001947 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1948 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (IS_ERR(bdev)) {
1950 printk(KERN_ERR "md: could not open %s.\n",
1951 __bdevname(dev, b));
1952 return PTR_ERR(bdev);
1953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 rdev->bdev = bdev;
1955 return err;
1956}
1957
1958static void unlock_rdev(mdk_rdev_t *rdev)
1959{
1960 struct block_device *bdev = rdev->bdev;
1961 rdev->bdev = NULL;
1962 if (!bdev)
1963 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01001964 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967void md_autodetect_dev(dev_t dev);
1968
1969static void export_rdev(mdk_rdev_t * rdev)
1970{
1971 char b[BDEVNAME_SIZE];
1972 printk(KERN_INFO "md: export_rdev(%s)\n",
1973 bdevname(rdev->bdev,b));
1974 if (rdev->mddev)
1975 MD_BUG();
1976 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001978 if (test_bit(AutoDetected, &rdev->flags))
1979 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#endif
1981 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001982 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
1985static void kick_rdev_from_array(mdk_rdev_t * rdev)
1986{
1987 unbind_rdev_from_array(rdev);
1988 export_rdev(rdev);
1989}
1990
1991static void export_array(mddev_t *mddev)
1992{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001993 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
NeilBrownd089c6a2008-02-06 01:39:59 -08001995 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (!rdev->mddev) {
1997 MD_BUG();
1998 continue;
1999 }
2000 kick_rdev_from_array(rdev);
2001 }
2002 if (!list_empty(&mddev->disks))
2003 MD_BUG();
2004 mddev->raid_disks = 0;
2005 mddev->major_version = 0;
2006}
2007
2008static void print_desc(mdp_disk_t *desc)
2009{
2010 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2011 desc->major,desc->minor,desc->raid_disk,desc->state);
2012}
2013
Cheng Renquancd2ac932009-01-09 08:31:08 +11002014static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 int i;
2017
2018 printk(KERN_INFO
2019 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2020 sb->major_version, sb->minor_version, sb->patch_version,
2021 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2022 sb->ctime);
2023 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2024 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2025 sb->md_minor, sb->layout, sb->chunk_size);
2026 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2027 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2028 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2029 sb->failed_disks, sb->spare_disks,
2030 sb->sb_csum, (unsigned long)sb->events_lo);
2031
2032 printk(KERN_INFO);
2033 for (i = 0; i < MD_SB_DISKS; i++) {
2034 mdp_disk_t *desc;
2035
2036 desc = sb->disks + i;
2037 if (desc->number || desc->major || desc->minor ||
2038 desc->raid_disk || (desc->state && (desc->state != 4))) {
2039 printk(" D %2d: ", i);
2040 print_desc(desc);
2041 }
2042 }
2043 printk(KERN_INFO "md: THIS: ");
2044 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045}
2046
Cheng Renquancd2ac932009-01-09 08:31:08 +11002047static void print_sb_1(struct mdp_superblock_1 *sb)
2048{
2049 __u8 *uuid;
2050
2051 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002052 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002053 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002054 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002055 le32_to_cpu(sb->major_version),
2056 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002057 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002058 sb->set_name,
2059 (unsigned long long)le64_to_cpu(sb->ctime)
2060 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2061
2062 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002063 printk(KERN_INFO
2064 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002065 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002066 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002067 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2068 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002069 le32_to_cpu(sb->level),
2070 (unsigned long long)le64_to_cpu(sb->size),
2071 le32_to_cpu(sb->raid_disks),
2072 le32_to_cpu(sb->layout),
2073 le32_to_cpu(sb->chunksize),
2074 (unsigned long long)le64_to_cpu(sb->data_offset),
2075 (unsigned long long)le64_to_cpu(sb->data_size),
2076 (unsigned long long)le64_to_cpu(sb->super_offset),
2077 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2078 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002079 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002080 sb->devflags,
2081 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2082 (unsigned long long)le64_to_cpu(sb->events),
2083 (unsigned long long)le64_to_cpu(sb->resync_offset),
2084 le32_to_cpu(sb->sb_csum),
2085 le32_to_cpu(sb->max_dev)
2086 );
2087}
2088
2089static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002092 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2093 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002094 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2095 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002097 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2098 switch (major_version) {
2099 case 0:
2100 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2101 break;
2102 case 1:
2103 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2104 break;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 } else
2107 printk(KERN_INFO "md: no rdev superblock!\n");
2108}
2109
Adrian Bunk5e563412006-06-26 00:27:42 -07002110static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002112 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 mdk_rdev_t *rdev;
2114 mddev_t *mddev;
2115 char b[BDEVNAME_SIZE];
2116
2117 printk("\n");
2118 printk("md: **********************************\n");
2119 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2120 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002121 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
NeilBrown32a76272005-06-21 17:17:14 -07002123 if (mddev->bitmap)
2124 bitmap_print_sb(mddev->bitmap);
2125 else
2126 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002127 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 printk("<%s>", bdevname(rdev->bdev,b));
2129 printk("\n");
2130
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002131 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002132 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 printk("md: **********************************\n");
2135 printk("\n");
2136}
2137
2138
NeilBrown42543762006-06-26 00:27:57 -07002139static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
NeilBrown42543762006-06-26 00:27:57 -07002141 /* Update each superblock (in-memory image), but
2142 * if we are allowed to, skip spares which already
2143 * have the right event counter, or have one earlier
2144 * (which would mean they aren't being marked as dirty
2145 * with the rest of the array)
2146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002148 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002149 if (rdev->sb_events == mddev->events ||
2150 (nospares &&
2151 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002152 rdev->sb_events+1 == mddev->events)) {
2153 /* Don't update this superblock */
2154 rdev->sb_loaded = 2;
2155 } else {
2156 super_types[mddev->major_version].
2157 sync_super(mddev, rdev);
2158 rdev->sb_loaded = 1;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161}
2162
NeilBrown850b2b42006-10-03 01:15:46 -07002163static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002166 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002167 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002170 /* First make sure individual recovery_offsets are correct */
2171 list_for_each_entry(rdev, &mddev->disks, same_set) {
2172 if (rdev->raid_disk >= 0 &&
2173 mddev->delta_disks >= 0 &&
2174 !test_bit(In_sync, &rdev->flags) &&
2175 mddev->curr_resync_completed > rdev->recovery_offset)
2176 rdev->recovery_offset = mddev->curr_resync_completed;
2177
2178 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002179 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002180 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002181 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002182 if (!mddev->external)
2183 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002184 wake_up(&mddev->sb_wait);
2185 return;
2186 }
2187
NeilBrowna9701a32005-11-08 21:39:34 -08002188 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002189
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002190 mddev->utime = get_seconds();
2191
NeilBrown850b2b42006-10-03 01:15:46 -07002192 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2193 force_change = 1;
2194 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2195 /* just a clean<-> dirty transition, possibly leave spares alone,
2196 * though if events isn't the right even/odd, we will have to do
2197 * spares after all
2198 */
2199 nospares = 1;
2200 if (force_change)
2201 nospares = 0;
2202 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002203 /* If the array is degraded, then skipping spares is both
2204 * dangerous and fairly pointless.
2205 * Dangerous because a device that was removed from the array
2206 * might have a event_count that still looks up-to-date,
2207 * so it can be re-added without a resync.
2208 * Pointless because if there are any spares to skip,
2209 * then a recovery will happen and soon that array won't
2210 * be degraded any more and the spare can go back to sleep then.
2211 */
NeilBrown850b2b42006-10-03 01:15:46 -07002212 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002213
NeilBrown06d91a52005-06-21 17:17:12 -07002214 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002215
2216 /* If this is just a dirty<->clean transition, and the array is clean
2217 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002218 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002219 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002220 && mddev->can_decrease_events
2221 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002222 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002223 mddev->can_decrease_events = 0;
2224 } else {
NeilBrown42543762006-06-26 00:27:57 -07002225 /* otherwise we have to go forward and ... */
2226 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002227 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 if (!mddev->events) {
2231 /*
2232 * oops, this 64-bit counter should never wrap.
2233 * Either we are in around ~1 trillion A.C., assuming
2234 * 1 reboot per second, or we have a bug:
2235 */
2236 MD_BUG();
2237 mddev->events --;
2238 }
NeilBrowne6910632008-02-06 01:39:51 -08002239 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002240 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 dprintk(KERN_INFO
2243 "md: updating %s RAID superblock on device (in sync %d)\n",
2244 mdname(mddev),mddev->in_sync);
2245
NeilBrown4ad13662007-07-17 04:06:13 -07002246 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002247 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 char b[BDEVNAME_SIZE];
2249 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002250 if (rdev->sb_loaded != 1)
2251 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002252 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 dprintk("(skipping faulty ");
2254
2255 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002256 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002257 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002258 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002259 rdev->sb_page);
2260 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2261 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002262 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002263 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 } else
2266 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002267 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /* only need to write one superblock... */
2269 break;
2270 }
NeilBrowna9701a32005-11-08 21:39:34 -08002271 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002272 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002273
NeilBrowna9701a32005-11-08 21:39:34 -08002274 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002275 if (mddev->in_sync != sync_req ||
2276 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002277 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002278 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002279 goto repeat;
2280 }
NeilBrown850b2b42006-10-03 01:15:46 -07002281 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002282 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002283 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002284 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2285 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Andre Noll7f6ce762008-03-23 18:34:54 +01002289/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002290 * We want to accept with case. For this we use cmd_match.
2291 */
2292static int cmd_match(const char *cmd, const char *str)
2293{
2294 /* See if cmd, written into a sysfs file, matches
2295 * str. They must either be the same, or cmd can
2296 * have a trailing newline
2297 */
2298 while (*cmd && *str && *cmd == *str) {
2299 cmd++;
2300 str++;
2301 }
2302 if (*cmd == '\n')
2303 cmd++;
2304 if (*str || *cmd)
2305 return 0;
2306 return 1;
2307}
2308
NeilBrown86e6ffd2005-11-08 21:39:24 -08002309struct rdev_sysfs_entry {
2310 struct attribute attr;
2311 ssize_t (*show)(mdk_rdev_t *, char *);
2312 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2313};
2314
2315static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002316state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002317{
2318 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002319 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002320
NeilBrownb2d444d2005-11-08 21:39:31 -08002321 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002322 len+= sprintf(page+len, "%sfaulty",sep);
2323 sep = ",";
2324 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002325 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002326 len += sprintf(page+len, "%sin_sync",sep);
2327 sep = ",";
2328 }
NeilBrownf6556752006-06-26 00:28:01 -07002329 if (test_bit(WriteMostly, &rdev->flags)) {
2330 len += sprintf(page+len, "%swrite_mostly",sep);
2331 sep = ",";
2332 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002333 if (test_bit(Blocked, &rdev->flags)) {
2334 len += sprintf(page+len, "%sblocked", sep);
2335 sep = ",";
2336 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002337 if (!test_bit(Faulty, &rdev->flags) &&
2338 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002339 len += sprintf(page+len, "%sspare", sep);
2340 sep = ",";
2341 }
2342 return len+sprintf(page+len, "\n");
2343}
2344
NeilBrown45dc2de2006-06-26 00:27:58 -07002345static ssize_t
2346state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2347{
2348 /* can write
2349 * faulty - simulates and error
2350 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002351 * writemostly - sets write_mostly
2352 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002353 * blocked - sets the Blocked flag
2354 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002355 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002356 */
2357 int err = -EINVAL;
2358 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2359 md_error(rdev->mddev, rdev);
2360 err = 0;
2361 } else if (cmd_match(buf, "remove")) {
2362 if (rdev->raid_disk >= 0)
2363 err = -EBUSY;
2364 else {
2365 mddev_t *mddev = rdev->mddev;
2366 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002367 if (mddev->pers)
2368 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002369 md_new_event(mddev);
2370 err = 0;
2371 }
NeilBrownf6556752006-06-26 00:28:01 -07002372 } else if (cmd_match(buf, "writemostly")) {
2373 set_bit(WriteMostly, &rdev->flags);
2374 err = 0;
2375 } else if (cmd_match(buf, "-writemostly")) {
2376 clear_bit(WriteMostly, &rdev->flags);
2377 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002378 } else if (cmd_match(buf, "blocked")) {
2379 set_bit(Blocked, &rdev->flags);
2380 err = 0;
2381 } else if (cmd_match(buf, "-blocked")) {
2382 clear_bit(Blocked, &rdev->flags);
2383 wake_up(&rdev->blocked_wait);
2384 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2385 md_wakeup_thread(rdev->mddev->thread);
2386
2387 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002388 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2389 set_bit(In_sync, &rdev->flags);
2390 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002391 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002392 if (!err)
2393 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002394 return err ? err : len;
2395}
NeilBrown80ca3a42006-07-10 04:44:18 -07002396static struct rdev_sysfs_entry rdev_state =
2397__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002398
2399static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002400errors_show(mdk_rdev_t *rdev, char *page)
2401{
2402 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2403}
2404
2405static ssize_t
2406errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2407{
2408 char *e;
2409 unsigned long n = simple_strtoul(buf, &e, 10);
2410 if (*buf && (*e == 0 || *e == '\n')) {
2411 atomic_set(&rdev->corrected_errors, n);
2412 return len;
2413 }
2414 return -EINVAL;
2415}
2416static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002417__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002418
NeilBrown014236d2006-01-06 00:20:55 -08002419static ssize_t
2420slot_show(mdk_rdev_t *rdev, char *page)
2421{
2422 if (rdev->raid_disk < 0)
2423 return sprintf(page, "none\n");
2424 else
2425 return sprintf(page, "%d\n", rdev->raid_disk);
2426}
2427
2428static ssize_t
2429slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2430{
2431 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002432 int err;
2433 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002434 int slot = simple_strtoul(buf, &e, 10);
2435 if (strncmp(buf, "none", 4)==0)
2436 slot = -1;
2437 else if (e==buf || (*e && *e!= '\n'))
2438 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002439 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002440 /* Setting 'slot' on an active array requires also
2441 * updating the 'rd%d' link, and communicating
2442 * with the personality with ->hot_*_disk.
2443 * For now we only support removing
2444 * failed/spare devices. This normally happens automatically,
2445 * but not when the metadata is externally managed.
2446 */
NeilBrownc303da62008-02-06 01:39:51 -08002447 if (rdev->raid_disk == -1)
2448 return -EEXIST;
2449 /* personality does all needed checks */
2450 if (rdev->mddev->pers->hot_add_disk == NULL)
2451 return -EINVAL;
2452 err = rdev->mddev->pers->
2453 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2454 if (err)
2455 return err;
2456 sprintf(nm, "rd%d", rdev->raid_disk);
2457 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002458 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002459 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2460 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002461 } else if (rdev->mddev->pers) {
2462 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002463 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002464 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002465 */
2466
2467 if (rdev->raid_disk != -1)
2468 return -EBUSY;
2469
NeilBrownc6751b22011-02-02 11:57:13 +11002470 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2471 return -EBUSY;
2472
Neil Brown6c2fce22008-06-28 08:31:31 +10002473 if (rdev->mddev->pers->hot_add_disk == NULL)
2474 return -EINVAL;
2475
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002476 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002477 if (rdev2->raid_disk == slot)
2478 return -EEXIST;
2479
NeilBrownba1b41b2011-01-14 09:14:34 +11002480 if (slot >= rdev->mddev->raid_disks &&
2481 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2482 return -ENOSPC;
2483
Neil Brown6c2fce22008-06-28 08:31:31 +10002484 rdev->raid_disk = slot;
2485 if (test_bit(In_sync, &rdev->flags))
2486 rdev->saved_raid_disk = slot;
2487 else
2488 rdev->saved_raid_disk = -1;
2489 err = rdev->mddev->pers->
2490 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002491 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002492 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002493 return err;
Neil Brown52664732008-06-28 08:31:44 +10002494 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002495 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002496 sprintf(nm, "rd%d", rdev->raid_disk);
2497 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002498 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002499 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002500 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002501 if (slot >= rdev->mddev->raid_disks &&
2502 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002503 return -ENOSPC;
2504 rdev->raid_disk = slot;
2505 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002506 clear_bit(Faulty, &rdev->flags);
2507 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002508 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002509 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002510 }
NeilBrown014236d2006-01-06 00:20:55 -08002511 return len;
2512}
2513
2514
2515static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002516__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002517
NeilBrown93c8cad2006-01-06 00:20:56 -08002518static ssize_t
2519offset_show(mdk_rdev_t *rdev, char *page)
2520{
Andrew Morton6961ece2006-01-06 00:20:59 -08002521 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002522}
2523
2524static ssize_t
2525offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2526{
2527 char *e;
2528 unsigned long long offset = simple_strtoull(buf, &e, 10);
2529 if (e==buf || (*e && *e != '\n'))
2530 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002531 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002532 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002533 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002534 /* Must set offset before size, so overlap checks
2535 * can be sane */
2536 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002537 rdev->data_offset = offset;
2538 return len;
2539}
2540
2541static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002542__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002543
NeilBrown83303b62006-01-06 00:21:06 -08002544static ssize_t
2545rdev_size_show(mdk_rdev_t *rdev, char *page)
2546{
Andre Nolldd8ac332009-03-31 14:33:13 +11002547 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002548}
2549
NeilBrownc5d79ad2008-02-06 01:39:54 -08002550static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2551{
2552 /* check if two start/length pairs overlap */
2553 if (s1+l1 <= s2)
2554 return 0;
2555 if (s2+l2 <= s1)
2556 return 0;
2557 return 1;
2558}
2559
Dan Williamsb522adc2009-03-31 15:00:31 +11002560static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2561{
2562 unsigned long long blocks;
2563 sector_t new;
2564
2565 if (strict_strtoull(buf, 10, &blocks) < 0)
2566 return -EINVAL;
2567
2568 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2569 return -EINVAL; /* sector conversion overflow */
2570
2571 new = blocks * 2;
2572 if (new != blocks * 2)
2573 return -EINVAL; /* unsigned long long to sector_t overflow */
2574
2575 *sectors = new;
2576 return 0;
2577}
2578
NeilBrown83303b62006-01-06 00:21:06 -08002579static ssize_t
2580rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2581{
NeilBrown27c529b2008-03-04 14:29:33 -08002582 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002583 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002584 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002585
Dan Williamsb522adc2009-03-31 15:00:31 +11002586 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002587 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002588 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002589 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002590 sectors = super_types[my_mddev->major_version].
2591 rdev_size_change(rdev, sectors);
2592 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002593 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002594 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002595 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002596 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002597 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002598 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002599 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002600
Andre Nolldd8ac332009-03-31 14:33:13 +11002601 rdev->sectors = sectors;
2602 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002603 /* need to check that all other rdevs with the same ->bdev
2604 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002605 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002606 * we have to change it back, we will have the lock again.
2607 */
2608 mddev_t *mddev;
2609 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002610 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002611
NeilBrown27c529b2008-03-04 14:29:33 -08002612 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002613 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002614 mdk_rdev_t *rdev2;
2615
2616 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002617 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002618 if (rdev->bdev == rdev2->bdev &&
2619 rdev != rdev2 &&
2620 overlaps(rdev->data_offset, rdev->sectors,
2621 rdev2->data_offset,
2622 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002623 overlap = 1;
2624 break;
2625 }
2626 mddev_unlock(mddev);
2627 if (overlap) {
2628 mddev_put(mddev);
2629 break;
2630 }
2631 }
NeilBrown27c529b2008-03-04 14:29:33 -08002632 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002633 if (overlap) {
2634 /* Someone else could have slipped in a size
2635 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002636 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002637 * safe, and trust userspace not to race with
2638 * itself
2639 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002640 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002641 return -EBUSY;
2642 }
2643 }
NeilBrown83303b62006-01-06 00:21:06 -08002644 return len;
2645}
2646
2647static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002648__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002649
Dan Williams06e3c812009-12-12 21:17:12 -07002650
2651static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2652{
2653 unsigned long long recovery_start = rdev->recovery_offset;
2654
2655 if (test_bit(In_sync, &rdev->flags) ||
2656 recovery_start == MaxSector)
2657 return sprintf(page, "none\n");
2658
2659 return sprintf(page, "%llu\n", recovery_start);
2660}
2661
2662static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2663{
2664 unsigned long long recovery_start;
2665
2666 if (cmd_match(buf, "none"))
2667 recovery_start = MaxSector;
2668 else if (strict_strtoull(buf, 10, &recovery_start))
2669 return -EINVAL;
2670
2671 if (rdev->mddev->pers &&
2672 rdev->raid_disk >= 0)
2673 return -EBUSY;
2674
2675 rdev->recovery_offset = recovery_start;
2676 if (recovery_start == MaxSector)
2677 set_bit(In_sync, &rdev->flags);
2678 else
2679 clear_bit(In_sync, &rdev->flags);
2680 return len;
2681}
2682
2683static struct rdev_sysfs_entry rdev_recovery_start =
2684__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2685
NeilBrown86e6ffd2005-11-08 21:39:24 -08002686static struct attribute *rdev_default_attrs[] = {
2687 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002688 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002689 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002690 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002691 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002692 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002693 NULL,
2694};
2695static ssize_t
2696rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2697{
2698 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2699 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002700 mddev_t *mddev = rdev->mddev;
2701 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002702
2703 if (!entry->show)
2704 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002705
2706 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2707 if (!rv) {
2708 if (rdev->mddev == NULL)
2709 rv = -EBUSY;
2710 else
2711 rv = entry->show(rdev, page);
2712 mddev_unlock(mddev);
2713 }
2714 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002715}
2716
2717static ssize_t
2718rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2719 const char *page, size_t length)
2720{
2721 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2722 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002723 ssize_t rv;
2724 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002725
2726 if (!entry->store)
2727 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002728 if (!capable(CAP_SYS_ADMIN))
2729 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002730 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002731 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002732 if (rdev->mddev == NULL)
2733 rv = -EBUSY;
2734 else
2735 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002736 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002737 }
2738 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002739}
2740
2741static void rdev_free(struct kobject *ko)
2742{
2743 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2744 kfree(rdev);
2745}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002746static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002747 .show = rdev_attr_show,
2748 .store = rdev_attr_store,
2749};
2750static struct kobj_type rdev_ktype = {
2751 .release = rdev_free,
2752 .sysfs_ops = &rdev_sysfs_ops,
2753 .default_attrs = rdev_default_attrs,
2754};
2755
NeilBrowne8bb9a82010-06-01 19:37:26 +10002756void md_rdev_init(mdk_rdev_t *rdev)
2757{
2758 rdev->desc_nr = -1;
2759 rdev->saved_raid_disk = -1;
2760 rdev->raid_disk = -1;
2761 rdev->flags = 0;
2762 rdev->data_offset = 0;
2763 rdev->sb_events = 0;
2764 rdev->last_read_error.tv_sec = 0;
2765 rdev->last_read_error.tv_nsec = 0;
2766 atomic_set(&rdev->nr_pending, 0);
2767 atomic_set(&rdev->read_errors, 0);
2768 atomic_set(&rdev->corrected_errors, 0);
2769
2770 INIT_LIST_HEAD(&rdev->same_set);
2771 init_waitqueue_head(&rdev->blocked_wait);
2772}
2773EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774/*
2775 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2776 *
2777 * mark the device faulty if:
2778 *
2779 * - the device is nonexistent (zero size)
2780 * - the device has no valid superblock
2781 *
2782 * a faulty rdev _never_ has rdev->sb set.
2783 */
2784static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2785{
2786 char b[BDEVNAME_SIZE];
2787 int err;
2788 mdk_rdev_t *rdev;
2789 sector_t size;
2790
NeilBrown9ffae0c2006-01-06 00:20:32 -08002791 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (!rdev) {
2793 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2794 return ERR_PTR(-ENOMEM);
2795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
NeilBrowne8bb9a82010-06-01 19:37:26 +10002797 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if ((err = alloc_disk_sb(rdev)))
2799 goto abort_free;
2800
NeilBrownc5d79ad2008-02-06 01:39:54 -08002801 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (err)
2803 goto abort_free;
2804
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002805 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002806
Mike Snitzer77304d22010-11-08 14:39:12 +01002807 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (!size) {
2809 printk(KERN_WARNING
2810 "md: %s has zero or unknown size, marking faulty!\n",
2811 bdevname(rdev->bdev,b));
2812 err = -EINVAL;
2813 goto abort_free;
2814 }
2815
2816 if (super_format >= 0) {
2817 err = super_types[super_format].
2818 load_super(rdev, NULL, super_minor);
2819 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002820 printk(KERN_WARNING
2821 "md: %s does not have a valid v%d.%d "
2822 "superblock, not importing!\n",
2823 bdevname(rdev->bdev,b),
2824 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 goto abort_free;
2826 }
2827 if (err < 0) {
2828 printk(KERN_WARNING
2829 "md: could not read %s's sb, not importing!\n",
2830 bdevname(rdev->bdev,b));
2831 goto abort_free;
2832 }
2833 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return rdev;
2836
2837abort_free:
2838 if (rdev->sb_page) {
2839 if (rdev->bdev)
2840 unlock_rdev(rdev);
2841 free_disk_sb(rdev);
2842 }
2843 kfree(rdev);
2844 return ERR_PTR(err);
2845}
2846
2847/*
2848 * Check a full RAID array for plausibility
2849 */
2850
2851
NeilBrowna757e642005-04-16 15:26:42 -07002852static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
2854 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002855 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 char b[BDEVNAME_SIZE];
2857
2858 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002859 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 switch (super_types[mddev->major_version].
2861 load_super(rdev, freshest, mddev->minor_version)) {
2862 case 1:
2863 freshest = rdev;
2864 break;
2865 case 0:
2866 break;
2867 default:
2868 printk( KERN_ERR \
2869 "md: fatal superblock inconsistency in %s"
2870 " -- removing from array\n",
2871 bdevname(rdev->bdev,b));
2872 kick_rdev_from_array(rdev);
2873 }
2874
2875
2876 super_types[mddev->major_version].
2877 validate_super(mddev, freshest);
2878
2879 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002880 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002881 if (mddev->max_disks &&
2882 (rdev->desc_nr >= mddev->max_disks ||
2883 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002884 printk(KERN_WARNING
2885 "md: %s: %s: only %d devices permitted\n",
2886 mdname(mddev), bdevname(rdev->bdev, b),
2887 mddev->max_disks);
2888 kick_rdev_from_array(rdev);
2889 continue;
2890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (rdev != freshest)
2892 if (super_types[mddev->major_version].
2893 validate_super(mddev, rdev)) {
2894 printk(KERN_WARNING "md: kicking non-fresh %s"
2895 " from array!\n",
2896 bdevname(rdev->bdev,b));
2897 kick_rdev_from_array(rdev);
2898 continue;
2899 }
2900 if (mddev->level == LEVEL_MULTIPATH) {
2901 rdev->desc_nr = i++;
2902 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002903 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002904 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002905 rdev->raid_disk = -1;
2906 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
NeilBrown72e02072009-12-14 12:49:55 +11002911/* Read a fixed-point number.
2912 * Numbers in sysfs attributes should be in "standard" units where
2913 * possible, so time should be in seconds.
2914 * However we internally use a a much smaller unit such as
2915 * milliseconds or jiffies.
2916 * This function takes a decimal number with a possible fractional
2917 * component, and produces an integer which is the result of
2918 * multiplying that number by 10^'scale'.
2919 * all without any floating-point arithmetic.
2920 */
2921int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2922{
2923 unsigned long result = 0;
2924 long decimals = -1;
2925 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2926 if (*cp == '.')
2927 decimals = 0;
2928 else if (decimals < scale) {
2929 unsigned int value;
2930 value = *cp - '0';
2931 result = result * 10 + value;
2932 if (decimals >= 0)
2933 decimals++;
2934 }
2935 cp++;
2936 }
2937 if (*cp == '\n')
2938 cp++;
2939 if (*cp)
2940 return -EINVAL;
2941 if (decimals < 0)
2942 decimals = 0;
2943 while (decimals < scale) {
2944 result *= 10;
2945 decimals ++;
2946 }
2947 *res = result;
2948 return 0;
2949}
2950
2951
NeilBrown19052c02008-08-05 15:54:13 +10002952static void md_safemode_timeout(unsigned long data);
2953
NeilBrowneae17012005-11-08 21:39:23 -08002954static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002955safe_delay_show(mddev_t *mddev, char *page)
2956{
2957 int msec = (mddev->safemode_delay*1000)/HZ;
2958 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2959}
2960static ssize_t
2961safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2962{
NeilBrown16f17b32006-06-26 00:27:37 -07002963 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002964
NeilBrown72e02072009-12-14 12:49:55 +11002965 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002966 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002967 if (msec == 0)
2968 mddev->safemode_delay = 0;
2969 else {
NeilBrown19052c02008-08-05 15:54:13 +10002970 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002971 mddev->safemode_delay = (msec*HZ)/1000;
2972 if (mddev->safemode_delay == 0)
2973 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002974 if (mddev->safemode_delay < old_delay)
2975 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002976 }
2977 return len;
2978}
2979static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002980__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002981
2982static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002983level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002984{
NeilBrown2604b702006-01-06 00:20:36 -08002985 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002986 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002987 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002988 else if (mddev->clevel[0])
2989 return sprintf(page, "%s\n", mddev->clevel);
2990 else if (mddev->level != LEVEL_NONE)
2991 return sprintf(page, "%d\n", mddev->level);
2992 else
2993 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002994}
2995
NeilBrownd9d166c2006-01-06 00:20:51 -08002996static ssize_t
2997level_store(mddev_t *mddev, const char *buf, size_t len)
2998{
Dan Williamsf2859af2010-05-02 10:04:16 -07002999 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003000 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003001 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003002 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003003 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003004 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003005
3006 if (mddev->pers == NULL) {
3007 if (len == 0)
3008 return 0;
3009 if (len >= sizeof(mddev->clevel))
3010 return -ENOSPC;
3011 strncpy(mddev->clevel, buf, len);
3012 if (mddev->clevel[len-1] == '\n')
3013 len--;
3014 mddev->clevel[len] = 0;
3015 mddev->level = LEVEL_NONE;
3016 return rv;
3017 }
3018
3019 /* request to change the personality. Need to ensure:
3020 * - array is not engaged in resync/recovery/reshape
3021 * - old personality can be suspended
3022 * - new personality will access other array.
3023 */
3024
NeilBrownbb4f1e92010-08-08 21:18:03 +10003025 if (mddev->sync_thread ||
3026 mddev->reshape_position != MaxSector ||
3027 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003028 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003029
3030 if (!mddev->pers->quiesce) {
3031 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3032 mdname(mddev), mddev->pers->name);
3033 return -EINVAL;
3034 }
3035
3036 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003037 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003038 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003039 strncpy(clevel, buf, len);
3040 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003041 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003042 clevel[len] = 0;
3043 if (strict_strtol(clevel, 10, &level))
3044 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003045
Dan Williamsf2859af2010-05-02 10:04:16 -07003046 if (request_module("md-%s", clevel) != 0)
3047 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003048 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003049 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003050 if (!pers || !try_module_get(pers->owner)) {
3051 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003052 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003053 return -EINVAL;
3054 }
3055 spin_unlock(&pers_lock);
3056
3057 if (pers == mddev->pers) {
3058 /* Nothing to do! */
3059 module_put(pers->owner);
3060 return rv;
3061 }
3062 if (!pers->takeover) {
3063 module_put(pers->owner);
3064 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003065 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003066 return -EINVAL;
3067 }
3068
NeilBrowne93f68a2010-06-15 09:36:03 +01003069 list_for_each_entry(rdev, &mddev->disks, same_set)
3070 rdev->new_raid_disk = rdev->raid_disk;
3071
NeilBrown245f46c2009-03-31 14:39:39 +11003072 /* ->takeover must set new_* and/or delta_disks
3073 * if it succeeds, and may set them when it fails.
3074 */
3075 priv = pers->takeover(mddev);
3076 if (IS_ERR(priv)) {
3077 mddev->new_level = mddev->level;
3078 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003079 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003080 mddev->raid_disks -= mddev->delta_disks;
3081 mddev->delta_disks = 0;
3082 module_put(pers->owner);
3083 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003084 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003085 return PTR_ERR(priv);
3086 }
3087
3088 /* Looks like we have a winner */
3089 mddev_suspend(mddev);
3090 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003091
3092 if (mddev->pers->sync_request == NULL &&
3093 pers->sync_request != NULL) {
3094 /* need to add the md_redundancy_group */
3095 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3096 printk(KERN_WARNING
3097 "md: cannot register extra attributes for %s\n",
3098 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003099 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003100 }
3101 if (mddev->pers->sync_request != NULL &&
3102 pers->sync_request == NULL) {
3103 /* need to remove the md_redundancy_group */
3104 if (mddev->to_remove == NULL)
3105 mddev->to_remove = &md_redundancy_group;
3106 }
3107
Trela Maciej54071b32010-03-08 16:02:42 +11003108 if (mddev->pers->sync_request == NULL &&
3109 mddev->external) {
3110 /* We are converting from a no-redundancy array
3111 * to a redundancy array and metadata is managed
3112 * externally so we need to be sure that writes
3113 * won't block due to a need to transition
3114 * clean->dirty
3115 * until external management is started.
3116 */
3117 mddev->in_sync = 0;
3118 mddev->safemode_delay = 0;
3119 mddev->safemode = 0;
3120 }
3121
NeilBrowne93f68a2010-06-15 09:36:03 +01003122 list_for_each_entry(rdev, &mddev->disks, same_set) {
3123 char nm[20];
3124 if (rdev->raid_disk < 0)
3125 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003126 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003127 rdev->new_raid_disk = -1;
3128 if (rdev->new_raid_disk == rdev->raid_disk)
3129 continue;
3130 sprintf(nm, "rd%d", rdev->raid_disk);
3131 sysfs_remove_link(&mddev->kobj, nm);
3132 }
3133 list_for_each_entry(rdev, &mddev->disks, same_set) {
3134 if (rdev->raid_disk < 0)
3135 continue;
3136 if (rdev->new_raid_disk == rdev->raid_disk)
3137 continue;
3138 rdev->raid_disk = rdev->new_raid_disk;
3139 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003140 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003141 else {
3142 char nm[20];
3143 sprintf(nm, "rd%d", rdev->raid_disk);
3144 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3145 printk("md: cannot register %s for %s after level change\n",
3146 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003147 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003148 }
3149
3150 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003151 mddev->pers = pers;
3152 mddev->private = priv;
3153 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3154 mddev->level = mddev->new_level;
3155 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003156 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003157 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003158 if (mddev->pers->sync_request == NULL) {
3159 /* this is now an array without redundancy, so
3160 * it must always be in_sync
3161 */
3162 mddev->in_sync = 1;
3163 del_timer_sync(&mddev->safemode_timer);
3164 }
NeilBrown245f46c2009-03-31 14:39:39 +11003165 pers->run(mddev);
3166 mddev_resume(mddev);
3167 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3168 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3169 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003170 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003171 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003172 return rv;
3173}
3174
3175static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003176__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003177
NeilBrownd4dbd022006-06-26 00:27:59 -07003178
3179static ssize_t
3180layout_show(mddev_t *mddev, char *page)
3181{
3182 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003183 if (mddev->reshape_position != MaxSector &&
3184 mddev->layout != mddev->new_layout)
3185 return sprintf(page, "%d (%d)\n",
3186 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003187 return sprintf(page, "%d\n", mddev->layout);
3188}
3189
3190static ssize_t
3191layout_store(mddev_t *mddev, const char *buf, size_t len)
3192{
3193 char *e;
3194 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003195
3196 if (!*buf || (*e && *e != '\n'))
3197 return -EINVAL;
3198
NeilBrownb3546032009-03-31 14:56:41 +11003199 if (mddev->pers) {
3200 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003201 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003202 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003203 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003204 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003205 if (err) {
3206 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003207 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003208 }
NeilBrownb3546032009-03-31 14:56:41 +11003209 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003210 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003211 if (mddev->reshape_position == MaxSector)
3212 mddev->layout = n;
3213 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003214 return len;
3215}
3216static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003217__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003218
3219
NeilBrowneae17012005-11-08 21:39:23 -08003220static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003221raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003222{
NeilBrownbb636542005-11-08 21:39:45 -08003223 if (mddev->raid_disks == 0)
3224 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003225 if (mddev->reshape_position != MaxSector &&
3226 mddev->delta_disks != 0)
3227 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3228 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003229 return sprintf(page, "%d\n", mddev->raid_disks);
3230}
3231
NeilBrownda943b92006-01-06 00:20:54 -08003232static int update_raid_disks(mddev_t *mddev, int raid_disks);
3233
3234static ssize_t
3235raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3236{
NeilBrownda943b92006-01-06 00:20:54 -08003237 char *e;
3238 int rv = 0;
3239 unsigned long n = simple_strtoul(buf, &e, 10);
3240
3241 if (!*buf || (*e && *e != '\n'))
3242 return -EINVAL;
3243
3244 if (mddev->pers)
3245 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003246 else if (mddev->reshape_position != MaxSector) {
3247 int olddisks = mddev->raid_disks - mddev->delta_disks;
3248 mddev->delta_disks = n - olddisks;
3249 mddev->raid_disks = n;
3250 } else
NeilBrownda943b92006-01-06 00:20:54 -08003251 mddev->raid_disks = n;
3252 return rv ? rv : len;
3253}
3254static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003255__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003256
NeilBrown24dd4692005-11-08 21:39:26 -08003257static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003258chunk_size_show(mddev_t *mddev, char *page)
3259{
NeilBrown08a02ec2007-05-09 02:35:38 -07003260 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003261 mddev->chunk_sectors != mddev->new_chunk_sectors)
3262 return sprintf(page, "%d (%d)\n",
3263 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003264 mddev->chunk_sectors << 9);
3265 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003266}
3267
3268static ssize_t
3269chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3270{
NeilBrown3b343802006-01-06 00:20:47 -08003271 char *e;
3272 unsigned long n = simple_strtoul(buf, &e, 10);
3273
NeilBrown3b343802006-01-06 00:20:47 -08003274 if (!*buf || (*e && *e != '\n'))
3275 return -EINVAL;
3276
NeilBrownb3546032009-03-31 14:56:41 +11003277 if (mddev->pers) {
3278 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003279 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003280 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003281 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003282 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003283 if (err) {
3284 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003285 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003286 }
NeilBrownb3546032009-03-31 14:56:41 +11003287 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003288 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003289 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003290 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003291 }
NeilBrown3b343802006-01-06 00:20:47 -08003292 return len;
3293}
3294static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003295__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003296
NeilBrowna94213b2006-06-26 00:28:00 -07003297static ssize_t
3298resync_start_show(mddev_t *mddev, char *page)
3299{
NeilBrownd1a7c502009-03-31 15:24:32 +11003300 if (mddev->recovery_cp == MaxSector)
3301 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003302 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3303}
3304
3305static ssize_t
3306resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3307{
NeilBrowna94213b2006-06-26 00:28:00 -07003308 char *e;
3309 unsigned long long n = simple_strtoull(buf, &e, 10);
3310
3311 if (mddev->pers)
3312 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003313 if (cmd_match(buf, "none"))
3314 n = MaxSector;
3315 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003316 return -EINVAL;
3317
3318 mddev->recovery_cp = n;
3319 return len;
3320}
3321static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003322__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003323
NeilBrown9e653b62006-06-26 00:27:58 -07003324/*
3325 * The array state can be:
3326 *
3327 * clear
3328 * No devices, no size, no level
3329 * Equivalent to STOP_ARRAY ioctl
3330 * inactive
3331 * May have some settings, but array is not active
3332 * all IO results in error
3333 * When written, doesn't tear down array, but just stops it
3334 * suspended (not supported yet)
3335 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003336 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003337 * readonly
3338 * no resync can happen. no superblocks get written.
3339 * write requests fail
3340 * read-auto
3341 * like readonly, but behaves like 'clean' on a write request.
3342 *
3343 * clean - no pending writes, but otherwise active.
3344 * When written to inactive array, starts without resync
3345 * If a write request arrives then
3346 * if metadata is known, mark 'dirty' and switch to 'active'.
3347 * if not known, block and switch to write-pending
3348 * If written to an active array that has pending writes, then fails.
3349 * active
3350 * fully active: IO and resync can be happening.
3351 * When written to inactive array, starts with resync
3352 *
3353 * write-pending
3354 * clean, but writes are blocked waiting for 'active' to be written.
3355 *
3356 * active-idle
3357 * like active, but no writes have been seen for a while (100msec).
3358 *
3359 */
3360enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3361 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003362static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003363 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3364 "write-pending", "active-idle", NULL };
3365
3366static int match_word(const char *word, char **list)
3367{
3368 int n;
3369 for (n=0; list[n]; n++)
3370 if (cmd_match(word, list[n]))
3371 break;
3372 return n;
3373}
3374
3375static ssize_t
3376array_state_show(mddev_t *mddev, char *page)
3377{
3378 enum array_state st = inactive;
3379
3380 if (mddev->pers)
3381 switch(mddev->ro) {
3382 case 1:
3383 st = readonly;
3384 break;
3385 case 2:
3386 st = read_auto;
3387 break;
3388 case 0:
3389 if (mddev->in_sync)
3390 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003391 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003392 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003393 else if (mddev->safemode)
3394 st = active_idle;
3395 else
3396 st = active;
3397 }
3398 else {
3399 if (list_empty(&mddev->disks) &&
3400 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003401 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003402 st = clear;
3403 else
3404 st = inactive;
3405 }
3406 return sprintf(page, "%s\n", array_states[st]);
3407}
3408
Neil Browndf5b20c2008-07-11 22:02:22 +10003409static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003410static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003411static int do_md_run(mddev_t * mddev);
3412static int restart_array(mddev_t *mddev);
3413
3414static ssize_t
3415array_state_store(mddev_t *mddev, const char *buf, size_t len)
3416{
3417 int err = -EINVAL;
3418 enum array_state st = match_word(buf, array_states);
3419 switch(st) {
3420 case bad_word:
3421 break;
3422 case clear:
3423 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003424 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003425 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003426 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003427 break;
3428 case inactive:
3429 /* stopping an active array */
3430 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003431 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003432 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003433 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003434 } else
3435 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003436 break;
3437 case suspended:
3438 break; /* not supported yet */
3439 case readonly:
3440 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003441 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003442 else {
3443 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003444 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003445 err = do_md_run(mddev);
3446 }
3447 break;
3448 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003449 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003450 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003451 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003452 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003453 err = restart_array(mddev);
3454 if (err == 0) {
3455 mddev->ro = 2;
3456 set_disk_ro(mddev->gendisk, 0);
3457 }
NeilBrown9e653b62006-06-26 00:27:58 -07003458 } else {
3459 mddev->ro = 2;
3460 err = do_md_run(mddev);
3461 }
3462 break;
3463 case clean:
3464 if (mddev->pers) {
3465 restart_array(mddev);
3466 spin_lock_irq(&mddev->write_lock);
3467 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003468 if (mddev->in_sync == 0) {
3469 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003470 if (mddev->safemode == 1)
3471 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003472 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003473 }
3474 err = 0;
3475 } else
3476 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003477 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003478 } else
3479 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003480 break;
3481 case active:
3482 if (mddev->pers) {
3483 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003484 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003485 wake_up(&mddev->sb_wait);
3486 err = 0;
3487 } else {
3488 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003489 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003490 err = do_md_run(mddev);
3491 }
3492 break;
3493 case write_pending:
3494 case active_idle:
3495 /* these cannot be set */
3496 break;
3497 }
3498 if (err)
3499 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003500 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003501 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003502 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003503 }
NeilBrown9e653b62006-06-26 00:27:58 -07003504}
NeilBrown80ca3a42006-07-10 04:44:18 -07003505static struct md_sysfs_entry md_array_state =
3506__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003507
NeilBrown6d7ff732006-01-06 00:21:16 -08003508static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003509max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3510 return sprintf(page, "%d\n",
3511 atomic_read(&mddev->max_corr_read_errors));
3512}
3513
3514static ssize_t
3515max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3516{
3517 char *e;
3518 unsigned long n = simple_strtoul(buf, &e, 10);
3519
3520 if (*buf && (*e == 0 || *e == '\n')) {
3521 atomic_set(&mddev->max_corr_read_errors, n);
3522 return len;
3523 }
3524 return -EINVAL;
3525}
3526
3527static struct md_sysfs_entry max_corr_read_errors =
3528__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3529 max_corrected_read_errors_store);
3530
3531static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003532null_show(mddev_t *mddev, char *page)
3533{
3534 return -EINVAL;
3535}
3536
3537static ssize_t
3538new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3539{
3540 /* buf must be %d:%d\n? giving major and minor numbers */
3541 /* The new device is added to the array.
3542 * If the array has a persistent superblock, we read the
3543 * superblock to initialise info and check validity.
3544 * Otherwise, only checking done is that in bind_rdev_to_array,
3545 * which mainly checks size.
3546 */
3547 char *e;
3548 int major = simple_strtoul(buf, &e, 10);
3549 int minor;
3550 dev_t dev;
3551 mdk_rdev_t *rdev;
3552 int err;
3553
3554 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3555 return -EINVAL;
3556 minor = simple_strtoul(e+1, &e, 10);
3557 if (*e && *e != '\n')
3558 return -EINVAL;
3559 dev = MKDEV(major, minor);
3560 if (major != MAJOR(dev) ||
3561 minor != MINOR(dev))
3562 return -EOVERFLOW;
3563
3564
3565 if (mddev->persistent) {
3566 rdev = md_import_device(dev, mddev->major_version,
3567 mddev->minor_version);
3568 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3569 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3570 mdk_rdev_t, same_set);
3571 err = super_types[mddev->major_version]
3572 .load_super(rdev, rdev0, mddev->minor_version);
3573 if (err < 0)
3574 goto out;
3575 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003576 } else if (mddev->external)
3577 rdev = md_import_device(dev, -2, -1);
3578 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003579 rdev = md_import_device(dev, -1, -1);
3580
3581 if (IS_ERR(rdev))
3582 return PTR_ERR(rdev);
3583 err = bind_rdev_to_array(rdev, mddev);
3584 out:
3585 if (err)
3586 export_rdev(rdev);
3587 return err ? err : len;
3588}
3589
3590static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003591__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003592
3593static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003594bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3595{
3596 char *end;
3597 unsigned long chunk, end_chunk;
3598
3599 if (!mddev->bitmap)
3600 goto out;
3601 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3602 while (*buf) {
3603 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3604 if (buf == end) break;
3605 if (*end == '-') { /* range */
3606 buf = end + 1;
3607 end_chunk = simple_strtoul(buf, &end, 0);
3608 if (buf == end) break;
3609 }
3610 if (*end && !isspace(*end)) break;
3611 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003612 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003613 }
3614 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3615out:
3616 return len;
3617}
3618
3619static struct md_sysfs_entry md_bitmap =
3620__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3621
3622static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003623size_show(mddev_t *mddev, char *page)
3624{
Andre Noll58c0fed2009-03-31 14:33:13 +11003625 return sprintf(page, "%llu\n",
3626 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003627}
3628
Andre Nolld71f9f82008-07-11 22:02:22 +10003629static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003630
3631static ssize_t
3632size_store(mddev_t *mddev, const char *buf, size_t len)
3633{
3634 /* If array is inactive, we can reduce the component size, but
3635 * not increase it (except from 0).
3636 * If array is active, we can try an on-line resize
3637 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003638 sector_t sectors;
3639 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003640
Andre Noll58c0fed2009-03-31 14:33:13 +11003641 if (err < 0)
3642 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003643 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003644 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003645 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003646 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003647 if (mddev->dev_sectors == 0 ||
3648 mddev->dev_sectors > sectors)
3649 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003650 else
3651 err = -ENOSPC;
3652 }
3653 return err ? err : len;
3654}
3655
3656static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003657__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003658
NeilBrown8bb93aa2006-01-06 00:20:50 -08003659
3660/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003661 * This is one of
3662 * 'none' for arrays with no metadata (good luck...)
3663 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003664 * or N.M for internally known formats
3665 */
3666static ssize_t
3667metadata_show(mddev_t *mddev, char *page)
3668{
3669 if (mddev->persistent)
3670 return sprintf(page, "%d.%d\n",
3671 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003672 else if (mddev->external)
3673 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003674 else
3675 return sprintf(page, "none\n");
3676}
3677
3678static ssize_t
3679metadata_store(mddev_t *mddev, const char *buf, size_t len)
3680{
3681 int major, minor;
3682 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003683 /* Changing the details of 'external' metadata is
3684 * always permitted. Otherwise there must be
3685 * no devices attached to the array.
3686 */
3687 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3688 ;
3689 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003690 return -EBUSY;
3691
3692 if (cmd_match(buf, "none")) {
3693 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003694 mddev->external = 0;
3695 mddev->major_version = 0;
3696 mddev->minor_version = 90;
3697 return len;
3698 }
3699 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003700 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003701 if (namelen >= sizeof(mddev->metadata_type))
3702 namelen = sizeof(mddev->metadata_type)-1;
3703 strncpy(mddev->metadata_type, buf+9, namelen);
3704 mddev->metadata_type[namelen] = 0;
3705 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3706 mddev->metadata_type[--namelen] = 0;
3707 mddev->persistent = 0;
3708 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003709 mddev->major_version = 0;
3710 mddev->minor_version = 90;
3711 return len;
3712 }
3713 major = simple_strtoul(buf, &e, 10);
3714 if (e==buf || *e != '.')
3715 return -EINVAL;
3716 buf = e+1;
3717 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003718 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003719 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003720 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003721 return -ENOENT;
3722 mddev->major_version = major;
3723 mddev->minor_version = minor;
3724 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003725 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003726 return len;
3727}
3728
3729static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003730__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003731
NeilBrowna35b0d62006-01-06 00:20:49 -08003732static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003733action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003734{
NeilBrown7eec3142005-11-08 21:39:44 -08003735 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003736 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3737 type = "frozen";
3738 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003739 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003740 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3741 type = "reshape";
3742 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003743 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3744 type = "resync";
3745 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3746 type = "check";
3747 else
3748 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003749 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003750 type = "recover";
3751 }
3752 return sprintf(page, "%s\n", type);
3753}
3754
NeilBrown7ebc0be2011-01-14 09:14:33 +11003755static void reap_sync_thread(mddev_t *mddev);
3756
NeilBrown24dd4692005-11-08 21:39:26 -08003757static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003758action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003759{
NeilBrown7eec3142005-11-08 21:39:44 -08003760 if (!mddev->pers || !mddev->pers->sync_request)
3761 return -EINVAL;
3762
NeilBrownb6a9ce62009-05-26 09:41:17 +10003763 if (cmd_match(page, "frozen"))
3764 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3765 else
3766 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3767
3768 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003769 if (mddev->sync_thread) {
3770 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003771 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003772 }
NeilBrown03c902e2006-01-06 00:20:46 -08003773 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3774 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003775 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003776 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003778 else if (cmd_match(page, "recover")) {
3779 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3780 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3781 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003782 int err;
3783 if (mddev->pers->start_reshape == NULL)
3784 return -EINVAL;
3785 err = mddev->pers->start_reshape(mddev);
3786 if (err)
3787 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003788 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003789 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003790 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003791 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003792 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003793 return -EINVAL;
3794 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3795 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003796 }
NeilBrown03c902e2006-01-06 00:20:46 -08003797 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003798 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003799 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003800 return len;
3801}
3802
NeilBrown9d888832005-11-08 21:39:26 -08003803static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003804mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003805{
3806 return sprintf(page, "%llu\n",
3807 (unsigned long long) mddev->resync_mismatches);
3808}
3809
NeilBrown80ca3a42006-07-10 04:44:18 -07003810static struct md_sysfs_entry md_scan_mode =
3811__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003812
NeilBrown96de1e62005-11-08 21:39:39 -08003813
NeilBrown80ca3a42006-07-10 04:44:18 -07003814static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003815
NeilBrown88202a02006-01-06 00:21:36 -08003816static ssize_t
3817sync_min_show(mddev_t *mddev, char *page)
3818{
3819 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3820 mddev->sync_speed_min ? "local": "system");
3821}
3822
3823static ssize_t
3824sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3825{
3826 int min;
3827 char *e;
3828 if (strncmp(buf, "system", 6)==0) {
3829 mddev->sync_speed_min = 0;
3830 return len;
3831 }
3832 min = simple_strtoul(buf, &e, 10);
3833 if (buf == e || (*e && *e != '\n') || min <= 0)
3834 return -EINVAL;
3835 mddev->sync_speed_min = min;
3836 return len;
3837}
3838
3839static struct md_sysfs_entry md_sync_min =
3840__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3841
3842static ssize_t
3843sync_max_show(mddev_t *mddev, char *page)
3844{
3845 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3846 mddev->sync_speed_max ? "local": "system");
3847}
3848
3849static ssize_t
3850sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3851{
3852 int max;
3853 char *e;
3854 if (strncmp(buf, "system", 6)==0) {
3855 mddev->sync_speed_max = 0;
3856 return len;
3857 }
3858 max = simple_strtoul(buf, &e, 10);
3859 if (buf == e || (*e && *e != '\n') || max <= 0)
3860 return -EINVAL;
3861 mddev->sync_speed_max = max;
3862 return len;
3863}
3864
3865static struct md_sysfs_entry md_sync_max =
3866__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3867
Iustin Popd7f3d292007-10-16 23:30:54 -07003868static ssize_t
3869degraded_show(mddev_t *mddev, char *page)
3870{
3871 return sprintf(page, "%d\n", mddev->degraded);
3872}
3873static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003874
3875static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003876sync_force_parallel_show(mddev_t *mddev, char *page)
3877{
3878 return sprintf(page, "%d\n", mddev->parallel_resync);
3879}
3880
3881static ssize_t
3882sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3883{
3884 long n;
3885
3886 if (strict_strtol(buf, 10, &n))
3887 return -EINVAL;
3888
3889 if (n != 0 && n != 1)
3890 return -EINVAL;
3891
3892 mddev->parallel_resync = n;
3893
3894 if (mddev->sync_thread)
3895 wake_up(&resync_wait);
3896
3897 return len;
3898}
3899
3900/* force parallel resync, even with shared block devices */
3901static struct md_sysfs_entry md_sync_force_parallel =
3902__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3903 sync_force_parallel_show, sync_force_parallel_store);
3904
3905static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003906sync_speed_show(mddev_t *mddev, char *page)
3907{
3908 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003909 if (mddev->curr_resync == 0)
3910 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003911 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3912 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003913 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003914 db = resync - mddev->resync_mark_cnt;
3915 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003916}
3917
NeilBrown80ca3a42006-07-10 04:44:18 -07003918static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003919
3920static ssize_t
3921sync_completed_show(mddev_t *mddev, char *page)
3922{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003923 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003924
NeilBrownacb180b2009-04-14 16:28:34 +10003925 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3926 return sprintf(page, "none\n");
3927
NeilBrown88202a02006-01-06 00:21:36 -08003928 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003929 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003930 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003931 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003932
NeilBrownacb180b2009-04-14 16:28:34 +10003933 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003934 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003935}
3936
NeilBrown80ca3a42006-07-10 04:44:18 -07003937static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003938
NeilBrowne464eaf2006-03-27 01:18:14 -08003939static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003940min_sync_show(mddev_t *mddev, char *page)
3941{
3942 return sprintf(page, "%llu\n",
3943 (unsigned long long)mddev->resync_min);
3944}
3945static ssize_t
3946min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3947{
3948 unsigned long long min;
3949 if (strict_strtoull(buf, 10, &min))
3950 return -EINVAL;
3951 if (min > mddev->resync_max)
3952 return -EINVAL;
3953 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3954 return -EBUSY;
3955
3956 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003957 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003958 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003959 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003960 return -EINVAL;
3961 }
3962 mddev->resync_min = min;
3963
3964 return len;
3965}
3966
3967static struct md_sysfs_entry md_min_sync =
3968__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3969
3970static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003971max_sync_show(mddev_t *mddev, char *page)
3972{
3973 if (mddev->resync_max == MaxSector)
3974 return sprintf(page, "max\n");
3975 else
3976 return sprintf(page, "%llu\n",
3977 (unsigned long long)mddev->resync_max);
3978}
3979static ssize_t
3980max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3981{
3982 if (strncmp(buf, "max", 3) == 0)
3983 mddev->resync_max = MaxSector;
3984 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003985 unsigned long long max;
3986 if (strict_strtoull(buf, 10, &max))
3987 return -EINVAL;
3988 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003989 return -EINVAL;
3990 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003991 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003992 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3993 return -EBUSY;
3994
3995 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003996 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003997 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003998 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003999 return -EINVAL;
4000 }
4001 mddev->resync_max = max;
4002 }
4003 wake_up(&mddev->recovery_wait);
4004 return len;
4005}
4006
4007static struct md_sysfs_entry md_max_sync =
4008__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4009
4010static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004011suspend_lo_show(mddev_t *mddev, char *page)
4012{
4013 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4014}
4015
4016static ssize_t
4017suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4018{
4019 char *e;
4020 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004021 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004022
NeilBrownb8d966e2009-07-01 11:14:04 +10004023 if (mddev->pers == NULL ||
4024 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004025 return -EINVAL;
4026 if (buf == e || (*e && *e != '\n'))
4027 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004028
4029 mddev->suspend_lo = new;
4030 if (new >= old)
4031 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004032 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004033 else {
4034 /* Expanding suspended region - need to wait */
4035 mddev->pers->quiesce(mddev, 1);
4036 mddev->pers->quiesce(mddev, 0);
4037 }
4038 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004039}
4040static struct md_sysfs_entry md_suspend_lo =
4041__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4042
4043
4044static ssize_t
4045suspend_hi_show(mddev_t *mddev, char *page)
4046{
4047 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4048}
4049
4050static ssize_t
4051suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4052{
4053 char *e;
4054 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004055 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004056
NeilBrownb8d966e2009-07-01 11:14:04 +10004057 if (mddev->pers == NULL ||
4058 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004059 return -EINVAL;
4060 if (buf == e || (*e && *e != '\n'))
4061 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004062
4063 mddev->suspend_hi = new;
4064 if (new <= old)
4065 /* Shrinking suspended region */
4066 mddev->pers->quiesce(mddev, 2);
4067 else {
4068 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004069 mddev->pers->quiesce(mddev, 1);
4070 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004071 }
4072 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004073}
4074static struct md_sysfs_entry md_suspend_hi =
4075__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4076
NeilBrown08a02ec2007-05-09 02:35:38 -07004077static ssize_t
4078reshape_position_show(mddev_t *mddev, char *page)
4079{
4080 if (mddev->reshape_position != MaxSector)
4081 return sprintf(page, "%llu\n",
4082 (unsigned long long)mddev->reshape_position);
4083 strcpy(page, "none\n");
4084 return 5;
4085}
4086
4087static ssize_t
4088reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4089{
4090 char *e;
4091 unsigned long long new = simple_strtoull(buf, &e, 10);
4092 if (mddev->pers)
4093 return -EBUSY;
4094 if (buf == e || (*e && *e != '\n'))
4095 return -EINVAL;
4096 mddev->reshape_position = new;
4097 mddev->delta_disks = 0;
4098 mddev->new_level = mddev->level;
4099 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004100 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004101 return len;
4102}
4103
4104static struct md_sysfs_entry md_reshape_position =
4105__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4106 reshape_position_store);
4107
Dan Williamsb522adc2009-03-31 15:00:31 +11004108static ssize_t
4109array_size_show(mddev_t *mddev, char *page)
4110{
4111 if (mddev->external_size)
4112 return sprintf(page, "%llu\n",
4113 (unsigned long long)mddev->array_sectors/2);
4114 else
4115 return sprintf(page, "default\n");
4116}
4117
4118static ssize_t
4119array_size_store(mddev_t *mddev, const char *buf, size_t len)
4120{
4121 sector_t sectors;
4122
4123 if (strncmp(buf, "default", 7) == 0) {
4124 if (mddev->pers)
4125 sectors = mddev->pers->size(mddev, 0, 0);
4126 else
4127 sectors = mddev->array_sectors;
4128
4129 mddev->external_size = 0;
4130 } else {
4131 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4132 return -EINVAL;
4133 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004134 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004135
4136 mddev->external_size = 1;
4137 }
4138
4139 mddev->array_sectors = sectors;
4140 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004141 if (mddev->pers)
4142 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004143
4144 return len;
4145}
4146
4147static struct md_sysfs_entry md_array_size =
4148__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4149 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004150
NeilBrowneae17012005-11-08 21:39:23 -08004151static struct attribute *md_default_attrs[] = {
4152 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004153 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004154 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004155 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004156 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004157 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004158 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004159 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004160 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004161 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004162 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004163 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004164 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004165 NULL,
4166};
4167
4168static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004169 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004170 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004171 &md_sync_min.attr,
4172 &md_sync_max.attr,
4173 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004174 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004175 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004176 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004177 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004178 &md_suspend_lo.attr,
4179 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004180 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004181 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004182 NULL,
4183};
NeilBrown411036f2005-11-08 21:39:40 -08004184static struct attribute_group md_redundancy_group = {
4185 .name = NULL,
4186 .attrs = md_redundancy_attrs,
4187};
4188
NeilBrowneae17012005-11-08 21:39:23 -08004189
4190static ssize_t
4191md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4192{
4193 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4194 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004195 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004196
4197 if (!entry->show)
4198 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004199 rv = mddev_lock(mddev);
4200 if (!rv) {
4201 rv = entry->show(mddev, page);
4202 mddev_unlock(mddev);
4203 }
NeilBrown96de1e62005-11-08 21:39:39 -08004204 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004205}
4206
4207static ssize_t
4208md_attr_store(struct kobject *kobj, struct attribute *attr,
4209 const char *page, size_t length)
4210{
4211 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4212 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004213 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004214
4215 if (!entry->store)
4216 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004217 if (!capable(CAP_SYS_ADMIN))
4218 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004219 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004220 if (mddev->hold_active == UNTIL_IOCTL)
4221 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004222 if (!rv) {
4223 rv = entry->store(mddev, page, length);
4224 mddev_unlock(mddev);
4225 }
NeilBrown96de1e62005-11-08 21:39:39 -08004226 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004227}
4228
4229static void md_free(struct kobject *ko)
4230{
4231 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004232
4233 if (mddev->sysfs_state)
4234 sysfs_put(mddev->sysfs_state);
4235
4236 if (mddev->gendisk) {
4237 del_gendisk(mddev->gendisk);
4238 put_disk(mddev->gendisk);
4239 }
4240 if (mddev->queue)
4241 blk_cleanup_queue(mddev->queue);
4242
NeilBrowneae17012005-11-08 21:39:23 -08004243 kfree(mddev);
4244}
4245
Emese Revfy52cf25d2010-01-19 02:58:23 +01004246static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004247 .show = md_attr_show,
4248 .store = md_attr_store,
4249};
4250static struct kobj_type md_ktype = {
4251 .release = md_free,
4252 .sysfs_ops = &md_sysfs_ops,
4253 .default_attrs = md_default_attrs,
4254};
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256int mdp_major = 0;
4257
Dan Williams5fd3a172009-03-04 00:57:25 -07004258static void mddev_delayed_delete(struct work_struct *ws)
4259{
4260 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4261
NeilBrown43a70502009-12-14 12:49:55 +11004262 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004263 kobject_del(&mddev->kobj);
4264 kobject_put(&mddev->kobj);
4265}
4266
NeilBrownefeb53c2009-01-09 08:31:10 +11004267static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004269 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 mddev_t *mddev = mddev_find(dev);
4271 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004272 int partitioned;
4273 int shift;
4274 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004275 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004278 return -ENODEV;
4279
4280 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4281 shift = partitioned ? MdpMinorShift : 0;
4282 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Tejun Heoe804ac72010-10-15 15:36:08 +02004284 /* wait for any previous instance of this device to be
4285 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004286 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004287 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004288
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004289 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004290 error = -EEXIST;
4291 if (mddev->gendisk)
4292 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004293
4294 if (name) {
4295 /* Need to ensure that 'name' is not a duplicate.
4296 */
4297 mddev_t *mddev2;
4298 spin_lock(&all_mddevs_lock);
4299
4300 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4301 if (mddev2->gendisk &&
4302 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4303 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004304 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004305 }
4306 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 }
NeilBrown8b765392009-01-09 08:31:08 +11004308
NeilBrown0909dc42009-07-01 12:27:21 +10004309 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004310 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004311 if (!mddev->queue)
4312 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004313 mddev->queue->queuedata = mddev;
4314
NeilBrown409c57f2009-03-31 14:39:39 +11004315 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 disk = alloc_disk(1 << shift);
4318 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004319 blk_cleanup_queue(mddev->queue);
4320 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004321 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004323 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004325 if (name)
4326 strcpy(disk->disk_name, name);
4327 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004329 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 disk->fops = &md_fops;
4332 disk->private_data = mddev;
4333 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004334 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004335 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004336 * remove it now.
4337 */
4338 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 add_disk(disk);
4340 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004341 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4342 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004343 if (error) {
4344 /* This isn't possible, but as kobject_init_and_add is marked
4345 * __must_check, we must do something with the result
4346 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004347 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4348 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004349 error = 0;
4350 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004351 if (mddev->kobj.sd &&
4352 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004353 printk(KERN_DEBUG "pointless warning\n");
Darrick J. Wongbe20e6c2010-11-24 16:40:33 +11004354
4355 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown0909dc42009-07-01 12:27:21 +10004356 abort:
4357 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004358 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004359 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004360 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004361 }
NeilBrownd3374822009-01-09 08:31:10 +11004362 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004363 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004364}
4365
4366static struct kobject *md_probe(dev_t dev, int *part, void *data)
4367{
4368 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 return NULL;
4370}
4371
NeilBrownefeb53c2009-01-09 08:31:10 +11004372static int add_named_array(const char *val, struct kernel_param *kp)
4373{
4374 /* val must be "md_*" where * is not all digits.
4375 * We allocate an array with a large free minor number, and
4376 * set the name to val. val must not already be an active name.
4377 */
4378 int len = strlen(val);
4379 char buf[DISK_NAME_LEN];
4380
4381 while (len && val[len-1] == '\n')
4382 len--;
4383 if (len >= DISK_NAME_LEN)
4384 return -E2BIG;
4385 strlcpy(buf, val, len+1);
4386 if (strncmp(buf, "md_", 3) != 0)
4387 return -EINVAL;
4388 return md_alloc(0, buf);
4389}
4390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391static void md_safemode_timeout(unsigned long data)
4392{
4393 mddev_t *mddev = (mddev_t *) data;
4394
Neil Brown0fd62b82008-06-28 08:31:36 +10004395 if (!atomic_read(&mddev->writes_pending)) {
4396 mddev->safemode = 1;
4397 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004398 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 md_wakeup_thread(mddev->thread);
4401}
4402
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004403static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
NeilBrown390ee602010-06-01 19:37:27 +10004405int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
NeilBrown2604b702006-01-06 00:20:36 -08004407 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004409 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
NeilBrowna757e642005-04-16 15:26:42 -07004411 if (list_empty(&mddev->disks))
4412 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 if (mddev->pers)
4416 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004417 /* Cannot run until previous stop completes properly */
4418 if (mddev->sysfs_active)
4419 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 /*
4422 * Analyze all RAID superblock(s)
4423 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004424 if (!mddev->raid_disks) {
4425 if (!mddev->persistent)
4426 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004427 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
NeilBrownd9d166c2006-01-06 00:20:51 -08004430 if (mddev->level != LEVEL_NONE)
4431 request_module("md-level-%d", mddev->level);
4432 else if (mddev->clevel[0])
4433 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 /*
4436 * Drop all container device buffers, from now on
4437 * the only valid external interface is through the md
4438 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004440 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004441 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 continue;
4443 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004444 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004445
4446 /* perform some consistency tests on the device.
4447 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004448 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004449 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004450 if (rdev->meta_bdev) {
4451 /* Nothing to check */;
4452 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004453 if (mddev->dev_sectors &&
4454 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004455 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004456 printk("md: %s: data overlaps metadata\n",
4457 mdname(mddev));
4458 return -EINVAL;
4459 }
4460 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004461 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004462 > rdev->data_offset) {
4463 printk("md: %s: metadata overlaps data\n",
4464 mdname(mddev));
4465 return -EINVAL;
4466 }
4467 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004468 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 }
4470
NeilBrowna167f662010-10-26 18:31:13 +11004471 if (mddev->bio_set == NULL)
4472 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004475 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004476 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004478 if (mddev->level != LEVEL_NONE)
4479 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4480 mddev->level);
4481 else
4482 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4483 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return -EINVAL;
4485 }
NeilBrown2604b702006-01-06 00:20:36 -08004486 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004488 if (mddev->level != pers->level) {
4489 mddev->level = pers->level;
4490 mddev->new_level = pers->level;
4491 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004492 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
NeilBrownf6705572006-03-27 01:18:11 -08004494 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004495 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004496 /* This personality cannot handle reshaping... */
4497 mddev->pers = NULL;
4498 module_put(pers->owner);
4499 return -EINVAL;
4500 }
4501
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004502 if (pers->sync_request) {
4503 /* Warn if this is a potentially silly
4504 * configuration.
4505 */
4506 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4507 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004508 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004509
4510 list_for_each_entry(rdev, &mddev->disks, same_set)
4511 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004512 if (rdev < rdev2 &&
4513 rdev->bdev->bd_contains ==
4514 rdev2->bdev->bd_contains) {
4515 printk(KERN_WARNING
4516 "%s: WARNING: %s appears to be"
4517 " on the same physical disk as"
4518 " %s.\n",
4519 mdname(mddev),
4520 bdevname(rdev->bdev,b),
4521 bdevname(rdev2->bdev,b2));
4522 warned = 1;
4523 }
4524 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004525
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004526 if (warned)
4527 printk(KERN_WARNING
4528 "True protection against single-disk"
4529 " failure might be compromised.\n");
4530 }
4531
NeilBrown657390d2005-08-26 18:34:16 -07004532 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004533 /* may be over-ridden by personality */
4534 mddev->resync_max_sectors = mddev->dev_sectors;
4535
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004536 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
NeilBrown0f9552b2009-12-30 12:08:50 +11004538 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004539 mddev->ro = 2; /* read-only, but switch on first write */
4540
NeilBrownb15c2e52006-01-06 00:20:16 -08004541 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004542 if (err)
4543 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004544 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4545 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4546 " but 'external_size' not in effect?\n", __func__);
4547 printk(KERN_ERR
4548 "md: invalid array_size %llu > default size %llu\n",
4549 (unsigned long long)mddev->array_sectors / 2,
4550 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4551 err = -EINVAL;
4552 mddev->pers->stop(mddev);
4553 }
4554 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004555 err = bitmap_create(mddev);
4556 if (err) {
4557 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4558 mdname(mddev), err);
4559 mddev->pers->stop(mddev);
4560 }
4561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 module_put(mddev->pers->owner);
4564 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004565 bitmap_destroy(mddev);
4566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004568 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004569 if (mddev->kobj.sd &&
4570 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004571 printk(KERN_WARNING
4572 "md: cannot register extra attributes for %s\n",
4573 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004574 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004575 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004576 mddev->ro = 0;
4577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004579 atomic_set(&mddev->max_corr_read_errors,
4580 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 mddev->safemode = 0;
4582 mddev->safemode_timer.function = md_safemode_timeout;
4583 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004584 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004586 smp_wmb();
4587 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004588 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004589 if (rdev->raid_disk >= 0) {
4590 char nm[20];
4591 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004592 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004593 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
4596 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4597
NeilBrown850b2b42006-10-03 01:15:46 -07004598 if (mddev->flags)
4599 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
NeilBrown0b8c9de2006-07-10 04:44:15 -07004601 md_wakeup_thread(mddev->thread);
4602 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004603
NeilBrownd7603b72006-01-06 00:20:30 -08004604 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004605 sysfs_notify_dirent_safe(mddev->sysfs_state);
4606 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004607 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return 0;
4609}
NeilBrown390ee602010-06-01 19:37:27 +10004610EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
NeilBrownfe60b012010-03-29 11:10:42 +11004612static int do_md_run(mddev_t *mddev)
4613{
4614 int err;
4615
4616 err = md_run(mddev);
4617 if (err)
4618 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004619 err = bitmap_load(mddev);
4620 if (err) {
4621 bitmap_destroy(mddev);
4622 goto out;
4623 }
NeilBrownfe60b012010-03-29 11:10:42 +11004624 set_capacity(mddev->gendisk, mddev->array_sectors);
4625 revalidate_disk(mddev->gendisk);
4626 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4627out:
4628 return err;
4629}
4630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631static int restart_array(mddev_t *mddev)
4632{
4633 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Andre Noll80fab1d2008-07-11 22:02:21 +10004635 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004637 return -ENXIO;
4638 if (!mddev->pers)
4639 return -EINVAL;
4640 if (!mddev->ro)
4641 return -EBUSY;
4642 mddev->safemode = 0;
4643 mddev->ro = 0;
4644 set_disk_ro(disk, 0);
4645 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4646 mdname(mddev));
4647 /* Kick recovery or resync if necessary */
4648 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4649 md_wakeup_thread(mddev->thread);
4650 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004651 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
NeilBrownacc55e22006-06-26 00:27:47 -07004655/* similar to deny_write_access, but accounts for our holding a reference
4656 * to the file ourselves */
4657static int deny_bitmap_write_access(struct file * file)
4658{
4659 struct inode *inode = file->f_mapping->host;
4660
4661 spin_lock(&inode->i_lock);
4662 if (atomic_read(&inode->i_writecount) > 1) {
4663 spin_unlock(&inode->i_lock);
4664 return -ETXTBSY;
4665 }
4666 atomic_set(&inode->i_writecount, -1);
4667 spin_unlock(&inode->i_lock);
4668
4669 return 0;
4670}
4671
NeilBrown43a70502009-12-14 12:49:55 +11004672void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004673{
4674 struct inode *inode = file->f_mapping->host;
4675
4676 spin_lock(&inode->i_lock);
4677 atomic_set(&inode->i_writecount, 1);
4678 spin_unlock(&inode->i_lock);
4679}
4680
NeilBrown6177b472010-03-29 11:37:13 +11004681static void md_clean(mddev_t *mddev)
4682{
4683 mddev->array_sectors = 0;
4684 mddev->external_size = 0;
4685 mddev->dev_sectors = 0;
4686 mddev->raid_disks = 0;
4687 mddev->recovery_cp = 0;
4688 mddev->resync_min = 0;
4689 mddev->resync_max = MaxSector;
4690 mddev->reshape_position = MaxSector;
4691 mddev->external = 0;
4692 mddev->persistent = 0;
4693 mddev->level = LEVEL_NONE;
4694 mddev->clevel[0] = 0;
4695 mddev->flags = 0;
4696 mddev->ro = 0;
4697 mddev->metadata_type[0] = 0;
4698 mddev->chunk_sectors = 0;
4699 mddev->ctime = mddev->utime = 0;
4700 mddev->layout = 0;
4701 mddev->max_disks = 0;
4702 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004703 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004704 mddev->delta_disks = 0;
4705 mddev->new_level = LEVEL_NONE;
4706 mddev->new_layout = 0;
4707 mddev->new_chunk_sectors = 0;
4708 mddev->curr_resync = 0;
4709 mddev->resync_mismatches = 0;
4710 mddev->suspend_lo = mddev->suspend_hi = 0;
4711 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4712 mddev->recovery = 0;
4713 mddev->in_sync = 0;
4714 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004715 mddev->safemode = 0;
4716 mddev->bitmap_info.offset = 0;
4717 mddev->bitmap_info.default_offset = 0;
4718 mddev->bitmap_info.chunksize = 0;
4719 mddev->bitmap_info.daemon_sleep = 0;
4720 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004721 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004722}
4723
NeilBrowndefad612011-01-14 09:14:33 +11004724static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004725{
4726 if (mddev->sync_thread) {
4727 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4728 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004729 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004730 }
4731
4732 del_timer_sync(&mddev->safemode_timer);
4733
4734 bitmap_flush(mddev);
4735 md_super_wait(mddev);
4736
4737 if (!mddev->in_sync || mddev->flags) {
4738 /* mark array as shutdown cleanly */
4739 mddev->in_sync = 1;
4740 md_update_sb(mddev, 1);
4741 }
4742}
NeilBrowndefad612011-01-14 09:14:33 +11004743
4744void md_stop_writes(mddev_t *mddev)
4745{
4746 mddev_lock(mddev);
4747 __md_stop_writes(mddev);
4748 mddev_unlock(mddev);
4749}
NeilBrown390ee602010-06-01 19:37:27 +10004750EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004751
NeilBrown390ee602010-06-01 19:37:27 +10004752void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004753{
NeilBrown0ca69882011-01-14 09:14:33 +11004754 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004755 mddev->pers->stop(mddev);
4756 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4757 mddev->to_remove = &md_redundancy_group;
4758 module_put(mddev->pers->owner);
4759 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004760 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004761}
NeilBrown390ee602010-06-01 19:37:27 +10004762EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004763
NeilBrowna4bd82d2010-03-29 13:23:10 +11004764static int md_set_readonly(mddev_t *mddev, int is_open)
4765{
4766 int err = 0;
4767 mutex_lock(&mddev->open_mutex);
4768 if (atomic_read(&mddev->openers) > is_open) {
4769 printk("md: %s still in use.\n",mdname(mddev));
4770 err = -EBUSY;
4771 goto out;
4772 }
4773 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004774 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004775
4776 err = -ENXIO;
4777 if (mddev->ro==1)
4778 goto out;
4779 mddev->ro = 1;
4780 set_disk_ro(mddev->gendisk, 1);
4781 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004782 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004783 err = 0;
4784 }
4785out:
4786 mutex_unlock(&mddev->open_mutex);
4787 return err;
4788}
4789
NeilBrown9e653b62006-06-26 00:27:58 -07004790/* mode:
4791 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004792 * 2 - stop but do not disassemble array
4793 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004794static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004797 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
NeilBrownc8c00a62009-08-10 12:50:52 +10004799 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004800 if (atomic_read(&mddev->openers) > is_open ||
4801 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004802 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004803 mutex_unlock(&mddev->open_mutex);
4804 return -EBUSY;
4805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806
NeilBrown6e17b022010-08-07 21:41:19 +10004807 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004808 if (mddev->ro)
4809 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004810
NeilBrowndefad612011-01-14 09:14:33 +11004811 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004812 md_stop(mddev);
4813 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004814 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004815
NeilBrowna4bd82d2010-03-29 13:23:10 +11004816 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004817 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004818
NeilBrowna4bd82d2010-03-29 13:23:10 +11004819 list_for_each_entry(rdev, &mddev->disks, same_set)
4820 if (rdev->raid_disk >= 0) {
4821 char nm[20];
4822 sprintf(nm, "rd%d", rdev->raid_disk);
4823 sysfs_remove_link(&mddev->kobj, nm);
4824 }
NeilBrownc4647292009-05-07 12:51:06 +10004825
NeilBrowna4bd82d2010-03-29 13:23:10 +11004826 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004827 mutex_unlock(&mddev->open_mutex);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004828 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004829
NeilBrowna4bd82d2010-03-29 13:23:10 +11004830 if (mddev->ro)
4831 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004832 } else
4833 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 /*
4835 * Free resources if final stop
4836 */
NeilBrown9e653b62006-06-26 00:27:58 -07004837 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4839
NeilBrown978f9462006-02-02 14:28:05 -08004840 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004841 if (mddev->bitmap_info.file) {
4842 restore_bitmap_write_access(mddev->bitmap_info.file);
4843 fput(mddev->bitmap_info.file);
4844 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004845 }
NeilBrownc3d97142009-12-14 12:49:52 +11004846 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 export_array(mddev);
4849
NeilBrown6177b472010-03-29 11:37:13 +11004850 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004851 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004852 if (mddev->hold_active == UNTIL_STOP)
4853 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004854 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004855 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004856 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004857 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
4860
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004861#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862static void autorun_array(mddev_t *mddev)
4863{
4864 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 int err;
4866
NeilBrowna757e642005-04-16 15:26:42 -07004867 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
4870 printk(KERN_INFO "md: running: ");
4871
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004872 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 char b[BDEVNAME_SIZE];
4874 printk("<%s>", bdevname(rdev->bdev,b));
4875 }
4876 printk("\n");
4877
NeilBrownd710e132008-10-13 11:55:12 +11004878 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 if (err) {
4880 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004881 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 }
4883}
4884
4885/*
4886 * lets try to run arrays based on all disks that have arrived
4887 * until now. (those are in pending_raid_disks)
4888 *
4889 * the method: pick the first pending disk, collect all disks with
4890 * the same UUID, remove all from the pending list and put them into
4891 * the 'same_array' list. Then order this list based on superblock
4892 * update time (freshest comes first), kick out 'old' disks and
4893 * compare superblocks. If everything's fine then run it.
4894 *
4895 * If "unit" is allocated, then bump its reference count
4896 */
4897static void autorun_devices(int part)
4898{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004899 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 mddev_t *mddev;
4901 char b[BDEVNAME_SIZE];
4902
4903 printk(KERN_INFO "md: autorun ...\n");
4904 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004905 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004907 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 rdev0 = list_entry(pending_raid_disks.next,
4909 mdk_rdev_t, same_set);
4910
4911 printk(KERN_INFO "md: considering %s ...\n",
4912 bdevname(rdev0->bdev,b));
4913 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004914 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 if (super_90_load(rdev, rdev0, 0) >= 0) {
4916 printk(KERN_INFO "md: adding %s ...\n",
4917 bdevname(rdev->bdev,b));
4918 list_move(&rdev->same_set, &candidates);
4919 }
4920 /*
4921 * now we have a set of devices, with all of them having
4922 * mostly sane superblocks. It's time to allocate the
4923 * mddev.
4924 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004925 if (part) {
4926 dev = MKDEV(mdp_major,
4927 rdev0->preferred_minor << MdpMinorShift);
4928 unit = MINOR(dev) >> MdpMinorShift;
4929 } else {
4930 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4931 unit = MINOR(dev);
4932 }
4933 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4935 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4936 break;
4937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
4939 md_probe(dev, NULL, NULL);
4940 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004941 if (!mddev || !mddev->gendisk) {
4942 if (mddev)
4943 mddev_put(mddev);
4944 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 "md: cannot allocate memory for md drive.\n");
4946 break;
4947 }
4948 if (mddev_lock(mddev))
4949 printk(KERN_WARNING "md: %s locked, cannot run\n",
4950 mdname(mddev));
4951 else if (mddev->raid_disks || mddev->major_version
4952 || !list_empty(&mddev->disks)) {
4953 printk(KERN_WARNING
4954 "md: %s already running, cannot run %s\n",
4955 mdname(mddev), bdevname(rdev0->bdev,b));
4956 mddev_unlock(mddev);
4957 } else {
4958 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004959 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004960 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 list_del_init(&rdev->same_set);
4962 if (bind_rdev_to_array(rdev, mddev))
4963 export_rdev(rdev);
4964 }
4965 autorun_array(mddev);
4966 mddev_unlock(mddev);
4967 }
4968 /* on success, candidates will be empty, on error
4969 * it won't...
4970 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004971 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004972 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 mddev_put(mddev);
4976 }
4977 printk(KERN_INFO "md: ... autorun DONE.\n");
4978}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004979#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981static int get_version(void __user * arg)
4982{
4983 mdu_version_t ver;
4984
4985 ver.major = MD_MAJOR_VERSION;
4986 ver.minor = MD_MINOR_VERSION;
4987 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4988
4989 if (copy_to_user(arg, &ver, sizeof(ver)))
4990 return -EFAULT;
4991
4992 return 0;
4993}
4994
4995static int get_array_info(mddev_t * mddev, void __user * arg)
4996{
4997 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004998 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
NeilBrowna9f326e2009-09-23 18:06:41 +10005001 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005002 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005004 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 failed++;
5006 else {
5007 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005008 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005009 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 else
5011 spare++;
5012 }
5013 }
5014
5015 info.major_version = mddev->major_version;
5016 info.minor_version = mddev->minor_version;
5017 info.patch_version = MD_PATCHLEVEL_VERSION;
5018 info.ctime = mddev->ctime;
5019 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005020 info.size = mddev->dev_sectors / 2;
5021 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005022 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 info.nr_disks = nr;
5024 info.raid_disks = mddev->raid_disks;
5025 info.md_minor = mddev->md_minor;
5026 info.not_persistent= !mddev->persistent;
5027
5028 info.utime = mddev->utime;
5029 info.state = 0;
5030 if (mddev->in_sync)
5031 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005032 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005033 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005034 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 info.working_disks = working;
5036 info.failed_disks = failed;
5037 info.spare_disks = spare;
5038
5039 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005040 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
5042 if (copy_to_user(arg, &info, sizeof(info)))
5043 return -EFAULT;
5044
5045 return 0;
5046}
5047
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005048static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005049{
5050 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5051 char *ptr, *buf = NULL;
5052 int err = -ENOMEM;
5053
Dan Williamsb5470dc2008-06-27 21:44:04 -07005054 if (md_allow_write(mddev))
5055 file = kmalloc(sizeof(*file), GFP_NOIO);
5056 else
5057 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005058
NeilBrown32a76272005-06-21 17:17:14 -07005059 if (!file)
5060 goto out;
5061
5062 /* bitmap disabled, zero the first byte and copy out */
5063 if (!mddev->bitmap || !mddev->bitmap->file) {
5064 file->pathname[0] = '\0';
5065 goto copy_out;
5066 }
5067
5068 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5069 if (!buf)
5070 goto out;
5071
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005072 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5073 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005074 goto out;
5075
5076 strcpy(file->pathname, ptr);
5077
5078copy_out:
5079 err = 0;
5080 if (copy_to_user(arg, file, sizeof(*file)))
5081 err = -EFAULT;
5082out:
5083 kfree(buf);
5084 kfree(file);
5085 return err;
5086}
5087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088static int get_disk_info(mddev_t * mddev, void __user * arg)
5089{
5090 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 mdk_rdev_t *rdev;
5092
5093 if (copy_from_user(&info, arg, sizeof(info)))
5094 return -EFAULT;
5095
Andre Noll26ef3792008-07-11 22:02:21 +10005096 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (rdev) {
5098 info.major = MAJOR(rdev->bdev->bd_dev);
5099 info.minor = MINOR(rdev->bdev->bd_dev);
5100 info.raid_disk = rdev->raid_disk;
5101 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005102 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005104 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 info.state |= (1<<MD_DISK_ACTIVE);
5106 info.state |= (1<<MD_DISK_SYNC);
5107 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005108 if (test_bit(WriteMostly, &rdev->flags))
5109 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 } else {
5111 info.major = info.minor = 0;
5112 info.raid_disk = -1;
5113 info.state = (1<<MD_DISK_REMOVED);
5114 }
5115
5116 if (copy_to_user(arg, &info, sizeof(info)))
5117 return -EFAULT;
5118
5119 return 0;
5120}
5121
5122static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5123{
5124 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5125 mdk_rdev_t *rdev;
5126 dev_t dev = MKDEV(info->major,info->minor);
5127
5128 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5129 return -EOVERFLOW;
5130
5131 if (!mddev->raid_disks) {
5132 int err;
5133 /* expecting a device which has a superblock */
5134 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5135 if (IS_ERR(rdev)) {
5136 printk(KERN_WARNING
5137 "md: md_import_device returned %ld\n",
5138 PTR_ERR(rdev));
5139 return PTR_ERR(rdev);
5140 }
5141 if (!list_empty(&mddev->disks)) {
5142 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5143 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005144 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 .load_super(rdev, rdev0, mddev->minor_version);
5146 if (err < 0) {
5147 printk(KERN_WARNING
5148 "md: %s has different UUID to %s\n",
5149 bdevname(rdev->bdev,b),
5150 bdevname(rdev0->bdev,b2));
5151 export_rdev(rdev);
5152 return -EINVAL;
5153 }
5154 }
5155 err = bind_rdev_to_array(rdev, mddev);
5156 if (err)
5157 export_rdev(rdev);
5158 return err;
5159 }
5160
5161 /*
5162 * add_new_disk can be used once the array is assembled
5163 * to add "hot spares". They must already have a superblock
5164 * written
5165 */
5166 if (mddev->pers) {
5167 int err;
5168 if (!mddev->pers->hot_add_disk) {
5169 printk(KERN_WARNING
5170 "%s: personality does not support diskops!\n",
5171 mdname(mddev));
5172 return -EINVAL;
5173 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005174 if (mddev->persistent)
5175 rdev = md_import_device(dev, mddev->major_version,
5176 mddev->minor_version);
5177 else
5178 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 if (IS_ERR(rdev)) {
5180 printk(KERN_WARNING
5181 "md: md_import_device returned %ld\n",
5182 PTR_ERR(rdev));
5183 return PTR_ERR(rdev);
5184 }
NeilBrown1a855a02010-12-09 16:36:28 +11005185 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005186 if (!mddev->persistent) {
5187 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005188 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005189 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005190 set_bit(In_sync, &rdev->flags);
5191 } else
NeilBrown41158c72005-06-21 17:17:25 -07005192 rdev->raid_disk = -1;
5193 } else
5194 super_types[mddev->major_version].
5195 validate_super(mddev, rdev);
NeilBrown1a855a02010-12-09 16:36:28 +11005196 if (test_bit(In_sync, &rdev->flags))
5197 rdev->saved_raid_disk = rdev->raid_disk;
5198 else
5199 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005200
NeilBrownb2d444d2005-11-08 21:39:31 -08005201 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005202 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5203 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005204 else
5205 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 rdev->raid_disk = -1;
5208 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005209 if (!err && !mddev->pers->hot_remove_disk) {
5210 /* If there is hot_add_disk but no hot_remove_disk
5211 * then added disks for geometry changes,
5212 * and should be added immediately.
5213 */
5214 super_types[mddev->major_version].
5215 validate_super(mddev, rdev);
5216 err = mddev->pers->hot_add_disk(mddev, rdev);
5217 if (err)
5218 unbind_rdev_from_array(rdev);
5219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 if (err)
5221 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005222 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005223 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005224
NeilBrown17571282006-12-10 02:20:52 -08005225 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005226 if (mddev->degraded)
5227 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005228 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005229 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return err;
5231 }
5232
5233 /* otherwise, add_new_disk is only allowed
5234 * for major_version==0 superblocks
5235 */
5236 if (mddev->major_version != 0) {
5237 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5238 mdname(mddev));
5239 return -EINVAL;
5240 }
5241
5242 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5243 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005244 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 if (IS_ERR(rdev)) {
5246 printk(KERN_WARNING
5247 "md: error, md_import_device() returned %ld\n",
5248 PTR_ERR(rdev));
5249 return PTR_ERR(rdev);
5250 }
5251 rdev->desc_nr = info->number;
5252 if (info->raid_disk < mddev->raid_disks)
5253 rdev->raid_disk = info->raid_disk;
5254 else
5255 rdev->raid_disk = -1;
5256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005258 if (info->state & (1<<MD_DISK_SYNC))
5259 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005261 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5262 set_bit(WriteMostly, &rdev->flags);
5263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 if (!mddev->persistent) {
5265 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005266 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5267 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005268 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005269 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
NeilBrown2bf071b2006-01-06 00:20:55 -08005271 err = bind_rdev_to_array(rdev, mddev);
5272 if (err) {
5273 export_rdev(rdev);
5274 return err;
5275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 }
5277
5278 return 0;
5279}
5280
5281static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5282{
5283 char b[BDEVNAME_SIZE];
5284 mdk_rdev_t *rdev;
5285
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 rdev = find_rdev(mddev, dev);
5287 if (!rdev)
5288 return -ENXIO;
5289
5290 if (rdev->raid_disk >= 0)
5291 goto busy;
5292
5293 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005294 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005295 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296
5297 return 0;
5298busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005299 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 bdevname(rdev->bdev,b), mdname(mddev));
5301 return -EBUSY;
5302}
5303
5304static int hot_add_disk(mddev_t * mddev, dev_t dev)
5305{
5306 char b[BDEVNAME_SIZE];
5307 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 mdk_rdev_t *rdev;
5309
5310 if (!mddev->pers)
5311 return -ENODEV;
5312
5313 if (mddev->major_version != 0) {
5314 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5315 " version-0 superblocks.\n",
5316 mdname(mddev));
5317 return -EINVAL;
5318 }
5319 if (!mddev->pers->hot_add_disk) {
5320 printk(KERN_WARNING
5321 "%s: personality does not support diskops!\n",
5322 mdname(mddev));
5323 return -EINVAL;
5324 }
5325
NeilBrownd710e132008-10-13 11:55:12 +11005326 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 if (IS_ERR(rdev)) {
5328 printk(KERN_WARNING
5329 "md: error, md_import_device() returned %ld\n",
5330 PTR_ERR(rdev));
5331 return -EINVAL;
5332 }
5333
5334 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005335 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005337 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
NeilBrown8190e752009-06-18 08:48:58 +10005339 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
NeilBrownb2d444d2005-11-08 21:39:31 -08005341 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 printk(KERN_WARNING
5343 "md: can not hot-add faulty %s disk to %s!\n",
5344 bdevname(rdev->bdev,b), mdname(mddev));
5345 err = -EINVAL;
5346 goto abort_export;
5347 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005348 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005350 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005351 err = bind_rdev_to_array(rdev, mddev);
5352 if (err)
5353 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 /*
5356 * The rest should better be atomic, we can have disk failures
5357 * noticed in interrupt contexts ...
5358 */
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 rdev->raid_disk = -1;
5361
NeilBrown850b2b42006-10-03 01:15:46 -07005362 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363
5364 /*
5365 * Kick recovery, maybe this spare has to be added to the
5366 * array immediately.
5367 */
5368 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5369 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005370 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 return 0;
5372
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373abort_export:
5374 export_rdev(rdev);
5375 return err;
5376}
5377
NeilBrown32a76272005-06-21 17:17:14 -07005378static int set_bitmap_file(mddev_t *mddev, int fd)
5379{
5380 int err;
5381
NeilBrown36fa3062005-09-09 16:23:45 -07005382 if (mddev->pers) {
5383 if (!mddev->pers->quiesce)
5384 return -EBUSY;
5385 if (mddev->recovery || mddev->sync_thread)
5386 return -EBUSY;
5387 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005388 }
5389
NeilBrown36fa3062005-09-09 16:23:45 -07005390
5391 if (fd >= 0) {
5392 if (mddev->bitmap)
5393 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005394 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005395
NeilBrownc3d97142009-12-14 12:49:52 +11005396 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005397 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5398 mdname(mddev));
5399 return -EBADF;
5400 }
5401
NeilBrownc3d97142009-12-14 12:49:52 +11005402 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005403 if (err) {
5404 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5405 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005406 fput(mddev->bitmap_info.file);
5407 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005408 return err;
5409 }
NeilBrownc3d97142009-12-14 12:49:52 +11005410 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005411 } else if (mddev->bitmap == NULL)
5412 return -ENOENT; /* cannot remove what isn't there */
5413 err = 0;
5414 if (mddev->pers) {
5415 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005416 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005417 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005418 if (!err)
5419 err = bitmap_load(mddev);
5420 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005421 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005422 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005423 fd = -1; /* make sure to put the file */
5424 }
NeilBrown36fa3062005-09-09 16:23:45 -07005425 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005426 }
5427 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005428 if (mddev->bitmap_info.file) {
5429 restore_bitmap_write_access(mddev->bitmap_info.file);
5430 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005431 }
NeilBrownc3d97142009-12-14 12:49:52 +11005432 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005433 }
5434
NeilBrown32a76272005-06-21 17:17:14 -07005435 return err;
5436}
5437
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438/*
5439 * set_array_info is used two different ways
5440 * The original usage is when creating a new array.
5441 * In this usage, raid_disks is > 0 and it together with
5442 * level, size, not_persistent,layout,chunksize determine the
5443 * shape of the array.
5444 * This will always create an array with a type-0.90.0 superblock.
5445 * The newer usage is when assembling an array.
5446 * In this case raid_disks will be 0, and the major_version field is
5447 * use to determine which style super-blocks are to be found on the devices.
5448 * The minor and patch _version numbers are also kept incase the
5449 * super_block handler wishes to interpret them.
5450 */
5451static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5452{
5453
5454 if (info->raid_disks == 0) {
5455 /* just setting version number for superblock loading */
5456 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005457 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 super_types[info->major_version].name == NULL) {
5459 /* maybe try to auto-load a module? */
5460 printk(KERN_INFO
5461 "md: superblock version %d not known\n",
5462 info->major_version);
5463 return -EINVAL;
5464 }
5465 mddev->major_version = info->major_version;
5466 mddev->minor_version = info->minor_version;
5467 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005468 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005469 /* ensure mddev_put doesn't delete this now that there
5470 * is some minimal configuration.
5471 */
5472 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 return 0;
5474 }
5475 mddev->major_version = MD_MAJOR_VERSION;
5476 mddev->minor_version = MD_MINOR_VERSION;
5477 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5478 mddev->ctime = get_seconds();
5479
5480 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005481 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005482 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 mddev->raid_disks = info->raid_disks;
5484 /* don't set md_minor, it is determined by which /dev/md* was
5485 * openned
5486 */
5487 if (info->state & (1<<MD_SB_CLEAN))
5488 mddev->recovery_cp = MaxSector;
5489 else
5490 mddev->recovery_cp = 0;
5491 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005492 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
5494 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005495 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
5497 mddev->max_disks = MD_SB_DISKS;
5498
NeilBrowne6910632008-02-06 01:39:51 -08005499 if (mddev->persistent)
5500 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005501 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
NeilBrownc3d97142009-12-14 12:49:52 +11005503 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5504 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005505
NeilBrownf6705572006-03-27 01:18:11 -08005506 mddev->reshape_position = MaxSector;
5507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 /*
5509 * Generate a 128 bit UUID
5510 */
5511 get_random_bytes(mddev->uuid, 16);
5512
NeilBrownf6705572006-03-27 01:18:11 -08005513 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005514 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005515 mddev->new_layout = mddev->layout;
5516 mddev->delta_disks = 0;
5517
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 return 0;
5519}
5520
Dan Williams1f403622009-03-31 14:59:03 +11005521void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5522{
Dan Williamsb522adc2009-03-31 15:00:31 +11005523 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5524
5525 if (mddev->external_size)
5526 return;
5527
Dan Williams1f403622009-03-31 14:59:03 +11005528 mddev->array_sectors = array_sectors;
5529}
5530EXPORT_SYMBOL(md_set_array_sectors);
5531
Andre Nolld71f9f82008-07-11 22:02:22 +10005532static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005533{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005534 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005535 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005536 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005537
5538 if (mddev->pers->resize == NULL)
5539 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005540 /* The "num_sectors" is the number of sectors of each device that
5541 * is used. This can only make sense for arrays with redundancy.
5542 * linear and raid0 always use whatever space is available. We can only
5543 * consider changing this number if no resync or reconstruction is
5544 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005545 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005546 * of each device. If num_sectors is zero, we find the largest size
5547 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005548 */
5549 if (mddev->sync_thread)
5550 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005551 if (mddev->bitmap)
5552 /* Sorry, cannot grow a bitmap yet, just remove it,
5553 * grow, and re-add.
5554 */
5555 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005556 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005557 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005558
Andre Nolld71f9f82008-07-11 22:02:22 +10005559 if (fit && (num_sectors == 0 || num_sectors > avail))
5560 num_sectors = avail;
5561 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005562 return -ENOSPC;
5563 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005564 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005565 if (!rv)
5566 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005567 return rv;
5568}
5569
NeilBrownda943b92006-01-06 00:20:54 -08005570static int update_raid_disks(mddev_t *mddev, int raid_disks)
5571{
5572 int rv;
5573 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005574 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005575 return -EINVAL;
5576 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005577 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005578 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005579 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005580 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005581 mddev->delta_disks = raid_disks - mddev->raid_disks;
5582
5583 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005584 if (rv < 0)
5585 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005586 return rv;
5587}
5588
5589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590/*
5591 * update_array_info is used to change the configuration of an
5592 * on-line array.
5593 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5594 * fields in the info are checked against the array.
5595 * Any differences that cannot be handled will cause an error.
5596 * Normally, only one change can be managed at a time.
5597 */
5598static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5599{
5600 int rv = 0;
5601 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005602 int state = 0;
5603
5604 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005605 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005606 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
5608 if (mddev->major_version != info->major_version ||
5609 mddev->minor_version != info->minor_version ||
5610/* mddev->patch_version != info->patch_version || */
5611 mddev->ctime != info->ctime ||
5612 mddev->level != info->level ||
5613/* mddev->layout != info->layout || */
5614 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005615 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005616 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5617 ((state^info->state) & 0xfffffe00)
5618 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 return -EINVAL;
5620 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005621 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5622 cnt++;
5623 if (mddev->raid_disks != info->raid_disks)
5624 cnt++;
5625 if (mddev->layout != info->layout)
5626 cnt++;
5627 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5628 cnt++;
5629 if (cnt == 0)
5630 return 0;
5631 if (cnt > 1)
5632 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634 if (mddev->layout != info->layout) {
5635 /* Change layout
5636 * we don't need to do anything at the md level, the
5637 * personality will take care of it all.
5638 */
NeilBrown50ac1682009-06-18 08:47:55 +10005639 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005641 else {
5642 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005643 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005644 if (rv)
5645 mddev->new_layout = mddev->layout;
5646 return rv;
5647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005649 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005650 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
NeilBrownda943b92006-01-06 00:20:54 -08005652 if (mddev->raid_disks != info->raid_disks)
5653 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654
NeilBrown36fa3062005-09-09 16:23:45 -07005655 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5656 if (mddev->pers->quiesce == NULL)
5657 return -EINVAL;
5658 if (mddev->recovery || mddev->sync_thread)
5659 return -EBUSY;
5660 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5661 /* add the bitmap */
5662 if (mddev->bitmap)
5663 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005664 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005665 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005666 mddev->bitmap_info.offset =
5667 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005668 mddev->pers->quiesce(mddev, 1);
5669 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005670 if (!rv)
5671 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005672 if (rv)
5673 bitmap_destroy(mddev);
5674 mddev->pers->quiesce(mddev, 0);
5675 } else {
5676 /* remove the bitmap */
5677 if (!mddev->bitmap)
5678 return -ENOENT;
5679 if (mddev->bitmap->file)
5680 return -EINVAL;
5681 mddev->pers->quiesce(mddev, 1);
5682 bitmap_destroy(mddev);
5683 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005684 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005685 }
5686 }
NeilBrown850b2b42006-10-03 01:15:46 -07005687 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 return rv;
5689}
5690
5691static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5692{
5693 mdk_rdev_t *rdev;
5694
5695 if (mddev->pers == NULL)
5696 return -ENODEV;
5697
5698 rdev = find_rdev(mddev, dev);
5699 if (!rdev)
5700 return -ENODEV;
5701
5702 md_error(mddev, rdev);
5703 return 0;
5704}
5705
Andre Noll2f9618c2008-04-25 18:57:58 +02005706/*
5707 * We have a problem here : there is no easy way to give a CHS
5708 * virtual geometry. We currently pretend that we have a 2 heads
5709 * 4 sectors (with a BIG number of cylinders...). This drives
5710 * dosfs just mad... ;-)
5711 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005712static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5713{
5714 mddev_t *mddev = bdev->bd_disk->private_data;
5715
5716 geo->heads = 2;
5717 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005718 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005719 return 0;
5720}
5721
Al Viroa39907f2008-03-02 10:31:15 -05005722static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 unsigned int cmd, unsigned long arg)
5724{
5725 int err = 0;
5726 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005728 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
5730 if (!capable(CAP_SYS_ADMIN))
5731 return -EACCES;
5732
5733 /*
5734 * Commands dealing with the RAID driver but not any
5735 * particular array:
5736 */
5737 switch (cmd)
5738 {
5739 case RAID_VERSION:
5740 err = get_version(argp);
5741 goto done;
5742
5743 case PRINT_RAID_DEBUG:
5744 err = 0;
5745 md_print_devices();
5746 goto done;
5747
5748#ifndef MODULE
5749 case RAID_AUTORUN:
5750 err = 0;
5751 autostart_arrays(arg);
5752 goto done;
5753#endif
5754 default:;
5755 }
5756
5757 /*
5758 * Commands creating/starting a new array:
5759 */
5760
Al Viroa39907f2008-03-02 10:31:15 -05005761 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 if (!mddev) {
5764 BUG();
5765 goto abort;
5766 }
5767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 err = mddev_lock(mddev);
5769 if (err) {
5770 printk(KERN_INFO
5771 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5772 err, cmd);
5773 goto abort;
5774 }
5775
5776 switch (cmd)
5777 {
5778 case SET_ARRAY_INFO:
5779 {
5780 mdu_array_info_t info;
5781 if (!arg)
5782 memset(&info, 0, sizeof(info));
5783 else if (copy_from_user(&info, argp, sizeof(info))) {
5784 err = -EFAULT;
5785 goto abort_unlock;
5786 }
5787 if (mddev->pers) {
5788 err = update_array_info(mddev, &info);
5789 if (err) {
5790 printk(KERN_WARNING "md: couldn't update"
5791 " array info. %d\n", err);
5792 goto abort_unlock;
5793 }
5794 goto done_unlock;
5795 }
5796 if (!list_empty(&mddev->disks)) {
5797 printk(KERN_WARNING
5798 "md: array %s already has disks!\n",
5799 mdname(mddev));
5800 err = -EBUSY;
5801 goto abort_unlock;
5802 }
5803 if (mddev->raid_disks) {
5804 printk(KERN_WARNING
5805 "md: array %s already initialised!\n",
5806 mdname(mddev));
5807 err = -EBUSY;
5808 goto abort_unlock;
5809 }
5810 err = set_array_info(mddev, &info);
5811 if (err) {
5812 printk(KERN_WARNING "md: couldn't set"
5813 " array info. %d\n", err);
5814 goto abort_unlock;
5815 }
5816 }
5817 goto done_unlock;
5818
5819 default:;
5820 }
5821
5822 /*
5823 * Commands querying/configuring an existing array:
5824 */
NeilBrown32a76272005-06-21 17:17:14 -07005825 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005826 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005827 if ((!mddev->raid_disks && !mddev->external)
5828 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5829 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5830 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 err = -ENODEV;
5832 goto abort_unlock;
5833 }
5834
5835 /*
5836 * Commands even a read-only array can execute:
5837 */
5838 switch (cmd)
5839 {
5840 case GET_ARRAY_INFO:
5841 err = get_array_info(mddev, argp);
5842 goto done_unlock;
5843
NeilBrown32a76272005-06-21 17:17:14 -07005844 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005845 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005846 goto done_unlock;
5847
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 case GET_DISK_INFO:
5849 err = get_disk_info(mddev, argp);
5850 goto done_unlock;
5851
5852 case RESTART_ARRAY_RW:
5853 err = restart_array(mddev);
5854 goto done_unlock;
5855
5856 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005857 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 goto done_unlock;
5859
5860 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005861 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 goto done_unlock;
5863
Dan Williamse2218352010-05-12 08:25:37 +10005864 case BLKROSET:
5865 if (get_user(ro, (int __user *)(arg))) {
5866 err = -EFAULT;
5867 goto done_unlock;
5868 }
5869 err = -EINVAL;
5870
5871 /* if the bdev is going readonly the value of mddev->ro
5872 * does not matter, no writes are coming
5873 */
5874 if (ro)
5875 goto done_unlock;
5876
5877 /* are we are already prepared for writes? */
5878 if (mddev->ro != 1)
5879 goto done_unlock;
5880
5881 /* transitioning to readauto need only happen for
5882 * arrays that call md_write_start
5883 */
5884 if (mddev->pers) {
5885 err = restart_array(mddev);
5886 if (err == 0) {
5887 mddev->ro = 2;
5888 set_disk_ro(mddev->gendisk, 0);
5889 }
5890 }
5891 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 }
5893
5894 /*
5895 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005896 * superblock, so we do not allow them on read-only arrays.
5897 * However non-MD ioctls (e.g. get-size) will still come through
5898 * here and hit the 'default' below, so only disallow
5899 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005901 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005902 if (mddev->ro == 2) {
5903 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005904 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5906 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005907 } else {
5908 err = -EROFS;
5909 goto abort_unlock;
5910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
5912
5913 switch (cmd)
5914 {
5915 case ADD_NEW_DISK:
5916 {
5917 mdu_disk_info_t info;
5918 if (copy_from_user(&info, argp, sizeof(info)))
5919 err = -EFAULT;
5920 else
5921 err = add_new_disk(mddev, &info);
5922 goto done_unlock;
5923 }
5924
5925 case HOT_REMOVE_DISK:
5926 err = hot_remove_disk(mddev, new_decode_dev(arg));
5927 goto done_unlock;
5928
5929 case HOT_ADD_DISK:
5930 err = hot_add_disk(mddev, new_decode_dev(arg));
5931 goto done_unlock;
5932
5933 case SET_DISK_FAULTY:
5934 err = set_disk_faulty(mddev, new_decode_dev(arg));
5935 goto done_unlock;
5936
5937 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005938 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 goto done_unlock;
5940
NeilBrown32a76272005-06-21 17:17:14 -07005941 case SET_BITMAP_FILE:
5942 err = set_bitmap_file(mddev, (int)arg);
5943 goto done_unlock;
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 err = -EINVAL;
5947 goto abort_unlock;
5948 }
5949
5950done_unlock:
5951abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005952 if (mddev->hold_active == UNTIL_IOCTL &&
5953 err != -EINVAL)
5954 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 mddev_unlock(mddev);
5956
5957 return err;
5958done:
5959 if (err)
5960 MD_BUG();
5961abort:
5962 return err;
5963}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005964#ifdef CONFIG_COMPAT
5965static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5966 unsigned int cmd, unsigned long arg)
5967{
5968 switch (cmd) {
5969 case HOT_REMOVE_DISK:
5970 case HOT_ADD_DISK:
5971 case SET_DISK_FAULTY:
5972 case SET_BITMAP_FILE:
5973 /* These take in integer arg, do not convert */
5974 break;
5975 default:
5976 arg = (unsigned long)compat_ptr(arg);
5977 break;
5978 }
5979
5980 return md_ioctl(bdev, mode, cmd, arg);
5981}
5982#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Al Viroa39907f2008-03-02 10:31:15 -05005984static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985{
5986 /*
5987 * Succeed if we can lock the mddev, which confirms that
5988 * it isn't being stopped right now.
5989 */
NeilBrownd3374822009-01-09 08:31:10 +11005990 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 int err;
5992
NeilBrownd3374822009-01-09 08:31:10 +11005993 if (mddev->gendisk != bdev->bd_disk) {
5994 /* we are racing with mddev_put which is discarding this
5995 * bd_disk.
5996 */
5997 mddev_put(mddev);
5998 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02005999 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006000 /* Then retry the open from the top */
6001 return -ERESTARTSYS;
6002 }
6003 BUG_ON(mddev != bdev->bd_disk->private_data);
6004
NeilBrownc8c00a62009-08-10 12:50:52 +10006005 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 goto out;
6007
6008 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006009 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006010 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
NeilBrownf3b99be2010-06-24 13:31:03 +10006012 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 out:
6014 return err;
6015}
6016
Al Viroa39907f2008-03-02 10:31:15 -05006017static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
Al Viroa39907f2008-03-02 10:31:15 -05006019 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006021 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006022 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 mddev_put(mddev);
6024
6025 return 0;
6026}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006027static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028{
6029 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006030 .open = md_open,
6031 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006032 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006033#ifdef CONFIG_COMPAT
6034 .compat_ioctl = md_compat_ioctl,
6035#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006036 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037};
6038
Adrian Bunk75c96f82005-05-05 16:16:09 -07006039static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040{
6041 mdk_thread_t *thread = arg;
6042
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 /*
6044 * md_thread is a 'system-thread', it's priority should be very
6045 * high. We avoid resource deadlocks individually in each
6046 * raid personality. (RAID5 does preallocation) We also use RR and
6047 * the very same RT priority as kswapd, thus we will never get
6048 * into a priority inversion deadlock.
6049 *
6050 * we definitely have to have equal or higher priority than
6051 * bdflush, otherwise bdflush will deadlock if there are too
6052 * many dirty RAID5 blocks.
6053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
NeilBrown6985c432005-10-19 21:23:47 -07006055 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006056 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
NeilBrown93588e22005-11-15 00:09:12 -08006058 /* We need to wait INTERRUPTIBLE so that
6059 * we don't add to the load-average.
6060 * That means we need to be sure no signals are
6061 * pending
6062 */
6063 if (signal_pending(current))
6064 flush_signals(current);
6065
6066 wait_event_interruptible_timeout
6067 (thread->wqueue,
6068 test_bit(THREAD_WAKEUP, &thread->flags)
6069 || kthread_should_stop(),
6070 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
NeilBrown6c987912011-01-14 09:13:53 +11006072 clear_bit(THREAD_WAKEUP, &thread->flags);
6073 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006074 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006076
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 return 0;
6078}
6079
6080void md_wakeup_thread(mdk_thread_t *thread)
6081{
6082 if (thread) {
6083 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6084 set_bit(THREAD_WAKEUP, &thread->flags);
6085 wake_up(&thread->wqueue);
6086 }
6087}
6088
6089mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6090 const char *name)
6091{
6092 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
NeilBrown9ffae0c2006-01-06 00:20:32 -08006094 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 if (!thread)
6096 return NULL;
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 init_waitqueue_head(&thread->wqueue);
6099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 thread->run = run;
6101 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006102 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006103 thread->tsk = kthread_run(md_thread, thread,
6104 "%s_%s",
6105 mdname(thread->mddev),
6106 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006107 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 kfree(thread);
6109 return NULL;
6110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 return thread;
6112}
6113
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114void md_unregister_thread(mdk_thread_t *thread)
6115{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006116 if (!thread)
6117 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006118 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006119
6120 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 kfree(thread);
6122}
6123
6124void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6125{
6126 if (!mddev) {
6127 MD_BUG();
6128 return;
6129 }
6130
NeilBrownb2d444d2005-11-08 21:39:31 -08006131 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006133
6134 if (mddev->external)
6135 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6138 mdname(mddev),
6139 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6140 __builtin_return_address(0),__builtin_return_address(1),
6141 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006142*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006143 if (!mddev->pers)
6144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 if (!mddev->pers->error_handler)
6146 return;
6147 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006148 if (mddev->degraded)
6149 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006150 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6152 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6153 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006154 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006155 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006156 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157}
6158
6159/* seq_file implementation /proc/mdstat */
6160
6161static void status_unused(struct seq_file *seq)
6162{
6163 int i = 0;
6164 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
6166 seq_printf(seq, "unused devices: ");
6167
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006168 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 char b[BDEVNAME_SIZE];
6170 i++;
6171 seq_printf(seq, "%s ",
6172 bdevname(rdev->bdev,b));
6173 }
6174 if (!i)
6175 seq_printf(seq, "<none>");
6176
6177 seq_printf(seq, "\n");
6178}
6179
6180
6181static void status_resync(struct seq_file *seq, mddev_t * mddev)
6182{
NeilBrowndd71cf62009-05-07 12:49:35 +10006183 sector_t max_sectors, resync, res;
6184 unsigned long dt, db;
6185 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006186 int scale;
6187 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
NeilBrowndd71cf62009-05-07 12:49:35 +10006189 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
6191 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006192 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006194 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195
6196 /*
6197 * Should not happen.
6198 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006199 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 MD_BUG();
6201 return;
6202 }
NeilBrown4588b422006-03-27 01:18:04 -08006203 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006204 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006205 * u32, as those are the requirements for sector_div.
6206 * Thus 'scale' must be at least 10
6207 */
6208 scale = 10;
6209 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006210 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006211 scale++;
6212 }
6213 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006214 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006215
6216 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 {
NeilBrown4588b422006-03-27 01:18:04 -08006218 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 seq_printf(seq, "[");
6220 for (i = 0; i < x; i++)
6221 seq_printf(seq, "=");
6222 seq_printf(seq, ">");
6223 for (i = 0; i < y; i++)
6224 seq_printf(seq, ".");
6225 seq_printf(seq, "] ");
6226 }
NeilBrown4588b422006-03-27 01:18:04 -08006227 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006228 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6229 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006230 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6231 "check" :
6232 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6233 "resync" : "recovery"))),
6234 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006235 (unsigned long long) resync/2,
6236 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
6238 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 * dt: time from mark until now
6240 * db: blocks written from mark until now
6241 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006242 *
6243 * rt is a sector_t, so could be 32bit or 64bit.
6244 * So we divide before multiply in case it is 32bit and close
6245 * to the limit.
6246 * We scale the divisor (db) by 32 to avoid loosing precision
6247 * near the end of resync when the number of remaining sectors
6248 * is close to 'db'.
6249 * We then divide rt by 32 after multiplying by db to compensate.
6250 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 */
6252 dt = ((jiffies - mddev->resync_mark) / HZ);
6253 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006254 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6255 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
NeilBrowndd71cf62009-05-07 12:49:35 +10006257 rt = max_sectors - resync; /* number of remaining sectors */
6258 sector_div(rt, db/32+1);
6259 rt *= dt;
6260 rt >>= 5;
6261
6262 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6263 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264
NeilBrownff4e8d92006-07-10 04:44:16 -07006265 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266}
6267
6268static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6269{
6270 struct list_head *tmp;
6271 loff_t l = *pos;
6272 mddev_t *mddev;
6273
6274 if (l >= 0x10000)
6275 return NULL;
6276 if (!l--)
6277 /* header */
6278 return (void*)1;
6279
6280 spin_lock(&all_mddevs_lock);
6281 list_for_each(tmp,&all_mddevs)
6282 if (!l--) {
6283 mddev = list_entry(tmp, mddev_t, all_mddevs);
6284 mddev_get(mddev);
6285 spin_unlock(&all_mddevs_lock);
6286 return mddev;
6287 }
6288 spin_unlock(&all_mddevs_lock);
6289 if (!l--)
6290 return (void*)2;/* tail */
6291 return NULL;
6292}
6293
6294static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6295{
6296 struct list_head *tmp;
6297 mddev_t *next_mddev, *mddev = v;
6298
6299 ++*pos;
6300 if (v == (void*)2)
6301 return NULL;
6302
6303 spin_lock(&all_mddevs_lock);
6304 if (v == (void*)1)
6305 tmp = all_mddevs.next;
6306 else
6307 tmp = mddev->all_mddevs.next;
6308 if (tmp != &all_mddevs)
6309 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6310 else {
6311 next_mddev = (void*)2;
6312 *pos = 0x10000;
6313 }
6314 spin_unlock(&all_mddevs_lock);
6315
6316 if (v != (void*)1)
6317 mddev_put(mddev);
6318 return next_mddev;
6319
6320}
6321
6322static void md_seq_stop(struct seq_file *seq, void *v)
6323{
6324 mddev_t *mddev = v;
6325
6326 if (mddev && v != (void*)1 && v != (void*)2)
6327 mddev_put(mddev);
6328}
6329
NeilBrownd7603b72006-01-06 00:20:30 -08006330struct mdstat_info {
6331 int event;
6332};
6333
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334static int md_seq_show(struct seq_file *seq, void *v)
6335{
6336 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006337 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006339 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006340 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
6342 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006343 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 seq_printf(seq, "Personalities : ");
6345 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006346 list_for_each_entry(pers, &pers_list, list)
6347 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
6349 spin_unlock(&pers_lock);
6350 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006351 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 return 0;
6353 }
6354 if (v == (void*)2) {
6355 status_unused(seq);
6356 return 0;
6357 }
6358
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006359 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006361
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6363 seq_printf(seq, "%s : %sactive", mdname(mddev),
6364 mddev->pers ? "" : "in");
6365 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006366 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006368 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006369 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 seq_printf(seq, " %s", mddev->pers->name);
6371 }
6372
Andre Nolldd8ac332009-03-31 14:33:13 +11006373 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006374 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 char b[BDEVNAME_SIZE];
6376 seq_printf(seq, " %s[%d]",
6377 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006378 if (test_bit(WriteMostly, &rdev->flags))
6379 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006380 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 seq_printf(seq, "(F)");
6382 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006383 } else if (rdev->raid_disk < 0)
6384 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006385 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 }
6387
6388 if (!list_empty(&mddev->disks)) {
6389 if (mddev->pers)
6390 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006391 (unsigned long long)
6392 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 else
6394 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006395 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006397 if (mddev->persistent) {
6398 if (mddev->major_version != 0 ||
6399 mddev->minor_version != 90) {
6400 seq_printf(seq," super %d.%d",
6401 mddev->major_version,
6402 mddev->minor_version);
6403 }
NeilBrowne6910632008-02-06 01:39:51 -08006404 } else if (mddev->external)
6405 seq_printf(seq, " super external:%s",
6406 mddev->metadata_type);
6407 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006408 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
6410 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006411 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006413 if (mddev->pers->sync_request) {
6414 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006415 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006416 seq_printf(seq, "\n ");
6417 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6418 seq_printf(seq, "\tresync=DELAYED\n ");
6419 else if (mddev->recovery_cp < MaxSector)
6420 seq_printf(seq, "\tresync=PENDING\n ");
6421 }
NeilBrown32a76272005-06-21 17:17:14 -07006422 } else
6423 seq_printf(seq, "\n ");
6424
6425 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006426 unsigned long chunk_kb;
6427 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006428 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006429 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006430 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6431 "%lu%s chunk",
6432 bitmap->pages - bitmap->missing_pages,
6433 bitmap->pages,
6434 (bitmap->pages - bitmap->missing_pages)
6435 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006436 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006437 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006438 if (bitmap->file) {
6439 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006440 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006441 }
NeilBrown78d742d2005-06-21 17:17:15 -07006442
NeilBrown32a76272005-06-21 17:17:14 -07006443 seq_printf(seq, "\n");
6444 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 }
6446
6447 seq_printf(seq, "\n");
6448 }
6449 mddev_unlock(mddev);
6450
6451 return 0;
6452}
6453
Jan Engelhardt110518b2009-05-07 12:49:37 +10006454static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 .start = md_seq_start,
6456 .next = md_seq_next,
6457 .stop = md_seq_stop,
6458 .show = md_seq_show,
6459};
6460
6461static int md_seq_open(struct inode *inode, struct file *file)
6462{
6463 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006464 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6465 if (mi == NULL)
6466 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
6468 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006469 if (error)
6470 kfree(mi);
6471 else {
6472 struct seq_file *p = file->private_data;
6473 p->private = mi;
6474 mi->event = atomic_read(&md_event_count);
6475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 return error;
6477}
6478
NeilBrownd7603b72006-01-06 00:20:30 -08006479static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6480{
6481 struct seq_file *m = filp->private_data;
6482 struct mdstat_info *mi = m->private;
6483 int mask;
6484
6485 poll_wait(filp, &md_event_waiters, wait);
6486
6487 /* always allow read */
6488 mask = POLLIN | POLLRDNORM;
6489
6490 if (mi->event != atomic_read(&md_event_count))
6491 mask |= POLLERR | POLLPRI;
6492 return mask;
6493}
6494
Arjan van de Venfa027c22007-02-12 00:55:33 -08006495static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006496 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 .open = md_seq_open,
6498 .read = seq_read,
6499 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006500 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006501 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502};
6503
NeilBrown2604b702006-01-06 00:20:36 -08006504int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006507 list_add_tail(&p->list, &pers_list);
6508 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 spin_unlock(&pers_lock);
6510 return 0;
6511}
6512
NeilBrown2604b702006-01-06 00:20:36 -08006513int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514{
NeilBrown2604b702006-01-06 00:20:36 -08006515 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006517 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 spin_unlock(&pers_lock);
6519 return 0;
6520}
6521
NeilBrowneea1bf32009-03-31 14:27:02 +11006522static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523{
6524 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006526 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
6528 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006529 rcu_read_lock();
6530 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006532 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6533 (int)part_stat_read(&disk->part0, sectors[1]) -
6534 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006535 /* sync IO will cause sync_io to increase before the disk_stats
6536 * as sync_io is counted when a request starts, and
6537 * disk_stats is counted when it completes.
6538 * So resync activity will cause curr_events to be smaller than
6539 * when there was no such activity.
6540 * non-sync IO will cause disk_stat to increase without
6541 * increasing sync_io so curr_events will (eventually)
6542 * be larger than it was before. Once it becomes
6543 * substantially larger, the test below will cause
6544 * the array to appear non-idle, and resync will slow
6545 * down.
6546 * If there is a lot of outstanding resync activity when
6547 * we set last_event to curr_events, then all that activity
6548 * completing might cause the array to appear non-idle
6549 * and resync will be slowed down even though there might
6550 * not have been non-resync activity. This will only
6551 * happen once though. 'last_events' will soon reflect
6552 * the state where there is little or no outstanding
6553 * resync requests, and further resync activity will
6554 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006555 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006557 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 rdev->last_events = curr_events;
6559 idle = 0;
6560 }
6561 }
NeilBrown4b809912008-07-21 17:05:25 +10006562 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 return idle;
6564}
6565
6566void md_done_sync(mddev_t *mddev, int blocks, int ok)
6567{
6568 /* another "blocks" (512byte) blocks have been synced */
6569 atomic_sub(blocks, &mddev->recovery_active);
6570 wake_up(&mddev->recovery_wait);
6571 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006572 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 md_wakeup_thread(mddev->thread);
6574 // stop recovery, signal do_sync ....
6575 }
6576}
6577
6578
NeilBrown06d91a52005-06-21 17:17:12 -07006579/* md_write_start(mddev, bi)
6580 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006581 * in superblock) before writing, schedule a superblock update
6582 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006583 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006584void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585{
Neil Brown0fd62b82008-06-28 08:31:36 +10006586 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006587 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006588 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006589
NeilBrownf91de922005-11-08 21:39:36 -08006590 BUG_ON(mddev->ro == 1);
6591 if (mddev->ro == 2) {
6592 /* need to switch to read/write */
6593 mddev->ro = 0;
6594 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6595 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006596 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006597 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006598 }
NeilBrown06d91a52005-06-21 17:17:12 -07006599 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006600 if (mddev->safemode == 1)
6601 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006602 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006603 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006604 if (mddev->in_sync) {
6605 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006606 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006607 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006608 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006609 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006610 }
NeilBrowna9701a32005-11-08 21:39:34 -08006611 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006612 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006613 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006614 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006615 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006616 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617}
6618
6619void md_write_end(mddev_t *mddev)
6620{
6621 if (atomic_dec_and_test(&mddev->writes_pending)) {
6622 if (mddev->safemode == 2)
6623 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006624 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6626 }
6627}
6628
NeilBrown2a2275d2007-01-26 00:57:11 -08006629/* md_allow_write(mddev)
6630 * Calling this ensures that the array is marked 'active' so that writes
6631 * may proceed without blocking. It is important to call this before
6632 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6633 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006634 *
6635 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6636 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006637 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006638int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006639{
6640 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006641 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006642 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006643 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006644 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006645 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006646
6647 spin_lock_irq(&mddev->write_lock);
6648 if (mddev->in_sync) {
6649 mddev->in_sync = 0;
6650 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006651 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006652 if (mddev->safemode_delay &&
6653 mddev->safemode == 0)
6654 mddev->safemode = 1;
6655 spin_unlock_irq(&mddev->write_lock);
6656 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006657 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006658 } else
6659 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006660
NeilBrown070dc6d2010-08-30 17:33:34 +10006661 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006662 return -EAGAIN;
6663 else
6664 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006665}
6666EXPORT_SYMBOL_GPL(md_allow_write);
6667
NeilBrownb63d7c22010-06-01 19:37:33 +10006668void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006669{
NeilBrown252ac522010-06-01 19:37:29 +10006670 if (mddev->plug)
6671 mddev->plug->unplug_fn(mddev->plug);
6672}
6673
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674#define SYNC_MARKS 10
6675#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006676void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677{
6678 mddev_t *mddev2;
6679 unsigned int currspeed = 0,
6680 window;
NeilBrown57afd892005-06-21 17:17:13 -07006681 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 unsigned long mark[SYNC_MARKS];
6683 sector_t mark_cnt[SYNC_MARKS];
6684 int last_mark,m;
6685 struct list_head *tmp;
6686 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006687 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006688 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006689 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
6691 /* just incase thread restarts... */
6692 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6693 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006694 if (mddev->ro) /* never try to sync a read-only array */
6695 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
NeilBrown61df9d92006-10-03 01:15:57 -07006697 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6698 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6699 desc = "data-check";
6700 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6701 desc = "requested-resync";
6702 else
6703 desc = "resync";
6704 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6705 desc = "reshape";
6706 else
6707 desc = "recovery";
6708
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 /* we overload curr_resync somewhat here.
6710 * 0 == not engaged in resync at all
6711 * 2 == checking that there is no conflict with another sync
6712 * 1 == like 2, but have yielded to allow conflicting resync to
6713 * commense
6714 * other == active in resync - this many blocks
6715 *
6716 * Before starting a resync we must have set curr_resync to
6717 * 2, and then checked that every "conflicting" array has curr_resync
6718 * less than ours. When we find one that is the same or higher
6719 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6720 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6721 * This will mean we have to start checking from the beginning again.
6722 *
6723 */
6724
6725 do {
6726 mddev->curr_resync = 2;
6727
6728 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006729 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006730 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006731
6732 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006734 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 if (mddev2 == mddev)
6736 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006737 if (!mddev->parallel_resync
6738 && mddev2->curr_resync
6739 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 DEFINE_WAIT(wq);
6741 if (mddev < mddev2 && mddev->curr_resync == 2) {
6742 /* arbitrarily yield */
6743 mddev->curr_resync = 1;
6744 wake_up(&resync_wait);
6745 }
6746 if (mddev > mddev2 && mddev->curr_resync == 1)
6747 /* no need to wait here, we can wait the next
6748 * time 'round when curr_resync == 2
6749 */
6750 continue;
NeilBrown97441972008-09-19 11:49:54 +10006751 /* We need to wait 'interruptible' so as not to
6752 * contribute to the load average, and not to
6753 * be caught by 'softlockup'
6754 */
6755 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006756 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006757 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006758 printk(KERN_INFO "md: delaying %s of %s"
6759 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006761 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006763 if (signal_pending(current))
6764 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 schedule();
6766 finish_wait(&resync_wait, &wq);
6767 goto try_again;
6768 }
6769 finish_wait(&resync_wait, &wq);
6770 }
6771 }
6772 } while (mddev->curr_resync < 2);
6773
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006774 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006775 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006777 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 */
6779 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006780 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006781 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006782 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6783 j = mddev->resync_min;
6784 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006785 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006786
NeilBrownccfcc3c2006-03-27 01:18:09 -08006787 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006788 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006789 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006791 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006792 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006793 rcu_read_lock();
6794 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006795 if (rdev->raid_disk >= 0 &&
6796 !test_bit(Faulty, &rdev->flags) &&
6797 !test_bit(In_sync, &rdev->flags) &&
6798 rdev->recovery_offset < j)
6799 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006800 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
NeilBrown61df9d92006-10-03 01:15:57 -07006803 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6804 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6805 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006806 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006807 "(but not more than %d KB/sec) for %s.\n",
6808 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
NeilBrowneea1bf32009-03-31 14:27:02 +11006810 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006811
NeilBrown57afd892005-06-21 17:17:13 -07006812 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 for (m = 0; m < SYNC_MARKS; m++) {
6814 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006815 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 }
6817 last_mark = 0;
6818 mddev->resync_mark = mark[last_mark];
6819 mddev->resync_mark_cnt = mark_cnt[last_mark];
6820
6821 /*
6822 * Tune reconstruction:
6823 */
6824 window = 32*(PAGE_SIZE/512);
6825 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6826 window/2,(unsigned long long) max_sectors/2);
6827
6828 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 last_check = 0;
6830
6831 if (j>2) {
6832 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006833 "md: resuming %s of %s from checkpoint.\n",
6834 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 mddev->curr_resync = j;
6836 }
NeilBrown75d3da42011-01-14 09:14:34 +11006837 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006840 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
NeilBrown57afd892005-06-21 17:17:13 -07006842 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006843
NeilBrown7a91ee12009-05-26 12:57:21 +10006844 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6845 ((mddev->curr_resync > mddev->curr_resync_completed &&
6846 (mddev->curr_resync - mddev->curr_resync_completed)
6847 > (max_sectors >> 4)) ||
6848 (j - mddev->curr_resync_completed)*2
6849 >= mddev->resync_max - mddev->curr_resync_completed
6850 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006851 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11006852 wait_event(mddev->recovery_wait,
6853 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11006854 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10006855 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006856 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006857 }
NeilBrownacb180b2009-04-14 16:28:34 +10006858
NeilBrowne62e58a2009-07-01 13:15:35 +10006859 while (j >= mddev->resync_max && !kthread_should_stop()) {
6860 /* As this condition is controlled by user-space,
6861 * we can block indefinitely, so use '_interruptible'
6862 * to avoid triggering warnings.
6863 */
6864 flush_signals(current); /* just in case */
6865 wait_event_interruptible(mddev->recovery_wait,
6866 mddev->resync_max > j
6867 || kthread_should_stop());
6868 }
NeilBrownacb180b2009-04-14 16:28:34 +10006869
6870 if (kthread_should_stop())
6871 goto interrupted;
6872
NeilBrown57afd892005-06-21 17:17:13 -07006873 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006874 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006875 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006876 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 goto out;
6878 }
NeilBrown57afd892005-06-21 17:17:13 -07006879
6880 if (!skipped) { /* actual IO requested */
6881 io_sectors += sectors;
6882 atomic_add(sectors, &mddev->recovery_active);
6883 }
6884
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 j += sectors;
6886 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006887 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006888 if (last_check == 0)
6889 /* this is the earliers that rebuilt will be
6890 * visible in /proc/mdstat
6891 */
6892 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006893
6894 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 continue;
6896
NeilBrown57afd892005-06-21 17:17:13 -07006897 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
NeilBrowndfc70642008-05-23 13:04:39 -07006899 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 break;
6901
6902 repeat:
6903 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6904 /* step marks */
6905 int next = (last_mark+1) % SYNC_MARKS;
6906
6907 mddev->resync_mark = mark[next];
6908 mddev->resync_mark_cnt = mark_cnt[next];
6909 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006910 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 last_mark = next;
6912 }
6913
6914
NeilBrownc6207272008-02-06 01:39:52 -08006915 if (kthread_should_stop())
6916 goto interrupted;
6917
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
6919 /*
6920 * this loop exits only if either when we are slower than
6921 * the 'hard' speed limit, or the system was IO-idle for
6922 * a jiffy.
6923 * the system might be non-idle CPU-wise, but we only care
6924 * about not overloading the IO subsystem. (things like an
6925 * e2fsck being done on the RAID array should execute fast)
6926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 cond_resched();
6928
NeilBrown57afd892005-06-21 17:17:13 -07006929 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6930 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931
NeilBrown88202a02006-01-06 00:21:36 -08006932 if (currspeed > speed_min(mddev)) {
6933 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006934 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006935 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 goto repeat;
6937 }
6938 }
6939 }
NeilBrown61df9d92006-10-03 01:15:57 -07006940 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 /*
6942 * this also signals 'finished resyncing' to md_stop
6943 */
6944 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6946
6947 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006948 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949
NeilBrowndfc70642008-05-23 13:04:39 -07006950 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006951 mddev->curr_resync > 2) {
6952 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6953 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6954 if (mddev->curr_resync >= mddev->recovery_cp) {
6955 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006956 "md: checkpointing %s of %s.\n",
6957 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006958 mddev->recovery_cp = mddev->curr_resync;
6959 }
6960 } else
6961 mddev->recovery_cp = MaxSector;
6962 } else {
6963 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6964 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006965 rcu_read_lock();
6966 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006967 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006968 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006969 !test_bit(Faulty, &rdev->flags) &&
6970 !test_bit(In_sync, &rdev->flags) &&
6971 rdev->recovery_offset < mddev->curr_resync)
6972 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006973 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 }
NeilBrown17571282006-12-10 02:20:52 -08006976 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006979 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6980 /* We completed so min/max setting can be forgotten if used. */
6981 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6982 mddev->resync_min = 0;
6983 mddev->resync_max = MaxSector;
6984 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6985 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 mddev->curr_resync = 0;
6987 wake_up(&resync_wait);
6988 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6989 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006990 return;
6991
6992 interrupted:
6993 /*
6994 * got a signal, exit.
6995 */
6996 printk(KERN_INFO
6997 "md: md_do_sync() got signal ... exiting\n");
6998 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6999 goto out;
7000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001}
NeilBrown29269552006-03-27 01:18:10 -08007002EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
7004
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007005static int remove_and_add_spares(mddev_t *mddev)
7006{
7007 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007008 int spares = 0;
7009
NeilBrown97e4f422009-03-31 14:33:13 +11007010 mddev->curr_resync_completed = 0;
7011
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007012 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007013 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007014 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007015 (test_bit(Faulty, &rdev->flags) ||
7016 ! test_bit(In_sync, &rdev->flags)) &&
7017 atomic_read(&rdev->nr_pending)==0) {
7018 if (mddev->pers->hot_remove_disk(
7019 mddev, rdev->raid_disk)==0) {
7020 char nm[20];
7021 sprintf(nm,"rd%d", rdev->raid_disk);
7022 sysfs_remove_link(&mddev->kobj, nm);
7023 rdev->raid_disk = -1;
7024 }
7025 }
7026
NeilBrowna8c42c72011-01-31 13:47:13 +11007027 if (mddev->degraded && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007028 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007029 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007030 !test_bit(In_sync, &rdev->flags) &&
7031 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007032 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007033 if (rdev->raid_disk < 0
7034 && !test_bit(Faulty, &rdev->flags)) {
7035 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007036 if (mddev->pers->
7037 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007038 char nm[20];
7039 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007040 if (sysfs_create_link(&mddev->kobj,
7041 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007042 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007043 spares++;
7044 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007045 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007046 } else
7047 break;
7048 }
NeilBrowndfc70642008-05-23 13:04:39 -07007049 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007050 }
7051 return spares;
7052}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007053
7054static void reap_sync_thread(mddev_t *mddev)
7055{
7056 mdk_rdev_t *rdev;
7057
7058 /* resync has finished, collect result */
7059 md_unregister_thread(mddev->sync_thread);
7060 mddev->sync_thread = NULL;
7061 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7062 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7063 /* success...*/
7064 /* activate any spares */
7065 if (mddev->pers->spare_active(mddev))
7066 sysfs_notify(&mddev->kobj, NULL,
7067 "degraded");
7068 }
7069 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7070 mddev->pers->finish_reshape)
7071 mddev->pers->finish_reshape(mddev);
7072 md_update_sb(mddev, 1);
7073
7074 /* if array is no-longer degraded, then any saved_raid_disk
7075 * information must be scrapped
7076 */
7077 if (!mddev->degraded)
7078 list_for_each_entry(rdev, &mddev->disks, same_set)
7079 rdev->saved_raid_disk = -1;
7080
7081 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7082 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7083 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7084 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7085 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7086 /* flag recovery needed just to double check */
7087 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7088 sysfs_notify_dirent_safe(mddev->sysfs_action);
7089 md_new_event(mddev);
7090}
7091
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092/*
7093 * This routine is regularly called by all per-raid-array threads to
7094 * deal with generic issues like resync and super-block update.
7095 * Raid personalities that don't have a thread (linear/raid0) do not
7096 * need this as they never do any recovery or update the superblock.
7097 *
7098 * It does not do any resync itself, but rather "forks" off other threads
7099 * to do that as needed.
7100 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7101 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007102 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 * and wakeups up this thread which will reap the thread and finish up.
7104 * This thread also removes any faulty devices (with nr_pending == 0).
7105 *
7106 * The overall approach is:
7107 * 1/ if the superblock needs updating, update it.
7108 * 2/ If a recovery thread is running, don't do anything else.
7109 * 3/ If recovery has finished, clean up, possibly marking spares active.
7110 * 4/ If there are any faulty devices, remove them.
7111 * 5/ If array is degraded, try to add spares devices
7112 * 6/ If array has spares or is not in-sync, start a resync thread.
7113 */
7114void md_check_recovery(mddev_t *mddev)
7115{
NeilBrown5f404022005-06-21 17:17:16 -07007116 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007117 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118
7119 if (mddev->ro)
7120 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007121
7122 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007123 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007124 printk(KERN_INFO "md: %s in immediate safe mode\n",
7125 mdname(mddev));
7126 mddev->safemode = 2;
7127 }
7128 flush_signals(current);
7129 }
7130
NeilBrownc89a8ee2008-08-05 15:54:13 +10007131 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007134 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007136 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007137 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007138 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7139 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 ))
7141 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007142
NeilBrowndf5b89b2006-03-27 01:18:20 -08007143 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007144 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007145
NeilBrownc89a8ee2008-08-05 15:54:13 +10007146 if (mddev->ro) {
7147 /* Only thing we do on a ro array is remove
7148 * failed devices.
7149 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007150 mdk_rdev_t *rdev;
7151 list_for_each_entry(rdev, &mddev->disks, same_set)
7152 if (rdev->raid_disk >= 0 &&
7153 !test_bit(Blocked, &rdev->flags) &&
7154 test_bit(Faulty, &rdev->flags) &&
7155 atomic_read(&rdev->nr_pending)==0) {
7156 if (mddev->pers->hot_remove_disk(
7157 mddev, rdev->raid_disk)==0) {
7158 char nm[20];
7159 sprintf(nm,"rd%d", rdev->raid_disk);
7160 sysfs_remove_link(&mddev->kobj, nm);
7161 rdev->raid_disk = -1;
7162 }
7163 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007164 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7165 goto unlock;
7166 }
7167
NeilBrown31a59e32008-04-30 00:52:30 -07007168 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007169 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007170 spin_lock_irq(&mddev->write_lock);
7171 if (mddev->safemode &&
7172 !atomic_read(&mddev->writes_pending) &&
7173 !mddev->in_sync &&
7174 mddev->recovery_cp == MaxSector) {
7175 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007176 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007177 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007178 }
7179 if (mddev->safemode == 1)
7180 mddev->safemode = 0;
7181 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007182 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007183 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007184 }
NeilBrownfca4d842005-06-21 17:17:11 -07007185
NeilBrown850b2b42006-10-03 01:15:46 -07007186 if (mddev->flags)
7187 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007188
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7190 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7191 /* resync/recovery still happening */
7192 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7193 goto unlock;
7194 }
7195 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007196 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 goto unlock;
7198 }
Neil Brown72a23c22008-06-28 08:31:41 +10007199 /* Set RUNNING before clearing NEEDED to avoid
7200 * any transients in the value of "sync_action".
7201 */
7202 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7203 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007204 /* Clear some bits that don't mean anything, but
7205 * might be left set
7206 */
NeilBrown24dd4692005-11-08 21:39:26 -08007207 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7208 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007210 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7211 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 /* no recovery is running.
7213 * remove any failed drives, then
7214 * add spares if possible.
7215 * Spare are also removed and re-added, to allow
7216 * the personality to fail the re-add.
7217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007219 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007220 if (mddev->pers->check_reshape == NULL ||
7221 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007222 /* Cannot proceed */
7223 goto unlock;
7224 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007225 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007226 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007227 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7228 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007229 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007230 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007231 } else if (mddev->recovery_cp < MaxSector) {
7232 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007233 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007234 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7235 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007237
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007239 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7240 /* We are adding a device or devices to an array
7241 * which has the bitmap stored on all devices.
7242 * So make sure all bitmap pages get written
7243 */
7244 bitmap_write_all(mddev->bitmap);
7245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 mddev->sync_thread = md_register_thread(md_do_sync,
7247 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007248 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 if (!mddev->sync_thread) {
7250 printk(KERN_ERR "%s: could not start resync"
7251 " thread...\n",
7252 mdname(mddev));
7253 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007254 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7255 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7256 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7257 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7258 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007259 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007261 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007262 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 }
7264 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007265 if (!mddev->sync_thread) {
7266 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7267 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7268 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007269 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007270 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 mddev_unlock(mddev);
7273 }
7274}
7275
Dan Williams6bfe0b42008-04-30 00:52:32 -07007276void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7277{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007278 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007279 wait_event_timeout(rdev->blocked_wait,
7280 !test_bit(Blocked, &rdev->flags),
7281 msecs_to_jiffies(5000));
7282 rdev_dec_pending(rdev, mddev);
7283}
7284EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7285
Adrian Bunk75c96f82005-05-05 16:16:09 -07007286static int md_notify_reboot(struct notifier_block *this,
7287 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288{
7289 struct list_head *tmp;
7290 mddev_t *mddev;
7291
7292 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7293
7294 printk(KERN_INFO "md: stopping all md devices.\n");
7295
NeilBrown29ac4aa2008-02-06 01:39:58 -08007296 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007297 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007298 /* Force a switch to readonly even array
7299 * appears to still be in use. Hence
7300 * the '100'.
7301 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007302 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007303 mddev_unlock(mddev);
7304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 /*
7306 * certain more exotic SCSI devices are known to be
7307 * volatile wrt too early system reboots. While the
7308 * right place to handle this issue is the given
7309 * driver, we do want to have a safe RAID driver ...
7310 */
7311 mdelay(1000*1);
7312 }
7313 return NOTIFY_DONE;
7314}
7315
Adrian Bunk75c96f82005-05-05 16:16:09 -07007316static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 .notifier_call = md_notify_reboot,
7318 .next = NULL,
7319 .priority = INT_MAX, /* before any real devices */
7320};
7321
7322static void md_geninit(void)
7323{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7325
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007326 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327}
7328
Adrian Bunk75c96f82005-05-05 16:16:09 -07007329static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330{
Tejun Heoe804ac72010-10-15 15:36:08 +02007331 int ret = -ENOMEM;
7332
7333 md_wq = alloc_workqueue("md", WQ_RESCUER, 0);
7334 if (!md_wq)
7335 goto err_wq;
7336
7337 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7338 if (!md_misc_wq)
7339 goto err_misc_wq;
7340
7341 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7342 goto err_md;
7343
7344 if ((ret = register_blkdev(0, "mdp")) < 0)
7345 goto err_mdp;
7346 mdp_major = ret;
7347
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007348 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007349 md_probe, NULL, NULL);
7350 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 md_probe, NULL, NULL);
7352
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007354 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355
7356 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358
Tejun Heoe804ac72010-10-15 15:36:08 +02007359err_mdp:
7360 unregister_blkdev(MD_MAJOR, "md");
7361err_md:
7362 destroy_workqueue(md_misc_wq);
7363err_misc_wq:
7364 destroy_workqueue(md_wq);
7365err_wq:
7366 return ret;
7367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368
7369#ifndef MODULE
7370
7371/*
7372 * Searches all registered partitions for autorun RAID arrays
7373 * at boot time.
7374 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007375
7376static LIST_HEAD(all_detected_devices);
7377struct detected_devices_node {
7378 struct list_head list;
7379 dev_t dev;
7380};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
7382void md_autodetect_dev(dev_t dev)
7383{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007384 struct detected_devices_node *node_detected_dev;
7385
7386 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7387 if (node_detected_dev) {
7388 node_detected_dev->dev = dev;
7389 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7390 } else {
7391 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7392 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394}
7395
7396
7397static void autostart_arrays(int part)
7398{
7399 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007400 struct detected_devices_node *node_detected_dev;
7401 dev_t dev;
7402 int i_scanned, i_passed;
7403
7404 i_scanned = 0;
7405 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406
7407 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7408
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007409 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7410 i_scanned++;
7411 node_detected_dev = list_entry(all_detected_devices.next,
7412 struct detected_devices_node, list);
7413 list_del(&node_detected_dev->list);
7414 dev = node_detected_dev->dev;
7415 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007416 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 if (IS_ERR(rdev))
7418 continue;
7419
NeilBrownb2d444d2005-11-08 21:39:31 -08007420 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 MD_BUG();
7422 continue;
7423 }
NeilBrownd0fae182008-03-04 14:29:31 -08007424 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007426 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007428
7429 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7430 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431
7432 autorun_devices(part);
7433}
7434
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007435#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
7437static __exit void md_exit(void)
7438{
7439 mddev_t *mddev;
7440 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007441
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007442 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007443 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007445 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 unregister_blkdev(mdp_major, "mdp");
7447 unregister_reboot_notifier(&md_notifier);
7448 unregister_sysctl_table(raid_table_header);
7449 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007450 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007452 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007454 destroy_workqueue(md_misc_wq);
7455 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456}
7457
Dan Williams685784a2007-07-09 11:56:42 -07007458subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459module_exit(md_exit)
7460
NeilBrownf91de922005-11-08 21:39:36 -08007461static int get_ro(char *buffer, struct kernel_param *kp)
7462{
7463 return sprintf(buffer, "%d", start_readonly);
7464}
7465static int set_ro(const char *val, struct kernel_param *kp)
7466{
7467 char *e;
7468 int num = simple_strtoul(val, &e, 10);
7469 if (*val && (*e == '\0' || *e == '\n')) {
7470 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007471 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007472 }
7473 return -EINVAL;
7474}
7475
NeilBrown80ca3a42006-07-10 04:44:18 -07007476module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7477module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08007478
NeilBrownefeb53c2009-01-09 08:31:10 +11007479module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007480
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481EXPORT_SYMBOL(register_md_personality);
7482EXPORT_SYMBOL(unregister_md_personality);
7483EXPORT_SYMBOL(md_error);
7484EXPORT_SYMBOL(md_done_sync);
7485EXPORT_SYMBOL(md_write_start);
7486EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487EXPORT_SYMBOL(md_register_thread);
7488EXPORT_SYMBOL(md_unregister_thread);
7489EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490EXPORT_SYMBOL(md_check_recovery);
7491MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007492MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007493MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007494MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);