blob: d5ad7723b1727ea0609241ffd77b9b99ac5a009a [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
NeilBrown8f5f02c2011-02-16 13:58:51 +1100556 if (unit && MAJOR(unit) != MD_MAJOR)
557 unit &= ~((1<<MdpMinorShift)-1);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 retry:
560 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
NeilBrownefeb53c2009-01-09 08:31:10 +1100562 if (unit) {
563 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
564 if (mddev->unit == unit) {
565 mddev_get(mddev);
566 spin_unlock(&all_mddevs_lock);
567 kfree(new);
568 return mddev;
569 }
570
571 if (new) {
572 list_add(&new->all_mddevs, &all_mddevs);
573 spin_unlock(&all_mddevs_lock);
574 new->hold_active = UNTIL_IOCTL;
575 return new;
576 }
577 } else if (new) {
578 /* find an unused unit number */
579 static int next_minor = 512;
580 int start = next_minor;
581 int is_free = 0;
582 int dev = 0;
583 while (!is_free) {
584 dev = MKDEV(MD_MAJOR, next_minor);
585 next_minor++;
586 if (next_minor > MINORMASK)
587 next_minor = 0;
588 if (next_minor == start) {
589 /* Oh dear, all in use. */
590 spin_unlock(&all_mddevs_lock);
591 kfree(new);
592 return NULL;
593 }
594
595 is_free = 1;
596 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
597 if (mddev->unit == dev) {
598 is_free = 0;
599 break;
600 }
601 }
602 new->unit = dev;
603 new->md_minor = MINOR(dev);
604 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 list_add(&new->all_mddevs, &all_mddevs);
606 spin_unlock(&all_mddevs_lock);
607 return new;
608 }
609 spin_unlock(&all_mddevs_lock);
610
NeilBrown9ffae0c2006-01-06 00:20:32 -0800611 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!new)
613 return NULL;
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 new->unit = unit;
616 if (MAJOR(unit) == MD_MAJOR)
617 new->md_minor = MINOR(unit);
618 else
619 new->md_minor = MINOR(unit) >> MdpMinorShift;
620
NeilBrownfafd7fb2010-04-01 15:55:30 +1100621 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto retry;
624}
625
626static inline int mddev_lock(mddev_t * mddev)
627{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800628 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Dan Williamsb522adc2009-03-31 15:00:31 +1100631static inline int mddev_is_locked(mddev_t *mddev)
632{
633 return mutex_is_locked(&mddev->reconfig_mutex);
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636static inline int mddev_trylock(mddev_t * mddev)
637{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800638 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
NeilBrownb6eb1272010-04-15 10:13:47 +1000641static struct attribute_group md_redundancy_group;
642
NeilBrowna64c8762010-04-14 17:15:37 +1000643static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
NeilBrowna64c8762010-04-14 17:15:37 +1000645 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000646 /* These cannot be removed under reconfig_mutex as
647 * an access to the files will try to take reconfig_mutex
648 * while holding the file unremovable, which leads to
649 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000650 * So hold set sysfs_active while the remove in happeing,
651 * and anything else which might set ->to_remove or my
652 * otherwise change the sysfs namespace will fail with
653 * -EBUSY if sysfs_active is still set.
654 * We set sysfs_active under reconfig_mutex and elsewhere
655 * test it under the same mutex to ensure its correct value
656 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000657 */
NeilBrowna64c8762010-04-14 17:15:37 +1000658 struct attribute_group *to_remove = mddev->to_remove;
659 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000660 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000661 mutex_unlock(&mddev->reconfig_mutex);
662
NeilBrown00bcb4a2010-06-01 19:37:23 +1000663 if (mddev->kobj.sd) {
664 if (to_remove != &md_redundancy_group)
665 sysfs_remove_group(&mddev->kobj, to_remove);
666 if (mddev->pers == NULL ||
667 mddev->pers->sync_request == NULL) {
668 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
669 if (mddev->sysfs_action)
670 sysfs_put(mddev->sysfs_action);
671 mddev->sysfs_action = NULL;
672 }
NeilBrowna64c8762010-04-14 17:15:37 +1000673 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000674 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000675 } else
676 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
NeilBrown005eca52005-08-22 13:11:08 -0700678 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
NeilBrown2989ddb2006-01-06 00:20:43 -0800681static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100683 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100685 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (rdev->desc_nr == nr)
687 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return NULL;
690}
691
692static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 mdk_rdev_t *rdev;
695
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100696 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (rdev->bdev->bd_dev == dev)
698 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return NULL;
701}
702
NeilBrownd9d166c2006-01-06 00:20:51 -0800703static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800704{
705 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800706 list_for_each_entry(pers, &pers_list, list) {
707 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800708 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800709 if (strcmp(pers->name, clevel)==0)
710 return pers;
711 }
NeilBrown2604b702006-01-06 00:20:36 -0800712 return NULL;
713}
714
Andre Nollb73df2d2008-07-11 22:02:23 +1000715/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100716static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100718 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000719 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722static int alloc_disk_sb(mdk_rdev_t * rdev)
723{
724 if (rdev->sb_page)
725 MD_BUG();
726
727 rdev->sb_page = alloc_page(GFP_KERNEL);
728 if (!rdev->sb_page) {
729 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000730 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
733 return 0;
734}
735
736static void free_disk_sb(mdk_rdev_t * rdev)
737{
738 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800739 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 rdev->sb_loaded = 0;
741 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000742 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100743 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745}
746
747
NeilBrown6712ecf2007-09-27 12:47:43 +0200748static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700749{
750 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800751 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700752
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700753 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
754 printk("md: super_written gets error=%d, uptodate=%d\n",
755 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
756 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800757 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700758 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700759
NeilBrowna9701a32005-11-08 21:39:34 -0800760 if (atomic_dec_and_test(&mddev->pending_writes))
761 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700762 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700763}
764
765void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
766 sector_t sector, int size, struct page *page)
767{
768 /* write first size bytes of page to sector of rdev
769 * Increment mddev->pending_writes before returning
770 * and decrement it on completion, waking up sb_wait
771 * if zero is reached.
772 * If an error occurred, call md_error
773 */
NeilBrowna167f662010-10-26 18:31:13 +1100774 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700775
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100776 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700777 bio->bi_sector = sector;
778 bio_add_page(bio, page, size, 0);
779 bio->bi_private = rdev;
780 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800781
NeilBrown7bfa19f2005-06-21 17:17:28 -0700782 atomic_inc(&mddev->pending_writes);
Tejun Heoe9c74692010-09-03 11:56:18 +0200783 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA,
784 bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800785}
786
787void md_super_wait(mddev_t *mddev)
788{
Tejun Heoe9c74692010-09-03 11:56:18 +0200789 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800790 DEFINE_WAIT(wq);
791 for(;;) {
792 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
793 if (atomic_read(&mddev->pending_writes)==0)
794 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800795 schedule();
796 }
797 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700798}
799
NeilBrown6712ecf2007-09-27 12:47:43 +0200800static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
NeilBrown2b193362010-10-27 15:16:40 +1100805int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100806 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
NeilBrowna167f662010-10-26 18:31:13 +1100808 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct completion event;
810 int ret;
811
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200812 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100814 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
815 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100816 if (metadata_op)
817 bio->bi_sector = sector + rdev->sb_start;
818 else
819 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 bio_add_page(bio, page, size, 0);
821 init_completion(&event);
822 bio->bi_private = &event;
823 bio->bi_end_io = bi_complete;
824 submit_bio(rw, bio);
825 wait_for_completion(&event);
826
827 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
828 bio_put(bio);
829 return ret;
830}
NeilBrowna8745db2006-01-06 00:20:34 -0800831EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
NeilBrown0002b272005-09-09 16:23:53 -0700833static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 char b[BDEVNAME_SIZE];
836 if (!rdev->sb_page) {
837 MD_BUG();
838 return -EINVAL;
839 }
840 if (rdev->sb_loaded)
841 return 0;
842
843
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100844 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto fail;
846 rdev->sb_loaded = 1;
847 return 0;
848
849fail:
850 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
851 bdevname(rdev->bdev,b));
852 return -EINVAL;
853}
854
855static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
856{
Andre Noll05710462008-07-11 22:02:20 +1000857 return sb1->set_uuid0 == sb2->set_uuid0 &&
858 sb1->set_uuid1 == sb2->set_uuid1 &&
859 sb1->set_uuid2 == sb2->set_uuid2 &&
860 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
864{
865 int ret;
866 mdp_super_t *tmp1, *tmp2;
867
868 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
869 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
870
871 if (!tmp1 || !tmp2) {
872 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100873 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 goto abort;
875 }
876
877 *tmp1 = *sb1;
878 *tmp2 = *sb2;
879
880 /*
881 * nr_disks is not constant
882 */
883 tmp1->nr_disks = 0;
884 tmp2->nr_disks = 0;
885
Andre Nollce0c8e02008-07-11 22:02:20 +1000886 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700888 kfree(tmp1);
889 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return ret;
891}
892
NeilBrown4d167f02007-05-09 02:35:37 -0700893
894static u32 md_csum_fold(u32 csum)
895{
896 csum = (csum & 0xffff) + (csum >> 16);
897 return (csum & 0xffff) + (csum >> 16);
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static unsigned int calc_sb_csum(mdp_super_t * sb)
901{
NeilBrown4d167f02007-05-09 02:35:37 -0700902 u64 newcsum = 0;
903 u32 *sb32 = (u32*)sb;
904 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned int disk_csum, csum;
906
907 disk_csum = sb->sb_csum;
908 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700909
910 for (i = 0; i < MD_SB_BYTES/4 ; i++)
911 newcsum += sb32[i];
912 csum = (newcsum & 0xffffffff) + (newcsum>>32);
913
914
915#ifdef CONFIG_ALPHA
916 /* This used to use csum_partial, which was wrong for several
917 * reasons including that different results are returned on
918 * different architectures. It isn't critical that we get exactly
919 * the same return value as before (we always csum_fold before
920 * testing, and that removes any differences). However as we
921 * know that csum_partial always returned a 16bit value on
922 * alphas, do a fold to maximise conformity to previous behaviour.
923 */
924 sb->sb_csum = md_csum_fold(disk_csum);
925#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return csum;
929}
930
931
932/*
933 * Handle superblock details.
934 * We want to be able to handle multiple superblock formats
935 * so we have a common interface to them all, and an array of
936 * different handlers.
937 * We rely on user-space to write the initial superblock, and support
938 * reading and updating of superblocks.
939 * Interface methods are:
940 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
941 * loads and validates a superblock on dev.
942 * if refdev != NULL, compare superblocks on both devices
943 * Return:
944 * 0 - dev has a superblock that is compatible with refdev
945 * 1 - dev has a superblock that is compatible and newer than refdev
946 * so dev should be used as the refdev in future
947 * -EINVAL superblock incompatible or invalid
948 * -othererror e.g. -EIO
949 *
950 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
951 * Verify that dev is acceptable into mddev.
952 * The first time, mddev->raid_disks will be 0, and data from
953 * dev should be merged in. Subsequent calls check that dev
954 * is new enough. Return 0 or -EINVAL
955 *
956 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
957 * Update the superblock for rdev with data in mddev
958 * This does not write to disc.
959 *
960 */
961
962struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000963 char *name;
964 struct module *owner;
965 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
966 int minor_version);
967 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
968 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
969 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000970 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971};
972
973/*
Andre Noll0894cc32009-06-18 08:49:23 +1000974 * Check that the given mddev has no bitmap.
975 *
976 * This function is called from the run method of all personalities that do not
977 * support bitmaps. It prints an error message and returns non-zero if mddev
978 * has a bitmap. Otherwise, it returns 0.
979 *
980 */
981int md_check_no_bitmap(mddev_t *mddev)
982{
NeilBrownc3d97142009-12-14 12:49:52 +1100983 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000984 return 0;
985 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
986 mdname(mddev), mddev->pers->name);
987 return 1;
988}
989EXPORT_SYMBOL(md_check_no_bitmap);
990
991/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * load_super for 0.90.0
993 */
994static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
995{
996 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
997 mdp_super_t *sb;
998 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /*
Andre Noll0f420352008-07-11 22:02:23 +10001001 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * it's at the end of the disk.
1003 *
1004 * It also happens to be a multiple of 4Kb.
1005 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001006 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
NeilBrown0002b272005-09-09 16:23:53 -07001008 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (ret) return ret;
1010
1011 ret = -EINVAL;
1012
1013 bdevname(rdev->bdev, b);
1014 sb = (mdp_super_t*)page_address(rdev->sb_page);
1015
1016 if (sb->md_magic != MD_SB_MAGIC) {
1017 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1018 b);
1019 goto abort;
1020 }
1021
1022 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001023 sb->minor_version < 90 ||
1024 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1026 sb->major_version, sb->minor_version,
1027 b);
1028 goto abort;
1029 }
1030
1031 if (sb->raid_disks <= 0)
1032 goto abort;
1033
NeilBrown4d167f02007-05-09 02:35:37 -07001034 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1036 b);
1037 goto abort;
1038 }
1039
1040 rdev->preferred_minor = sb->md_minor;
1041 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001042 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (sb->level == LEVEL_MULTIPATH)
1045 rdev->desc_nr = -1;
1046 else
1047 rdev->desc_nr = sb->this_disk.number;
1048
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001049 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001051 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 __u64 ev1, ev2;
1053 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1054 if (!uuid_equal(refsb, sb)) {
1055 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1056 b, bdevname(refdev->bdev,b2));
1057 goto abort;
1058 }
1059 if (!sb_equal(refsb, sb)) {
1060 printk(KERN_WARNING "md: %s has same UUID"
1061 " but different superblock to %s\n",
1062 b, bdevname(refdev->bdev, b2));
1063 goto abort;
1064 }
1065 ev1 = md_event(sb);
1066 ev2 = md_event(refsb);
1067 if (ev1 > ev2)
1068 ret = 1;
1069 else
1070 ret = 0;
1071 }
NeilBrown8190e752009-06-18 08:48:58 +10001072 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Andre Nolldd8ac332009-03-31 14:33:13 +11001074 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001075 /* "this cannot possibly happen" ... */
1076 ret = -EINVAL;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 abort:
1079 return ret;
1080}
1081
1082/*
1083 * validate_super for 0.90.0
1084 */
1085static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1086{
1087 mdp_disk_t *desc;
1088 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001089 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
NeilBrown41158c72005-06-21 17:17:25 -07001091 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001092 clear_bit(Faulty, &rdev->flags);
1093 clear_bit(In_sync, &rdev->flags);
1094 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (mddev->raid_disks == 0) {
1097 mddev->major_version = 0;
1098 mddev->minor_version = sb->minor_version;
1099 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001100 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001101 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 mddev->ctime = sb->ctime;
1103 mddev->utime = sb->utime;
1104 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001105 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 mddev->layout = sb->layout;
1107 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001108 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001109 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001110 mddev->bitmap_info.offset = 0;
1111 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
NeilBrownf6705572006-03-27 01:18:11 -08001113 if (mddev->minor_version >= 91) {
1114 mddev->reshape_position = sb->reshape_position;
1115 mddev->delta_disks = sb->delta_disks;
1116 mddev->new_level = sb->new_level;
1117 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001118 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001119 } else {
1120 mddev->reshape_position = MaxSector;
1121 mddev->delta_disks = 0;
1122 mddev->new_level = mddev->level;
1123 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001124 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001125 }
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (sb->state & (1<<MD_SB_CLEAN))
1128 mddev->recovery_cp = MaxSector;
1129 else {
1130 if (sb->events_hi == sb->cp_events_hi &&
1131 sb->events_lo == sb->cp_events_lo) {
1132 mddev->recovery_cp = sb->recovery_cp;
1133 } else
1134 mddev->recovery_cp = 0;
1135 }
1136
1137 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1138 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1139 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1140 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1141
1142 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001143
1144 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001145 mddev->bitmap_info.file == NULL)
1146 mddev->bitmap_info.offset =
1147 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001148
NeilBrown41158c72005-06-21 17:17:25 -07001149 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001150 /* Insist on good event counter while assembling, except
1151 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001153 if (sb->disks[rdev->desc_nr].state & (
1154 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1155 if (ev1 < mddev->events)
1156 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001157 } else if (mddev->bitmap) {
1158 /* if adding to array with a bitmap, then we can accept an
1159 * older device ... but not too old.
1160 */
NeilBrown41158c72005-06-21 17:17:25 -07001161 if (ev1 < mddev->bitmap->events_cleared)
1162 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001163 } else {
1164 if (ev1 < mddev->events)
1165 /* just a hot-add of a new device, leave raid_disk at -1 */
1166 return 0;
1167 }
NeilBrown41158c72005-06-21 17:17:25 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 desc = sb->disks + rdev->desc_nr;
1171
1172 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001173 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001174 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1175 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001176 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001178 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1179 /* active but not in sync implies recovery up to
1180 * reshape position. We don't know exactly where
1181 * that is, so set to zero for now */
1182 if (mddev->minor_version >= 91) {
1183 rdev->recovery_offset = 0;
1184 rdev->raid_disk = desc->raid_disk;
1185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001187 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1188 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001189 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001190 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192}
1193
1194/*
1195 * sync_super for 0.90.0
1196 */
1197static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1198{
1199 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 mdk_rdev_t *rdev2;
1201 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /* make rdev->sb match mddev data..
1205 *
1206 * 1/ zero out disks
1207 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1208 * 3/ any empty disks < next_spare become removed
1209 *
1210 * disks[0] gets initialised to REMOVED because
1211 * we cannot be sure from other fields if it has
1212 * been initialised or not.
1213 */
1214 int i;
1215 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1216
NeilBrown61181562005-09-09 16:24:02 -07001217 rdev->sb_size = MD_SB_BYTES;
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 sb = (mdp_super_t*)page_address(rdev->sb_page);
1220
1221 memset(sb, 0, sizeof(*sb));
1222
1223 sb->md_magic = MD_SB_MAGIC;
1224 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 sb->patch_version = mddev->patch_version;
1226 sb->gvalid_words = 0; /* ignored */
1227 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1228 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1229 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1230 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1231
1232 sb->ctime = mddev->ctime;
1233 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001234 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 sb->raid_disks = mddev->raid_disks;
1236 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001237 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 sb->utime = mddev->utime;
1239 sb->state = 0;
1240 sb->events_hi = (mddev->events>>32);
1241 sb->events_lo = (u32)mddev->events;
1242
NeilBrownf6705572006-03-27 01:18:11 -08001243 if (mddev->reshape_position == MaxSector)
1244 sb->minor_version = 90;
1245 else {
1246 sb->minor_version = 91;
1247 sb->reshape_position = mddev->reshape_position;
1248 sb->new_level = mddev->new_level;
1249 sb->delta_disks = mddev->delta_disks;
1250 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001251 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001252 }
1253 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (mddev->in_sync)
1255 {
1256 sb->recovery_cp = mddev->recovery_cp;
1257 sb->cp_events_hi = (mddev->events>>32);
1258 sb->cp_events_lo = (u32)mddev->events;
1259 if (mddev->recovery_cp == MaxSector)
1260 sb->state = (1<< MD_SB_CLEAN);
1261 } else
1262 sb->recovery_cp = 0;
1263
1264 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001265 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
NeilBrownc3d97142009-12-14 12:49:52 +11001267 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001268 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001271 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001273 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001274 int is_active = test_bit(In_sync, &rdev2->flags);
1275
1276 if (rdev2->raid_disk >= 0 &&
1277 sb->minor_version >= 91)
1278 /* we have nowhere to store the recovery_offset,
1279 * but if it is not below the reshape_position,
1280 * we can piggy-back on that.
1281 */
1282 is_active = 1;
1283 if (rdev2->raid_disk < 0 ||
1284 test_bit(Faulty, &rdev2->flags))
1285 is_active = 0;
1286 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001287 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001289 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001290 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 d = &sb->disks[rdev2->desc_nr];
1292 nr_disks++;
1293 d->number = rdev2->desc_nr;
1294 d->major = MAJOR(rdev2->bdev->bd_dev);
1295 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001296 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 d->raid_disk = rdev2->raid_disk;
1298 else
1299 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001300 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001302 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001304 if (test_bit(In_sync, &rdev2->flags))
1305 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 active++;
1307 working++;
1308 } else {
1309 d->state = 0;
1310 spare++;
1311 working++;
1312 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001313 if (test_bit(WriteMostly, &rdev2->flags))
1314 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* now set the "removed" and "faulty" bits on any missing devices */
1317 for (i=0 ; i < mddev->raid_disks ; i++) {
1318 mdp_disk_t *d = &sb->disks[i];
1319 if (d->state == 0 && d->number == 0) {
1320 d->number = i;
1321 d->raid_disk = i;
1322 d->state = (1<<MD_DISK_REMOVED);
1323 d->state |= (1<<MD_DISK_FAULTY);
1324 failed++;
1325 }
1326 }
1327 sb->nr_disks = nr_disks;
1328 sb->active_disks = active;
1329 sb->working_disks = working;
1330 sb->failed_disks = failed;
1331 sb->spare_disks = spare;
1332
1333 sb->this_disk = sb->disks[rdev->desc_nr];
1334 sb->sb_csum = calc_sb_csum(sb);
1335}
1336
1337/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338 * rdev_size_change for 0.90.0
1339 */
1340static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001341super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342{
Andre Noll58c0fed2009-03-31 14:33:13 +11001343 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001344 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001345 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001346 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001347 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001348 if (!num_sectors || num_sectors > rdev->sb_start)
1349 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001350 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001351 rdev->sb_page);
1352 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001353 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001354}
1355
1356
1357/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * version 1 superblock
1359 */
1360
NeilBrown1c05b4b2006-10-21 10:24:08 -07001361static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001363 __le32 disk_csum;
1364 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 unsigned long long newcsum;
1366 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001367 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int i;
1369
1370 disk_csum = sb->sb_csum;
1371 sb->sb_csum = 0;
1372 newcsum = 0;
1373 for (i=0; size>=4; size -= 4 )
1374 newcsum += le32_to_cpu(*isuper++);
1375
1376 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001377 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1380 sb->sb_csum = disk_csum;
1381 return cpu_to_le32(csum);
1382}
1383
1384static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1385{
1386 struct mdp_superblock_1 *sb;
1387 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001388 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001390 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
Andre Noll0f420352008-07-11 22:02:23 +10001393 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * It is always aligned to a 4K boundary and
1395 * depeding on minor_version, it can be:
1396 * 0: At least 8K, but less than 12K, from end of device
1397 * 1: At start of device
1398 * 2: 4K from start of device.
1399 */
1400 switch(minor_version) {
1401 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001402 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001403 sb_start -= 8*2;
1404 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 break;
1406 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001407 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 break;
1409 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001410 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412 default:
1413 return -EINVAL;
1414 }
Andre Noll0f420352008-07-11 22:02:23 +10001415 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
NeilBrown0002b272005-09-09 16:23:53 -07001417 /* superblock is rarely larger than 1K, but it can be larger,
1418 * and it is safe to read 4k, so we do that
1419 */
1420 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (ret) return ret;
1422
1423
1424 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1425
1426 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1427 sb->major_version != cpu_to_le32(1) ||
1428 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001429 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001430 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return -EINVAL;
1432
1433 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1434 printk("md: invalid superblock checksum on %s\n",
1435 bdevname(rdev->bdev,b));
1436 return -EINVAL;
1437 }
1438 if (le64_to_cpu(sb->data_size) < 10) {
1439 printk("md: data_size too small on %s\n",
1440 bdevname(rdev->bdev,b));
1441 return -EINVAL;
1442 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 rdev->preferred_minor = 0xffff;
1445 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001446 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
NeilBrown0002b272005-09-09 16:23:53 -07001448 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001449 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001450 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001451 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1452
1453 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001454 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001455 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001456
NeilBrown31b65a02006-07-10 04:44:14 -07001457 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1458 rdev->desc_nr = -1;
1459 else
1460 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1461
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001462 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001463 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001464 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 __u64 ev1, ev2;
1466 struct mdp_superblock_1 *refsb =
1467 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1468
1469 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1470 sb->level != refsb->level ||
1471 sb->layout != refsb->layout ||
1472 sb->chunksize != refsb->chunksize) {
1473 printk(KERN_WARNING "md: %s has strangely different"
1474 " superblock to %s\n",
1475 bdevname(rdev->bdev,b),
1476 bdevname(refdev->bdev,b2));
1477 return -EINVAL;
1478 }
1479 ev1 = le64_to_cpu(sb->events);
1480 ev2 = le64_to_cpu(refsb->events);
1481
1482 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001483 ret = 1;
1484 else
1485 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
NeilBrowna1801f82008-03-04 14:29:31 -08001487 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001488 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001489 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001491 rdev->sectors = rdev->sb_start;
1492 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001494 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001495 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001496 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001497 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1501{
1502 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001503 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
NeilBrown41158c72005-06-21 17:17:25 -07001505 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001506 clear_bit(Faulty, &rdev->flags);
1507 clear_bit(In_sync, &rdev->flags);
1508 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (mddev->raid_disks == 0) {
1511 mddev->major_version = 1;
1512 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001513 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001514 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1516 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1517 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001518 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 mddev->layout = le32_to_cpu(sb->layout);
1520 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001521 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001522 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001523 mddev->bitmap_info.offset = 0;
1524 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1527 memcpy(mddev->uuid, sb->set_uuid, 16);
1528
1529 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001530
NeilBrown71c08052005-09-09 16:23:51 -07001531 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001532 mddev->bitmap_info.file == NULL )
1533 mddev->bitmap_info.offset =
1534 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001535
NeilBrownf6705572006-03-27 01:18:11 -08001536 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1537 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1538 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1539 mddev->new_level = le32_to_cpu(sb->new_level);
1540 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001541 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001542 } else {
1543 mddev->reshape_position = MaxSector;
1544 mddev->delta_disks = 0;
1545 mddev->new_level = mddev->level;
1546 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001547 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001548 }
1549
NeilBrown41158c72005-06-21 17:17:25 -07001550 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001551 /* Insist of good event counter while assembling, except for
1552 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001554 if (rdev->desc_nr >= 0 &&
1555 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1556 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1557 if (ev1 < mddev->events)
1558 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001559 } else if (mddev->bitmap) {
1560 /* If adding to array with a bitmap, then we can accept an
1561 * older device, but not too old.
1562 */
NeilBrown41158c72005-06-21 17:17:25 -07001563 if (ev1 < mddev->bitmap->events_cleared)
1564 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001565 } else {
1566 if (ev1 < mddev->events)
1567 /* just a hot-add of a new device, leave raid_disk at -1 */
1568 return 0;
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (mddev->level != LEVEL_MULTIPATH) {
1571 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001572 if (rdev->desc_nr < 0 ||
1573 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1574 role = 0xffff;
1575 rdev->desc_nr = -1;
1576 } else
1577 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 switch(role) {
1579 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 break;
1581 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001582 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 break;
1584 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001585 if ((le32_to_cpu(sb->feature_map) &
1586 MD_FEATURE_RECOVERY_OFFSET))
1587 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1588 else
1589 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 rdev->raid_disk = role;
1591 break;
1592 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001593 if (sb->devflags & WriteMostly1)
1594 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001595 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001596 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return 0;
1599}
1600
1601static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1602{
1603 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 mdk_rdev_t *rdev2;
1605 int max_dev, i;
1606 /* make rdev->sb match mddev and rdev data. */
1607
1608 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1609
1610 sb->feature_map = 0;
1611 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001612 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 memset(sb->pad1, 0, sizeof(sb->pad1));
1614 memset(sb->pad2, 0, sizeof(sb->pad2));
1615 memset(sb->pad3, 0, sizeof(sb->pad3));
1616
1617 sb->utime = cpu_to_le64((__u64)mddev->utime);
1618 sb->events = cpu_to_le64(mddev->events);
1619 if (mddev->in_sync)
1620 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1621 else
1622 sb->resync_offset = cpu_to_le64(0);
1623
NeilBrown1c05b4b2006-10-21 10:24:08 -07001624 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001625
NeilBrownf0ca3402006-02-02 14:28:04 -08001626 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001627 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001628 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001629 sb->level = cpu_to_le32(mddev->level);
1630 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001631
NeilBrownc3d97142009-12-14 12:49:52 +11001632 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1633 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001634 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001635 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001636
1637 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001638 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001639 sb->feature_map |=
1640 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1641 sb->recovery_offset =
1642 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001643 }
1644
NeilBrownf6705572006-03-27 01:18:11 -08001645 if (mddev->reshape_position != MaxSector) {
1646 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1647 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1648 sb->new_layout = cpu_to_le32(mddev->new_layout);
1649 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1650 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001651 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001652 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001655 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (rdev2->desc_nr+1 > max_dev)
1657 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001658
NeilBrown70471da2009-08-03 10:59:57 +10001659 if (max_dev > le32_to_cpu(sb->max_dev)) {
1660 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001661 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001662 rdev->sb_size = max_dev * 2 + 256;
1663 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1664 if (rdev->sb_size & bmask)
1665 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001666 } else
1667 max_dev = le32_to_cpu(sb->max_dev);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 for (i=0; i<max_dev;i++)
1670 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1671
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001672 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001674 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001676 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001678 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001679 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 else
1681 sb->dev_roles[i] = cpu_to_le16(0xffff);
1682 }
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 sb->sb_csum = calc_sb_1_csum(sb);
1685}
1686
Chris Webb0cd17fe2008-06-28 08:31:46 +10001687static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001688super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001689{
1690 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001691 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001692 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001693 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001694 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001695 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001696 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001697 max_sectors -= rdev->data_offset;
1698 if (!num_sectors || num_sectors > max_sectors)
1699 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001700 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001701 /* minor version 0 with bitmap we can't move */
1702 return 0;
1703 } else {
1704 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001705 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001706 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001707 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001708 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001709 if (!num_sectors || num_sectors > max_sectors)
1710 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001711 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001712 }
1713 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001714 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001715 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001716 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001717 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001718 rdev->sb_page);
1719 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001720 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Adrian Bunk75c96f82005-05-05 16:16:09 -07001723static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 [0] = {
1725 .name = "0.90.0",
1726 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001727 .load_super = super_90_load,
1728 .validate_super = super_90_validate,
1729 .sync_super = super_90_sync,
1730 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 },
1732 [1] = {
1733 .name = "md-1",
1734 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001735 .load_super = super_1_load,
1736 .validate_super = super_1_validate,
1737 .sync_super = super_1_sync,
1738 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 },
1740};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1743{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001744 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
NeilBrown4b809912008-07-21 17:05:25 +10001746 rcu_read_lock();
1747 rdev_for_each_rcu(rdev, mddev1)
1748 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001749 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001750 rdev2->bdev->bd_contains) {
1751 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001752 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001753 }
1754 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return 0;
1756}
1757
1758static LIST_HEAD(pending_raid_disks);
1759
Andre Nollac5e7112009-08-03 10:59:47 +10001760/*
1761 * Try to register data integrity profile for an mddev
1762 *
1763 * This is called when an array is started and after a disk has been kicked
1764 * from the array. It only succeeds if all working and active component devices
1765 * are integrity capable with matching profiles.
1766 */
1767int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001768{
Andre Nollac5e7112009-08-03 10:59:47 +10001769 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001770
Andre Nollac5e7112009-08-03 10:59:47 +10001771 if (list_empty(&mddev->disks))
1772 return 0; /* nothing to do */
1773 if (blk_get_integrity(mddev->gendisk))
1774 return 0; /* already registered */
1775 list_for_each_entry(rdev, &mddev->disks, same_set) {
1776 /* skip spares and non-functional disks */
1777 if (test_bit(Faulty, &rdev->flags))
1778 continue;
1779 if (rdev->raid_disk < 0)
1780 continue;
1781 /*
1782 * If at least one rdev is not integrity capable, we can not
1783 * enable data integrity for the md device.
1784 */
1785 if (!bdev_get_integrity(rdev->bdev))
1786 return -EINVAL;
1787 if (!reference) {
1788 /* Use the first rdev as the reference */
1789 reference = rdev;
1790 continue;
1791 }
1792 /* does this rdev's profile match the reference profile? */
1793 if (blk_integrity_compare(reference->bdev->bd_disk,
1794 rdev->bdev->bd_disk) < 0)
1795 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001796 }
Andre Nollac5e7112009-08-03 10:59:47 +10001797 /*
1798 * All component devices are integrity capable and have matching
1799 * profiles, register the common profile for the md device.
1800 */
1801 if (blk_integrity_register(mddev->gendisk,
1802 bdev_get_integrity(reference->bdev)) != 0) {
1803 printk(KERN_ERR "md: failed to register integrity for %s\n",
1804 mdname(mddev));
1805 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001806 }
Andre Nollac5e7112009-08-03 10:59:47 +10001807 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1808 mdname(mddev));
1809 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001810}
Andre Nollac5e7112009-08-03 10:59:47 +10001811EXPORT_SYMBOL(md_integrity_register);
1812
1813/* Disable data integrity if non-capable/non-matching disk is being added */
1814void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1815{
1816 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1817 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1818
1819 if (!bi_mddev) /* nothing to do */
1820 return;
1821 if (rdev->raid_disk < 0) /* skip spares */
1822 return;
1823 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1824 rdev->bdev->bd_disk) >= 0)
1825 return;
1826 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1827 blk_integrity_unregister(mddev->gendisk);
1828}
1829EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1832{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001833 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001834 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001835 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001836 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 if (rdev->mddev) {
1839 MD_BUG();
1840 return -EINVAL;
1841 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001842
1843 /* prevent duplicates */
1844 if (find_rdev(mddev, rdev->bdev->bd_dev))
1845 return -EEXIST;
1846
Andre Nolldd8ac332009-03-31 14:33:13 +11001847 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1848 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1849 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001850 if (mddev->pers) {
1851 /* Cannot change size, so fail
1852 * If mddev->level <= 0, then we don't care
1853 * about aligning sizes (e.g. linear)
1854 */
1855 if (mddev->level > 0)
1856 return -ENOSPC;
1857 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001858 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* Verify rdev->desc_nr is unique.
1862 * If it is -1, assign a free number, else
1863 * check number is not in use
1864 */
1865 if (rdev->desc_nr < 0) {
1866 int choice = 0;
1867 if (mddev->pers) choice = mddev->raid_disks;
1868 while (find_rdev_nr(mddev, choice))
1869 choice++;
1870 rdev->desc_nr = choice;
1871 } else {
1872 if (find_rdev_nr(mddev, rdev->desc_nr))
1873 return -EBUSY;
1874 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001875 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1876 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1877 mdname(mddev), mddev->max_disks);
1878 return -EBUSY;
1879 }
NeilBrown19133a42005-11-08 21:39:35 -08001880 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001881 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001882 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001885 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001886
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001887 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001888 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001889
Tejun Heo0762b8b2008-08-25 19:56:12 +09001890 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001891 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1892 /* failure here is OK */;
1893 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001894
NeilBrown4b809912008-07-21 17:05:25 +10001895 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01001896 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001897
1898 /* May as well allow recovery to be retried once */
1899 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001902
1903 fail:
1904 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1905 b, mdname(mddev));
1906 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
NeilBrown177a99b2008-02-06 01:39:56 -08001909static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001910{
1911 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1912 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001913 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1917{
1918 char b[BDEVNAME_SIZE];
1919 if (!rdev->mddev) {
1920 MD_BUG();
1921 return;
1922 }
Tejun Heo49731ba2011-01-14 18:43:57 +01001923 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001924 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1926 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001927 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001928 sysfs_put(rdev->sysfs_state);
1929 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001930 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001931 * writing to 'remove' to "dev/state". We also need
1932 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001933 */
NeilBrown4b809912008-07-21 17:05:25 +10001934 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001935 INIT_WORK(&rdev->del_work, md_delayed_delete);
1936 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001937 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
1940/*
1941 * prevent the device from being mounted, repartitioned or
1942 * otherwise reused by a RAID array (or any other kernel
1943 * subsystem), by bd_claiming the device.
1944 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001945static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 int err = 0;
1948 struct block_device *bdev;
1949 char b[BDEVNAME_SIZE];
1950
Tejun Heod4d77622010-11-13 11:55:18 +01001951 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1952 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (IS_ERR(bdev)) {
1954 printk(KERN_ERR "md: could not open %s.\n",
1955 __bdevname(dev, b));
1956 return PTR_ERR(bdev);
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 rdev->bdev = bdev;
1959 return err;
1960}
1961
1962static void unlock_rdev(mdk_rdev_t *rdev)
1963{
1964 struct block_device *bdev = rdev->bdev;
1965 rdev->bdev = NULL;
1966 if (!bdev)
1967 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01001968 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971void md_autodetect_dev(dev_t dev);
1972
1973static void export_rdev(mdk_rdev_t * rdev)
1974{
1975 char b[BDEVNAME_SIZE];
1976 printk(KERN_INFO "md: export_rdev(%s)\n",
1977 bdevname(rdev->bdev,b));
1978 if (rdev->mddev)
1979 MD_BUG();
1980 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001982 if (test_bit(AutoDetected, &rdev->flags))
1983 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984#endif
1985 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001986 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
1989static void kick_rdev_from_array(mdk_rdev_t * rdev)
1990{
1991 unbind_rdev_from_array(rdev);
1992 export_rdev(rdev);
1993}
1994
1995static void export_array(mddev_t *mddev)
1996{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001997 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
NeilBrownd089c6a2008-02-06 01:39:59 -08001999 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (!rdev->mddev) {
2001 MD_BUG();
2002 continue;
2003 }
2004 kick_rdev_from_array(rdev);
2005 }
2006 if (!list_empty(&mddev->disks))
2007 MD_BUG();
2008 mddev->raid_disks = 0;
2009 mddev->major_version = 0;
2010}
2011
2012static void print_desc(mdp_disk_t *desc)
2013{
2014 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2015 desc->major,desc->minor,desc->raid_disk,desc->state);
2016}
2017
Cheng Renquancd2ac932009-01-09 08:31:08 +11002018static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 int i;
2021
2022 printk(KERN_INFO
2023 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2024 sb->major_version, sb->minor_version, sb->patch_version,
2025 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2026 sb->ctime);
2027 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2028 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2029 sb->md_minor, sb->layout, sb->chunk_size);
2030 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2031 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2032 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2033 sb->failed_disks, sb->spare_disks,
2034 sb->sb_csum, (unsigned long)sb->events_lo);
2035
2036 printk(KERN_INFO);
2037 for (i = 0; i < MD_SB_DISKS; i++) {
2038 mdp_disk_t *desc;
2039
2040 desc = sb->disks + i;
2041 if (desc->number || desc->major || desc->minor ||
2042 desc->raid_disk || (desc->state && (desc->state != 4))) {
2043 printk(" D %2d: ", i);
2044 print_desc(desc);
2045 }
2046 }
2047 printk(KERN_INFO "md: THIS: ");
2048 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Cheng Renquancd2ac932009-01-09 08:31:08 +11002051static void print_sb_1(struct mdp_superblock_1 *sb)
2052{
2053 __u8 *uuid;
2054
2055 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002056 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002057 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002058 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002059 le32_to_cpu(sb->major_version),
2060 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002061 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002062 sb->set_name,
2063 (unsigned long long)le64_to_cpu(sb->ctime)
2064 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2065
2066 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002067 printk(KERN_INFO
2068 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002069 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002070 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002071 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2072 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002073 le32_to_cpu(sb->level),
2074 (unsigned long long)le64_to_cpu(sb->size),
2075 le32_to_cpu(sb->raid_disks),
2076 le32_to_cpu(sb->layout),
2077 le32_to_cpu(sb->chunksize),
2078 (unsigned long long)le64_to_cpu(sb->data_offset),
2079 (unsigned long long)le64_to_cpu(sb->data_size),
2080 (unsigned long long)le64_to_cpu(sb->super_offset),
2081 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2082 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002083 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002084 sb->devflags,
2085 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2086 (unsigned long long)le64_to_cpu(sb->events),
2087 (unsigned long long)le64_to_cpu(sb->resync_offset),
2088 le32_to_cpu(sb->sb_csum),
2089 le32_to_cpu(sb->max_dev)
2090 );
2091}
2092
2093static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002096 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2097 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002098 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2099 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002101 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2102 switch (major_version) {
2103 case 0:
2104 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2105 break;
2106 case 1:
2107 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2108 break;
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 } else
2111 printk(KERN_INFO "md: no rdev superblock!\n");
2112}
2113
Adrian Bunk5e563412006-06-26 00:27:42 -07002114static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002116 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 mdk_rdev_t *rdev;
2118 mddev_t *mddev;
2119 char b[BDEVNAME_SIZE];
2120
2121 printk("\n");
2122 printk("md: **********************************\n");
2123 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2124 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002125 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
NeilBrown32a76272005-06-21 17:17:14 -07002127 if (mddev->bitmap)
2128 bitmap_print_sb(mddev->bitmap);
2129 else
2130 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002131 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 printk("<%s>", bdevname(rdev->bdev,b));
2133 printk("\n");
2134
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002135 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002136 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138 printk("md: **********************************\n");
2139 printk("\n");
2140}
2141
2142
NeilBrown42543762006-06-26 00:27:57 -07002143static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
NeilBrown42543762006-06-26 00:27:57 -07002145 /* Update each superblock (in-memory image), but
2146 * if we are allowed to, skip spares which already
2147 * have the right event counter, or have one earlier
2148 * (which would mean they aren't being marked as dirty
2149 * with the rest of the array)
2150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002152 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002153 if (rdev->sb_events == mddev->events ||
2154 (nospares &&
2155 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002156 rdev->sb_events+1 == mddev->events)) {
2157 /* Don't update this superblock */
2158 rdev->sb_loaded = 2;
2159 } else {
2160 super_types[mddev->major_version].
2161 sync_super(mddev, rdev);
2162 rdev->sb_loaded = 1;
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165}
2166
NeilBrown850b2b42006-10-03 01:15:46 -07002167static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002170 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002171 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002174 /* First make sure individual recovery_offsets are correct */
2175 list_for_each_entry(rdev, &mddev->disks, same_set) {
2176 if (rdev->raid_disk >= 0 &&
2177 mddev->delta_disks >= 0 &&
2178 !test_bit(In_sync, &rdev->flags) &&
2179 mddev->curr_resync_completed > rdev->recovery_offset)
2180 rdev->recovery_offset = mddev->curr_resync_completed;
2181
2182 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002183 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002184 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002185 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002186 if (!mddev->external)
2187 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002188 wake_up(&mddev->sb_wait);
2189 return;
2190 }
2191
NeilBrowna9701a32005-11-08 21:39:34 -08002192 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002193
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002194 mddev->utime = get_seconds();
2195
NeilBrown850b2b42006-10-03 01:15:46 -07002196 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2197 force_change = 1;
2198 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2199 /* just a clean<-> dirty transition, possibly leave spares alone,
2200 * though if events isn't the right even/odd, we will have to do
2201 * spares after all
2202 */
2203 nospares = 1;
2204 if (force_change)
2205 nospares = 0;
2206 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002207 /* If the array is degraded, then skipping spares is both
2208 * dangerous and fairly pointless.
2209 * Dangerous because a device that was removed from the array
2210 * might have a event_count that still looks up-to-date,
2211 * so it can be re-added without a resync.
2212 * Pointless because if there are any spares to skip,
2213 * then a recovery will happen and soon that array won't
2214 * be degraded any more and the spare can go back to sleep then.
2215 */
NeilBrown850b2b42006-10-03 01:15:46 -07002216 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002217
NeilBrown06d91a52005-06-21 17:17:12 -07002218 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002219
2220 /* If this is just a dirty<->clean transition, and the array is clean
2221 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002222 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002223 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002224 && mddev->can_decrease_events
2225 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002226 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002227 mddev->can_decrease_events = 0;
2228 } else {
NeilBrown42543762006-06-26 00:27:57 -07002229 /* otherwise we have to go forward and ... */
2230 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002231 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 if (!mddev->events) {
2235 /*
2236 * oops, this 64-bit counter should never wrap.
2237 * Either we are in around ~1 trillion A.C., assuming
2238 * 1 reboot per second, or we have a bug:
2239 */
2240 MD_BUG();
2241 mddev->events --;
2242 }
NeilBrowne6910632008-02-06 01:39:51 -08002243 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002244 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 dprintk(KERN_INFO
2247 "md: updating %s RAID superblock on device (in sync %d)\n",
2248 mdname(mddev),mddev->in_sync);
2249
NeilBrown4ad13662007-07-17 04:06:13 -07002250 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002251 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 char b[BDEVNAME_SIZE];
2253 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002254 if (rdev->sb_loaded != 1)
2255 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002256 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 dprintk("(skipping faulty ");
2258
2259 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002260 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002261 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002262 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002263 rdev->sb_page);
2264 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2265 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002266 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002267 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 } else
2270 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002271 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /* only need to write one superblock... */
2273 break;
2274 }
NeilBrowna9701a32005-11-08 21:39:34 -08002275 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002276 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002277
NeilBrowna9701a32005-11-08 21:39:34 -08002278 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002279 if (mddev->in_sync != sync_req ||
2280 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002281 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002282 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002283 goto repeat;
2284 }
NeilBrown850b2b42006-10-03 01:15:46 -07002285 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002286 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002287 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002288 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2289 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
Andre Noll7f6ce762008-03-23 18:34:54 +01002293/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002294 * We want to accept with case. For this we use cmd_match.
2295 */
2296static int cmd_match(const char *cmd, const char *str)
2297{
2298 /* See if cmd, written into a sysfs file, matches
2299 * str. They must either be the same, or cmd can
2300 * have a trailing newline
2301 */
2302 while (*cmd && *str && *cmd == *str) {
2303 cmd++;
2304 str++;
2305 }
2306 if (*cmd == '\n')
2307 cmd++;
2308 if (*str || *cmd)
2309 return 0;
2310 return 1;
2311}
2312
NeilBrown86e6ffd2005-11-08 21:39:24 -08002313struct rdev_sysfs_entry {
2314 struct attribute attr;
2315 ssize_t (*show)(mdk_rdev_t *, char *);
2316 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2317};
2318
2319static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002320state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002321{
2322 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002323 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002324
NeilBrownb2d444d2005-11-08 21:39:31 -08002325 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002326 len+= sprintf(page+len, "%sfaulty",sep);
2327 sep = ",";
2328 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002329 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002330 len += sprintf(page+len, "%sin_sync",sep);
2331 sep = ",";
2332 }
NeilBrownf6556752006-06-26 00:28:01 -07002333 if (test_bit(WriteMostly, &rdev->flags)) {
2334 len += sprintf(page+len, "%swrite_mostly",sep);
2335 sep = ",";
2336 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002337 if (test_bit(Blocked, &rdev->flags)) {
2338 len += sprintf(page+len, "%sblocked", sep);
2339 sep = ",";
2340 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002341 if (!test_bit(Faulty, &rdev->flags) &&
2342 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002343 len += sprintf(page+len, "%sspare", sep);
2344 sep = ",";
2345 }
2346 return len+sprintf(page+len, "\n");
2347}
2348
NeilBrown45dc2de2006-06-26 00:27:58 -07002349static ssize_t
2350state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2351{
2352 /* can write
2353 * faulty - simulates and error
2354 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002355 * writemostly - sets write_mostly
2356 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002357 * blocked - sets the Blocked flag
2358 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002359 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002360 */
2361 int err = -EINVAL;
2362 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2363 md_error(rdev->mddev, rdev);
2364 err = 0;
2365 } else if (cmd_match(buf, "remove")) {
2366 if (rdev->raid_disk >= 0)
2367 err = -EBUSY;
2368 else {
2369 mddev_t *mddev = rdev->mddev;
2370 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002371 if (mddev->pers)
2372 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002373 md_new_event(mddev);
2374 err = 0;
2375 }
NeilBrownf6556752006-06-26 00:28:01 -07002376 } else if (cmd_match(buf, "writemostly")) {
2377 set_bit(WriteMostly, &rdev->flags);
2378 err = 0;
2379 } else if (cmd_match(buf, "-writemostly")) {
2380 clear_bit(WriteMostly, &rdev->flags);
2381 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002382 } else if (cmd_match(buf, "blocked")) {
2383 set_bit(Blocked, &rdev->flags);
2384 err = 0;
2385 } else if (cmd_match(buf, "-blocked")) {
2386 clear_bit(Blocked, &rdev->flags);
2387 wake_up(&rdev->blocked_wait);
2388 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2389 md_wakeup_thread(rdev->mddev->thread);
2390
2391 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002392 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2393 set_bit(In_sync, &rdev->flags);
2394 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002395 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002396 if (!err)
2397 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002398 return err ? err : len;
2399}
NeilBrown80ca3a42006-07-10 04:44:18 -07002400static struct rdev_sysfs_entry rdev_state =
2401__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002402
2403static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002404errors_show(mdk_rdev_t *rdev, char *page)
2405{
2406 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2407}
2408
2409static ssize_t
2410errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2411{
2412 char *e;
2413 unsigned long n = simple_strtoul(buf, &e, 10);
2414 if (*buf && (*e == 0 || *e == '\n')) {
2415 atomic_set(&rdev->corrected_errors, n);
2416 return len;
2417 }
2418 return -EINVAL;
2419}
2420static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002421__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002422
NeilBrown014236d2006-01-06 00:20:55 -08002423static ssize_t
2424slot_show(mdk_rdev_t *rdev, char *page)
2425{
2426 if (rdev->raid_disk < 0)
2427 return sprintf(page, "none\n");
2428 else
2429 return sprintf(page, "%d\n", rdev->raid_disk);
2430}
2431
2432static ssize_t
2433slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2434{
2435 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002436 int err;
2437 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002438 int slot = simple_strtoul(buf, &e, 10);
2439 if (strncmp(buf, "none", 4)==0)
2440 slot = -1;
2441 else if (e==buf || (*e && *e!= '\n'))
2442 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002443 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002444 /* Setting 'slot' on an active array requires also
2445 * updating the 'rd%d' link, and communicating
2446 * with the personality with ->hot_*_disk.
2447 * For now we only support removing
2448 * failed/spare devices. This normally happens automatically,
2449 * but not when the metadata is externally managed.
2450 */
NeilBrownc303da62008-02-06 01:39:51 -08002451 if (rdev->raid_disk == -1)
2452 return -EEXIST;
2453 /* personality does all needed checks */
2454 if (rdev->mddev->pers->hot_add_disk == NULL)
2455 return -EINVAL;
2456 err = rdev->mddev->pers->
2457 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2458 if (err)
2459 return err;
2460 sprintf(nm, "rd%d", rdev->raid_disk);
2461 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002462 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002463 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2464 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002465 } else if (rdev->mddev->pers) {
2466 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002467 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002468 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002469 */
2470
2471 if (rdev->raid_disk != -1)
2472 return -EBUSY;
2473
NeilBrownc6751b22011-02-02 11:57:13 +11002474 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2475 return -EBUSY;
2476
Neil Brown6c2fce22008-06-28 08:31:31 +10002477 if (rdev->mddev->pers->hot_add_disk == NULL)
2478 return -EINVAL;
2479
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002480 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002481 if (rdev2->raid_disk == slot)
2482 return -EEXIST;
2483
NeilBrownba1b41b2011-01-14 09:14:34 +11002484 if (slot >= rdev->mddev->raid_disks &&
2485 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2486 return -ENOSPC;
2487
Neil Brown6c2fce22008-06-28 08:31:31 +10002488 rdev->raid_disk = slot;
2489 if (test_bit(In_sync, &rdev->flags))
2490 rdev->saved_raid_disk = slot;
2491 else
2492 rdev->saved_raid_disk = -1;
2493 err = rdev->mddev->pers->
2494 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002495 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002496 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002497 return err;
Neil Brown52664732008-06-28 08:31:44 +10002498 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002499 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002500 sprintf(nm, "rd%d", rdev->raid_disk);
2501 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002502 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002503 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002504 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002505 if (slot >= rdev->mddev->raid_disks &&
2506 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002507 return -ENOSPC;
2508 rdev->raid_disk = slot;
2509 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002510 clear_bit(Faulty, &rdev->flags);
2511 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002512 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002513 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002514 }
NeilBrown014236d2006-01-06 00:20:55 -08002515 return len;
2516}
2517
2518
2519static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002520__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002521
NeilBrown93c8cad2006-01-06 00:20:56 -08002522static ssize_t
2523offset_show(mdk_rdev_t *rdev, char *page)
2524{
Andrew Morton6961ece2006-01-06 00:20:59 -08002525 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002526}
2527
2528static ssize_t
2529offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2530{
2531 char *e;
2532 unsigned long long offset = simple_strtoull(buf, &e, 10);
2533 if (e==buf || (*e && *e != '\n'))
2534 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002535 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002536 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002537 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002538 /* Must set offset before size, so overlap checks
2539 * can be sane */
2540 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002541 rdev->data_offset = offset;
2542 return len;
2543}
2544
2545static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002546__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002547
NeilBrown83303b62006-01-06 00:21:06 -08002548static ssize_t
2549rdev_size_show(mdk_rdev_t *rdev, char *page)
2550{
Andre Nolldd8ac332009-03-31 14:33:13 +11002551 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002552}
2553
NeilBrownc5d79ad2008-02-06 01:39:54 -08002554static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2555{
2556 /* check if two start/length pairs overlap */
2557 if (s1+l1 <= s2)
2558 return 0;
2559 if (s2+l2 <= s1)
2560 return 0;
2561 return 1;
2562}
2563
Dan Williamsb522adc2009-03-31 15:00:31 +11002564static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2565{
2566 unsigned long long blocks;
2567 sector_t new;
2568
2569 if (strict_strtoull(buf, 10, &blocks) < 0)
2570 return -EINVAL;
2571
2572 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2573 return -EINVAL; /* sector conversion overflow */
2574
2575 new = blocks * 2;
2576 if (new != blocks * 2)
2577 return -EINVAL; /* unsigned long long to sector_t overflow */
2578
2579 *sectors = new;
2580 return 0;
2581}
2582
NeilBrown83303b62006-01-06 00:21:06 -08002583static ssize_t
2584rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2585{
NeilBrown27c529b2008-03-04 14:29:33 -08002586 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002587 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002588 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002589
Dan Williamsb522adc2009-03-31 15:00:31 +11002590 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002591 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002592 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002593 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002594 sectors = super_types[my_mddev->major_version].
2595 rdev_size_change(rdev, sectors);
2596 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002597 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002598 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002599 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002600 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002601 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002602 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002603 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002604
Andre Nolldd8ac332009-03-31 14:33:13 +11002605 rdev->sectors = sectors;
2606 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002607 /* need to check that all other rdevs with the same ->bdev
2608 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002609 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002610 * we have to change it back, we will have the lock again.
2611 */
2612 mddev_t *mddev;
2613 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002614 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002615
NeilBrown27c529b2008-03-04 14:29:33 -08002616 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002617 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002618 mdk_rdev_t *rdev2;
2619
2620 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002621 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002622 if (rdev->bdev == rdev2->bdev &&
2623 rdev != rdev2 &&
2624 overlaps(rdev->data_offset, rdev->sectors,
2625 rdev2->data_offset,
2626 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002627 overlap = 1;
2628 break;
2629 }
2630 mddev_unlock(mddev);
2631 if (overlap) {
2632 mddev_put(mddev);
2633 break;
2634 }
2635 }
NeilBrown27c529b2008-03-04 14:29:33 -08002636 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002637 if (overlap) {
2638 /* Someone else could have slipped in a size
2639 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002640 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002641 * safe, and trust userspace not to race with
2642 * itself
2643 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002644 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002645 return -EBUSY;
2646 }
2647 }
NeilBrown83303b62006-01-06 00:21:06 -08002648 return len;
2649}
2650
2651static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002652__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002653
Dan Williams06e3c812009-12-12 21:17:12 -07002654
2655static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2656{
2657 unsigned long long recovery_start = rdev->recovery_offset;
2658
2659 if (test_bit(In_sync, &rdev->flags) ||
2660 recovery_start == MaxSector)
2661 return sprintf(page, "none\n");
2662
2663 return sprintf(page, "%llu\n", recovery_start);
2664}
2665
2666static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2667{
2668 unsigned long long recovery_start;
2669
2670 if (cmd_match(buf, "none"))
2671 recovery_start = MaxSector;
2672 else if (strict_strtoull(buf, 10, &recovery_start))
2673 return -EINVAL;
2674
2675 if (rdev->mddev->pers &&
2676 rdev->raid_disk >= 0)
2677 return -EBUSY;
2678
2679 rdev->recovery_offset = recovery_start;
2680 if (recovery_start == MaxSector)
2681 set_bit(In_sync, &rdev->flags);
2682 else
2683 clear_bit(In_sync, &rdev->flags);
2684 return len;
2685}
2686
2687static struct rdev_sysfs_entry rdev_recovery_start =
2688__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2689
NeilBrown86e6ffd2005-11-08 21:39:24 -08002690static struct attribute *rdev_default_attrs[] = {
2691 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002692 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002693 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002694 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002695 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002696 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002697 NULL,
2698};
2699static ssize_t
2700rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2701{
2702 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2703 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002704 mddev_t *mddev = rdev->mddev;
2705 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002706
2707 if (!entry->show)
2708 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002709
2710 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2711 if (!rv) {
2712 if (rdev->mddev == NULL)
2713 rv = -EBUSY;
2714 else
2715 rv = entry->show(rdev, page);
2716 mddev_unlock(mddev);
2717 }
2718 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002719}
2720
2721static ssize_t
2722rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2723 const char *page, size_t length)
2724{
2725 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2726 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002727 ssize_t rv;
2728 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002729
2730 if (!entry->store)
2731 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002732 if (!capable(CAP_SYS_ADMIN))
2733 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002734 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002735 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002736 if (rdev->mddev == NULL)
2737 rv = -EBUSY;
2738 else
2739 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002740 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002741 }
2742 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002743}
2744
2745static void rdev_free(struct kobject *ko)
2746{
2747 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2748 kfree(rdev);
2749}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002750static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002751 .show = rdev_attr_show,
2752 .store = rdev_attr_store,
2753};
2754static struct kobj_type rdev_ktype = {
2755 .release = rdev_free,
2756 .sysfs_ops = &rdev_sysfs_ops,
2757 .default_attrs = rdev_default_attrs,
2758};
2759
NeilBrowne8bb9a82010-06-01 19:37:26 +10002760void md_rdev_init(mdk_rdev_t *rdev)
2761{
2762 rdev->desc_nr = -1;
2763 rdev->saved_raid_disk = -1;
2764 rdev->raid_disk = -1;
2765 rdev->flags = 0;
2766 rdev->data_offset = 0;
2767 rdev->sb_events = 0;
2768 rdev->last_read_error.tv_sec = 0;
2769 rdev->last_read_error.tv_nsec = 0;
2770 atomic_set(&rdev->nr_pending, 0);
2771 atomic_set(&rdev->read_errors, 0);
2772 atomic_set(&rdev->corrected_errors, 0);
2773
2774 INIT_LIST_HEAD(&rdev->same_set);
2775 init_waitqueue_head(&rdev->blocked_wait);
2776}
2777EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778/*
2779 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2780 *
2781 * mark the device faulty if:
2782 *
2783 * - the device is nonexistent (zero size)
2784 * - the device has no valid superblock
2785 *
2786 * a faulty rdev _never_ has rdev->sb set.
2787 */
2788static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2789{
2790 char b[BDEVNAME_SIZE];
2791 int err;
2792 mdk_rdev_t *rdev;
2793 sector_t size;
2794
NeilBrown9ffae0c2006-01-06 00:20:32 -08002795 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 if (!rdev) {
2797 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2798 return ERR_PTR(-ENOMEM);
2799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
NeilBrowne8bb9a82010-06-01 19:37:26 +10002801 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if ((err = alloc_disk_sb(rdev)))
2803 goto abort_free;
2804
NeilBrownc5d79ad2008-02-06 01:39:54 -08002805 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (err)
2807 goto abort_free;
2808
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002809 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002810
Mike Snitzer77304d22010-11-08 14:39:12 +01002811 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (!size) {
2813 printk(KERN_WARNING
2814 "md: %s has zero or unknown size, marking faulty!\n",
2815 bdevname(rdev->bdev,b));
2816 err = -EINVAL;
2817 goto abort_free;
2818 }
2819
2820 if (super_format >= 0) {
2821 err = super_types[super_format].
2822 load_super(rdev, NULL, super_minor);
2823 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002824 printk(KERN_WARNING
2825 "md: %s does not have a valid v%d.%d "
2826 "superblock, not importing!\n",
2827 bdevname(rdev->bdev,b),
2828 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 goto abort_free;
2830 }
2831 if (err < 0) {
2832 printk(KERN_WARNING
2833 "md: could not read %s's sb, not importing!\n",
2834 bdevname(rdev->bdev,b));
2835 goto abort_free;
2836 }
2837 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return rdev;
2840
2841abort_free:
2842 if (rdev->sb_page) {
2843 if (rdev->bdev)
2844 unlock_rdev(rdev);
2845 free_disk_sb(rdev);
2846 }
2847 kfree(rdev);
2848 return ERR_PTR(err);
2849}
2850
2851/*
2852 * Check a full RAID array for plausibility
2853 */
2854
2855
NeilBrowna757e642005-04-16 15:26:42 -07002856static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002859 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 char b[BDEVNAME_SIZE];
2861
2862 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002863 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 switch (super_types[mddev->major_version].
2865 load_super(rdev, freshest, mddev->minor_version)) {
2866 case 1:
2867 freshest = rdev;
2868 break;
2869 case 0:
2870 break;
2871 default:
2872 printk( KERN_ERR \
2873 "md: fatal superblock inconsistency in %s"
2874 " -- removing from array\n",
2875 bdevname(rdev->bdev,b));
2876 kick_rdev_from_array(rdev);
2877 }
2878
2879
2880 super_types[mddev->major_version].
2881 validate_super(mddev, freshest);
2882
2883 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002884 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002885 if (mddev->max_disks &&
2886 (rdev->desc_nr >= mddev->max_disks ||
2887 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002888 printk(KERN_WARNING
2889 "md: %s: %s: only %d devices permitted\n",
2890 mdname(mddev), bdevname(rdev->bdev, b),
2891 mddev->max_disks);
2892 kick_rdev_from_array(rdev);
2893 continue;
2894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (rdev != freshest)
2896 if (super_types[mddev->major_version].
2897 validate_super(mddev, rdev)) {
2898 printk(KERN_WARNING "md: kicking non-fresh %s"
2899 " from array!\n",
2900 bdevname(rdev->bdev,b));
2901 kick_rdev_from_array(rdev);
2902 continue;
2903 }
2904 if (mddev->level == LEVEL_MULTIPATH) {
2905 rdev->desc_nr = i++;
2906 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002907 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002908 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002909 rdev->raid_disk = -1;
2910 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
2912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
NeilBrown72e02072009-12-14 12:49:55 +11002915/* Read a fixed-point number.
2916 * Numbers in sysfs attributes should be in "standard" units where
2917 * possible, so time should be in seconds.
2918 * However we internally use a a much smaller unit such as
2919 * milliseconds or jiffies.
2920 * This function takes a decimal number with a possible fractional
2921 * component, and produces an integer which is the result of
2922 * multiplying that number by 10^'scale'.
2923 * all without any floating-point arithmetic.
2924 */
2925int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2926{
2927 unsigned long result = 0;
2928 long decimals = -1;
2929 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2930 if (*cp == '.')
2931 decimals = 0;
2932 else if (decimals < scale) {
2933 unsigned int value;
2934 value = *cp - '0';
2935 result = result * 10 + value;
2936 if (decimals >= 0)
2937 decimals++;
2938 }
2939 cp++;
2940 }
2941 if (*cp == '\n')
2942 cp++;
2943 if (*cp)
2944 return -EINVAL;
2945 if (decimals < 0)
2946 decimals = 0;
2947 while (decimals < scale) {
2948 result *= 10;
2949 decimals ++;
2950 }
2951 *res = result;
2952 return 0;
2953}
2954
2955
NeilBrown19052c02008-08-05 15:54:13 +10002956static void md_safemode_timeout(unsigned long data);
2957
NeilBrowneae17012005-11-08 21:39:23 -08002958static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002959safe_delay_show(mddev_t *mddev, char *page)
2960{
2961 int msec = (mddev->safemode_delay*1000)/HZ;
2962 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2963}
2964static ssize_t
2965safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2966{
NeilBrown16f17b32006-06-26 00:27:37 -07002967 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002968
NeilBrown72e02072009-12-14 12:49:55 +11002969 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002970 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002971 if (msec == 0)
2972 mddev->safemode_delay = 0;
2973 else {
NeilBrown19052c02008-08-05 15:54:13 +10002974 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002975 mddev->safemode_delay = (msec*HZ)/1000;
2976 if (mddev->safemode_delay == 0)
2977 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002978 if (mddev->safemode_delay < old_delay)
2979 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002980 }
2981 return len;
2982}
2983static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002984__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002985
2986static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002987level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002988{
NeilBrown2604b702006-01-06 00:20:36 -08002989 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002990 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002991 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002992 else if (mddev->clevel[0])
2993 return sprintf(page, "%s\n", mddev->clevel);
2994 else if (mddev->level != LEVEL_NONE)
2995 return sprintf(page, "%d\n", mddev->level);
2996 else
2997 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002998}
2999
NeilBrownd9d166c2006-01-06 00:20:51 -08003000static ssize_t
3001level_store(mddev_t *mddev, const char *buf, size_t len)
3002{
Dan Williamsf2859af2010-05-02 10:04:16 -07003003 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003004 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003005 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003006 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003007 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003008 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003009
3010 if (mddev->pers == NULL) {
3011 if (len == 0)
3012 return 0;
3013 if (len >= sizeof(mddev->clevel))
3014 return -ENOSPC;
3015 strncpy(mddev->clevel, buf, len);
3016 if (mddev->clevel[len-1] == '\n')
3017 len--;
3018 mddev->clevel[len] = 0;
3019 mddev->level = LEVEL_NONE;
3020 return rv;
3021 }
3022
3023 /* request to change the personality. Need to ensure:
3024 * - array is not engaged in resync/recovery/reshape
3025 * - old personality can be suspended
3026 * - new personality will access other array.
3027 */
3028
NeilBrownbb4f1e92010-08-08 21:18:03 +10003029 if (mddev->sync_thread ||
3030 mddev->reshape_position != MaxSector ||
3031 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003032 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003033
3034 if (!mddev->pers->quiesce) {
3035 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3036 mdname(mddev), mddev->pers->name);
3037 return -EINVAL;
3038 }
3039
3040 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003041 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003042 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003043 strncpy(clevel, buf, len);
3044 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003045 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003046 clevel[len] = 0;
3047 if (strict_strtol(clevel, 10, &level))
3048 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003049
Dan Williamsf2859af2010-05-02 10:04:16 -07003050 if (request_module("md-%s", clevel) != 0)
3051 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003052 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003053 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003054 if (!pers || !try_module_get(pers->owner)) {
3055 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003056 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003057 return -EINVAL;
3058 }
3059 spin_unlock(&pers_lock);
3060
3061 if (pers == mddev->pers) {
3062 /* Nothing to do! */
3063 module_put(pers->owner);
3064 return rv;
3065 }
3066 if (!pers->takeover) {
3067 module_put(pers->owner);
3068 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003069 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003070 return -EINVAL;
3071 }
3072
NeilBrowne93f68a2010-06-15 09:36:03 +01003073 list_for_each_entry(rdev, &mddev->disks, same_set)
3074 rdev->new_raid_disk = rdev->raid_disk;
3075
NeilBrown245f46c2009-03-31 14:39:39 +11003076 /* ->takeover must set new_* and/or delta_disks
3077 * if it succeeds, and may set them when it fails.
3078 */
3079 priv = pers->takeover(mddev);
3080 if (IS_ERR(priv)) {
3081 mddev->new_level = mddev->level;
3082 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003083 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003084 mddev->raid_disks -= mddev->delta_disks;
3085 mddev->delta_disks = 0;
3086 module_put(pers->owner);
3087 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003088 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003089 return PTR_ERR(priv);
3090 }
3091
3092 /* Looks like we have a winner */
3093 mddev_suspend(mddev);
3094 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003095
3096 if (mddev->pers->sync_request == NULL &&
3097 pers->sync_request != NULL) {
3098 /* need to add the md_redundancy_group */
3099 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3100 printk(KERN_WARNING
3101 "md: cannot register extra attributes for %s\n",
3102 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003103 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003104 }
3105 if (mddev->pers->sync_request != NULL &&
3106 pers->sync_request == NULL) {
3107 /* need to remove the md_redundancy_group */
3108 if (mddev->to_remove == NULL)
3109 mddev->to_remove = &md_redundancy_group;
3110 }
3111
Trela Maciej54071b32010-03-08 16:02:42 +11003112 if (mddev->pers->sync_request == NULL &&
3113 mddev->external) {
3114 /* We are converting from a no-redundancy array
3115 * to a redundancy array and metadata is managed
3116 * externally so we need to be sure that writes
3117 * won't block due to a need to transition
3118 * clean->dirty
3119 * until external management is started.
3120 */
3121 mddev->in_sync = 0;
3122 mddev->safemode_delay = 0;
3123 mddev->safemode = 0;
3124 }
3125
NeilBrowne93f68a2010-06-15 09:36:03 +01003126 list_for_each_entry(rdev, &mddev->disks, same_set) {
3127 char nm[20];
3128 if (rdev->raid_disk < 0)
3129 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003130 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003131 rdev->new_raid_disk = -1;
3132 if (rdev->new_raid_disk == rdev->raid_disk)
3133 continue;
3134 sprintf(nm, "rd%d", rdev->raid_disk);
3135 sysfs_remove_link(&mddev->kobj, nm);
3136 }
3137 list_for_each_entry(rdev, &mddev->disks, same_set) {
3138 if (rdev->raid_disk < 0)
3139 continue;
3140 if (rdev->new_raid_disk == rdev->raid_disk)
3141 continue;
3142 rdev->raid_disk = rdev->new_raid_disk;
3143 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003144 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003145 else {
3146 char nm[20];
3147 sprintf(nm, "rd%d", rdev->raid_disk);
3148 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3149 printk("md: cannot register %s for %s after level change\n",
3150 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003151 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003152 }
3153
3154 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003155 mddev->pers = pers;
3156 mddev->private = priv;
3157 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3158 mddev->level = mddev->new_level;
3159 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003160 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003161 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003162 if (mddev->pers->sync_request == NULL) {
3163 /* this is now an array without redundancy, so
3164 * it must always be in_sync
3165 */
3166 mddev->in_sync = 1;
3167 del_timer_sync(&mddev->safemode_timer);
3168 }
NeilBrown245f46c2009-03-31 14:39:39 +11003169 pers->run(mddev);
3170 mddev_resume(mddev);
3171 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3172 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3173 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003174 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003175 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003176 return rv;
3177}
3178
3179static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003180__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003181
NeilBrownd4dbd022006-06-26 00:27:59 -07003182
3183static ssize_t
3184layout_show(mddev_t *mddev, char *page)
3185{
3186 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003187 if (mddev->reshape_position != MaxSector &&
3188 mddev->layout != mddev->new_layout)
3189 return sprintf(page, "%d (%d)\n",
3190 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003191 return sprintf(page, "%d\n", mddev->layout);
3192}
3193
3194static ssize_t
3195layout_store(mddev_t *mddev, const char *buf, size_t len)
3196{
3197 char *e;
3198 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003199
3200 if (!*buf || (*e && *e != '\n'))
3201 return -EINVAL;
3202
NeilBrownb3546032009-03-31 14:56:41 +11003203 if (mddev->pers) {
3204 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003205 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003206 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003207 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003208 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003209 if (err) {
3210 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003211 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003212 }
NeilBrownb3546032009-03-31 14:56:41 +11003213 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003214 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003215 if (mddev->reshape_position == MaxSector)
3216 mddev->layout = n;
3217 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003218 return len;
3219}
3220static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003221__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003222
3223
NeilBrowneae17012005-11-08 21:39:23 -08003224static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003225raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003226{
NeilBrownbb636542005-11-08 21:39:45 -08003227 if (mddev->raid_disks == 0)
3228 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003229 if (mddev->reshape_position != MaxSector &&
3230 mddev->delta_disks != 0)
3231 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3232 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003233 return sprintf(page, "%d\n", mddev->raid_disks);
3234}
3235
NeilBrownda943b92006-01-06 00:20:54 -08003236static int update_raid_disks(mddev_t *mddev, int raid_disks);
3237
3238static ssize_t
3239raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3240{
NeilBrownda943b92006-01-06 00:20:54 -08003241 char *e;
3242 int rv = 0;
3243 unsigned long n = simple_strtoul(buf, &e, 10);
3244
3245 if (!*buf || (*e && *e != '\n'))
3246 return -EINVAL;
3247
3248 if (mddev->pers)
3249 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003250 else if (mddev->reshape_position != MaxSector) {
3251 int olddisks = mddev->raid_disks - mddev->delta_disks;
3252 mddev->delta_disks = n - olddisks;
3253 mddev->raid_disks = n;
3254 } else
NeilBrownda943b92006-01-06 00:20:54 -08003255 mddev->raid_disks = n;
3256 return rv ? rv : len;
3257}
3258static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003259__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003260
NeilBrown24dd4692005-11-08 21:39:26 -08003261static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003262chunk_size_show(mddev_t *mddev, char *page)
3263{
NeilBrown08a02ec2007-05-09 02:35:38 -07003264 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003265 mddev->chunk_sectors != mddev->new_chunk_sectors)
3266 return sprintf(page, "%d (%d)\n",
3267 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003268 mddev->chunk_sectors << 9);
3269 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003270}
3271
3272static ssize_t
3273chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3274{
NeilBrown3b343802006-01-06 00:20:47 -08003275 char *e;
3276 unsigned long n = simple_strtoul(buf, &e, 10);
3277
NeilBrown3b343802006-01-06 00:20:47 -08003278 if (!*buf || (*e && *e != '\n'))
3279 return -EINVAL;
3280
NeilBrownb3546032009-03-31 14:56:41 +11003281 if (mddev->pers) {
3282 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003283 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003284 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003285 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003286 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003287 if (err) {
3288 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003289 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003290 }
NeilBrownb3546032009-03-31 14:56:41 +11003291 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003292 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003293 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003294 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003295 }
NeilBrown3b343802006-01-06 00:20:47 -08003296 return len;
3297}
3298static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003299__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003300
NeilBrowna94213b2006-06-26 00:28:00 -07003301static ssize_t
3302resync_start_show(mddev_t *mddev, char *page)
3303{
NeilBrownd1a7c502009-03-31 15:24:32 +11003304 if (mddev->recovery_cp == MaxSector)
3305 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003306 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3307}
3308
3309static ssize_t
3310resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3311{
NeilBrowna94213b2006-06-26 00:28:00 -07003312 char *e;
3313 unsigned long long n = simple_strtoull(buf, &e, 10);
3314
3315 if (mddev->pers)
3316 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003317 if (cmd_match(buf, "none"))
3318 n = MaxSector;
3319 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003320 return -EINVAL;
3321
3322 mddev->recovery_cp = n;
3323 return len;
3324}
3325static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003326__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003327
NeilBrown9e653b62006-06-26 00:27:58 -07003328/*
3329 * The array state can be:
3330 *
3331 * clear
3332 * No devices, no size, no level
3333 * Equivalent to STOP_ARRAY ioctl
3334 * inactive
3335 * May have some settings, but array is not active
3336 * all IO results in error
3337 * When written, doesn't tear down array, but just stops it
3338 * suspended (not supported yet)
3339 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003340 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003341 * readonly
3342 * no resync can happen. no superblocks get written.
3343 * write requests fail
3344 * read-auto
3345 * like readonly, but behaves like 'clean' on a write request.
3346 *
3347 * clean - no pending writes, but otherwise active.
3348 * When written to inactive array, starts without resync
3349 * If a write request arrives then
3350 * if metadata is known, mark 'dirty' and switch to 'active'.
3351 * if not known, block and switch to write-pending
3352 * If written to an active array that has pending writes, then fails.
3353 * active
3354 * fully active: IO and resync can be happening.
3355 * When written to inactive array, starts with resync
3356 *
3357 * write-pending
3358 * clean, but writes are blocked waiting for 'active' to be written.
3359 *
3360 * active-idle
3361 * like active, but no writes have been seen for a while (100msec).
3362 *
3363 */
3364enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3365 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003366static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003367 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3368 "write-pending", "active-idle", NULL };
3369
3370static int match_word(const char *word, char **list)
3371{
3372 int n;
3373 for (n=0; list[n]; n++)
3374 if (cmd_match(word, list[n]))
3375 break;
3376 return n;
3377}
3378
3379static ssize_t
3380array_state_show(mddev_t *mddev, char *page)
3381{
3382 enum array_state st = inactive;
3383
3384 if (mddev->pers)
3385 switch(mddev->ro) {
3386 case 1:
3387 st = readonly;
3388 break;
3389 case 2:
3390 st = read_auto;
3391 break;
3392 case 0:
3393 if (mddev->in_sync)
3394 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003395 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003396 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003397 else if (mddev->safemode)
3398 st = active_idle;
3399 else
3400 st = active;
3401 }
3402 else {
3403 if (list_empty(&mddev->disks) &&
3404 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003405 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003406 st = clear;
3407 else
3408 st = inactive;
3409 }
3410 return sprintf(page, "%s\n", array_states[st]);
3411}
3412
Neil Browndf5b20c2008-07-11 22:02:22 +10003413static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003414static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003415static int do_md_run(mddev_t * mddev);
3416static int restart_array(mddev_t *mddev);
3417
3418static ssize_t
3419array_state_store(mddev_t *mddev, const char *buf, size_t len)
3420{
3421 int err = -EINVAL;
3422 enum array_state st = match_word(buf, array_states);
3423 switch(st) {
3424 case bad_word:
3425 break;
3426 case clear:
3427 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003428 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003429 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003430 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003431 break;
3432 case inactive:
3433 /* stopping an active array */
3434 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003435 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003436 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003437 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003438 } else
3439 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003440 break;
3441 case suspended:
3442 break; /* not supported yet */
3443 case readonly:
3444 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003445 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003446 else {
3447 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003448 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003449 err = do_md_run(mddev);
3450 }
3451 break;
3452 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003453 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003454 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003455 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003456 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003457 err = restart_array(mddev);
3458 if (err == 0) {
3459 mddev->ro = 2;
3460 set_disk_ro(mddev->gendisk, 0);
3461 }
NeilBrown9e653b62006-06-26 00:27:58 -07003462 } else {
3463 mddev->ro = 2;
3464 err = do_md_run(mddev);
3465 }
3466 break;
3467 case clean:
3468 if (mddev->pers) {
3469 restart_array(mddev);
3470 spin_lock_irq(&mddev->write_lock);
3471 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003472 if (mddev->in_sync == 0) {
3473 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003474 if (mddev->safemode == 1)
3475 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003476 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003477 }
3478 err = 0;
3479 } else
3480 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003481 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003482 } else
3483 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003484 break;
3485 case active:
3486 if (mddev->pers) {
3487 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003488 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003489 wake_up(&mddev->sb_wait);
3490 err = 0;
3491 } else {
3492 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003493 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003494 err = do_md_run(mddev);
3495 }
3496 break;
3497 case write_pending:
3498 case active_idle:
3499 /* these cannot be set */
3500 break;
3501 }
3502 if (err)
3503 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003504 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003505 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003506 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003507 }
NeilBrown9e653b62006-06-26 00:27:58 -07003508}
NeilBrown80ca3a42006-07-10 04:44:18 -07003509static struct md_sysfs_entry md_array_state =
3510__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003511
NeilBrown6d7ff732006-01-06 00:21:16 -08003512static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003513max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3514 return sprintf(page, "%d\n",
3515 atomic_read(&mddev->max_corr_read_errors));
3516}
3517
3518static ssize_t
3519max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3520{
3521 char *e;
3522 unsigned long n = simple_strtoul(buf, &e, 10);
3523
3524 if (*buf && (*e == 0 || *e == '\n')) {
3525 atomic_set(&mddev->max_corr_read_errors, n);
3526 return len;
3527 }
3528 return -EINVAL;
3529}
3530
3531static struct md_sysfs_entry max_corr_read_errors =
3532__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3533 max_corrected_read_errors_store);
3534
3535static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003536null_show(mddev_t *mddev, char *page)
3537{
3538 return -EINVAL;
3539}
3540
3541static ssize_t
3542new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3543{
3544 /* buf must be %d:%d\n? giving major and minor numbers */
3545 /* The new device is added to the array.
3546 * If the array has a persistent superblock, we read the
3547 * superblock to initialise info and check validity.
3548 * Otherwise, only checking done is that in bind_rdev_to_array,
3549 * which mainly checks size.
3550 */
3551 char *e;
3552 int major = simple_strtoul(buf, &e, 10);
3553 int minor;
3554 dev_t dev;
3555 mdk_rdev_t *rdev;
3556 int err;
3557
3558 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3559 return -EINVAL;
3560 minor = simple_strtoul(e+1, &e, 10);
3561 if (*e && *e != '\n')
3562 return -EINVAL;
3563 dev = MKDEV(major, minor);
3564 if (major != MAJOR(dev) ||
3565 minor != MINOR(dev))
3566 return -EOVERFLOW;
3567
3568
3569 if (mddev->persistent) {
3570 rdev = md_import_device(dev, mddev->major_version,
3571 mddev->minor_version);
3572 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3573 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3574 mdk_rdev_t, same_set);
3575 err = super_types[mddev->major_version]
3576 .load_super(rdev, rdev0, mddev->minor_version);
3577 if (err < 0)
3578 goto out;
3579 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003580 } else if (mddev->external)
3581 rdev = md_import_device(dev, -2, -1);
3582 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003583 rdev = md_import_device(dev, -1, -1);
3584
3585 if (IS_ERR(rdev))
3586 return PTR_ERR(rdev);
3587 err = bind_rdev_to_array(rdev, mddev);
3588 out:
3589 if (err)
3590 export_rdev(rdev);
3591 return err ? err : len;
3592}
3593
3594static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003595__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003596
3597static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003598bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3599{
3600 char *end;
3601 unsigned long chunk, end_chunk;
3602
3603 if (!mddev->bitmap)
3604 goto out;
3605 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3606 while (*buf) {
3607 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3608 if (buf == end) break;
3609 if (*end == '-') { /* range */
3610 buf = end + 1;
3611 end_chunk = simple_strtoul(buf, &end, 0);
3612 if (buf == end) break;
3613 }
3614 if (*end && !isspace(*end)) break;
3615 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003616 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003617 }
3618 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3619out:
3620 return len;
3621}
3622
3623static struct md_sysfs_entry md_bitmap =
3624__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3625
3626static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003627size_show(mddev_t *mddev, char *page)
3628{
Andre Noll58c0fed2009-03-31 14:33:13 +11003629 return sprintf(page, "%llu\n",
3630 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003631}
3632
Andre Nolld71f9f82008-07-11 22:02:22 +10003633static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003634
3635static ssize_t
3636size_store(mddev_t *mddev, const char *buf, size_t len)
3637{
3638 /* If array is inactive, we can reduce the component size, but
3639 * not increase it (except from 0).
3640 * If array is active, we can try an on-line resize
3641 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003642 sector_t sectors;
3643 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003644
Andre Noll58c0fed2009-03-31 14:33:13 +11003645 if (err < 0)
3646 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003647 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003648 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003649 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003650 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003651 if (mddev->dev_sectors == 0 ||
3652 mddev->dev_sectors > sectors)
3653 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003654 else
3655 err = -ENOSPC;
3656 }
3657 return err ? err : len;
3658}
3659
3660static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003661__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003662
NeilBrown8bb93aa2006-01-06 00:20:50 -08003663
3664/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003665 * This is one of
3666 * 'none' for arrays with no metadata (good luck...)
3667 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003668 * or N.M for internally known formats
3669 */
3670static ssize_t
3671metadata_show(mddev_t *mddev, char *page)
3672{
3673 if (mddev->persistent)
3674 return sprintf(page, "%d.%d\n",
3675 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003676 else if (mddev->external)
3677 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003678 else
3679 return sprintf(page, "none\n");
3680}
3681
3682static ssize_t
3683metadata_store(mddev_t *mddev, const char *buf, size_t len)
3684{
3685 int major, minor;
3686 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003687 /* Changing the details of 'external' metadata is
3688 * always permitted. Otherwise there must be
3689 * no devices attached to the array.
3690 */
3691 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3692 ;
3693 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003694 return -EBUSY;
3695
3696 if (cmd_match(buf, "none")) {
3697 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003698 mddev->external = 0;
3699 mddev->major_version = 0;
3700 mddev->minor_version = 90;
3701 return len;
3702 }
3703 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003704 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003705 if (namelen >= sizeof(mddev->metadata_type))
3706 namelen = sizeof(mddev->metadata_type)-1;
3707 strncpy(mddev->metadata_type, buf+9, namelen);
3708 mddev->metadata_type[namelen] = 0;
3709 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3710 mddev->metadata_type[--namelen] = 0;
3711 mddev->persistent = 0;
3712 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003713 mddev->major_version = 0;
3714 mddev->minor_version = 90;
3715 return len;
3716 }
3717 major = simple_strtoul(buf, &e, 10);
3718 if (e==buf || *e != '.')
3719 return -EINVAL;
3720 buf = e+1;
3721 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003722 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003723 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003724 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003725 return -ENOENT;
3726 mddev->major_version = major;
3727 mddev->minor_version = minor;
3728 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003729 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003730 return len;
3731}
3732
3733static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003734__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003735
NeilBrowna35b0d62006-01-06 00:20:49 -08003736static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003737action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003738{
NeilBrown7eec3142005-11-08 21:39:44 -08003739 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003740 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3741 type = "frozen";
3742 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003743 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003744 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3745 type = "reshape";
3746 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003747 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3748 type = "resync";
3749 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3750 type = "check";
3751 else
3752 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003753 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003754 type = "recover";
3755 }
3756 return sprintf(page, "%s\n", type);
3757}
3758
NeilBrown7ebc0be2011-01-14 09:14:33 +11003759static void reap_sync_thread(mddev_t *mddev);
3760
NeilBrown24dd4692005-11-08 21:39:26 -08003761static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003762action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003763{
NeilBrown7eec3142005-11-08 21:39:44 -08003764 if (!mddev->pers || !mddev->pers->sync_request)
3765 return -EINVAL;
3766
NeilBrownb6a9ce62009-05-26 09:41:17 +10003767 if (cmd_match(page, "frozen"))
3768 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3769 else
3770 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3771
3772 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003773 if (mddev->sync_thread) {
3774 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003775 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003776 }
NeilBrown03c902e2006-01-06 00:20:46 -08003777 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3778 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003779 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003780 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003781 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003782 else if (cmd_match(page, "recover")) {
3783 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3784 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3785 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003786 int err;
3787 if (mddev->pers->start_reshape == NULL)
3788 return -EINVAL;
3789 err = mddev->pers->start_reshape(mddev);
3790 if (err)
3791 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003792 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003793 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003794 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003795 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003796 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003797 return -EINVAL;
3798 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3799 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003800 }
NeilBrown03c902e2006-01-06 00:20:46 -08003801 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003802 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003803 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003804 return len;
3805}
3806
NeilBrown9d888832005-11-08 21:39:26 -08003807static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003808mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003809{
3810 return sprintf(page, "%llu\n",
3811 (unsigned long long) mddev->resync_mismatches);
3812}
3813
NeilBrown80ca3a42006-07-10 04:44:18 -07003814static struct md_sysfs_entry md_scan_mode =
3815__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003816
NeilBrown96de1e62005-11-08 21:39:39 -08003817
NeilBrown80ca3a42006-07-10 04:44:18 -07003818static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003819
NeilBrown88202a02006-01-06 00:21:36 -08003820static ssize_t
3821sync_min_show(mddev_t *mddev, char *page)
3822{
3823 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3824 mddev->sync_speed_min ? "local": "system");
3825}
3826
3827static ssize_t
3828sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3829{
3830 int min;
3831 char *e;
3832 if (strncmp(buf, "system", 6)==0) {
3833 mddev->sync_speed_min = 0;
3834 return len;
3835 }
3836 min = simple_strtoul(buf, &e, 10);
3837 if (buf == e || (*e && *e != '\n') || min <= 0)
3838 return -EINVAL;
3839 mddev->sync_speed_min = min;
3840 return len;
3841}
3842
3843static struct md_sysfs_entry md_sync_min =
3844__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3845
3846static ssize_t
3847sync_max_show(mddev_t *mddev, char *page)
3848{
3849 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3850 mddev->sync_speed_max ? "local": "system");
3851}
3852
3853static ssize_t
3854sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3855{
3856 int max;
3857 char *e;
3858 if (strncmp(buf, "system", 6)==0) {
3859 mddev->sync_speed_max = 0;
3860 return len;
3861 }
3862 max = simple_strtoul(buf, &e, 10);
3863 if (buf == e || (*e && *e != '\n') || max <= 0)
3864 return -EINVAL;
3865 mddev->sync_speed_max = max;
3866 return len;
3867}
3868
3869static struct md_sysfs_entry md_sync_max =
3870__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3871
Iustin Popd7f3d292007-10-16 23:30:54 -07003872static ssize_t
3873degraded_show(mddev_t *mddev, char *page)
3874{
3875 return sprintf(page, "%d\n", mddev->degraded);
3876}
3877static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003878
3879static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003880sync_force_parallel_show(mddev_t *mddev, char *page)
3881{
3882 return sprintf(page, "%d\n", mddev->parallel_resync);
3883}
3884
3885static ssize_t
3886sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3887{
3888 long n;
3889
3890 if (strict_strtol(buf, 10, &n))
3891 return -EINVAL;
3892
3893 if (n != 0 && n != 1)
3894 return -EINVAL;
3895
3896 mddev->parallel_resync = n;
3897
3898 if (mddev->sync_thread)
3899 wake_up(&resync_wait);
3900
3901 return len;
3902}
3903
3904/* force parallel resync, even with shared block devices */
3905static struct md_sysfs_entry md_sync_force_parallel =
3906__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3907 sync_force_parallel_show, sync_force_parallel_store);
3908
3909static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003910sync_speed_show(mddev_t *mddev, char *page)
3911{
3912 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003913 if (mddev->curr_resync == 0)
3914 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003915 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3916 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003917 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003918 db = resync - mddev->resync_mark_cnt;
3919 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003920}
3921
NeilBrown80ca3a42006-07-10 04:44:18 -07003922static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003923
3924static ssize_t
3925sync_completed_show(mddev_t *mddev, char *page)
3926{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003927 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003928
NeilBrownacb180b2009-04-14 16:28:34 +10003929 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3930 return sprintf(page, "none\n");
3931
NeilBrown88202a02006-01-06 00:21:36 -08003932 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003933 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003934 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003935 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003936
NeilBrownacb180b2009-04-14 16:28:34 +10003937 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003938 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003939}
3940
NeilBrown80ca3a42006-07-10 04:44:18 -07003941static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003942
NeilBrowne464eaf2006-03-27 01:18:14 -08003943static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003944min_sync_show(mddev_t *mddev, char *page)
3945{
3946 return sprintf(page, "%llu\n",
3947 (unsigned long long)mddev->resync_min);
3948}
3949static ssize_t
3950min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3951{
3952 unsigned long long min;
3953 if (strict_strtoull(buf, 10, &min))
3954 return -EINVAL;
3955 if (min > mddev->resync_max)
3956 return -EINVAL;
3957 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3958 return -EBUSY;
3959
3960 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003961 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003962 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003963 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003964 return -EINVAL;
3965 }
3966 mddev->resync_min = min;
3967
3968 return len;
3969}
3970
3971static struct md_sysfs_entry md_min_sync =
3972__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3973
3974static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003975max_sync_show(mddev_t *mddev, char *page)
3976{
3977 if (mddev->resync_max == MaxSector)
3978 return sprintf(page, "max\n");
3979 else
3980 return sprintf(page, "%llu\n",
3981 (unsigned long long)mddev->resync_max);
3982}
3983static ssize_t
3984max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3985{
3986 if (strncmp(buf, "max", 3) == 0)
3987 mddev->resync_max = MaxSector;
3988 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003989 unsigned long long max;
3990 if (strict_strtoull(buf, 10, &max))
3991 return -EINVAL;
3992 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003993 return -EINVAL;
3994 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003995 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003996 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3997 return -EBUSY;
3998
3999 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004000 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004001 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004002 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004003 return -EINVAL;
4004 }
4005 mddev->resync_max = max;
4006 }
4007 wake_up(&mddev->recovery_wait);
4008 return len;
4009}
4010
4011static struct md_sysfs_entry md_max_sync =
4012__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4013
4014static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004015suspend_lo_show(mddev_t *mddev, char *page)
4016{
4017 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4018}
4019
4020static ssize_t
4021suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4022{
4023 char *e;
4024 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004025 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004026
NeilBrownb8d966e2009-07-01 11:14:04 +10004027 if (mddev->pers == NULL ||
4028 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004029 return -EINVAL;
4030 if (buf == e || (*e && *e != '\n'))
4031 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004032
4033 mddev->suspend_lo = new;
4034 if (new >= old)
4035 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004036 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004037 else {
4038 /* Expanding suspended region - need to wait */
4039 mddev->pers->quiesce(mddev, 1);
4040 mddev->pers->quiesce(mddev, 0);
4041 }
4042 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004043}
4044static struct md_sysfs_entry md_suspend_lo =
4045__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4046
4047
4048static ssize_t
4049suspend_hi_show(mddev_t *mddev, char *page)
4050{
4051 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4052}
4053
4054static ssize_t
4055suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4056{
4057 char *e;
4058 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004059 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004060
NeilBrownb8d966e2009-07-01 11:14:04 +10004061 if (mddev->pers == NULL ||
4062 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004063 return -EINVAL;
4064 if (buf == e || (*e && *e != '\n'))
4065 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004066
4067 mddev->suspend_hi = new;
4068 if (new <= old)
4069 /* Shrinking suspended region */
4070 mddev->pers->quiesce(mddev, 2);
4071 else {
4072 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004073 mddev->pers->quiesce(mddev, 1);
4074 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004075 }
4076 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004077}
4078static struct md_sysfs_entry md_suspend_hi =
4079__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4080
NeilBrown08a02ec2007-05-09 02:35:38 -07004081static ssize_t
4082reshape_position_show(mddev_t *mddev, char *page)
4083{
4084 if (mddev->reshape_position != MaxSector)
4085 return sprintf(page, "%llu\n",
4086 (unsigned long long)mddev->reshape_position);
4087 strcpy(page, "none\n");
4088 return 5;
4089}
4090
4091static ssize_t
4092reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4093{
4094 char *e;
4095 unsigned long long new = simple_strtoull(buf, &e, 10);
4096 if (mddev->pers)
4097 return -EBUSY;
4098 if (buf == e || (*e && *e != '\n'))
4099 return -EINVAL;
4100 mddev->reshape_position = new;
4101 mddev->delta_disks = 0;
4102 mddev->new_level = mddev->level;
4103 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004104 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004105 return len;
4106}
4107
4108static struct md_sysfs_entry md_reshape_position =
4109__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4110 reshape_position_store);
4111
Dan Williamsb522adc2009-03-31 15:00:31 +11004112static ssize_t
4113array_size_show(mddev_t *mddev, char *page)
4114{
4115 if (mddev->external_size)
4116 return sprintf(page, "%llu\n",
4117 (unsigned long long)mddev->array_sectors/2);
4118 else
4119 return sprintf(page, "default\n");
4120}
4121
4122static ssize_t
4123array_size_store(mddev_t *mddev, const char *buf, size_t len)
4124{
4125 sector_t sectors;
4126
4127 if (strncmp(buf, "default", 7) == 0) {
4128 if (mddev->pers)
4129 sectors = mddev->pers->size(mddev, 0, 0);
4130 else
4131 sectors = mddev->array_sectors;
4132
4133 mddev->external_size = 0;
4134 } else {
4135 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4136 return -EINVAL;
4137 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004138 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004139
4140 mddev->external_size = 1;
4141 }
4142
4143 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004144 if (mddev->pers) {
4145 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004146 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004147 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004148 return len;
4149}
4150
4151static struct md_sysfs_entry md_array_size =
4152__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4153 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004154
NeilBrowneae17012005-11-08 21:39:23 -08004155static struct attribute *md_default_attrs[] = {
4156 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004157 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004158 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004159 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004160 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004161 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004162 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004163 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004164 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004165 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004166 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004167 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004168 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004169 NULL,
4170};
4171
4172static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004173 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004174 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004175 &md_sync_min.attr,
4176 &md_sync_max.attr,
4177 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004178 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004179 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004180 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004181 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004182 &md_suspend_lo.attr,
4183 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004184 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004185 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004186 NULL,
4187};
NeilBrown411036f2005-11-08 21:39:40 -08004188static struct attribute_group md_redundancy_group = {
4189 .name = NULL,
4190 .attrs = md_redundancy_attrs,
4191};
4192
NeilBrowneae17012005-11-08 21:39:23 -08004193
4194static ssize_t
4195md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4196{
4197 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4198 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004199 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004200
4201 if (!entry->show)
4202 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004203 rv = mddev_lock(mddev);
4204 if (!rv) {
4205 rv = entry->show(mddev, page);
4206 mddev_unlock(mddev);
4207 }
NeilBrown96de1e62005-11-08 21:39:39 -08004208 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004209}
4210
4211static ssize_t
4212md_attr_store(struct kobject *kobj, struct attribute *attr,
4213 const char *page, size_t length)
4214{
4215 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4216 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004217 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004218
4219 if (!entry->store)
4220 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004221 if (!capable(CAP_SYS_ADMIN))
4222 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004223 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004224 if (mddev->hold_active == UNTIL_IOCTL)
4225 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004226 if (!rv) {
4227 rv = entry->store(mddev, page, length);
4228 mddev_unlock(mddev);
4229 }
NeilBrown96de1e62005-11-08 21:39:39 -08004230 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004231}
4232
4233static void md_free(struct kobject *ko)
4234{
4235 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004236
4237 if (mddev->sysfs_state)
4238 sysfs_put(mddev->sysfs_state);
4239
4240 if (mddev->gendisk) {
4241 del_gendisk(mddev->gendisk);
4242 put_disk(mddev->gendisk);
4243 }
4244 if (mddev->queue)
4245 blk_cleanup_queue(mddev->queue);
4246
NeilBrowneae17012005-11-08 21:39:23 -08004247 kfree(mddev);
4248}
4249
Emese Revfy52cf25d2010-01-19 02:58:23 +01004250static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004251 .show = md_attr_show,
4252 .store = md_attr_store,
4253};
4254static struct kobj_type md_ktype = {
4255 .release = md_free,
4256 .sysfs_ops = &md_sysfs_ops,
4257 .default_attrs = md_default_attrs,
4258};
4259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260int mdp_major = 0;
4261
Dan Williams5fd3a172009-03-04 00:57:25 -07004262static void mddev_delayed_delete(struct work_struct *ws)
4263{
4264 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4265
NeilBrown43a70502009-12-14 12:49:55 +11004266 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004267 kobject_del(&mddev->kobj);
4268 kobject_put(&mddev->kobj);
4269}
4270
NeilBrownefeb53c2009-01-09 08:31:10 +11004271static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004273 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 mddev_t *mddev = mddev_find(dev);
4275 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004276 int partitioned;
4277 int shift;
4278 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004279 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
4281 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004282 return -ENODEV;
4283
4284 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4285 shift = partitioned ? MdpMinorShift : 0;
4286 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Tejun Heoe804ac72010-10-15 15:36:08 +02004288 /* wait for any previous instance of this device to be
4289 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004290 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004291 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004292
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004293 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004294 error = -EEXIST;
4295 if (mddev->gendisk)
4296 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004297
4298 if (name) {
4299 /* Need to ensure that 'name' is not a duplicate.
4300 */
4301 mddev_t *mddev2;
4302 spin_lock(&all_mddevs_lock);
4303
4304 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4305 if (mddev2->gendisk &&
4306 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4307 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004308 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004309 }
4310 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
NeilBrown8b765392009-01-09 08:31:08 +11004312
NeilBrown0909dc42009-07-01 12:27:21 +10004313 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004314 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004315 if (!mddev->queue)
4316 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004317 mddev->queue->queuedata = mddev;
4318
NeilBrown409c57f2009-03-31 14:39:39 +11004319 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 disk = alloc_disk(1 << shift);
4322 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004323 blk_cleanup_queue(mddev->queue);
4324 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004325 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004327 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004329 if (name)
4330 strcpy(disk->disk_name, name);
4331 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004333 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 disk->fops = &md_fops;
4336 disk->private_data = mddev;
4337 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004338 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004339 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004340 * remove it now.
4341 */
4342 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 add_disk(disk);
4344 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004345 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4346 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004347 if (error) {
4348 /* This isn't possible, but as kobject_init_and_add is marked
4349 * __must_check, we must do something with the result
4350 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004351 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4352 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004353 error = 0;
4354 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004355 if (mddev->kobj.sd &&
4356 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004357 printk(KERN_DEBUG "pointless warning\n");
Darrick J. Wongbe20e6c2010-11-24 16:40:33 +11004358
4359 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown0909dc42009-07-01 12:27:21 +10004360 abort:
4361 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004362 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004363 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004364 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004365 }
NeilBrownd3374822009-01-09 08:31:10 +11004366 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004367 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004368}
4369
4370static struct kobject *md_probe(dev_t dev, int *part, void *data)
4371{
4372 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return NULL;
4374}
4375
NeilBrownefeb53c2009-01-09 08:31:10 +11004376static int add_named_array(const char *val, struct kernel_param *kp)
4377{
4378 /* val must be "md_*" where * is not all digits.
4379 * We allocate an array with a large free minor number, and
4380 * set the name to val. val must not already be an active name.
4381 */
4382 int len = strlen(val);
4383 char buf[DISK_NAME_LEN];
4384
4385 while (len && val[len-1] == '\n')
4386 len--;
4387 if (len >= DISK_NAME_LEN)
4388 return -E2BIG;
4389 strlcpy(buf, val, len+1);
4390 if (strncmp(buf, "md_", 3) != 0)
4391 return -EINVAL;
4392 return md_alloc(0, buf);
4393}
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395static void md_safemode_timeout(unsigned long data)
4396{
4397 mddev_t *mddev = (mddev_t *) data;
4398
Neil Brown0fd62b82008-06-28 08:31:36 +10004399 if (!atomic_read(&mddev->writes_pending)) {
4400 mddev->safemode = 1;
4401 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004402 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 md_wakeup_thread(mddev->thread);
4405}
4406
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004407static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
NeilBrown390ee602010-06-01 19:37:27 +10004409int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
NeilBrown2604b702006-01-06 00:20:36 -08004411 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004413 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
NeilBrowna757e642005-04-16 15:26:42 -07004415 if (list_empty(&mddev->disks))
4416 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
4419 if (mddev->pers)
4420 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004421 /* Cannot run until previous stop completes properly */
4422 if (mddev->sysfs_active)
4423 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 /*
4426 * Analyze all RAID superblock(s)
4427 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004428 if (!mddev->raid_disks) {
4429 if (!mddev->persistent)
4430 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004431 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
NeilBrownd9d166c2006-01-06 00:20:51 -08004434 if (mddev->level != LEVEL_NONE)
4435 request_module("md-level-%d", mddev->level);
4436 else if (mddev->clevel[0])
4437 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
4439 /*
4440 * Drop all container device buffers, from now on
4441 * the only valid external interface is through the md
4442 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004444 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004445 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 continue;
4447 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004448 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004449
4450 /* perform some consistency tests on the device.
4451 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004452 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004453 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004454 if (rdev->meta_bdev) {
4455 /* Nothing to check */;
4456 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004457 if (mddev->dev_sectors &&
4458 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004459 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004460 printk("md: %s: data overlaps metadata\n",
4461 mdname(mddev));
4462 return -EINVAL;
4463 }
4464 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004465 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004466 > rdev->data_offset) {
4467 printk("md: %s: metadata overlaps data\n",
4468 mdname(mddev));
4469 return -EINVAL;
4470 }
4471 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004472 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 }
4474
NeilBrowna167f662010-10-26 18:31:13 +11004475 if (mddev->bio_set == NULL)
4476 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004479 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004480 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004482 if (mddev->level != LEVEL_NONE)
4483 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4484 mddev->level);
4485 else
4486 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4487 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return -EINVAL;
4489 }
NeilBrown2604b702006-01-06 00:20:36 -08004490 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004492 if (mddev->level != pers->level) {
4493 mddev->level = pers->level;
4494 mddev->new_level = pers->level;
4495 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004496 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
NeilBrownf6705572006-03-27 01:18:11 -08004498 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004499 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004500 /* This personality cannot handle reshaping... */
4501 mddev->pers = NULL;
4502 module_put(pers->owner);
4503 return -EINVAL;
4504 }
4505
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004506 if (pers->sync_request) {
4507 /* Warn if this is a potentially silly
4508 * configuration.
4509 */
4510 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4511 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004512 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004513
4514 list_for_each_entry(rdev, &mddev->disks, same_set)
4515 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004516 if (rdev < rdev2 &&
4517 rdev->bdev->bd_contains ==
4518 rdev2->bdev->bd_contains) {
4519 printk(KERN_WARNING
4520 "%s: WARNING: %s appears to be"
4521 " on the same physical disk as"
4522 " %s.\n",
4523 mdname(mddev),
4524 bdevname(rdev->bdev,b),
4525 bdevname(rdev2->bdev,b2));
4526 warned = 1;
4527 }
4528 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004529
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004530 if (warned)
4531 printk(KERN_WARNING
4532 "True protection against single-disk"
4533 " failure might be compromised.\n");
4534 }
4535
NeilBrown657390d2005-08-26 18:34:16 -07004536 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004537 /* may be over-ridden by personality */
4538 mddev->resync_max_sectors = mddev->dev_sectors;
4539
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004540 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
NeilBrown0f9552b2009-12-30 12:08:50 +11004542 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004543 mddev->ro = 2; /* read-only, but switch on first write */
4544
NeilBrownb15c2e52006-01-06 00:20:16 -08004545 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004546 if (err)
4547 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004548 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4549 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4550 " but 'external_size' not in effect?\n", __func__);
4551 printk(KERN_ERR
4552 "md: invalid array_size %llu > default size %llu\n",
4553 (unsigned long long)mddev->array_sectors / 2,
4554 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4555 err = -EINVAL;
4556 mddev->pers->stop(mddev);
4557 }
4558 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004559 err = bitmap_create(mddev);
4560 if (err) {
4561 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4562 mdname(mddev), err);
4563 mddev->pers->stop(mddev);
4564 }
4565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 module_put(mddev->pers->owner);
4568 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004569 bitmap_destroy(mddev);
4570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004572 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004573 if (mddev->kobj.sd &&
4574 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004575 printk(KERN_WARNING
4576 "md: cannot register extra attributes for %s\n",
4577 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004578 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004579 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004580 mddev->ro = 0;
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004583 atomic_set(&mddev->max_corr_read_errors,
4584 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 mddev->safemode = 0;
4586 mddev->safemode_timer.function = md_safemode_timeout;
4587 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004588 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004590 smp_wmb();
4591 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004592 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004593 if (rdev->raid_disk >= 0) {
4594 char nm[20];
4595 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004596 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004597 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
4600 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4601
NeilBrown850b2b42006-10-03 01:15:46 -07004602 if (mddev->flags)
4603 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
NeilBrown0b8c9de2006-07-10 04:44:15 -07004605 md_wakeup_thread(mddev->thread);
4606 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004607
NeilBrownd7603b72006-01-06 00:20:30 -08004608 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004609 sysfs_notify_dirent_safe(mddev->sysfs_state);
4610 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004611 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 return 0;
4613}
NeilBrown390ee602010-06-01 19:37:27 +10004614EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
NeilBrownfe60b012010-03-29 11:10:42 +11004616static int do_md_run(mddev_t *mddev)
4617{
4618 int err;
4619
4620 err = md_run(mddev);
4621 if (err)
4622 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004623 err = bitmap_load(mddev);
4624 if (err) {
4625 bitmap_destroy(mddev);
4626 goto out;
4627 }
NeilBrownfe60b012010-03-29 11:10:42 +11004628 set_capacity(mddev->gendisk, mddev->array_sectors);
4629 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004630 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004631 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4632out:
4633 return err;
4634}
4635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636static int restart_array(mddev_t *mddev)
4637{
4638 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Andre Noll80fab1d2008-07-11 22:02:21 +10004640 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004642 return -ENXIO;
4643 if (!mddev->pers)
4644 return -EINVAL;
4645 if (!mddev->ro)
4646 return -EBUSY;
4647 mddev->safemode = 0;
4648 mddev->ro = 0;
4649 set_disk_ro(disk, 0);
4650 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4651 mdname(mddev));
4652 /* Kick recovery or resync if necessary */
4653 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4654 md_wakeup_thread(mddev->thread);
4655 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004656 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
NeilBrownacc55e22006-06-26 00:27:47 -07004660/* similar to deny_write_access, but accounts for our holding a reference
4661 * to the file ourselves */
4662static int deny_bitmap_write_access(struct file * file)
4663{
4664 struct inode *inode = file->f_mapping->host;
4665
4666 spin_lock(&inode->i_lock);
4667 if (atomic_read(&inode->i_writecount) > 1) {
4668 spin_unlock(&inode->i_lock);
4669 return -ETXTBSY;
4670 }
4671 atomic_set(&inode->i_writecount, -1);
4672 spin_unlock(&inode->i_lock);
4673
4674 return 0;
4675}
4676
NeilBrown43a70502009-12-14 12:49:55 +11004677void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004678{
4679 struct inode *inode = file->f_mapping->host;
4680
4681 spin_lock(&inode->i_lock);
4682 atomic_set(&inode->i_writecount, 1);
4683 spin_unlock(&inode->i_lock);
4684}
4685
NeilBrown6177b472010-03-29 11:37:13 +11004686static void md_clean(mddev_t *mddev)
4687{
4688 mddev->array_sectors = 0;
4689 mddev->external_size = 0;
4690 mddev->dev_sectors = 0;
4691 mddev->raid_disks = 0;
4692 mddev->recovery_cp = 0;
4693 mddev->resync_min = 0;
4694 mddev->resync_max = MaxSector;
4695 mddev->reshape_position = MaxSector;
4696 mddev->external = 0;
4697 mddev->persistent = 0;
4698 mddev->level = LEVEL_NONE;
4699 mddev->clevel[0] = 0;
4700 mddev->flags = 0;
4701 mddev->ro = 0;
4702 mddev->metadata_type[0] = 0;
4703 mddev->chunk_sectors = 0;
4704 mddev->ctime = mddev->utime = 0;
4705 mddev->layout = 0;
4706 mddev->max_disks = 0;
4707 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004708 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004709 mddev->delta_disks = 0;
4710 mddev->new_level = LEVEL_NONE;
4711 mddev->new_layout = 0;
4712 mddev->new_chunk_sectors = 0;
4713 mddev->curr_resync = 0;
4714 mddev->resync_mismatches = 0;
4715 mddev->suspend_lo = mddev->suspend_hi = 0;
4716 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4717 mddev->recovery = 0;
4718 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004719 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004720 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004721 mddev->safemode = 0;
4722 mddev->bitmap_info.offset = 0;
4723 mddev->bitmap_info.default_offset = 0;
4724 mddev->bitmap_info.chunksize = 0;
4725 mddev->bitmap_info.daemon_sleep = 0;
4726 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004727 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004728}
4729
NeilBrowndefad612011-01-14 09:14:33 +11004730static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004731{
4732 if (mddev->sync_thread) {
4733 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4734 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004735 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004736 }
4737
4738 del_timer_sync(&mddev->safemode_timer);
4739
4740 bitmap_flush(mddev);
4741 md_super_wait(mddev);
4742
4743 if (!mddev->in_sync || mddev->flags) {
4744 /* mark array as shutdown cleanly */
4745 mddev->in_sync = 1;
4746 md_update_sb(mddev, 1);
4747 }
4748}
NeilBrowndefad612011-01-14 09:14:33 +11004749
4750void md_stop_writes(mddev_t *mddev)
4751{
4752 mddev_lock(mddev);
4753 __md_stop_writes(mddev);
4754 mddev_unlock(mddev);
4755}
NeilBrown390ee602010-06-01 19:37:27 +10004756EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004757
NeilBrown390ee602010-06-01 19:37:27 +10004758void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004759{
NeilBrown0ca69882011-01-14 09:14:33 +11004760 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004761 mddev->pers->stop(mddev);
4762 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4763 mddev->to_remove = &md_redundancy_group;
4764 module_put(mddev->pers->owner);
4765 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004766 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004767}
NeilBrown390ee602010-06-01 19:37:27 +10004768EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004769
NeilBrowna4bd82d2010-03-29 13:23:10 +11004770static int md_set_readonly(mddev_t *mddev, int is_open)
4771{
4772 int err = 0;
4773 mutex_lock(&mddev->open_mutex);
4774 if (atomic_read(&mddev->openers) > is_open) {
4775 printk("md: %s still in use.\n",mdname(mddev));
4776 err = -EBUSY;
4777 goto out;
4778 }
4779 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004780 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004781
4782 err = -ENXIO;
4783 if (mddev->ro==1)
4784 goto out;
4785 mddev->ro = 1;
4786 set_disk_ro(mddev->gendisk, 1);
4787 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004788 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004789 err = 0;
4790 }
4791out:
4792 mutex_unlock(&mddev->open_mutex);
4793 return err;
4794}
4795
NeilBrown9e653b62006-06-26 00:27:58 -07004796/* mode:
4797 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004798 * 2 - stop but do not disassemble array
4799 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004800static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004803 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
NeilBrownc8c00a62009-08-10 12:50:52 +10004805 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004806 if (atomic_read(&mddev->openers) > is_open ||
4807 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004808 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004809 mutex_unlock(&mddev->open_mutex);
4810 return -EBUSY;
4811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
NeilBrown6e17b022010-08-07 21:41:19 +10004813 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004814 if (mddev->ro)
4815 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004816
NeilBrowndefad612011-01-14 09:14:33 +11004817 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004818 md_stop(mddev);
4819 mddev->queue->merge_bvec_fn = NULL;
4820 mddev->queue->unplug_fn = NULL;
4821 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004822
NeilBrowna4bd82d2010-03-29 13:23:10 +11004823 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004824 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004825
NeilBrowna4bd82d2010-03-29 13:23:10 +11004826 list_for_each_entry(rdev, &mddev->disks, same_set)
4827 if (rdev->raid_disk >= 0) {
4828 char nm[20];
4829 sprintf(nm, "rd%d", rdev->raid_disk);
4830 sysfs_remove_link(&mddev->kobj, nm);
4831 }
NeilBrownc4647292009-05-07 12:51:06 +10004832
NeilBrowna4bd82d2010-03-29 13:23:10 +11004833 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004834 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004835 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004836 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004837
NeilBrowna4bd82d2010-03-29 13:23:10 +11004838 if (mddev->ro)
4839 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004840 } else
4841 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 /*
4843 * Free resources if final stop
4844 */
NeilBrown9e653b62006-06-26 00:27:58 -07004845 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4847
NeilBrown978f9462006-02-02 14:28:05 -08004848 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004849 if (mddev->bitmap_info.file) {
4850 restore_bitmap_write_access(mddev->bitmap_info.file);
4851 fput(mddev->bitmap_info.file);
4852 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004853 }
NeilBrownc3d97142009-12-14 12:49:52 +11004854 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 export_array(mddev);
4857
NeilBrown6177b472010-03-29 11:37:13 +11004858 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004859 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004860 if (mddev->hold_active == UNTIL_STOP)
4861 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004862 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004863 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004864 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004865 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867}
4868
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004869#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870static void autorun_array(mddev_t *mddev)
4871{
4872 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 int err;
4874
NeilBrowna757e642005-04-16 15:26:42 -07004875 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
4878 printk(KERN_INFO "md: running: ");
4879
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004880 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 char b[BDEVNAME_SIZE];
4882 printk("<%s>", bdevname(rdev->bdev,b));
4883 }
4884 printk("\n");
4885
NeilBrownd710e132008-10-13 11:55:12 +11004886 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 if (err) {
4888 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004889 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 }
4891}
4892
4893/*
4894 * lets try to run arrays based on all disks that have arrived
4895 * until now. (those are in pending_raid_disks)
4896 *
4897 * the method: pick the first pending disk, collect all disks with
4898 * the same UUID, remove all from the pending list and put them into
4899 * the 'same_array' list. Then order this list based on superblock
4900 * update time (freshest comes first), kick out 'old' disks and
4901 * compare superblocks. If everything's fine then run it.
4902 *
4903 * If "unit" is allocated, then bump its reference count
4904 */
4905static void autorun_devices(int part)
4906{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004907 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 mddev_t *mddev;
4909 char b[BDEVNAME_SIZE];
4910
4911 printk(KERN_INFO "md: autorun ...\n");
4912 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004913 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004915 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 rdev0 = list_entry(pending_raid_disks.next,
4917 mdk_rdev_t, same_set);
4918
4919 printk(KERN_INFO "md: considering %s ...\n",
4920 bdevname(rdev0->bdev,b));
4921 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004922 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (super_90_load(rdev, rdev0, 0) >= 0) {
4924 printk(KERN_INFO "md: adding %s ...\n",
4925 bdevname(rdev->bdev,b));
4926 list_move(&rdev->same_set, &candidates);
4927 }
4928 /*
4929 * now we have a set of devices, with all of them having
4930 * mostly sane superblocks. It's time to allocate the
4931 * mddev.
4932 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004933 if (part) {
4934 dev = MKDEV(mdp_major,
4935 rdev0->preferred_minor << MdpMinorShift);
4936 unit = MINOR(dev) >> MdpMinorShift;
4937 } else {
4938 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4939 unit = MINOR(dev);
4940 }
4941 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4943 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4944 break;
4945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947 md_probe(dev, NULL, NULL);
4948 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004949 if (!mddev || !mddev->gendisk) {
4950 if (mddev)
4951 mddev_put(mddev);
4952 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 "md: cannot allocate memory for md drive.\n");
4954 break;
4955 }
4956 if (mddev_lock(mddev))
4957 printk(KERN_WARNING "md: %s locked, cannot run\n",
4958 mdname(mddev));
4959 else if (mddev->raid_disks || mddev->major_version
4960 || !list_empty(&mddev->disks)) {
4961 printk(KERN_WARNING
4962 "md: %s already running, cannot run %s\n",
4963 mdname(mddev), bdevname(rdev0->bdev,b));
4964 mddev_unlock(mddev);
4965 } else {
4966 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004967 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004968 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 list_del_init(&rdev->same_set);
4970 if (bind_rdev_to_array(rdev, mddev))
4971 export_rdev(rdev);
4972 }
4973 autorun_array(mddev);
4974 mddev_unlock(mddev);
4975 }
4976 /* on success, candidates will be empty, on error
4977 * it won't...
4978 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004979 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004980 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 mddev_put(mddev);
4984 }
4985 printk(KERN_INFO "md: ... autorun DONE.\n");
4986}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004987#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989static int get_version(void __user * arg)
4990{
4991 mdu_version_t ver;
4992
4993 ver.major = MD_MAJOR_VERSION;
4994 ver.minor = MD_MINOR_VERSION;
4995 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4996
4997 if (copy_to_user(arg, &ver, sizeof(ver)))
4998 return -EFAULT;
4999
5000 return 0;
5001}
5002
5003static int get_array_info(mddev_t * mddev, void __user * arg)
5004{
5005 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005006 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
NeilBrowna9f326e2009-09-23 18:06:41 +10005009 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005010 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005012 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 failed++;
5014 else {
5015 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005016 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005017 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 else
5019 spare++;
5020 }
5021 }
5022
5023 info.major_version = mddev->major_version;
5024 info.minor_version = mddev->minor_version;
5025 info.patch_version = MD_PATCHLEVEL_VERSION;
5026 info.ctime = mddev->ctime;
5027 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005028 info.size = mddev->dev_sectors / 2;
5029 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005030 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 info.nr_disks = nr;
5032 info.raid_disks = mddev->raid_disks;
5033 info.md_minor = mddev->md_minor;
5034 info.not_persistent= !mddev->persistent;
5035
5036 info.utime = mddev->utime;
5037 info.state = 0;
5038 if (mddev->in_sync)
5039 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005040 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005041 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005042 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 info.working_disks = working;
5044 info.failed_disks = failed;
5045 info.spare_disks = spare;
5046
5047 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005048 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049
5050 if (copy_to_user(arg, &info, sizeof(info)))
5051 return -EFAULT;
5052
5053 return 0;
5054}
5055
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005056static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005057{
5058 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5059 char *ptr, *buf = NULL;
5060 int err = -ENOMEM;
5061
Dan Williamsb5470dc2008-06-27 21:44:04 -07005062 if (md_allow_write(mddev))
5063 file = kmalloc(sizeof(*file), GFP_NOIO);
5064 else
5065 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005066
NeilBrown32a76272005-06-21 17:17:14 -07005067 if (!file)
5068 goto out;
5069
5070 /* bitmap disabled, zero the first byte and copy out */
5071 if (!mddev->bitmap || !mddev->bitmap->file) {
5072 file->pathname[0] = '\0';
5073 goto copy_out;
5074 }
5075
5076 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5077 if (!buf)
5078 goto out;
5079
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005080 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5081 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005082 goto out;
5083
5084 strcpy(file->pathname, ptr);
5085
5086copy_out:
5087 err = 0;
5088 if (copy_to_user(arg, file, sizeof(*file)))
5089 err = -EFAULT;
5090out:
5091 kfree(buf);
5092 kfree(file);
5093 return err;
5094}
5095
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096static int get_disk_info(mddev_t * mddev, void __user * arg)
5097{
5098 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 mdk_rdev_t *rdev;
5100
5101 if (copy_from_user(&info, arg, sizeof(info)))
5102 return -EFAULT;
5103
Andre Noll26ef3792008-07-11 22:02:21 +10005104 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 if (rdev) {
5106 info.major = MAJOR(rdev->bdev->bd_dev);
5107 info.minor = MINOR(rdev->bdev->bd_dev);
5108 info.raid_disk = rdev->raid_disk;
5109 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005110 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005112 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 info.state |= (1<<MD_DISK_ACTIVE);
5114 info.state |= (1<<MD_DISK_SYNC);
5115 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005116 if (test_bit(WriteMostly, &rdev->flags))
5117 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 } else {
5119 info.major = info.minor = 0;
5120 info.raid_disk = -1;
5121 info.state = (1<<MD_DISK_REMOVED);
5122 }
5123
5124 if (copy_to_user(arg, &info, sizeof(info)))
5125 return -EFAULT;
5126
5127 return 0;
5128}
5129
5130static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5131{
5132 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5133 mdk_rdev_t *rdev;
5134 dev_t dev = MKDEV(info->major,info->minor);
5135
5136 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5137 return -EOVERFLOW;
5138
5139 if (!mddev->raid_disks) {
5140 int err;
5141 /* expecting a device which has a superblock */
5142 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5143 if (IS_ERR(rdev)) {
5144 printk(KERN_WARNING
5145 "md: md_import_device returned %ld\n",
5146 PTR_ERR(rdev));
5147 return PTR_ERR(rdev);
5148 }
5149 if (!list_empty(&mddev->disks)) {
5150 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5151 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005152 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 .load_super(rdev, rdev0, mddev->minor_version);
5154 if (err < 0) {
5155 printk(KERN_WARNING
5156 "md: %s has different UUID to %s\n",
5157 bdevname(rdev->bdev,b),
5158 bdevname(rdev0->bdev,b2));
5159 export_rdev(rdev);
5160 return -EINVAL;
5161 }
5162 }
5163 err = bind_rdev_to_array(rdev, mddev);
5164 if (err)
5165 export_rdev(rdev);
5166 return err;
5167 }
5168
5169 /*
5170 * add_new_disk can be used once the array is assembled
5171 * to add "hot spares". They must already have a superblock
5172 * written
5173 */
5174 if (mddev->pers) {
5175 int err;
5176 if (!mddev->pers->hot_add_disk) {
5177 printk(KERN_WARNING
5178 "%s: personality does not support diskops!\n",
5179 mdname(mddev));
5180 return -EINVAL;
5181 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005182 if (mddev->persistent)
5183 rdev = md_import_device(dev, mddev->major_version,
5184 mddev->minor_version);
5185 else
5186 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 if (IS_ERR(rdev)) {
5188 printk(KERN_WARNING
5189 "md: md_import_device returned %ld\n",
5190 PTR_ERR(rdev));
5191 return PTR_ERR(rdev);
5192 }
NeilBrown1a855a02010-12-09 16:36:28 +11005193 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005194 if (!mddev->persistent) {
5195 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005196 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005197 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005198 set_bit(In_sync, &rdev->flags);
5199 } else
NeilBrown41158c72005-06-21 17:17:25 -07005200 rdev->raid_disk = -1;
5201 } else
5202 super_types[mddev->major_version].
5203 validate_super(mddev, rdev);
NeilBrown1a855a02010-12-09 16:36:28 +11005204 if (test_bit(In_sync, &rdev->flags))
5205 rdev->saved_raid_disk = rdev->raid_disk;
5206 else
5207 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005208
NeilBrownb2d444d2005-11-08 21:39:31 -08005209 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005210 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5211 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005212 else
5213 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005214
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 rdev->raid_disk = -1;
5216 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005217 if (!err && !mddev->pers->hot_remove_disk) {
5218 /* If there is hot_add_disk but no hot_remove_disk
5219 * then added disks for geometry changes,
5220 * and should be added immediately.
5221 */
5222 super_types[mddev->major_version].
5223 validate_super(mddev, rdev);
5224 err = mddev->pers->hot_add_disk(mddev, rdev);
5225 if (err)
5226 unbind_rdev_from_array(rdev);
5227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 if (err)
5229 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005230 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005231 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005232
NeilBrown17571282006-12-10 02:20:52 -08005233 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005234 if (mddev->degraded)
5235 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005236 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005237 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 return err;
5239 }
5240
5241 /* otherwise, add_new_disk is only allowed
5242 * for major_version==0 superblocks
5243 */
5244 if (mddev->major_version != 0) {
5245 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5246 mdname(mddev));
5247 return -EINVAL;
5248 }
5249
5250 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5251 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005252 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 if (IS_ERR(rdev)) {
5254 printk(KERN_WARNING
5255 "md: error, md_import_device() returned %ld\n",
5256 PTR_ERR(rdev));
5257 return PTR_ERR(rdev);
5258 }
5259 rdev->desc_nr = info->number;
5260 if (info->raid_disk < mddev->raid_disks)
5261 rdev->raid_disk = info->raid_disk;
5262 else
5263 rdev->raid_disk = -1;
5264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005266 if (info->state & (1<<MD_DISK_SYNC))
5267 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005269 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5270 set_bit(WriteMostly, &rdev->flags);
5271
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 if (!mddev->persistent) {
5273 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005274 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5275 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005276 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005277 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
NeilBrown2bf071b2006-01-06 00:20:55 -08005279 err = bind_rdev_to_array(rdev, mddev);
5280 if (err) {
5281 export_rdev(rdev);
5282 return err;
5283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 }
5285
5286 return 0;
5287}
5288
5289static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5290{
5291 char b[BDEVNAME_SIZE];
5292 mdk_rdev_t *rdev;
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 rdev = find_rdev(mddev, dev);
5295 if (!rdev)
5296 return -ENXIO;
5297
5298 if (rdev->raid_disk >= 0)
5299 goto busy;
5300
5301 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005302 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005303 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
5305 return 0;
5306busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005307 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 bdevname(rdev->bdev,b), mdname(mddev));
5309 return -EBUSY;
5310}
5311
5312static int hot_add_disk(mddev_t * mddev, dev_t dev)
5313{
5314 char b[BDEVNAME_SIZE];
5315 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 mdk_rdev_t *rdev;
5317
5318 if (!mddev->pers)
5319 return -ENODEV;
5320
5321 if (mddev->major_version != 0) {
5322 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5323 " version-0 superblocks.\n",
5324 mdname(mddev));
5325 return -EINVAL;
5326 }
5327 if (!mddev->pers->hot_add_disk) {
5328 printk(KERN_WARNING
5329 "%s: personality does not support diskops!\n",
5330 mdname(mddev));
5331 return -EINVAL;
5332 }
5333
NeilBrownd710e132008-10-13 11:55:12 +11005334 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 if (IS_ERR(rdev)) {
5336 printk(KERN_WARNING
5337 "md: error, md_import_device() returned %ld\n",
5338 PTR_ERR(rdev));
5339 return -EINVAL;
5340 }
5341
5342 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005343 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005345 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
NeilBrown8190e752009-06-18 08:48:58 +10005347 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
NeilBrownb2d444d2005-11-08 21:39:31 -08005349 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 printk(KERN_WARNING
5351 "md: can not hot-add faulty %s disk to %s!\n",
5352 bdevname(rdev->bdev,b), mdname(mddev));
5353 err = -EINVAL;
5354 goto abort_export;
5355 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005356 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005358 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005359 err = bind_rdev_to_array(rdev, mddev);
5360 if (err)
5361 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
5363 /*
5364 * The rest should better be atomic, we can have disk failures
5365 * noticed in interrupt contexts ...
5366 */
5367
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 rdev->raid_disk = -1;
5369
NeilBrown850b2b42006-10-03 01:15:46 -07005370 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371
5372 /*
5373 * Kick recovery, maybe this spare has to be added to the
5374 * array immediately.
5375 */
5376 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5377 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005378 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return 0;
5380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381abort_export:
5382 export_rdev(rdev);
5383 return err;
5384}
5385
NeilBrown32a76272005-06-21 17:17:14 -07005386static int set_bitmap_file(mddev_t *mddev, int fd)
5387{
5388 int err;
5389
NeilBrown36fa3062005-09-09 16:23:45 -07005390 if (mddev->pers) {
5391 if (!mddev->pers->quiesce)
5392 return -EBUSY;
5393 if (mddev->recovery || mddev->sync_thread)
5394 return -EBUSY;
5395 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005396 }
5397
NeilBrown36fa3062005-09-09 16:23:45 -07005398
5399 if (fd >= 0) {
5400 if (mddev->bitmap)
5401 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005402 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005403
NeilBrownc3d97142009-12-14 12:49:52 +11005404 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005405 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5406 mdname(mddev));
5407 return -EBADF;
5408 }
5409
NeilBrownc3d97142009-12-14 12:49:52 +11005410 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005411 if (err) {
5412 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5413 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005414 fput(mddev->bitmap_info.file);
5415 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005416 return err;
5417 }
NeilBrownc3d97142009-12-14 12:49:52 +11005418 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005419 } else if (mddev->bitmap == NULL)
5420 return -ENOENT; /* cannot remove what isn't there */
5421 err = 0;
5422 if (mddev->pers) {
5423 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005424 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005425 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005426 if (!err)
5427 err = bitmap_load(mddev);
5428 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005429 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005430 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005431 fd = -1; /* make sure to put the file */
5432 }
NeilBrown36fa3062005-09-09 16:23:45 -07005433 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005434 }
5435 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005436 if (mddev->bitmap_info.file) {
5437 restore_bitmap_write_access(mddev->bitmap_info.file);
5438 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005439 }
NeilBrownc3d97142009-12-14 12:49:52 +11005440 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005441 }
5442
NeilBrown32a76272005-06-21 17:17:14 -07005443 return err;
5444}
5445
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446/*
5447 * set_array_info is used two different ways
5448 * The original usage is when creating a new array.
5449 * In this usage, raid_disks is > 0 and it together with
5450 * level, size, not_persistent,layout,chunksize determine the
5451 * shape of the array.
5452 * This will always create an array with a type-0.90.0 superblock.
5453 * The newer usage is when assembling an array.
5454 * In this case raid_disks will be 0, and the major_version field is
5455 * use to determine which style super-blocks are to be found on the devices.
5456 * The minor and patch _version numbers are also kept incase the
5457 * super_block handler wishes to interpret them.
5458 */
5459static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5460{
5461
5462 if (info->raid_disks == 0) {
5463 /* just setting version number for superblock loading */
5464 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005465 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 super_types[info->major_version].name == NULL) {
5467 /* maybe try to auto-load a module? */
5468 printk(KERN_INFO
5469 "md: superblock version %d not known\n",
5470 info->major_version);
5471 return -EINVAL;
5472 }
5473 mddev->major_version = info->major_version;
5474 mddev->minor_version = info->minor_version;
5475 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005476 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005477 /* ensure mddev_put doesn't delete this now that there
5478 * is some minimal configuration.
5479 */
5480 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 return 0;
5482 }
5483 mddev->major_version = MD_MAJOR_VERSION;
5484 mddev->minor_version = MD_MINOR_VERSION;
5485 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5486 mddev->ctime = get_seconds();
5487
5488 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005489 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005490 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 mddev->raid_disks = info->raid_disks;
5492 /* don't set md_minor, it is determined by which /dev/md* was
5493 * openned
5494 */
5495 if (info->state & (1<<MD_SB_CLEAN))
5496 mddev->recovery_cp = MaxSector;
5497 else
5498 mddev->recovery_cp = 0;
5499 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005500 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
5502 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005503 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 mddev->max_disks = MD_SB_DISKS;
5506
NeilBrowne6910632008-02-06 01:39:51 -08005507 if (mddev->persistent)
5508 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005509 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
NeilBrownc3d97142009-12-14 12:49:52 +11005511 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5512 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005513
NeilBrownf6705572006-03-27 01:18:11 -08005514 mddev->reshape_position = MaxSector;
5515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /*
5517 * Generate a 128 bit UUID
5518 */
5519 get_random_bytes(mddev->uuid, 16);
5520
NeilBrownf6705572006-03-27 01:18:11 -08005521 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005522 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005523 mddev->new_layout = mddev->layout;
5524 mddev->delta_disks = 0;
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 return 0;
5527}
5528
Dan Williams1f403622009-03-31 14:59:03 +11005529void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5530{
Dan Williamsb522adc2009-03-31 15:00:31 +11005531 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5532
5533 if (mddev->external_size)
5534 return;
5535
Dan Williams1f403622009-03-31 14:59:03 +11005536 mddev->array_sectors = array_sectors;
5537}
5538EXPORT_SYMBOL(md_set_array_sectors);
5539
Andre Nolld71f9f82008-07-11 22:02:22 +10005540static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005541{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005542 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005543 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005544 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005545
5546 if (mddev->pers->resize == NULL)
5547 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005548 /* The "num_sectors" is the number of sectors of each device that
5549 * is used. This can only make sense for arrays with redundancy.
5550 * linear and raid0 always use whatever space is available. We can only
5551 * consider changing this number if no resync or reconstruction is
5552 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005553 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005554 * of each device. If num_sectors is zero, we find the largest size
5555 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005556 */
5557 if (mddev->sync_thread)
5558 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005559 if (mddev->bitmap)
5560 /* Sorry, cannot grow a bitmap yet, just remove it,
5561 * grow, and re-add.
5562 */
5563 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005564 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005565 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005566
Andre Nolld71f9f82008-07-11 22:02:22 +10005567 if (fit && (num_sectors == 0 || num_sectors > avail))
5568 num_sectors = avail;
5569 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005570 return -ENOSPC;
5571 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005572 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005573 if (!rv)
5574 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005575 return rv;
5576}
5577
NeilBrownda943b92006-01-06 00:20:54 -08005578static int update_raid_disks(mddev_t *mddev, int raid_disks)
5579{
5580 int rv;
5581 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005582 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005583 return -EINVAL;
5584 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005585 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005586 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005587 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005588 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005589 mddev->delta_disks = raid_disks - mddev->raid_disks;
5590
5591 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005592 if (rv < 0)
5593 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005594 return rv;
5595}
5596
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598/*
5599 * update_array_info is used to change the configuration of an
5600 * on-line array.
5601 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5602 * fields in the info are checked against the array.
5603 * Any differences that cannot be handled will cause an error.
5604 * Normally, only one change can be managed at a time.
5605 */
5606static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5607{
5608 int rv = 0;
5609 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005610 int state = 0;
5611
5612 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005613 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005614 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
5616 if (mddev->major_version != info->major_version ||
5617 mddev->minor_version != info->minor_version ||
5618/* mddev->patch_version != info->patch_version || */
5619 mddev->ctime != info->ctime ||
5620 mddev->level != info->level ||
5621/* mddev->layout != info->layout || */
5622 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005623 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005624 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5625 ((state^info->state) & 0xfffffe00)
5626 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return -EINVAL;
5628 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005629 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5630 cnt++;
5631 if (mddev->raid_disks != info->raid_disks)
5632 cnt++;
5633 if (mddev->layout != info->layout)
5634 cnt++;
5635 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5636 cnt++;
5637 if (cnt == 0)
5638 return 0;
5639 if (cnt > 1)
5640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 if (mddev->layout != info->layout) {
5643 /* Change layout
5644 * we don't need to do anything at the md level, the
5645 * personality will take care of it all.
5646 */
NeilBrown50ac1682009-06-18 08:47:55 +10005647 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005649 else {
5650 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005651 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005652 if (rv)
5653 mddev->new_layout = mddev->layout;
5654 return rv;
5655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005657 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005658 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
NeilBrownda943b92006-01-06 00:20:54 -08005660 if (mddev->raid_disks != info->raid_disks)
5661 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
NeilBrown36fa3062005-09-09 16:23:45 -07005663 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5664 if (mddev->pers->quiesce == NULL)
5665 return -EINVAL;
5666 if (mddev->recovery || mddev->sync_thread)
5667 return -EBUSY;
5668 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5669 /* add the bitmap */
5670 if (mddev->bitmap)
5671 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005672 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005673 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005674 mddev->bitmap_info.offset =
5675 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005676 mddev->pers->quiesce(mddev, 1);
5677 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005678 if (!rv)
5679 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005680 if (rv)
5681 bitmap_destroy(mddev);
5682 mddev->pers->quiesce(mddev, 0);
5683 } else {
5684 /* remove the bitmap */
5685 if (!mddev->bitmap)
5686 return -ENOENT;
5687 if (mddev->bitmap->file)
5688 return -EINVAL;
5689 mddev->pers->quiesce(mddev, 1);
5690 bitmap_destroy(mddev);
5691 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005692 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005693 }
5694 }
NeilBrown850b2b42006-10-03 01:15:46 -07005695 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 return rv;
5697}
5698
5699static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5700{
5701 mdk_rdev_t *rdev;
5702
5703 if (mddev->pers == NULL)
5704 return -ENODEV;
5705
5706 rdev = find_rdev(mddev, dev);
5707 if (!rdev)
5708 return -ENODEV;
5709
5710 md_error(mddev, rdev);
5711 return 0;
5712}
5713
Andre Noll2f9618c2008-04-25 18:57:58 +02005714/*
5715 * We have a problem here : there is no easy way to give a CHS
5716 * virtual geometry. We currently pretend that we have a 2 heads
5717 * 4 sectors (with a BIG number of cylinders...). This drives
5718 * dosfs just mad... ;-)
5719 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005720static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5721{
5722 mddev_t *mddev = bdev->bd_disk->private_data;
5723
5724 geo->heads = 2;
5725 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005726 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005727 return 0;
5728}
5729
Al Viroa39907f2008-03-02 10:31:15 -05005730static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 unsigned int cmd, unsigned long arg)
5732{
5733 int err = 0;
5734 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005736 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
5738 if (!capable(CAP_SYS_ADMIN))
5739 return -EACCES;
5740
5741 /*
5742 * Commands dealing with the RAID driver but not any
5743 * particular array:
5744 */
5745 switch (cmd)
5746 {
5747 case RAID_VERSION:
5748 err = get_version(argp);
5749 goto done;
5750
5751 case PRINT_RAID_DEBUG:
5752 err = 0;
5753 md_print_devices();
5754 goto done;
5755
5756#ifndef MODULE
5757 case RAID_AUTORUN:
5758 err = 0;
5759 autostart_arrays(arg);
5760 goto done;
5761#endif
5762 default:;
5763 }
5764
5765 /*
5766 * Commands creating/starting a new array:
5767 */
5768
Al Viroa39907f2008-03-02 10:31:15 -05005769 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770
5771 if (!mddev) {
5772 BUG();
5773 goto abort;
5774 }
5775
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 err = mddev_lock(mddev);
5777 if (err) {
5778 printk(KERN_INFO
5779 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5780 err, cmd);
5781 goto abort;
5782 }
5783
5784 switch (cmd)
5785 {
5786 case SET_ARRAY_INFO:
5787 {
5788 mdu_array_info_t info;
5789 if (!arg)
5790 memset(&info, 0, sizeof(info));
5791 else if (copy_from_user(&info, argp, sizeof(info))) {
5792 err = -EFAULT;
5793 goto abort_unlock;
5794 }
5795 if (mddev->pers) {
5796 err = update_array_info(mddev, &info);
5797 if (err) {
5798 printk(KERN_WARNING "md: couldn't update"
5799 " array info. %d\n", err);
5800 goto abort_unlock;
5801 }
5802 goto done_unlock;
5803 }
5804 if (!list_empty(&mddev->disks)) {
5805 printk(KERN_WARNING
5806 "md: array %s already has disks!\n",
5807 mdname(mddev));
5808 err = -EBUSY;
5809 goto abort_unlock;
5810 }
5811 if (mddev->raid_disks) {
5812 printk(KERN_WARNING
5813 "md: array %s already initialised!\n",
5814 mdname(mddev));
5815 err = -EBUSY;
5816 goto abort_unlock;
5817 }
5818 err = set_array_info(mddev, &info);
5819 if (err) {
5820 printk(KERN_WARNING "md: couldn't set"
5821 " array info. %d\n", err);
5822 goto abort_unlock;
5823 }
5824 }
5825 goto done_unlock;
5826
5827 default:;
5828 }
5829
5830 /*
5831 * Commands querying/configuring an existing array:
5832 */
NeilBrown32a76272005-06-21 17:17:14 -07005833 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005834 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005835 if ((!mddev->raid_disks && !mddev->external)
5836 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5837 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5838 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 err = -ENODEV;
5840 goto abort_unlock;
5841 }
5842
5843 /*
5844 * Commands even a read-only array can execute:
5845 */
5846 switch (cmd)
5847 {
5848 case GET_ARRAY_INFO:
5849 err = get_array_info(mddev, argp);
5850 goto done_unlock;
5851
NeilBrown32a76272005-06-21 17:17:14 -07005852 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005853 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005854 goto done_unlock;
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 case GET_DISK_INFO:
5857 err = get_disk_info(mddev, argp);
5858 goto done_unlock;
5859
5860 case RESTART_ARRAY_RW:
5861 err = restart_array(mddev);
5862 goto done_unlock;
5863
5864 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005865 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 goto done_unlock;
5867
5868 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005869 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 goto done_unlock;
5871
Dan Williamse2218352010-05-12 08:25:37 +10005872 case BLKROSET:
5873 if (get_user(ro, (int __user *)(arg))) {
5874 err = -EFAULT;
5875 goto done_unlock;
5876 }
5877 err = -EINVAL;
5878
5879 /* if the bdev is going readonly the value of mddev->ro
5880 * does not matter, no writes are coming
5881 */
5882 if (ro)
5883 goto done_unlock;
5884
5885 /* are we are already prepared for writes? */
5886 if (mddev->ro != 1)
5887 goto done_unlock;
5888
5889 /* transitioning to readauto need only happen for
5890 * arrays that call md_write_start
5891 */
5892 if (mddev->pers) {
5893 err = restart_array(mddev);
5894 if (err == 0) {
5895 mddev->ro = 2;
5896 set_disk_ro(mddev->gendisk, 0);
5897 }
5898 }
5899 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 }
5901
5902 /*
5903 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005904 * superblock, so we do not allow them on read-only arrays.
5905 * However non-MD ioctls (e.g. get-size) will still come through
5906 * here and hit the 'default' below, so only disallow
5907 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005909 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005910 if (mddev->ro == 2) {
5911 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005912 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005913 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5914 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005915 } else {
5916 err = -EROFS;
5917 goto abort_unlock;
5918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 }
5920
5921 switch (cmd)
5922 {
5923 case ADD_NEW_DISK:
5924 {
5925 mdu_disk_info_t info;
5926 if (copy_from_user(&info, argp, sizeof(info)))
5927 err = -EFAULT;
5928 else
5929 err = add_new_disk(mddev, &info);
5930 goto done_unlock;
5931 }
5932
5933 case HOT_REMOVE_DISK:
5934 err = hot_remove_disk(mddev, new_decode_dev(arg));
5935 goto done_unlock;
5936
5937 case HOT_ADD_DISK:
5938 err = hot_add_disk(mddev, new_decode_dev(arg));
5939 goto done_unlock;
5940
5941 case SET_DISK_FAULTY:
5942 err = set_disk_faulty(mddev, new_decode_dev(arg));
5943 goto done_unlock;
5944
5945 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005946 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 goto done_unlock;
5948
NeilBrown32a76272005-06-21 17:17:14 -07005949 case SET_BITMAP_FILE:
5950 err = set_bitmap_file(mddev, (int)arg);
5951 goto done_unlock;
5952
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 err = -EINVAL;
5955 goto abort_unlock;
5956 }
5957
5958done_unlock:
5959abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005960 if (mddev->hold_active == UNTIL_IOCTL &&
5961 err != -EINVAL)
5962 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 mddev_unlock(mddev);
5964
5965 return err;
5966done:
5967 if (err)
5968 MD_BUG();
5969abort:
5970 return err;
5971}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005972#ifdef CONFIG_COMPAT
5973static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5974 unsigned int cmd, unsigned long arg)
5975{
5976 switch (cmd) {
5977 case HOT_REMOVE_DISK:
5978 case HOT_ADD_DISK:
5979 case SET_DISK_FAULTY:
5980 case SET_BITMAP_FILE:
5981 /* These take in integer arg, do not convert */
5982 break;
5983 default:
5984 arg = (unsigned long)compat_ptr(arg);
5985 break;
5986 }
5987
5988 return md_ioctl(bdev, mode, cmd, arg);
5989}
5990#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Al Viroa39907f2008-03-02 10:31:15 -05005992static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993{
5994 /*
5995 * Succeed if we can lock the mddev, which confirms that
5996 * it isn't being stopped right now.
5997 */
NeilBrownd3374822009-01-09 08:31:10 +11005998 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 int err;
6000
NeilBrownd3374822009-01-09 08:31:10 +11006001 if (mddev->gendisk != bdev->bd_disk) {
6002 /* we are racing with mddev_put which is discarding this
6003 * bd_disk.
6004 */
6005 mddev_put(mddev);
6006 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006007 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006008 /* Then retry the open from the top */
6009 return -ERESTARTSYS;
6010 }
6011 BUG_ON(mddev != bdev->bd_disk->private_data);
6012
NeilBrownc8c00a62009-08-10 12:50:52 +10006013 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 goto out;
6015
6016 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006017 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006018 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006020 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 out:
6022 return err;
6023}
6024
Al Viroa39907f2008-03-02 10:31:15 -05006025static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026{
Al Viroa39907f2008-03-02 10:31:15 -05006027 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006029 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006030 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 mddev_put(mddev);
6032
6033 return 0;
6034}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006035
6036static int md_media_changed(struct gendisk *disk)
6037{
6038 mddev_t *mddev = disk->private_data;
6039
6040 return mddev->changed;
6041}
6042
6043static int md_revalidate(struct gendisk *disk)
6044{
6045 mddev_t *mddev = disk->private_data;
6046
6047 mddev->changed = 0;
6048 return 0;
6049}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006050static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051{
6052 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006053 .open = md_open,
6054 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006055 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006056#ifdef CONFIG_COMPAT
6057 .compat_ioctl = md_compat_ioctl,
6058#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006059 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006060 .media_changed = md_media_changed,
6061 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062};
6063
Adrian Bunk75c96f82005-05-05 16:16:09 -07006064static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065{
6066 mdk_thread_t *thread = arg;
6067
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 /*
6069 * md_thread is a 'system-thread', it's priority should be very
6070 * high. We avoid resource deadlocks individually in each
6071 * raid personality. (RAID5 does preallocation) We also use RR and
6072 * the very same RT priority as kswapd, thus we will never get
6073 * into a priority inversion deadlock.
6074 *
6075 * we definitely have to have equal or higher priority than
6076 * bdflush, otherwise bdflush will deadlock if there are too
6077 * many dirty RAID5 blocks.
6078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
NeilBrown6985c432005-10-19 21:23:47 -07006080 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006081 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
NeilBrown93588e22005-11-15 00:09:12 -08006083 /* We need to wait INTERRUPTIBLE so that
6084 * we don't add to the load-average.
6085 * That means we need to be sure no signals are
6086 * pending
6087 */
6088 if (signal_pending(current))
6089 flush_signals(current);
6090
6091 wait_event_interruptible_timeout
6092 (thread->wqueue,
6093 test_bit(THREAD_WAKEUP, &thread->flags)
6094 || kthread_should_stop(),
6095 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
NeilBrown6c987912011-01-14 09:13:53 +11006097 clear_bit(THREAD_WAKEUP, &thread->flags);
6098 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006099 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006101
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 return 0;
6103}
6104
6105void md_wakeup_thread(mdk_thread_t *thread)
6106{
6107 if (thread) {
6108 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6109 set_bit(THREAD_WAKEUP, &thread->flags);
6110 wake_up(&thread->wqueue);
6111 }
6112}
6113
6114mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6115 const char *name)
6116{
6117 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118
NeilBrown9ffae0c2006-01-06 00:20:32 -08006119 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 if (!thread)
6121 return NULL;
6122
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 init_waitqueue_head(&thread->wqueue);
6124
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 thread->run = run;
6126 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006127 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006128 thread->tsk = kthread_run(md_thread, thread,
6129 "%s_%s",
6130 mdname(thread->mddev),
6131 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006132 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 kfree(thread);
6134 return NULL;
6135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 return thread;
6137}
6138
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139void md_unregister_thread(mdk_thread_t *thread)
6140{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006141 if (!thread)
6142 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006143 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006144
6145 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 kfree(thread);
6147}
6148
6149void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6150{
6151 if (!mddev) {
6152 MD_BUG();
6153 return;
6154 }
6155
NeilBrownb2d444d2005-11-08 21:39:31 -08006156 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006158
6159 if (mddev->external)
6160 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6163 mdname(mddev),
6164 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6165 __builtin_return_address(0),__builtin_return_address(1),
6166 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006167*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006168 if (!mddev->pers)
6169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 if (!mddev->pers->error_handler)
6171 return;
6172 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006173 if (mddev->degraded)
6174 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006175 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6177 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6178 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006179 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006180 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006181 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182}
6183
6184/* seq_file implementation /proc/mdstat */
6185
6186static void status_unused(struct seq_file *seq)
6187{
6188 int i = 0;
6189 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
6191 seq_printf(seq, "unused devices: ");
6192
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006193 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 char b[BDEVNAME_SIZE];
6195 i++;
6196 seq_printf(seq, "%s ",
6197 bdevname(rdev->bdev,b));
6198 }
6199 if (!i)
6200 seq_printf(seq, "<none>");
6201
6202 seq_printf(seq, "\n");
6203}
6204
6205
6206static void status_resync(struct seq_file *seq, mddev_t * mddev)
6207{
NeilBrowndd71cf62009-05-07 12:49:35 +10006208 sector_t max_sectors, resync, res;
6209 unsigned long dt, db;
6210 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006211 int scale;
6212 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
NeilBrowndd71cf62009-05-07 12:49:35 +10006214 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
6216 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006217 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006219 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
6221 /*
6222 * Should not happen.
6223 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006224 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 MD_BUG();
6226 return;
6227 }
NeilBrown4588b422006-03-27 01:18:04 -08006228 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006229 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006230 * u32, as those are the requirements for sector_div.
6231 * Thus 'scale' must be at least 10
6232 */
6233 scale = 10;
6234 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006235 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006236 scale++;
6237 }
6238 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006239 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006240
6241 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 {
NeilBrown4588b422006-03-27 01:18:04 -08006243 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 seq_printf(seq, "[");
6245 for (i = 0; i < x; i++)
6246 seq_printf(seq, "=");
6247 seq_printf(seq, ">");
6248 for (i = 0; i < y; i++)
6249 seq_printf(seq, ".");
6250 seq_printf(seq, "] ");
6251 }
NeilBrown4588b422006-03-27 01:18:04 -08006252 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006253 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6254 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006255 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6256 "check" :
6257 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6258 "resync" : "recovery"))),
6259 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006260 (unsigned long long) resync/2,
6261 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
6263 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 * dt: time from mark until now
6265 * db: blocks written from mark until now
6266 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006267 *
6268 * rt is a sector_t, so could be 32bit or 64bit.
6269 * So we divide before multiply in case it is 32bit and close
6270 * to the limit.
6271 * We scale the divisor (db) by 32 to avoid loosing precision
6272 * near the end of resync when the number of remaining sectors
6273 * is close to 'db'.
6274 * We then divide rt by 32 after multiplying by db to compensate.
6275 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 */
6277 dt = ((jiffies - mddev->resync_mark) / HZ);
6278 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006279 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6280 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
NeilBrowndd71cf62009-05-07 12:49:35 +10006282 rt = max_sectors - resync; /* number of remaining sectors */
6283 sector_div(rt, db/32+1);
6284 rt *= dt;
6285 rt >>= 5;
6286
6287 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6288 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
NeilBrownff4e8d92006-07-10 04:44:16 -07006290 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291}
6292
6293static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6294{
6295 struct list_head *tmp;
6296 loff_t l = *pos;
6297 mddev_t *mddev;
6298
6299 if (l >= 0x10000)
6300 return NULL;
6301 if (!l--)
6302 /* header */
6303 return (void*)1;
6304
6305 spin_lock(&all_mddevs_lock);
6306 list_for_each(tmp,&all_mddevs)
6307 if (!l--) {
6308 mddev = list_entry(tmp, mddev_t, all_mddevs);
6309 mddev_get(mddev);
6310 spin_unlock(&all_mddevs_lock);
6311 return mddev;
6312 }
6313 spin_unlock(&all_mddevs_lock);
6314 if (!l--)
6315 return (void*)2;/* tail */
6316 return NULL;
6317}
6318
6319static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6320{
6321 struct list_head *tmp;
6322 mddev_t *next_mddev, *mddev = v;
6323
6324 ++*pos;
6325 if (v == (void*)2)
6326 return NULL;
6327
6328 spin_lock(&all_mddevs_lock);
6329 if (v == (void*)1)
6330 tmp = all_mddevs.next;
6331 else
6332 tmp = mddev->all_mddevs.next;
6333 if (tmp != &all_mddevs)
6334 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6335 else {
6336 next_mddev = (void*)2;
6337 *pos = 0x10000;
6338 }
6339 spin_unlock(&all_mddevs_lock);
6340
6341 if (v != (void*)1)
6342 mddev_put(mddev);
6343 return next_mddev;
6344
6345}
6346
6347static void md_seq_stop(struct seq_file *seq, void *v)
6348{
6349 mddev_t *mddev = v;
6350
6351 if (mddev && v != (void*)1 && v != (void*)2)
6352 mddev_put(mddev);
6353}
6354
NeilBrownd7603b72006-01-06 00:20:30 -08006355struct mdstat_info {
6356 int event;
6357};
6358
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359static int md_seq_show(struct seq_file *seq, void *v)
6360{
6361 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006362 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006364 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006365 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
6367 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006368 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 seq_printf(seq, "Personalities : ");
6370 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006371 list_for_each_entry(pers, &pers_list, list)
6372 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
6374 spin_unlock(&pers_lock);
6375 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006376 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 return 0;
6378 }
6379 if (v == (void*)2) {
6380 status_unused(seq);
6381 return 0;
6382 }
6383
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006384 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6388 seq_printf(seq, "%s : %sactive", mdname(mddev),
6389 mddev->pers ? "" : "in");
6390 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006391 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006393 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006394 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 seq_printf(seq, " %s", mddev->pers->name);
6396 }
6397
Andre Nolldd8ac332009-03-31 14:33:13 +11006398 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006399 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 char b[BDEVNAME_SIZE];
6401 seq_printf(seq, " %s[%d]",
6402 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006403 if (test_bit(WriteMostly, &rdev->flags))
6404 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006405 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 seq_printf(seq, "(F)");
6407 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006408 } else if (rdev->raid_disk < 0)
6409 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006410 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 }
6412
6413 if (!list_empty(&mddev->disks)) {
6414 if (mddev->pers)
6415 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006416 (unsigned long long)
6417 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 else
6419 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006420 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006422 if (mddev->persistent) {
6423 if (mddev->major_version != 0 ||
6424 mddev->minor_version != 90) {
6425 seq_printf(seq," super %d.%d",
6426 mddev->major_version,
6427 mddev->minor_version);
6428 }
NeilBrowne6910632008-02-06 01:39:51 -08006429 } else if (mddev->external)
6430 seq_printf(seq, " super external:%s",
6431 mddev->metadata_type);
6432 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006433 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
6435 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006436 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006438 if (mddev->pers->sync_request) {
6439 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006440 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006441 seq_printf(seq, "\n ");
6442 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6443 seq_printf(seq, "\tresync=DELAYED\n ");
6444 else if (mddev->recovery_cp < MaxSector)
6445 seq_printf(seq, "\tresync=PENDING\n ");
6446 }
NeilBrown32a76272005-06-21 17:17:14 -07006447 } else
6448 seq_printf(seq, "\n ");
6449
6450 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006451 unsigned long chunk_kb;
6452 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006453 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006454 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006455 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6456 "%lu%s chunk",
6457 bitmap->pages - bitmap->missing_pages,
6458 bitmap->pages,
6459 (bitmap->pages - bitmap->missing_pages)
6460 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006461 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006462 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006463 if (bitmap->file) {
6464 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006465 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006466 }
NeilBrown78d742d2005-06-21 17:17:15 -07006467
NeilBrown32a76272005-06-21 17:17:14 -07006468 seq_printf(seq, "\n");
6469 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 }
6471
6472 seq_printf(seq, "\n");
6473 }
6474 mddev_unlock(mddev);
6475
6476 return 0;
6477}
6478
Jan Engelhardt110518b2009-05-07 12:49:37 +10006479static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 .start = md_seq_start,
6481 .next = md_seq_next,
6482 .stop = md_seq_stop,
6483 .show = md_seq_show,
6484};
6485
6486static int md_seq_open(struct inode *inode, struct file *file)
6487{
6488 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006489 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6490 if (mi == NULL)
6491 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492
6493 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006494 if (error)
6495 kfree(mi);
6496 else {
6497 struct seq_file *p = file->private_data;
6498 p->private = mi;
6499 mi->event = atomic_read(&md_event_count);
6500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 return error;
6502}
6503
NeilBrownd7603b72006-01-06 00:20:30 -08006504static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6505{
6506 struct seq_file *m = filp->private_data;
6507 struct mdstat_info *mi = m->private;
6508 int mask;
6509
6510 poll_wait(filp, &md_event_waiters, wait);
6511
6512 /* always allow read */
6513 mask = POLLIN | POLLRDNORM;
6514
6515 if (mi->event != atomic_read(&md_event_count))
6516 mask |= POLLERR | POLLPRI;
6517 return mask;
6518}
6519
Arjan van de Venfa027c22007-02-12 00:55:33 -08006520static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006521 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 .open = md_seq_open,
6523 .read = seq_read,
6524 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006525 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006526 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527};
6528
NeilBrown2604b702006-01-06 00:20:36 -08006529int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006532 list_add_tail(&p->list, &pers_list);
6533 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 spin_unlock(&pers_lock);
6535 return 0;
6536}
6537
NeilBrown2604b702006-01-06 00:20:36 -08006538int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539{
NeilBrown2604b702006-01-06 00:20:36 -08006540 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006542 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 spin_unlock(&pers_lock);
6544 return 0;
6545}
6546
NeilBrowneea1bf32009-03-31 14:27:02 +11006547static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548{
6549 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006551 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552
6553 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006554 rcu_read_lock();
6555 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006557 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6558 (int)part_stat_read(&disk->part0, sectors[1]) -
6559 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006560 /* sync IO will cause sync_io to increase before the disk_stats
6561 * as sync_io is counted when a request starts, and
6562 * disk_stats is counted when it completes.
6563 * So resync activity will cause curr_events to be smaller than
6564 * when there was no such activity.
6565 * non-sync IO will cause disk_stat to increase without
6566 * increasing sync_io so curr_events will (eventually)
6567 * be larger than it was before. Once it becomes
6568 * substantially larger, the test below will cause
6569 * the array to appear non-idle, and resync will slow
6570 * down.
6571 * If there is a lot of outstanding resync activity when
6572 * we set last_event to curr_events, then all that activity
6573 * completing might cause the array to appear non-idle
6574 * and resync will be slowed down even though there might
6575 * not have been non-resync activity. This will only
6576 * happen once though. 'last_events' will soon reflect
6577 * the state where there is little or no outstanding
6578 * resync requests, and further resync activity will
6579 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006582 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 rdev->last_events = curr_events;
6584 idle = 0;
6585 }
6586 }
NeilBrown4b809912008-07-21 17:05:25 +10006587 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 return idle;
6589}
6590
6591void md_done_sync(mddev_t *mddev, int blocks, int ok)
6592{
6593 /* another "blocks" (512byte) blocks have been synced */
6594 atomic_sub(blocks, &mddev->recovery_active);
6595 wake_up(&mddev->recovery_wait);
6596 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 md_wakeup_thread(mddev->thread);
6599 // stop recovery, signal do_sync ....
6600 }
6601}
6602
6603
NeilBrown06d91a52005-06-21 17:17:12 -07006604/* md_write_start(mddev, bi)
6605 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006606 * in superblock) before writing, schedule a superblock update
6607 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006608 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006609void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610{
Neil Brown0fd62b82008-06-28 08:31:36 +10006611 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006612 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006613 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006614
NeilBrownf91de922005-11-08 21:39:36 -08006615 BUG_ON(mddev->ro == 1);
6616 if (mddev->ro == 2) {
6617 /* need to switch to read/write */
6618 mddev->ro = 0;
6619 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6620 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006621 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006622 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006623 }
NeilBrown06d91a52005-06-21 17:17:12 -07006624 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006625 if (mddev->safemode == 1)
6626 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006627 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006628 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006629 if (mddev->in_sync) {
6630 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006631 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006632 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006633 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006634 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006635 }
NeilBrowna9701a32005-11-08 21:39:34 -08006636 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006637 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006638 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006639 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006640 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006641 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642}
6643
6644void md_write_end(mddev_t *mddev)
6645{
6646 if (atomic_dec_and_test(&mddev->writes_pending)) {
6647 if (mddev->safemode == 2)
6648 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006649 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6651 }
6652}
6653
NeilBrown2a2275d2007-01-26 00:57:11 -08006654/* md_allow_write(mddev)
6655 * Calling this ensures that the array is marked 'active' so that writes
6656 * may proceed without blocking. It is important to call this before
6657 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6658 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006659 *
6660 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6661 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006662 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006663int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006664{
6665 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006666 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006667 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006668 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006669 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006670 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006671
6672 spin_lock_irq(&mddev->write_lock);
6673 if (mddev->in_sync) {
6674 mddev->in_sync = 0;
6675 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006676 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006677 if (mddev->safemode_delay &&
6678 mddev->safemode == 0)
6679 mddev->safemode = 1;
6680 spin_unlock_irq(&mddev->write_lock);
6681 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006682 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006683 } else
6684 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006685
NeilBrown070dc6d2010-08-30 17:33:34 +10006686 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006687 return -EAGAIN;
6688 else
6689 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006690}
6691EXPORT_SYMBOL_GPL(md_allow_write);
6692
NeilBrownb63d7c22010-06-01 19:37:33 +10006693void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006694{
6695 if (mddev->queue)
6696 blk_unplug(mddev->queue);
6697 if (mddev->plug)
6698 mddev->plug->unplug_fn(mddev->plug);
6699}
6700
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701#define SYNC_MARKS 10
6702#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006703void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704{
6705 mddev_t *mddev2;
6706 unsigned int currspeed = 0,
6707 window;
NeilBrown57afd892005-06-21 17:17:13 -07006708 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 unsigned long mark[SYNC_MARKS];
6710 sector_t mark_cnt[SYNC_MARKS];
6711 int last_mark,m;
6712 struct list_head *tmp;
6713 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006714 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006715 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006716 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
6718 /* just incase thread restarts... */
6719 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6720 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006721 if (mddev->ro) /* never try to sync a read-only array */
6722 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
NeilBrown61df9d92006-10-03 01:15:57 -07006724 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6725 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6726 desc = "data-check";
6727 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6728 desc = "requested-resync";
6729 else
6730 desc = "resync";
6731 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6732 desc = "reshape";
6733 else
6734 desc = "recovery";
6735
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 /* we overload curr_resync somewhat here.
6737 * 0 == not engaged in resync at all
6738 * 2 == checking that there is no conflict with another sync
6739 * 1 == like 2, but have yielded to allow conflicting resync to
6740 * commense
6741 * other == active in resync - this many blocks
6742 *
6743 * Before starting a resync we must have set curr_resync to
6744 * 2, and then checked that every "conflicting" array has curr_resync
6745 * less than ours. When we find one that is the same or higher
6746 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6747 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6748 * This will mean we have to start checking from the beginning again.
6749 *
6750 */
6751
6752 do {
6753 mddev->curr_resync = 2;
6754
6755 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006756 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006757 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006758
6759 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006761 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 if (mddev2 == mddev)
6763 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006764 if (!mddev->parallel_resync
6765 && mddev2->curr_resync
6766 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 DEFINE_WAIT(wq);
6768 if (mddev < mddev2 && mddev->curr_resync == 2) {
6769 /* arbitrarily yield */
6770 mddev->curr_resync = 1;
6771 wake_up(&resync_wait);
6772 }
6773 if (mddev > mddev2 && mddev->curr_resync == 1)
6774 /* no need to wait here, we can wait the next
6775 * time 'round when curr_resync == 2
6776 */
6777 continue;
NeilBrown97441972008-09-19 11:49:54 +10006778 /* We need to wait 'interruptible' so as not to
6779 * contribute to the load average, and not to
6780 * be caught by 'softlockup'
6781 */
6782 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006783 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006784 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006785 printk(KERN_INFO "md: delaying %s of %s"
6786 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006788 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006790 if (signal_pending(current))
6791 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 schedule();
6793 finish_wait(&resync_wait, &wq);
6794 goto try_again;
6795 }
6796 finish_wait(&resync_wait, &wq);
6797 }
6798 }
6799 } while (mddev->curr_resync < 2);
6800
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006801 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006802 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006804 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 */
6806 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006807 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006808 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006809 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6810 j = mddev->resync_min;
6811 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006812 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006813
NeilBrownccfcc3c2006-03-27 01:18:09 -08006814 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006815 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006816 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006818 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006819 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006820 rcu_read_lock();
6821 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006822 if (rdev->raid_disk >= 0 &&
6823 !test_bit(Faulty, &rdev->flags) &&
6824 !test_bit(In_sync, &rdev->flags) &&
6825 rdev->recovery_offset < j)
6826 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006827 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829
NeilBrown61df9d92006-10-03 01:15:57 -07006830 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6831 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6832 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006833 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006834 "(but not more than %d KB/sec) for %s.\n",
6835 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
NeilBrowneea1bf32009-03-31 14:27:02 +11006837 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006838
NeilBrown57afd892005-06-21 17:17:13 -07006839 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 for (m = 0; m < SYNC_MARKS; m++) {
6841 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006842 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 }
6844 last_mark = 0;
6845 mddev->resync_mark = mark[last_mark];
6846 mddev->resync_mark_cnt = mark_cnt[last_mark];
6847
6848 /*
6849 * Tune reconstruction:
6850 */
6851 window = 32*(PAGE_SIZE/512);
6852 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6853 window/2,(unsigned long long) max_sectors/2);
6854
6855 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 last_check = 0;
6857
6858 if (j>2) {
6859 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006860 "md: resuming %s of %s from checkpoint.\n",
6861 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 mddev->curr_resync = j;
6863 }
NeilBrown75d3da42011-01-14 09:14:34 +11006864 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
6866 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006867 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
NeilBrown57afd892005-06-21 17:17:13 -07006869 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006870
NeilBrown7a91ee12009-05-26 12:57:21 +10006871 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6872 ((mddev->curr_resync > mddev->curr_resync_completed &&
6873 (mddev->curr_resync - mddev->curr_resync_completed)
6874 > (max_sectors >> 4)) ||
6875 (j - mddev->curr_resync_completed)*2
6876 >= mddev->resync_max - mddev->curr_resync_completed
6877 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006878 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006879 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006880 wait_event(mddev->recovery_wait,
6881 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11006882 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10006883 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006884 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006885 }
NeilBrownacb180b2009-04-14 16:28:34 +10006886
NeilBrowne62e58a2009-07-01 13:15:35 +10006887 while (j >= mddev->resync_max && !kthread_should_stop()) {
6888 /* As this condition is controlled by user-space,
6889 * we can block indefinitely, so use '_interruptible'
6890 * to avoid triggering warnings.
6891 */
6892 flush_signals(current); /* just in case */
6893 wait_event_interruptible(mddev->recovery_wait,
6894 mddev->resync_max > j
6895 || kthread_should_stop());
6896 }
NeilBrownacb180b2009-04-14 16:28:34 +10006897
6898 if (kthread_should_stop())
6899 goto interrupted;
6900
NeilBrown57afd892005-06-21 17:17:13 -07006901 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006902 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006903 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006904 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 goto out;
6906 }
NeilBrown57afd892005-06-21 17:17:13 -07006907
6908 if (!skipped) { /* actual IO requested */
6909 io_sectors += sectors;
6910 atomic_add(sectors, &mddev->recovery_active);
6911 }
6912
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 j += sectors;
6914 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006915 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006916 if (last_check == 0)
6917 /* this is the earliers that rebuilt will be
6918 * visible in /proc/mdstat
6919 */
6920 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006921
6922 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 continue;
6924
NeilBrown57afd892005-06-21 17:17:13 -07006925 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926
NeilBrowndfc70642008-05-23 13:04:39 -07006927 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 break;
6929
6930 repeat:
6931 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6932 /* step marks */
6933 int next = (last_mark+1) % SYNC_MARKS;
6934
6935 mddev->resync_mark = mark[next];
6936 mddev->resync_mark_cnt = mark_cnt[next];
6937 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006938 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 last_mark = next;
6940 }
6941
6942
NeilBrownc6207272008-02-06 01:39:52 -08006943 if (kthread_should_stop())
6944 goto interrupted;
6945
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
6947 /*
6948 * this loop exits only if either when we are slower than
6949 * the 'hard' speed limit, or the system was IO-idle for
6950 * a jiffy.
6951 * the system might be non-idle CPU-wise, but we only care
6952 * about not overloading the IO subsystem. (things like an
6953 * e2fsck being done on the RAID array should execute fast)
6954 */
NeilBrown252ac522010-06-01 19:37:29 +10006955 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 cond_resched();
6957
NeilBrown57afd892005-06-21 17:17:13 -07006958 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6959 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
NeilBrown88202a02006-01-06 00:21:36 -08006961 if (currspeed > speed_min(mddev)) {
6962 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006963 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006964 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 goto repeat;
6966 }
6967 }
6968 }
NeilBrown61df9d92006-10-03 01:15:57 -07006969 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 /*
6971 * this also signals 'finished resyncing' to md_stop
6972 */
6973 out:
NeilBrown252ac522010-06-01 19:37:29 +10006974 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
6976 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6977
6978 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006979 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980
NeilBrowndfc70642008-05-23 13:04:39 -07006981 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006982 mddev->curr_resync > 2) {
6983 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6984 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6985 if (mddev->curr_resync >= mddev->recovery_cp) {
6986 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006987 "md: checkpointing %s of %s.\n",
6988 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006989 mddev->recovery_cp = mddev->curr_resync;
6990 }
6991 } else
6992 mddev->recovery_cp = MaxSector;
6993 } else {
6994 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6995 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006996 rcu_read_lock();
6997 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006998 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006999 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007000 !test_bit(Faulty, &rdev->flags) &&
7001 !test_bit(In_sync, &rdev->flags) &&
7002 rdev->recovery_offset < mddev->curr_resync)
7003 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007004 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 }
NeilBrown17571282006-12-10 02:20:52 -08007007 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007010 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7011 /* We completed so min/max setting can be forgotten if used. */
7012 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7013 mddev->resync_min = 0;
7014 mddev->resync_max = MaxSector;
7015 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7016 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 mddev->curr_resync = 0;
7018 wake_up(&resync_wait);
7019 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7020 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007021 return;
7022
7023 interrupted:
7024 /*
7025 * got a signal, exit.
7026 */
7027 printk(KERN_INFO
7028 "md: md_do_sync() got signal ... exiting\n");
7029 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7030 goto out;
7031
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032}
NeilBrown29269552006-03-27 01:18:10 -08007033EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
7035
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007036static int remove_and_add_spares(mddev_t *mddev)
7037{
7038 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007039 int spares = 0;
7040
NeilBrown97e4f422009-03-31 14:33:13 +11007041 mddev->curr_resync_completed = 0;
7042
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007043 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007044 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007045 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007046 (test_bit(Faulty, &rdev->flags) ||
7047 ! test_bit(In_sync, &rdev->flags)) &&
7048 atomic_read(&rdev->nr_pending)==0) {
7049 if (mddev->pers->hot_remove_disk(
7050 mddev, rdev->raid_disk)==0) {
7051 char nm[20];
7052 sprintf(nm,"rd%d", rdev->raid_disk);
7053 sysfs_remove_link(&mddev->kobj, nm);
7054 rdev->raid_disk = -1;
7055 }
7056 }
7057
NeilBrowna8c42c72011-01-31 13:47:13 +11007058 if (mddev->degraded && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007059 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007060 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007061 !test_bit(In_sync, &rdev->flags) &&
7062 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007063 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007064 if (rdev->raid_disk < 0
7065 && !test_bit(Faulty, &rdev->flags)) {
7066 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007067 if (mddev->pers->
7068 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007069 char nm[20];
7070 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007071 if (sysfs_create_link(&mddev->kobj,
7072 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007073 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007074 spares++;
7075 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007076 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007077 } else
7078 break;
7079 }
NeilBrowndfc70642008-05-23 13:04:39 -07007080 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007081 }
7082 return spares;
7083}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007084
7085static void reap_sync_thread(mddev_t *mddev)
7086{
7087 mdk_rdev_t *rdev;
7088
7089 /* resync has finished, collect result */
7090 md_unregister_thread(mddev->sync_thread);
7091 mddev->sync_thread = NULL;
7092 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7093 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7094 /* success...*/
7095 /* activate any spares */
7096 if (mddev->pers->spare_active(mddev))
7097 sysfs_notify(&mddev->kobj, NULL,
7098 "degraded");
7099 }
7100 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7101 mddev->pers->finish_reshape)
7102 mddev->pers->finish_reshape(mddev);
7103 md_update_sb(mddev, 1);
7104
7105 /* if array is no-longer degraded, then any saved_raid_disk
7106 * information must be scrapped
7107 */
7108 if (!mddev->degraded)
7109 list_for_each_entry(rdev, &mddev->disks, same_set)
7110 rdev->saved_raid_disk = -1;
7111
7112 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7113 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7114 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7115 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7116 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7117 /* flag recovery needed just to double check */
7118 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7119 sysfs_notify_dirent_safe(mddev->sysfs_action);
7120 md_new_event(mddev);
7121}
7122
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123/*
7124 * This routine is regularly called by all per-raid-array threads to
7125 * deal with generic issues like resync and super-block update.
7126 * Raid personalities that don't have a thread (linear/raid0) do not
7127 * need this as they never do any recovery or update the superblock.
7128 *
7129 * It does not do any resync itself, but rather "forks" off other threads
7130 * to do that as needed.
7131 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7132 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007133 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 * and wakeups up this thread which will reap the thread and finish up.
7135 * This thread also removes any faulty devices (with nr_pending == 0).
7136 *
7137 * The overall approach is:
7138 * 1/ if the superblock needs updating, update it.
7139 * 2/ If a recovery thread is running, don't do anything else.
7140 * 3/ If recovery has finished, clean up, possibly marking spares active.
7141 * 4/ If there are any faulty devices, remove them.
7142 * 5/ If array is degraded, try to add spares devices
7143 * 6/ If array has spares or is not in-sync, start a resync thread.
7144 */
7145void md_check_recovery(mddev_t *mddev)
7146{
NeilBrown5f404022005-06-21 17:17:16 -07007147 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007148 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
7150 if (mddev->ro)
7151 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007152
7153 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007154 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007155 printk(KERN_INFO "md: %s in immediate safe mode\n",
7156 mdname(mddev));
7157 mddev->safemode = 2;
7158 }
7159 flush_signals(current);
7160 }
7161
NeilBrownc89a8ee2008-08-05 15:54:13 +10007162 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7163 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007165 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007167 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007168 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007169 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7170 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 ))
7172 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007173
NeilBrowndf5b89b2006-03-27 01:18:20 -08007174 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007175 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007176
NeilBrownc89a8ee2008-08-05 15:54:13 +10007177 if (mddev->ro) {
7178 /* Only thing we do on a ro array is remove
7179 * failed devices.
7180 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007181 mdk_rdev_t *rdev;
7182 list_for_each_entry(rdev, &mddev->disks, same_set)
7183 if (rdev->raid_disk >= 0 &&
7184 !test_bit(Blocked, &rdev->flags) &&
7185 test_bit(Faulty, &rdev->flags) &&
7186 atomic_read(&rdev->nr_pending)==0) {
7187 if (mddev->pers->hot_remove_disk(
7188 mddev, rdev->raid_disk)==0) {
7189 char nm[20];
7190 sprintf(nm,"rd%d", rdev->raid_disk);
7191 sysfs_remove_link(&mddev->kobj, nm);
7192 rdev->raid_disk = -1;
7193 }
7194 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007195 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7196 goto unlock;
7197 }
7198
NeilBrown31a59e32008-04-30 00:52:30 -07007199 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007200 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007201 spin_lock_irq(&mddev->write_lock);
7202 if (mddev->safemode &&
7203 !atomic_read(&mddev->writes_pending) &&
7204 !mddev->in_sync &&
7205 mddev->recovery_cp == MaxSector) {
7206 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007207 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007208 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007209 }
7210 if (mddev->safemode == 1)
7211 mddev->safemode = 0;
7212 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007213 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007214 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007215 }
NeilBrownfca4d842005-06-21 17:17:11 -07007216
NeilBrown850b2b42006-10-03 01:15:46 -07007217 if (mddev->flags)
7218 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007219
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7221 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7222 /* resync/recovery still happening */
7223 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7224 goto unlock;
7225 }
7226 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007227 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 goto unlock;
7229 }
Neil Brown72a23c22008-06-28 08:31:41 +10007230 /* Set RUNNING before clearing NEEDED to avoid
7231 * any transients in the value of "sync_action".
7232 */
7233 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7234 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007235 /* Clear some bits that don't mean anything, but
7236 * might be left set
7237 */
NeilBrown24dd4692005-11-08 21:39:26 -08007238 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7239 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007241 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7242 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 /* no recovery is running.
7244 * remove any failed drives, then
7245 * add spares if possible.
7246 * Spare are also removed and re-added, to allow
7247 * the personality to fail the re-add.
7248 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007250 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007251 if (mddev->pers->check_reshape == NULL ||
7252 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007253 /* Cannot proceed */
7254 goto unlock;
7255 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007256 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007257 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007258 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7259 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007260 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007261 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007262 } else if (mddev->recovery_cp < MaxSector) {
7263 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007264 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007265 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7266 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007268
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007270 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7271 /* We are adding a device or devices to an array
7272 * which has the bitmap stored on all devices.
7273 * So make sure all bitmap pages get written
7274 */
7275 bitmap_write_all(mddev->bitmap);
7276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 mddev->sync_thread = md_register_thread(md_do_sync,
7278 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007279 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 if (!mddev->sync_thread) {
7281 printk(KERN_ERR "%s: could not start resync"
7282 " thread...\n",
7283 mdname(mddev));
7284 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007285 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7286 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7287 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7288 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7289 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007290 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007292 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007293 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 }
7295 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007296 if (!mddev->sync_thread) {
7297 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7298 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7299 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007300 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007301 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 mddev_unlock(mddev);
7304 }
7305}
7306
Dan Williams6bfe0b42008-04-30 00:52:32 -07007307void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7308{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007309 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007310 wait_event_timeout(rdev->blocked_wait,
7311 !test_bit(Blocked, &rdev->flags),
7312 msecs_to_jiffies(5000));
7313 rdev_dec_pending(rdev, mddev);
7314}
7315EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7316
Adrian Bunk75c96f82005-05-05 16:16:09 -07007317static int md_notify_reboot(struct notifier_block *this,
7318 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319{
7320 struct list_head *tmp;
7321 mddev_t *mddev;
7322
7323 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7324
7325 printk(KERN_INFO "md: stopping all md devices.\n");
7326
NeilBrown29ac4aa2008-02-06 01:39:58 -08007327 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007328 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007329 /* Force a switch to readonly even array
7330 * appears to still be in use. Hence
7331 * the '100'.
7332 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007333 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007334 mddev_unlock(mddev);
7335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336 /*
7337 * certain more exotic SCSI devices are known to be
7338 * volatile wrt too early system reboots. While the
7339 * right place to handle this issue is the given
7340 * driver, we do want to have a safe RAID driver ...
7341 */
7342 mdelay(1000*1);
7343 }
7344 return NOTIFY_DONE;
7345}
7346
Adrian Bunk75c96f82005-05-05 16:16:09 -07007347static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 .notifier_call = md_notify_reboot,
7349 .next = NULL,
7350 .priority = INT_MAX, /* before any real devices */
7351};
7352
7353static void md_geninit(void)
7354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7356
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007357 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358}
7359
Adrian Bunk75c96f82005-05-05 16:16:09 -07007360static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361{
Tejun Heoe804ac72010-10-15 15:36:08 +02007362 int ret = -ENOMEM;
7363
Tejun Heoada609e2011-01-25 14:35:54 +01007364 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02007365 if (!md_wq)
7366 goto err_wq;
7367
7368 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7369 if (!md_misc_wq)
7370 goto err_misc_wq;
7371
7372 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7373 goto err_md;
7374
7375 if ((ret = register_blkdev(0, "mdp")) < 0)
7376 goto err_mdp;
7377 mdp_major = ret;
7378
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007379 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007380 md_probe, NULL, NULL);
7381 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 md_probe, NULL, NULL);
7383
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007385 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386
7387 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
Tejun Heoe804ac72010-10-15 15:36:08 +02007390err_mdp:
7391 unregister_blkdev(MD_MAJOR, "md");
7392err_md:
7393 destroy_workqueue(md_misc_wq);
7394err_misc_wq:
7395 destroy_workqueue(md_wq);
7396err_wq:
7397 return ret;
7398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
7400#ifndef MODULE
7401
7402/*
7403 * Searches all registered partitions for autorun RAID arrays
7404 * at boot time.
7405 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007406
7407static LIST_HEAD(all_detected_devices);
7408struct detected_devices_node {
7409 struct list_head list;
7410 dev_t dev;
7411};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
7413void md_autodetect_dev(dev_t dev)
7414{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007415 struct detected_devices_node *node_detected_dev;
7416
7417 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7418 if (node_detected_dev) {
7419 node_detected_dev->dev = dev;
7420 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7421 } else {
7422 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7423 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425}
7426
7427
7428static void autostart_arrays(int part)
7429{
7430 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007431 struct detected_devices_node *node_detected_dev;
7432 dev_t dev;
7433 int i_scanned, i_passed;
7434
7435 i_scanned = 0;
7436 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437
7438 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7439
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007440 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7441 i_scanned++;
7442 node_detected_dev = list_entry(all_detected_devices.next,
7443 struct detected_devices_node, list);
7444 list_del(&node_detected_dev->list);
7445 dev = node_detected_dev->dev;
7446 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007447 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 if (IS_ERR(rdev))
7449 continue;
7450
NeilBrownb2d444d2005-11-08 21:39:31 -08007451 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 MD_BUG();
7453 continue;
7454 }
NeilBrownd0fae182008-03-04 14:29:31 -08007455 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007457 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007459
7460 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7461 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
7463 autorun_devices(part);
7464}
7465
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007466#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
7468static __exit void md_exit(void)
7469{
7470 mddev_t *mddev;
7471 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007472
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007473 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007474 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007476 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477 unregister_blkdev(mdp_major, "mdp");
7478 unregister_reboot_notifier(&md_notifier);
7479 unregister_sysctl_table(raid_table_header);
7480 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007481 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007483 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007485 destroy_workqueue(md_misc_wq);
7486 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487}
7488
Dan Williams685784a2007-07-09 11:56:42 -07007489subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490module_exit(md_exit)
7491
NeilBrownf91de922005-11-08 21:39:36 -08007492static int get_ro(char *buffer, struct kernel_param *kp)
7493{
7494 return sprintf(buffer, "%d", start_readonly);
7495}
7496static int set_ro(const char *val, struct kernel_param *kp)
7497{
7498 char *e;
7499 int num = simple_strtoul(val, &e, 10);
7500 if (*val && (*e == '\0' || *e == '\n')) {
7501 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007502 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007503 }
7504 return -EINVAL;
7505}
7506
NeilBrown80ca3a42006-07-10 04:44:18 -07007507module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7508module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08007509
NeilBrownefeb53c2009-01-09 08:31:10 +11007510module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007511
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512EXPORT_SYMBOL(register_md_personality);
7513EXPORT_SYMBOL(unregister_md_personality);
7514EXPORT_SYMBOL(md_error);
7515EXPORT_SYMBOL(md_done_sync);
7516EXPORT_SYMBOL(md_write_start);
7517EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518EXPORT_SYMBOL(md_register_thread);
7519EXPORT_SYMBOL(md_unregister_thread);
7520EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521EXPORT_SYMBOL(md_check_recovery);
7522MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007523MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007524MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007525MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);