blob: 8b66e04c2ea6a833930ffbe57608911e16a494b2 [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);
Jens Axboe721a9602011-03-09 11:56:30 +0100783 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800784}
785
786void md_super_wait(mddev_t *mddev)
787{
Tejun Heoe9c74692010-09-03 11:56:18 +0200788 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800789 DEFINE_WAIT(wq);
790 for(;;) {
791 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
792 if (atomic_read(&mddev->pending_writes)==0)
793 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800794 schedule();
795 }
796 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700797}
798
NeilBrown6712ecf2007-09-27 12:47:43 +0200799static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
NeilBrown2b193362010-10-27 15:16:40 +1100804int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100805 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
NeilBrowna167f662010-10-26 18:31:13 +1100807 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct completion event;
809 int ret;
810
Jens Axboe721a9602011-03-09 11:56:30 +0100811 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100813 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
814 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100815 if (metadata_op)
816 bio->bi_sector = sector + rdev->sb_start;
817 else
818 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 bio_add_page(bio, page, size, 0);
820 init_completion(&event);
821 bio->bi_private = &event;
822 bio->bi_end_io = bi_complete;
823 submit_bio(rw, bio);
824 wait_for_completion(&event);
825
826 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
827 bio_put(bio);
828 return ret;
829}
NeilBrowna8745db2006-01-06 00:20:34 -0800830EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
NeilBrown0002b272005-09-09 16:23:53 -0700832static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 char b[BDEVNAME_SIZE];
835 if (!rdev->sb_page) {
836 MD_BUG();
837 return -EINVAL;
838 }
839 if (rdev->sb_loaded)
840 return 0;
841
842
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100843 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto fail;
845 rdev->sb_loaded = 1;
846 return 0;
847
848fail:
849 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
850 bdevname(rdev->bdev,b));
851 return -EINVAL;
852}
853
854static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
855{
Andre Noll05710462008-07-11 22:02:20 +1000856 return sb1->set_uuid0 == sb2->set_uuid0 &&
857 sb1->set_uuid1 == sb2->set_uuid1 &&
858 sb1->set_uuid2 == sb2->set_uuid2 &&
859 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
863{
864 int ret;
865 mdp_super_t *tmp1, *tmp2;
866
867 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
868 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
869
870 if (!tmp1 || !tmp2) {
871 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100872 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto abort;
874 }
875
876 *tmp1 = *sb1;
877 *tmp2 = *sb2;
878
879 /*
880 * nr_disks is not constant
881 */
882 tmp1->nr_disks = 0;
883 tmp2->nr_disks = 0;
884
Andre Nollce0c8e02008-07-11 22:02:20 +1000885 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700887 kfree(tmp1);
888 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return ret;
890}
891
NeilBrown4d167f02007-05-09 02:35:37 -0700892
893static u32 md_csum_fold(u32 csum)
894{
895 csum = (csum & 0xffff) + (csum >> 16);
896 return (csum & 0xffff) + (csum >> 16);
897}
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899static unsigned int calc_sb_csum(mdp_super_t * sb)
900{
NeilBrown4d167f02007-05-09 02:35:37 -0700901 u64 newcsum = 0;
902 u32 *sb32 = (u32*)sb;
903 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 unsigned int disk_csum, csum;
905
906 disk_csum = sb->sb_csum;
907 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700908
909 for (i = 0; i < MD_SB_BYTES/4 ; i++)
910 newcsum += sb32[i];
911 csum = (newcsum & 0xffffffff) + (newcsum>>32);
912
913
914#ifdef CONFIG_ALPHA
915 /* This used to use csum_partial, which was wrong for several
916 * reasons including that different results are returned on
917 * different architectures. It isn't critical that we get exactly
918 * the same return value as before (we always csum_fold before
919 * testing, and that removes any differences). However as we
920 * know that csum_partial always returned a 16bit value on
921 * alphas, do a fold to maximise conformity to previous behaviour.
922 */
923 sb->sb_csum = md_csum_fold(disk_csum);
924#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700926#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return csum;
928}
929
930
931/*
932 * Handle superblock details.
933 * We want to be able to handle multiple superblock formats
934 * so we have a common interface to them all, and an array of
935 * different handlers.
936 * We rely on user-space to write the initial superblock, and support
937 * reading and updating of superblocks.
938 * Interface methods are:
939 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
940 * loads and validates a superblock on dev.
941 * if refdev != NULL, compare superblocks on both devices
942 * Return:
943 * 0 - dev has a superblock that is compatible with refdev
944 * 1 - dev has a superblock that is compatible and newer than refdev
945 * so dev should be used as the refdev in future
946 * -EINVAL superblock incompatible or invalid
947 * -othererror e.g. -EIO
948 *
949 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
950 * Verify that dev is acceptable into mddev.
951 * The first time, mddev->raid_disks will be 0, and data from
952 * dev should be merged in. Subsequent calls check that dev
953 * is new enough. Return 0 or -EINVAL
954 *
955 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
956 * Update the superblock for rdev with data in mddev
957 * This does not write to disc.
958 *
959 */
960
961struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000962 char *name;
963 struct module *owner;
964 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
965 int minor_version);
966 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
967 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
968 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000969 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970};
971
972/*
Andre Noll0894cc32009-06-18 08:49:23 +1000973 * Check that the given mddev has no bitmap.
974 *
975 * This function is called from the run method of all personalities that do not
976 * support bitmaps. It prints an error message and returns non-zero if mddev
977 * has a bitmap. Otherwise, it returns 0.
978 *
979 */
980int md_check_no_bitmap(mddev_t *mddev)
981{
NeilBrownc3d97142009-12-14 12:49:52 +1100982 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000983 return 0;
984 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
985 mdname(mddev), mddev->pers->name);
986 return 1;
987}
988EXPORT_SYMBOL(md_check_no_bitmap);
989
990/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * load_super for 0.90.0
992 */
993static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
994{
995 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
996 mdp_super_t *sb;
997 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /*
Andre Noll0f420352008-07-11 22:02:23 +10001000 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * it's at the end of the disk.
1002 *
1003 * It also happens to be a multiple of 4Kb.
1004 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001005 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
NeilBrown0002b272005-09-09 16:23:53 -07001007 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (ret) return ret;
1009
1010 ret = -EINVAL;
1011
1012 bdevname(rdev->bdev, b);
1013 sb = (mdp_super_t*)page_address(rdev->sb_page);
1014
1015 if (sb->md_magic != MD_SB_MAGIC) {
1016 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1017 b);
1018 goto abort;
1019 }
1020
1021 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001022 sb->minor_version < 90 ||
1023 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1025 sb->major_version, sb->minor_version,
1026 b);
1027 goto abort;
1028 }
1029
1030 if (sb->raid_disks <= 0)
1031 goto abort;
1032
NeilBrown4d167f02007-05-09 02:35:37 -07001033 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1035 b);
1036 goto abort;
1037 }
1038
1039 rdev->preferred_minor = sb->md_minor;
1040 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001041 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (sb->level == LEVEL_MULTIPATH)
1044 rdev->desc_nr = -1;
1045 else
1046 rdev->desc_nr = sb->this_disk.number;
1047
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001048 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001050 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 __u64 ev1, ev2;
1052 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1053 if (!uuid_equal(refsb, sb)) {
1054 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1055 b, bdevname(refdev->bdev,b2));
1056 goto abort;
1057 }
1058 if (!sb_equal(refsb, sb)) {
1059 printk(KERN_WARNING "md: %s has same UUID"
1060 " but different superblock to %s\n",
1061 b, bdevname(refdev->bdev, b2));
1062 goto abort;
1063 }
1064 ev1 = md_event(sb);
1065 ev2 = md_event(refsb);
1066 if (ev1 > ev2)
1067 ret = 1;
1068 else
1069 ret = 0;
1070 }
NeilBrown8190e752009-06-18 08:48:58 +10001071 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Andre Nolldd8ac332009-03-31 14:33:13 +11001073 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001074 /* "this cannot possibly happen" ... */
1075 ret = -EINVAL;
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 abort:
1078 return ret;
1079}
1080
1081/*
1082 * validate_super for 0.90.0
1083 */
1084static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1085{
1086 mdp_disk_t *desc;
1087 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001088 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
NeilBrown41158c72005-06-21 17:17:25 -07001090 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001091 clear_bit(Faulty, &rdev->flags);
1092 clear_bit(In_sync, &rdev->flags);
1093 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (mddev->raid_disks == 0) {
1096 mddev->major_version = 0;
1097 mddev->minor_version = sb->minor_version;
1098 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001099 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001100 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 mddev->ctime = sb->ctime;
1102 mddev->utime = sb->utime;
1103 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001104 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 mddev->layout = sb->layout;
1106 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001107 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001108 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001109 mddev->bitmap_info.offset = 0;
1110 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
NeilBrownf6705572006-03-27 01:18:11 -08001112 if (mddev->minor_version >= 91) {
1113 mddev->reshape_position = sb->reshape_position;
1114 mddev->delta_disks = sb->delta_disks;
1115 mddev->new_level = sb->new_level;
1116 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001117 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001118 } else {
1119 mddev->reshape_position = MaxSector;
1120 mddev->delta_disks = 0;
1121 mddev->new_level = mddev->level;
1122 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001123 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001124 }
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (sb->state & (1<<MD_SB_CLEAN))
1127 mddev->recovery_cp = MaxSector;
1128 else {
1129 if (sb->events_hi == sb->cp_events_hi &&
1130 sb->events_lo == sb->cp_events_lo) {
1131 mddev->recovery_cp = sb->recovery_cp;
1132 } else
1133 mddev->recovery_cp = 0;
1134 }
1135
1136 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1137 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1138 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1139 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1140
1141 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001142
1143 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001144 mddev->bitmap_info.file == NULL)
1145 mddev->bitmap_info.offset =
1146 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001147
NeilBrown41158c72005-06-21 17:17:25 -07001148 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001149 /* Insist on good event counter while assembling, except
1150 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001152 if (sb->disks[rdev->desc_nr].state & (
1153 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1154 if (ev1 < mddev->events)
1155 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001156 } else if (mddev->bitmap) {
1157 /* if adding to array with a bitmap, then we can accept an
1158 * older device ... but not too old.
1159 */
NeilBrown41158c72005-06-21 17:17:25 -07001160 if (ev1 < mddev->bitmap->events_cleared)
1161 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001162 } else {
1163 if (ev1 < mddev->events)
1164 /* just a hot-add of a new device, leave raid_disk at -1 */
1165 return 0;
1166 }
NeilBrown41158c72005-06-21 17:17:25 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 desc = sb->disks + rdev->desc_nr;
1170
1171 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001172 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001173 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1174 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001175 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001177 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1178 /* active but not in sync implies recovery up to
1179 * reshape position. We don't know exactly where
1180 * that is, so set to zero for now */
1181 if (mddev->minor_version >= 91) {
1182 rdev->recovery_offset = 0;
1183 rdev->raid_disk = desc->raid_disk;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001186 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1187 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001188 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001189 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return 0;
1191}
1192
1193/*
1194 * sync_super for 0.90.0
1195 */
1196static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1197{
1198 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 mdk_rdev_t *rdev2;
1200 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /* make rdev->sb match mddev data..
1204 *
1205 * 1/ zero out disks
1206 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1207 * 3/ any empty disks < next_spare become removed
1208 *
1209 * disks[0] gets initialised to REMOVED because
1210 * we cannot be sure from other fields if it has
1211 * been initialised or not.
1212 */
1213 int i;
1214 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1215
NeilBrown61181562005-09-09 16:24:02 -07001216 rdev->sb_size = MD_SB_BYTES;
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 sb = (mdp_super_t*)page_address(rdev->sb_page);
1219
1220 memset(sb, 0, sizeof(*sb));
1221
1222 sb->md_magic = MD_SB_MAGIC;
1223 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 sb->patch_version = mddev->patch_version;
1225 sb->gvalid_words = 0; /* ignored */
1226 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1227 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1228 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1229 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1230
1231 sb->ctime = mddev->ctime;
1232 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001233 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 sb->raid_disks = mddev->raid_disks;
1235 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001236 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 sb->utime = mddev->utime;
1238 sb->state = 0;
1239 sb->events_hi = (mddev->events>>32);
1240 sb->events_lo = (u32)mddev->events;
1241
NeilBrownf6705572006-03-27 01:18:11 -08001242 if (mddev->reshape_position == MaxSector)
1243 sb->minor_version = 90;
1244 else {
1245 sb->minor_version = 91;
1246 sb->reshape_position = mddev->reshape_position;
1247 sb->new_level = mddev->new_level;
1248 sb->delta_disks = mddev->delta_disks;
1249 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001250 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001251 }
1252 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (mddev->in_sync)
1254 {
1255 sb->recovery_cp = mddev->recovery_cp;
1256 sb->cp_events_hi = (mddev->events>>32);
1257 sb->cp_events_lo = (u32)mddev->events;
1258 if (mddev->recovery_cp == MaxSector)
1259 sb->state = (1<< MD_SB_CLEAN);
1260 } else
1261 sb->recovery_cp = 0;
1262
1263 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001264 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
NeilBrownc3d97142009-12-14 12:49:52 +11001266 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001267 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001270 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001272 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001273 int is_active = test_bit(In_sync, &rdev2->flags);
1274
1275 if (rdev2->raid_disk >= 0 &&
1276 sb->minor_version >= 91)
1277 /* we have nowhere to store the recovery_offset,
1278 * but if it is not below the reshape_position,
1279 * we can piggy-back on that.
1280 */
1281 is_active = 1;
1282 if (rdev2->raid_disk < 0 ||
1283 test_bit(Faulty, &rdev2->flags))
1284 is_active = 0;
1285 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001286 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001288 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001289 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 d = &sb->disks[rdev2->desc_nr];
1291 nr_disks++;
1292 d->number = rdev2->desc_nr;
1293 d->major = MAJOR(rdev2->bdev->bd_dev);
1294 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001295 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 d->raid_disk = rdev2->raid_disk;
1297 else
1298 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001299 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001301 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001303 if (test_bit(In_sync, &rdev2->flags))
1304 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 active++;
1306 working++;
1307 } else {
1308 d->state = 0;
1309 spare++;
1310 working++;
1311 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001312 if (test_bit(WriteMostly, &rdev2->flags))
1313 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* now set the "removed" and "faulty" bits on any missing devices */
1316 for (i=0 ; i < mddev->raid_disks ; i++) {
1317 mdp_disk_t *d = &sb->disks[i];
1318 if (d->state == 0 && d->number == 0) {
1319 d->number = i;
1320 d->raid_disk = i;
1321 d->state = (1<<MD_DISK_REMOVED);
1322 d->state |= (1<<MD_DISK_FAULTY);
1323 failed++;
1324 }
1325 }
1326 sb->nr_disks = nr_disks;
1327 sb->active_disks = active;
1328 sb->working_disks = working;
1329 sb->failed_disks = failed;
1330 sb->spare_disks = spare;
1331
1332 sb->this_disk = sb->disks[rdev->desc_nr];
1333 sb->sb_csum = calc_sb_csum(sb);
1334}
1335
1336/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337 * rdev_size_change for 0.90.0
1338 */
1339static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001340super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341{
Andre Noll58c0fed2009-03-31 14:33:13 +11001342 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001343 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001344 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001345 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001346 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001347 if (!num_sectors || num_sectors > rdev->sb_start)
1348 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001349 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001350 rdev->sb_page);
1351 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001352 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001353}
1354
1355
1356/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * version 1 superblock
1358 */
1359
NeilBrown1c05b4b2006-10-21 10:24:08 -07001360static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001362 __le32 disk_csum;
1363 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 unsigned long long newcsum;
1365 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001366 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int i;
1368
1369 disk_csum = sb->sb_csum;
1370 sb->sb_csum = 0;
1371 newcsum = 0;
1372 for (i=0; size>=4; size -= 4 )
1373 newcsum += le32_to_cpu(*isuper++);
1374
1375 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001376 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1379 sb->sb_csum = disk_csum;
1380 return cpu_to_le32(csum);
1381}
1382
1383static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1384{
1385 struct mdp_superblock_1 *sb;
1386 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001387 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001389 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /*
Andre Noll0f420352008-07-11 22:02:23 +10001392 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * It is always aligned to a 4K boundary and
1394 * depeding on minor_version, it can be:
1395 * 0: At least 8K, but less than 12K, from end of device
1396 * 1: At start of device
1397 * 2: 4K from start of device.
1398 */
1399 switch(minor_version) {
1400 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001401 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001402 sb_start -= 8*2;
1403 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 break;
1405 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001406 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
1408 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001409 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
1411 default:
1412 return -EINVAL;
1413 }
Andre Noll0f420352008-07-11 22:02:23 +10001414 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
NeilBrown0002b272005-09-09 16:23:53 -07001416 /* superblock is rarely larger than 1K, but it can be larger,
1417 * and it is safe to read 4k, so we do that
1418 */
1419 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (ret) return ret;
1421
1422
1423 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1424
1425 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1426 sb->major_version != cpu_to_le32(1) ||
1427 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001428 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001429 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return -EINVAL;
1431
1432 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1433 printk("md: invalid superblock checksum on %s\n",
1434 bdevname(rdev->bdev,b));
1435 return -EINVAL;
1436 }
1437 if (le64_to_cpu(sb->data_size) < 10) {
1438 printk("md: data_size too small on %s\n",
1439 bdevname(rdev->bdev,b));
1440 return -EINVAL;
1441 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 rdev->preferred_minor = 0xffff;
1444 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001445 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
NeilBrown0002b272005-09-09 16:23:53 -07001447 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001448 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001449 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001450 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1451
1452 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001453 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001454 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001455
NeilBrown31b65a02006-07-10 04:44:14 -07001456 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1457 rdev->desc_nr = -1;
1458 else
1459 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1460
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001461 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001462 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001463 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 __u64 ev1, ev2;
1465 struct mdp_superblock_1 *refsb =
1466 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1467
1468 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1469 sb->level != refsb->level ||
1470 sb->layout != refsb->layout ||
1471 sb->chunksize != refsb->chunksize) {
1472 printk(KERN_WARNING "md: %s has strangely different"
1473 " superblock to %s\n",
1474 bdevname(rdev->bdev,b),
1475 bdevname(refdev->bdev,b2));
1476 return -EINVAL;
1477 }
1478 ev1 = le64_to_cpu(sb->events);
1479 ev2 = le64_to_cpu(refsb->events);
1480
1481 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001482 ret = 1;
1483 else
1484 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
NeilBrowna1801f82008-03-04 14:29:31 -08001486 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001487 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001488 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001490 rdev->sectors = rdev->sb_start;
1491 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001493 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001494 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001495 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1500{
1501 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001502 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
NeilBrown41158c72005-06-21 17:17:25 -07001504 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001505 clear_bit(Faulty, &rdev->flags);
1506 clear_bit(In_sync, &rdev->flags);
1507 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (mddev->raid_disks == 0) {
1510 mddev->major_version = 1;
1511 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001512 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001513 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1515 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1516 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001517 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 mddev->layout = le32_to_cpu(sb->layout);
1519 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001520 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001521 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001522 mddev->bitmap_info.offset = 0;
1523 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1526 memcpy(mddev->uuid, sb->set_uuid, 16);
1527
1528 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001529
NeilBrown71c08052005-09-09 16:23:51 -07001530 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001531 mddev->bitmap_info.file == NULL )
1532 mddev->bitmap_info.offset =
1533 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001534
NeilBrownf6705572006-03-27 01:18:11 -08001535 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1536 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1537 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1538 mddev->new_level = le32_to_cpu(sb->new_level);
1539 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001540 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001541 } else {
1542 mddev->reshape_position = MaxSector;
1543 mddev->delta_disks = 0;
1544 mddev->new_level = mddev->level;
1545 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001546 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001547 }
1548
NeilBrown41158c72005-06-21 17:17:25 -07001549 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001550 /* Insist of good event counter while assembling, except for
1551 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001553 if (rdev->desc_nr >= 0 &&
1554 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1555 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1556 if (ev1 < mddev->events)
1557 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001558 } else if (mddev->bitmap) {
1559 /* If adding to array with a bitmap, then we can accept an
1560 * older device, but not too old.
1561 */
NeilBrown41158c72005-06-21 17:17:25 -07001562 if (ev1 < mddev->bitmap->events_cleared)
1563 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001564 } else {
1565 if (ev1 < mddev->events)
1566 /* just a hot-add of a new device, leave raid_disk at -1 */
1567 return 0;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (mddev->level != LEVEL_MULTIPATH) {
1570 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001571 if (rdev->desc_nr < 0 ||
1572 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1573 role = 0xffff;
1574 rdev->desc_nr = -1;
1575 } else
1576 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 switch(role) {
1578 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 break;
1580 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001581 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
1583 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001584 if ((le32_to_cpu(sb->feature_map) &
1585 MD_FEATURE_RECOVERY_OFFSET))
1586 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1587 else
1588 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 rdev->raid_disk = role;
1590 break;
1591 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001592 if (sb->devflags & WriteMostly1)
1593 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001594 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001595 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return 0;
1598}
1599
1600static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1601{
1602 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 mdk_rdev_t *rdev2;
1604 int max_dev, i;
1605 /* make rdev->sb match mddev and rdev data. */
1606
1607 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1608
1609 sb->feature_map = 0;
1610 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001611 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 memset(sb->pad1, 0, sizeof(sb->pad1));
1613 memset(sb->pad2, 0, sizeof(sb->pad2));
1614 memset(sb->pad3, 0, sizeof(sb->pad3));
1615
1616 sb->utime = cpu_to_le64((__u64)mddev->utime);
1617 sb->events = cpu_to_le64(mddev->events);
1618 if (mddev->in_sync)
1619 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1620 else
1621 sb->resync_offset = cpu_to_le64(0);
1622
NeilBrown1c05b4b2006-10-21 10:24:08 -07001623 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001624
NeilBrownf0ca3402006-02-02 14:28:04 -08001625 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001626 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001627 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001628 sb->level = cpu_to_le32(mddev->level);
1629 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001630
NeilBrownc3d97142009-12-14 12:49:52 +11001631 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1632 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001633 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001634 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001635
1636 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001637 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001638 sb->feature_map |=
1639 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1640 sb->recovery_offset =
1641 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001642 }
1643
NeilBrownf6705572006-03-27 01:18:11 -08001644 if (mddev->reshape_position != MaxSector) {
1645 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1646 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1647 sb->new_layout = cpu_to_le32(mddev->new_layout);
1648 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1649 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001650 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001651 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001654 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (rdev2->desc_nr+1 > max_dev)
1656 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001657
NeilBrown70471da2009-08-03 10:59:57 +10001658 if (max_dev > le32_to_cpu(sb->max_dev)) {
1659 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001660 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001661 rdev->sb_size = max_dev * 2 + 256;
1662 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1663 if (rdev->sb_size & bmask)
1664 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001665 } else
1666 max_dev = le32_to_cpu(sb->max_dev);
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 for (i=0; i<max_dev;i++)
1669 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1670
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001671 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001673 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001675 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001677 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001678 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 else
1680 sb->dev_roles[i] = cpu_to_le16(0xffff);
1681 }
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 sb->sb_csum = calc_sb_1_csum(sb);
1684}
1685
Chris Webb0cd17fe2008-06-28 08:31:46 +10001686static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001687super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001688{
1689 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001690 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001691 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001692 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001693 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001694 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001695 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001696 max_sectors -= rdev->data_offset;
1697 if (!num_sectors || num_sectors > max_sectors)
1698 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001699 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001700 /* minor version 0 with bitmap we can't move */
1701 return 0;
1702 } else {
1703 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001704 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001705 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001706 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001707 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001708 if (!num_sectors || num_sectors > max_sectors)
1709 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001710 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001711 }
1712 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001713 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001714 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001715 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001716 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001717 rdev->sb_page);
1718 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001719 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001720}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Adrian Bunk75c96f82005-05-05 16:16:09 -07001722static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 [0] = {
1724 .name = "0.90.0",
1725 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001726 .load_super = super_90_load,
1727 .validate_super = super_90_validate,
1728 .sync_super = super_90_sync,
1729 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 },
1731 [1] = {
1732 .name = "md-1",
1733 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001734 .load_super = super_1_load,
1735 .validate_super = super_1_validate,
1736 .sync_super = super_1_sync,
1737 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 },
1739};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1742{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001743 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
NeilBrown4b809912008-07-21 17:05:25 +10001745 rcu_read_lock();
1746 rdev_for_each_rcu(rdev, mddev1)
1747 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001748 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001749 rdev2->bdev->bd_contains) {
1750 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001751 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001752 }
1753 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return 0;
1755}
1756
1757static LIST_HEAD(pending_raid_disks);
1758
Andre Nollac5e7112009-08-03 10:59:47 +10001759/*
1760 * Try to register data integrity profile for an mddev
1761 *
1762 * This is called when an array is started and after a disk has been kicked
1763 * from the array. It only succeeds if all working and active component devices
1764 * are integrity capable with matching profiles.
1765 */
1766int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001767{
Andre Nollac5e7112009-08-03 10:59:47 +10001768 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001769
Andre Nollac5e7112009-08-03 10:59:47 +10001770 if (list_empty(&mddev->disks))
1771 return 0; /* nothing to do */
1772 if (blk_get_integrity(mddev->gendisk))
1773 return 0; /* already registered */
1774 list_for_each_entry(rdev, &mddev->disks, same_set) {
1775 /* skip spares and non-functional disks */
1776 if (test_bit(Faulty, &rdev->flags))
1777 continue;
1778 if (rdev->raid_disk < 0)
1779 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001780 if (!reference) {
1781 /* Use the first rdev as the reference */
1782 reference = rdev;
1783 continue;
1784 }
1785 /* does this rdev's profile match the reference profile? */
1786 if (blk_integrity_compare(reference->bdev->bd_disk,
1787 rdev->bdev->bd_disk) < 0)
1788 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001789 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001790 if (!reference || !bdev_get_integrity(reference->bdev))
1791 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001792 /*
1793 * All component devices are integrity capable and have matching
1794 * profiles, register the common profile for the md device.
1795 */
1796 if (blk_integrity_register(mddev->gendisk,
1797 bdev_get_integrity(reference->bdev)) != 0) {
1798 printk(KERN_ERR "md: failed to register integrity for %s\n",
1799 mdname(mddev));
1800 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001801 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001802 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1803 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1804 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1805 mdname(mddev));
1806 return -EINVAL;
1807 }
Andre Nollac5e7112009-08-03 10:59:47 +10001808 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001809}
Andre Nollac5e7112009-08-03 10:59:47 +10001810EXPORT_SYMBOL(md_integrity_register);
1811
1812/* Disable data integrity if non-capable/non-matching disk is being added */
1813void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1814{
1815 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1816 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1817
1818 if (!bi_mddev) /* nothing to do */
1819 return;
1820 if (rdev->raid_disk < 0) /* skip spares */
1821 return;
1822 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1823 rdev->bdev->bd_disk) >= 0)
1824 return;
1825 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1826 blk_integrity_unregister(mddev->gendisk);
1827}
1828EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1831{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001832 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001833 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001834 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001835 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 if (rdev->mddev) {
1838 MD_BUG();
1839 return -EINVAL;
1840 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001841
1842 /* prevent duplicates */
1843 if (find_rdev(mddev, rdev->bdev->bd_dev))
1844 return -EEXIST;
1845
Andre Nolldd8ac332009-03-31 14:33:13 +11001846 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1847 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1848 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001849 if (mddev->pers) {
1850 /* Cannot change size, so fail
1851 * If mddev->level <= 0, then we don't care
1852 * about aligning sizes (e.g. linear)
1853 */
1854 if (mddev->level > 0)
1855 return -ENOSPC;
1856 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001857 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /* Verify rdev->desc_nr is unique.
1861 * If it is -1, assign a free number, else
1862 * check number is not in use
1863 */
1864 if (rdev->desc_nr < 0) {
1865 int choice = 0;
1866 if (mddev->pers) choice = mddev->raid_disks;
1867 while (find_rdev_nr(mddev, choice))
1868 choice++;
1869 rdev->desc_nr = choice;
1870 } else {
1871 if (find_rdev_nr(mddev, rdev->desc_nr))
1872 return -EBUSY;
1873 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001874 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1875 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1876 mdname(mddev), mddev->max_disks);
1877 return -EBUSY;
1878 }
NeilBrown19133a42005-11-08 21:39:35 -08001879 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001880 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001881 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001884 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001885
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001886 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001887 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001888
Tejun Heo0762b8b2008-08-25 19:56:12 +09001889 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001890 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1891 /* failure here is OK */;
1892 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001893
NeilBrown4b809912008-07-21 17:05:25 +10001894 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01001895 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001896
1897 /* May as well allow recovery to be retried once */
1898 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001901
1902 fail:
1903 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1904 b, mdname(mddev));
1905 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
NeilBrown177a99b2008-02-06 01:39:56 -08001908static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001909{
1910 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1911 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001912 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001913}
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1916{
1917 char b[BDEVNAME_SIZE];
1918 if (!rdev->mddev) {
1919 MD_BUG();
1920 return;
1921 }
Tejun Heo49731ba2011-01-14 18:43:57 +01001922 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001923 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1925 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001926 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001927 sysfs_put(rdev->sysfs_state);
1928 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001929 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001930 * writing to 'remove' to "dev/state". We also need
1931 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001932 */
NeilBrown4b809912008-07-21 17:05:25 +10001933 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001934 INIT_WORK(&rdev->del_work, md_delayed_delete);
1935 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001936 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
1939/*
1940 * prevent the device from being mounted, repartitioned or
1941 * otherwise reused by a RAID array (or any other kernel
1942 * subsystem), by bd_claiming the device.
1943 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001944static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 int err = 0;
1947 struct block_device *bdev;
1948 char b[BDEVNAME_SIZE];
1949
Tejun Heod4d77622010-11-13 11:55:18 +01001950 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1951 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (IS_ERR(bdev)) {
1953 printk(KERN_ERR "md: could not open %s.\n",
1954 __bdevname(dev, b));
1955 return PTR_ERR(bdev);
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 rdev->bdev = bdev;
1958 return err;
1959}
1960
1961static void unlock_rdev(mdk_rdev_t *rdev)
1962{
1963 struct block_device *bdev = rdev->bdev;
1964 rdev->bdev = NULL;
1965 if (!bdev)
1966 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01001967 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970void md_autodetect_dev(dev_t dev);
1971
1972static void export_rdev(mdk_rdev_t * rdev)
1973{
1974 char b[BDEVNAME_SIZE];
1975 printk(KERN_INFO "md: export_rdev(%s)\n",
1976 bdevname(rdev->bdev,b));
1977 if (rdev->mddev)
1978 MD_BUG();
1979 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001981 if (test_bit(AutoDetected, &rdev->flags))
1982 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983#endif
1984 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001985 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
1987
1988static void kick_rdev_from_array(mdk_rdev_t * rdev)
1989{
1990 unbind_rdev_from_array(rdev);
1991 export_rdev(rdev);
1992}
1993
1994static void export_array(mddev_t *mddev)
1995{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001996 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
NeilBrownd089c6a2008-02-06 01:39:59 -08001998 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (!rdev->mddev) {
2000 MD_BUG();
2001 continue;
2002 }
2003 kick_rdev_from_array(rdev);
2004 }
2005 if (!list_empty(&mddev->disks))
2006 MD_BUG();
2007 mddev->raid_disks = 0;
2008 mddev->major_version = 0;
2009}
2010
2011static void print_desc(mdp_disk_t *desc)
2012{
2013 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2014 desc->major,desc->minor,desc->raid_disk,desc->state);
2015}
2016
Cheng Renquancd2ac932009-01-09 08:31:08 +11002017static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 int i;
2020
2021 printk(KERN_INFO
2022 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2023 sb->major_version, sb->minor_version, sb->patch_version,
2024 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2025 sb->ctime);
2026 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2027 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2028 sb->md_minor, sb->layout, sb->chunk_size);
2029 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2030 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2031 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2032 sb->failed_disks, sb->spare_disks,
2033 sb->sb_csum, (unsigned long)sb->events_lo);
2034
2035 printk(KERN_INFO);
2036 for (i = 0; i < MD_SB_DISKS; i++) {
2037 mdp_disk_t *desc;
2038
2039 desc = sb->disks + i;
2040 if (desc->number || desc->major || desc->minor ||
2041 desc->raid_disk || (desc->state && (desc->state != 4))) {
2042 printk(" D %2d: ", i);
2043 print_desc(desc);
2044 }
2045 }
2046 printk(KERN_INFO "md: THIS: ");
2047 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Cheng Renquancd2ac932009-01-09 08:31:08 +11002050static void print_sb_1(struct mdp_superblock_1 *sb)
2051{
2052 __u8 *uuid;
2053
2054 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002055 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002056 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002057 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002058 le32_to_cpu(sb->major_version),
2059 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002060 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002061 sb->set_name,
2062 (unsigned long long)le64_to_cpu(sb->ctime)
2063 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2064
2065 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002066 printk(KERN_INFO
2067 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002068 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002069 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002070 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2071 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002072 le32_to_cpu(sb->level),
2073 (unsigned long long)le64_to_cpu(sb->size),
2074 le32_to_cpu(sb->raid_disks),
2075 le32_to_cpu(sb->layout),
2076 le32_to_cpu(sb->chunksize),
2077 (unsigned long long)le64_to_cpu(sb->data_offset),
2078 (unsigned long long)le64_to_cpu(sb->data_size),
2079 (unsigned long long)le64_to_cpu(sb->super_offset),
2080 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2081 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002082 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002083 sb->devflags,
2084 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2085 (unsigned long long)le64_to_cpu(sb->events),
2086 (unsigned long long)le64_to_cpu(sb->resync_offset),
2087 le32_to_cpu(sb->sb_csum),
2088 le32_to_cpu(sb->max_dev)
2089 );
2090}
2091
2092static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002095 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2096 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002097 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2098 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002100 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2101 switch (major_version) {
2102 case 0:
2103 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2104 break;
2105 case 1:
2106 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2107 break;
2108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 } else
2110 printk(KERN_INFO "md: no rdev superblock!\n");
2111}
2112
Adrian Bunk5e563412006-06-26 00:27:42 -07002113static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002115 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 mdk_rdev_t *rdev;
2117 mddev_t *mddev;
2118 char b[BDEVNAME_SIZE];
2119
2120 printk("\n");
2121 printk("md: **********************************\n");
2122 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2123 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002124 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
NeilBrown32a76272005-06-21 17:17:14 -07002126 if (mddev->bitmap)
2127 bitmap_print_sb(mddev->bitmap);
2128 else
2129 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002130 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 printk("<%s>", bdevname(rdev->bdev,b));
2132 printk("\n");
2133
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002134 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002135 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137 printk("md: **********************************\n");
2138 printk("\n");
2139}
2140
2141
NeilBrown42543762006-06-26 00:27:57 -07002142static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
NeilBrown42543762006-06-26 00:27:57 -07002144 /* Update each superblock (in-memory image), but
2145 * if we are allowed to, skip spares which already
2146 * have the right event counter, or have one earlier
2147 * (which would mean they aren't being marked as dirty
2148 * with the rest of the array)
2149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002151 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002152 if (rdev->sb_events == mddev->events ||
2153 (nospares &&
2154 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002155 rdev->sb_events+1 == mddev->events)) {
2156 /* Don't update this superblock */
2157 rdev->sb_loaded = 2;
2158 } else {
2159 super_types[mddev->major_version].
2160 sync_super(mddev, rdev);
2161 rdev->sb_loaded = 1;
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164}
2165
NeilBrown850b2b42006-10-03 01:15:46 -07002166static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002169 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002170 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002173 /* First make sure individual recovery_offsets are correct */
2174 list_for_each_entry(rdev, &mddev->disks, same_set) {
2175 if (rdev->raid_disk >= 0 &&
2176 mddev->delta_disks >= 0 &&
2177 !test_bit(In_sync, &rdev->flags) &&
2178 mddev->curr_resync_completed > rdev->recovery_offset)
2179 rdev->recovery_offset = mddev->curr_resync_completed;
2180
2181 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002182 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002183 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002184 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41dc2010-10-28 17:30:20 +11002185 if (!mddev->external)
2186 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002187 wake_up(&mddev->sb_wait);
2188 return;
2189 }
2190
NeilBrowna9701a32005-11-08 21:39:34 -08002191 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002192
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002193 mddev->utime = get_seconds();
2194
NeilBrown850b2b42006-10-03 01:15:46 -07002195 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2196 force_change = 1;
2197 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2198 /* just a clean<-> dirty transition, possibly leave spares alone,
2199 * though if events isn't the right even/odd, we will have to do
2200 * spares after all
2201 */
2202 nospares = 1;
2203 if (force_change)
2204 nospares = 0;
2205 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002206 /* If the array is degraded, then skipping spares is both
2207 * dangerous and fairly pointless.
2208 * Dangerous because a device that was removed from the array
2209 * might have a event_count that still looks up-to-date,
2210 * so it can be re-added without a resync.
2211 * Pointless because if there are any spares to skip,
2212 * then a recovery will happen and soon that array won't
2213 * be degraded any more and the spare can go back to sleep then.
2214 */
NeilBrown850b2b42006-10-03 01:15:46 -07002215 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002216
NeilBrown06d91a52005-06-21 17:17:12 -07002217 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002218
2219 /* If this is just a dirty<->clean transition, and the array is clean
2220 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002221 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002222 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002223 && mddev->can_decrease_events
2224 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002225 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002226 mddev->can_decrease_events = 0;
2227 } else {
NeilBrown42543762006-06-26 00:27:57 -07002228 /* otherwise we have to go forward and ... */
2229 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002230 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 if (!mddev->events) {
2234 /*
2235 * oops, this 64-bit counter should never wrap.
2236 * Either we are in around ~1 trillion A.C., assuming
2237 * 1 reboot per second, or we have a bug:
2238 */
2239 MD_BUG();
2240 mddev->events --;
2241 }
NeilBrowne6910632008-02-06 01:39:51 -08002242 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002243 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 dprintk(KERN_INFO
2246 "md: updating %s RAID superblock on device (in sync %d)\n",
2247 mdname(mddev),mddev->in_sync);
2248
NeilBrown4ad13662007-07-17 04:06:13 -07002249 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002250 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 char b[BDEVNAME_SIZE];
2252 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002253 if (rdev->sb_loaded != 1)
2254 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002255 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 dprintk("(skipping faulty ");
2257
2258 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002259 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002260 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002261 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002262 rdev->sb_page);
2263 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2264 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002265 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002266 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 } else
2269 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002270 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /* only need to write one superblock... */
2272 break;
2273 }
NeilBrowna9701a32005-11-08 21:39:34 -08002274 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002275 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002276
NeilBrowna9701a32005-11-08 21:39:34 -08002277 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002278 if (mddev->in_sync != sync_req ||
2279 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002280 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002281 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002282 goto repeat;
2283 }
NeilBrown850b2b42006-10-03 01:15:46 -07002284 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002285 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002286 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002287 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2288 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Andre Noll7f6ce762008-03-23 18:34:54 +01002292/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002293 * We want to accept with case. For this we use cmd_match.
2294 */
2295static int cmd_match(const char *cmd, const char *str)
2296{
2297 /* See if cmd, written into a sysfs file, matches
2298 * str. They must either be the same, or cmd can
2299 * have a trailing newline
2300 */
2301 while (*cmd && *str && *cmd == *str) {
2302 cmd++;
2303 str++;
2304 }
2305 if (*cmd == '\n')
2306 cmd++;
2307 if (*str || *cmd)
2308 return 0;
2309 return 1;
2310}
2311
NeilBrown86e6ffd2005-11-08 21:39:24 -08002312struct rdev_sysfs_entry {
2313 struct attribute attr;
2314 ssize_t (*show)(mdk_rdev_t *, char *);
2315 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2316};
2317
2318static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002319state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002320{
2321 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002322 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002323
NeilBrownb2d444d2005-11-08 21:39:31 -08002324 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002325 len+= sprintf(page+len, "%sfaulty",sep);
2326 sep = ",";
2327 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002328 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002329 len += sprintf(page+len, "%sin_sync",sep);
2330 sep = ",";
2331 }
NeilBrownf6556752006-06-26 00:28:01 -07002332 if (test_bit(WriteMostly, &rdev->flags)) {
2333 len += sprintf(page+len, "%swrite_mostly",sep);
2334 sep = ",";
2335 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002336 if (test_bit(Blocked, &rdev->flags)) {
2337 len += sprintf(page+len, "%sblocked", sep);
2338 sep = ",";
2339 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002340 if (!test_bit(Faulty, &rdev->flags) &&
2341 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002342 len += sprintf(page+len, "%sspare", sep);
2343 sep = ",";
2344 }
2345 return len+sprintf(page+len, "\n");
2346}
2347
NeilBrown45dc2de2006-06-26 00:27:58 -07002348static ssize_t
2349state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2350{
2351 /* can write
2352 * faulty - simulates and error
2353 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002354 * writemostly - sets write_mostly
2355 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002356 * blocked - sets the Blocked flag
2357 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002358 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002359 */
2360 int err = -EINVAL;
2361 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2362 md_error(rdev->mddev, rdev);
2363 err = 0;
2364 } else if (cmd_match(buf, "remove")) {
2365 if (rdev->raid_disk >= 0)
2366 err = -EBUSY;
2367 else {
2368 mddev_t *mddev = rdev->mddev;
2369 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002370 if (mddev->pers)
2371 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002372 md_new_event(mddev);
2373 err = 0;
2374 }
NeilBrownf6556752006-06-26 00:28:01 -07002375 } else if (cmd_match(buf, "writemostly")) {
2376 set_bit(WriteMostly, &rdev->flags);
2377 err = 0;
2378 } else if (cmd_match(buf, "-writemostly")) {
2379 clear_bit(WriteMostly, &rdev->flags);
2380 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002381 } else if (cmd_match(buf, "blocked")) {
2382 set_bit(Blocked, &rdev->flags);
2383 err = 0;
2384 } else if (cmd_match(buf, "-blocked")) {
2385 clear_bit(Blocked, &rdev->flags);
2386 wake_up(&rdev->blocked_wait);
2387 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2388 md_wakeup_thread(rdev->mddev->thread);
2389
2390 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002391 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2392 set_bit(In_sync, &rdev->flags);
2393 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002394 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002395 if (!err)
2396 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002397 return err ? err : len;
2398}
NeilBrown80ca3a42006-07-10 04:44:18 -07002399static struct rdev_sysfs_entry rdev_state =
2400__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002401
2402static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002403errors_show(mdk_rdev_t *rdev, char *page)
2404{
2405 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2406}
2407
2408static ssize_t
2409errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2410{
2411 char *e;
2412 unsigned long n = simple_strtoul(buf, &e, 10);
2413 if (*buf && (*e == 0 || *e == '\n')) {
2414 atomic_set(&rdev->corrected_errors, n);
2415 return len;
2416 }
2417 return -EINVAL;
2418}
2419static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002420__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002421
NeilBrown014236d2006-01-06 00:20:55 -08002422static ssize_t
2423slot_show(mdk_rdev_t *rdev, char *page)
2424{
2425 if (rdev->raid_disk < 0)
2426 return sprintf(page, "none\n");
2427 else
2428 return sprintf(page, "%d\n", rdev->raid_disk);
2429}
2430
2431static ssize_t
2432slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2433{
2434 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002435 int err;
2436 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002437 int slot = simple_strtoul(buf, &e, 10);
2438 if (strncmp(buf, "none", 4)==0)
2439 slot = -1;
2440 else if (e==buf || (*e && *e!= '\n'))
2441 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002442 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002443 /* Setting 'slot' on an active array requires also
2444 * updating the 'rd%d' link, and communicating
2445 * with the personality with ->hot_*_disk.
2446 * For now we only support removing
2447 * failed/spare devices. This normally happens automatically,
2448 * but not when the metadata is externally managed.
2449 */
NeilBrownc303da62008-02-06 01:39:51 -08002450 if (rdev->raid_disk == -1)
2451 return -EEXIST;
2452 /* personality does all needed checks */
2453 if (rdev->mddev->pers->hot_add_disk == NULL)
2454 return -EINVAL;
2455 err = rdev->mddev->pers->
2456 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2457 if (err)
2458 return err;
2459 sprintf(nm, "rd%d", rdev->raid_disk);
2460 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002461 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002462 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2463 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002464 } else if (rdev->mddev->pers) {
2465 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002466 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002467 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002468 */
2469
2470 if (rdev->raid_disk != -1)
2471 return -EBUSY;
2472
NeilBrownc6751b22011-02-02 11:57:13 +11002473 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2474 return -EBUSY;
2475
Neil Brown6c2fce22008-06-28 08:31:31 +10002476 if (rdev->mddev->pers->hot_add_disk == NULL)
2477 return -EINVAL;
2478
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002479 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002480 if (rdev2->raid_disk == slot)
2481 return -EEXIST;
2482
NeilBrownba1b41b2011-01-14 09:14:34 +11002483 if (slot >= rdev->mddev->raid_disks &&
2484 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2485 return -ENOSPC;
2486
Neil Brown6c2fce22008-06-28 08:31:31 +10002487 rdev->raid_disk = slot;
2488 if (test_bit(In_sync, &rdev->flags))
2489 rdev->saved_raid_disk = slot;
2490 else
2491 rdev->saved_raid_disk = -1;
2492 err = rdev->mddev->pers->
2493 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002494 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002495 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002496 return err;
Neil Brown52664732008-06-28 08:31:44 +10002497 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002498 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002499 sprintf(nm, "rd%d", rdev->raid_disk);
2500 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002501 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002502 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002503 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002504 if (slot >= rdev->mddev->raid_disks &&
2505 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002506 return -ENOSPC;
2507 rdev->raid_disk = slot;
2508 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002509 clear_bit(Faulty, &rdev->flags);
2510 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002511 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002512 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002513 }
NeilBrown014236d2006-01-06 00:20:55 -08002514 return len;
2515}
2516
2517
2518static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002519__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002520
NeilBrown93c8cad2006-01-06 00:20:56 -08002521static ssize_t
2522offset_show(mdk_rdev_t *rdev, char *page)
2523{
Andrew Morton6961ece2006-01-06 00:20:59 -08002524 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002525}
2526
2527static ssize_t
2528offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2529{
2530 char *e;
2531 unsigned long long offset = simple_strtoull(buf, &e, 10);
2532 if (e==buf || (*e && *e != '\n'))
2533 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002534 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002535 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002536 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002537 /* Must set offset before size, so overlap checks
2538 * can be sane */
2539 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002540 rdev->data_offset = offset;
2541 return len;
2542}
2543
2544static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002545__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002546
NeilBrown83303b62006-01-06 00:21:06 -08002547static ssize_t
2548rdev_size_show(mdk_rdev_t *rdev, char *page)
2549{
Andre Nolldd8ac332009-03-31 14:33:13 +11002550 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002551}
2552
NeilBrownc5d79ad2008-02-06 01:39:54 -08002553static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2554{
2555 /* check if two start/length pairs overlap */
2556 if (s1+l1 <= s2)
2557 return 0;
2558 if (s2+l2 <= s1)
2559 return 0;
2560 return 1;
2561}
2562
Dan Williamsb522adc2009-03-31 15:00:31 +11002563static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2564{
2565 unsigned long long blocks;
2566 sector_t new;
2567
2568 if (strict_strtoull(buf, 10, &blocks) < 0)
2569 return -EINVAL;
2570
2571 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2572 return -EINVAL; /* sector conversion overflow */
2573
2574 new = blocks * 2;
2575 if (new != blocks * 2)
2576 return -EINVAL; /* unsigned long long to sector_t overflow */
2577
2578 *sectors = new;
2579 return 0;
2580}
2581
NeilBrown83303b62006-01-06 00:21:06 -08002582static ssize_t
2583rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2584{
NeilBrown27c529b2008-03-04 14:29:33 -08002585 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002586 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002587 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002588
Dan Williamsb522adc2009-03-31 15:00:31 +11002589 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002590 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002591 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002592 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002593 sectors = super_types[my_mddev->major_version].
2594 rdev_size_change(rdev, sectors);
2595 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002596 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002597 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002598 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002599 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002600 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002601 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002602 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002603
Andre Nolldd8ac332009-03-31 14:33:13 +11002604 rdev->sectors = sectors;
2605 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002606 /* need to check that all other rdevs with the same ->bdev
2607 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002608 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002609 * we have to change it back, we will have the lock again.
2610 */
2611 mddev_t *mddev;
2612 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002613 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002614
NeilBrown27c529b2008-03-04 14:29:33 -08002615 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002616 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002617 mdk_rdev_t *rdev2;
2618
2619 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002620 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002621 if (rdev->bdev == rdev2->bdev &&
2622 rdev != rdev2 &&
2623 overlaps(rdev->data_offset, rdev->sectors,
2624 rdev2->data_offset,
2625 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002626 overlap = 1;
2627 break;
2628 }
2629 mddev_unlock(mddev);
2630 if (overlap) {
2631 mddev_put(mddev);
2632 break;
2633 }
2634 }
NeilBrown27c529b2008-03-04 14:29:33 -08002635 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002636 if (overlap) {
2637 /* Someone else could have slipped in a size
2638 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002639 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002640 * safe, and trust userspace not to race with
2641 * itself
2642 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002643 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002644 return -EBUSY;
2645 }
2646 }
NeilBrown83303b62006-01-06 00:21:06 -08002647 return len;
2648}
2649
2650static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002651__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002652
Dan Williams06e3c812009-12-12 21:17:12 -07002653
2654static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2655{
2656 unsigned long long recovery_start = rdev->recovery_offset;
2657
2658 if (test_bit(In_sync, &rdev->flags) ||
2659 recovery_start == MaxSector)
2660 return sprintf(page, "none\n");
2661
2662 return sprintf(page, "%llu\n", recovery_start);
2663}
2664
2665static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2666{
2667 unsigned long long recovery_start;
2668
2669 if (cmd_match(buf, "none"))
2670 recovery_start = MaxSector;
2671 else if (strict_strtoull(buf, 10, &recovery_start))
2672 return -EINVAL;
2673
2674 if (rdev->mddev->pers &&
2675 rdev->raid_disk >= 0)
2676 return -EBUSY;
2677
2678 rdev->recovery_offset = recovery_start;
2679 if (recovery_start == MaxSector)
2680 set_bit(In_sync, &rdev->flags);
2681 else
2682 clear_bit(In_sync, &rdev->flags);
2683 return len;
2684}
2685
2686static struct rdev_sysfs_entry rdev_recovery_start =
2687__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2688
NeilBrown86e6ffd2005-11-08 21:39:24 -08002689static struct attribute *rdev_default_attrs[] = {
2690 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002691 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002692 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002693 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002694 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002695 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002696 NULL,
2697};
2698static ssize_t
2699rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2700{
2701 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2702 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002703 mddev_t *mddev = rdev->mddev;
2704 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002705
2706 if (!entry->show)
2707 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002708
2709 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2710 if (!rv) {
2711 if (rdev->mddev == NULL)
2712 rv = -EBUSY;
2713 else
2714 rv = entry->show(rdev, page);
2715 mddev_unlock(mddev);
2716 }
2717 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002718}
2719
2720static ssize_t
2721rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2722 const char *page, size_t length)
2723{
2724 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2725 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002726 ssize_t rv;
2727 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002728
2729 if (!entry->store)
2730 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002731 if (!capable(CAP_SYS_ADMIN))
2732 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002733 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002734 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002735 if (rdev->mddev == NULL)
2736 rv = -EBUSY;
2737 else
2738 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002739 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002740 }
2741 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002742}
2743
2744static void rdev_free(struct kobject *ko)
2745{
2746 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2747 kfree(rdev);
2748}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002749static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002750 .show = rdev_attr_show,
2751 .store = rdev_attr_store,
2752};
2753static struct kobj_type rdev_ktype = {
2754 .release = rdev_free,
2755 .sysfs_ops = &rdev_sysfs_ops,
2756 .default_attrs = rdev_default_attrs,
2757};
2758
NeilBrowne8bb9a82010-06-01 19:37:26 +10002759void md_rdev_init(mdk_rdev_t *rdev)
2760{
2761 rdev->desc_nr = -1;
2762 rdev->saved_raid_disk = -1;
2763 rdev->raid_disk = -1;
2764 rdev->flags = 0;
2765 rdev->data_offset = 0;
2766 rdev->sb_events = 0;
2767 rdev->last_read_error.tv_sec = 0;
2768 rdev->last_read_error.tv_nsec = 0;
2769 atomic_set(&rdev->nr_pending, 0);
2770 atomic_set(&rdev->read_errors, 0);
2771 atomic_set(&rdev->corrected_errors, 0);
2772
2773 INIT_LIST_HEAD(&rdev->same_set);
2774 init_waitqueue_head(&rdev->blocked_wait);
2775}
2776EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777/*
2778 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2779 *
2780 * mark the device faulty if:
2781 *
2782 * - the device is nonexistent (zero size)
2783 * - the device has no valid superblock
2784 *
2785 * a faulty rdev _never_ has rdev->sb set.
2786 */
2787static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2788{
2789 char b[BDEVNAME_SIZE];
2790 int err;
2791 mdk_rdev_t *rdev;
2792 sector_t size;
2793
NeilBrown9ffae0c2006-01-06 00:20:32 -08002794 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (!rdev) {
2796 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2797 return ERR_PTR(-ENOMEM);
2798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
NeilBrowne8bb9a82010-06-01 19:37:26 +10002800 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if ((err = alloc_disk_sb(rdev)))
2802 goto abort_free;
2803
NeilBrownc5d79ad2008-02-06 01:39:54 -08002804 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (err)
2806 goto abort_free;
2807
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002808 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002809
Mike Snitzer77304d22010-11-08 14:39:12 +01002810 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (!size) {
2812 printk(KERN_WARNING
2813 "md: %s has zero or unknown size, marking faulty!\n",
2814 bdevname(rdev->bdev,b));
2815 err = -EINVAL;
2816 goto abort_free;
2817 }
2818
2819 if (super_format >= 0) {
2820 err = super_types[super_format].
2821 load_super(rdev, NULL, super_minor);
2822 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002823 printk(KERN_WARNING
2824 "md: %s does not have a valid v%d.%d "
2825 "superblock, not importing!\n",
2826 bdevname(rdev->bdev,b),
2827 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 goto abort_free;
2829 }
2830 if (err < 0) {
2831 printk(KERN_WARNING
2832 "md: could not read %s's sb, not importing!\n",
2833 bdevname(rdev->bdev,b));
2834 goto abort_free;
2835 }
2836 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return rdev;
2839
2840abort_free:
2841 if (rdev->sb_page) {
2842 if (rdev->bdev)
2843 unlock_rdev(rdev);
2844 free_disk_sb(rdev);
2845 }
2846 kfree(rdev);
2847 return ERR_PTR(err);
2848}
2849
2850/*
2851 * Check a full RAID array for plausibility
2852 */
2853
2854
NeilBrowna757e642005-04-16 15:26:42 -07002855static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
2857 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002858 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 char b[BDEVNAME_SIZE];
2860
2861 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002862 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 switch (super_types[mddev->major_version].
2864 load_super(rdev, freshest, mddev->minor_version)) {
2865 case 1:
2866 freshest = rdev;
2867 break;
2868 case 0:
2869 break;
2870 default:
2871 printk( KERN_ERR \
2872 "md: fatal superblock inconsistency in %s"
2873 " -- removing from array\n",
2874 bdevname(rdev->bdev,b));
2875 kick_rdev_from_array(rdev);
2876 }
2877
2878
2879 super_types[mddev->major_version].
2880 validate_super(mddev, freshest);
2881
2882 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002883 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002884 if (mddev->max_disks &&
2885 (rdev->desc_nr >= mddev->max_disks ||
2886 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002887 printk(KERN_WARNING
2888 "md: %s: %s: only %d devices permitted\n",
2889 mdname(mddev), bdevname(rdev->bdev, b),
2890 mddev->max_disks);
2891 kick_rdev_from_array(rdev);
2892 continue;
2893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (rdev != freshest)
2895 if (super_types[mddev->major_version].
2896 validate_super(mddev, rdev)) {
2897 printk(KERN_WARNING "md: kicking non-fresh %s"
2898 " from array!\n",
2899 bdevname(rdev->bdev,b));
2900 kick_rdev_from_array(rdev);
2901 continue;
2902 }
2903 if (mddev->level == LEVEL_MULTIPATH) {
2904 rdev->desc_nr = i++;
2905 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002906 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002907 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002908 rdev->raid_disk = -1;
2909 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
NeilBrown72e02072009-12-14 12:49:55 +11002914/* Read a fixed-point number.
2915 * Numbers in sysfs attributes should be in "standard" units where
2916 * possible, so time should be in seconds.
2917 * However we internally use a a much smaller unit such as
2918 * milliseconds or jiffies.
2919 * This function takes a decimal number with a possible fractional
2920 * component, and produces an integer which is the result of
2921 * multiplying that number by 10^'scale'.
2922 * all without any floating-point arithmetic.
2923 */
2924int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2925{
2926 unsigned long result = 0;
2927 long decimals = -1;
2928 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2929 if (*cp == '.')
2930 decimals = 0;
2931 else if (decimals < scale) {
2932 unsigned int value;
2933 value = *cp - '0';
2934 result = result * 10 + value;
2935 if (decimals >= 0)
2936 decimals++;
2937 }
2938 cp++;
2939 }
2940 if (*cp == '\n')
2941 cp++;
2942 if (*cp)
2943 return -EINVAL;
2944 if (decimals < 0)
2945 decimals = 0;
2946 while (decimals < scale) {
2947 result *= 10;
2948 decimals ++;
2949 }
2950 *res = result;
2951 return 0;
2952}
2953
2954
NeilBrown19052c02008-08-05 15:54:13 +10002955static void md_safemode_timeout(unsigned long data);
2956
NeilBrowneae17012005-11-08 21:39:23 -08002957static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002958safe_delay_show(mddev_t *mddev, char *page)
2959{
2960 int msec = (mddev->safemode_delay*1000)/HZ;
2961 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2962}
2963static ssize_t
2964safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2965{
NeilBrown16f17b32006-06-26 00:27:37 -07002966 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002967
NeilBrown72e02072009-12-14 12:49:55 +11002968 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002969 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002970 if (msec == 0)
2971 mddev->safemode_delay = 0;
2972 else {
NeilBrown19052c02008-08-05 15:54:13 +10002973 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002974 mddev->safemode_delay = (msec*HZ)/1000;
2975 if (mddev->safemode_delay == 0)
2976 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002977 if (mddev->safemode_delay < old_delay)
2978 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002979 }
2980 return len;
2981}
2982static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002983__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002984
2985static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002986level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002987{
NeilBrown2604b702006-01-06 00:20:36 -08002988 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002989 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002990 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002991 else if (mddev->clevel[0])
2992 return sprintf(page, "%s\n", mddev->clevel);
2993 else if (mddev->level != LEVEL_NONE)
2994 return sprintf(page, "%d\n", mddev->level);
2995 else
2996 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002997}
2998
NeilBrownd9d166c2006-01-06 00:20:51 -08002999static ssize_t
3000level_store(mddev_t *mddev, const char *buf, size_t len)
3001{
Dan Williamsf2859af2010-05-02 10:04:16 -07003002 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003003 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003004 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003005 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003006 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003007 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003008
3009 if (mddev->pers == NULL) {
3010 if (len == 0)
3011 return 0;
3012 if (len >= sizeof(mddev->clevel))
3013 return -ENOSPC;
3014 strncpy(mddev->clevel, buf, len);
3015 if (mddev->clevel[len-1] == '\n')
3016 len--;
3017 mddev->clevel[len] = 0;
3018 mddev->level = LEVEL_NONE;
3019 return rv;
3020 }
3021
3022 /* request to change the personality. Need to ensure:
3023 * - array is not engaged in resync/recovery/reshape
3024 * - old personality can be suspended
3025 * - new personality will access other array.
3026 */
3027
NeilBrownbb4f1e92010-08-08 21:18:03 +10003028 if (mddev->sync_thread ||
3029 mddev->reshape_position != MaxSector ||
3030 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003031 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003032
3033 if (!mddev->pers->quiesce) {
3034 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3035 mdname(mddev), mddev->pers->name);
3036 return -EINVAL;
3037 }
3038
3039 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003040 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003041 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003042 strncpy(clevel, buf, len);
3043 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003044 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003045 clevel[len] = 0;
3046 if (strict_strtol(clevel, 10, &level))
3047 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003048
Dan Williamsf2859af2010-05-02 10:04:16 -07003049 if (request_module("md-%s", clevel) != 0)
3050 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003051 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003052 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003053 if (!pers || !try_module_get(pers->owner)) {
3054 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003055 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003056 return -EINVAL;
3057 }
3058 spin_unlock(&pers_lock);
3059
3060 if (pers == mddev->pers) {
3061 /* Nothing to do! */
3062 module_put(pers->owner);
3063 return rv;
3064 }
3065 if (!pers->takeover) {
3066 module_put(pers->owner);
3067 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003068 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003069 return -EINVAL;
3070 }
3071
NeilBrowne93f68a2010-06-15 09:36:03 +01003072 list_for_each_entry(rdev, &mddev->disks, same_set)
3073 rdev->new_raid_disk = rdev->raid_disk;
3074
NeilBrown245f46c2009-03-31 14:39:39 +11003075 /* ->takeover must set new_* and/or delta_disks
3076 * if it succeeds, and may set them when it fails.
3077 */
3078 priv = pers->takeover(mddev);
3079 if (IS_ERR(priv)) {
3080 mddev->new_level = mddev->level;
3081 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003082 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003083 mddev->raid_disks -= mddev->delta_disks;
3084 mddev->delta_disks = 0;
3085 module_put(pers->owner);
3086 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003087 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003088 return PTR_ERR(priv);
3089 }
3090
3091 /* Looks like we have a winner */
3092 mddev_suspend(mddev);
3093 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003094
3095 if (mddev->pers->sync_request == NULL &&
3096 pers->sync_request != NULL) {
3097 /* need to add the md_redundancy_group */
3098 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3099 printk(KERN_WARNING
3100 "md: cannot register extra attributes for %s\n",
3101 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003102 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003103 }
3104 if (mddev->pers->sync_request != NULL &&
3105 pers->sync_request == NULL) {
3106 /* need to remove the md_redundancy_group */
3107 if (mddev->to_remove == NULL)
3108 mddev->to_remove = &md_redundancy_group;
3109 }
3110
Trela Maciej54071b32010-03-08 16:02:42 +11003111 if (mddev->pers->sync_request == NULL &&
3112 mddev->external) {
3113 /* We are converting from a no-redundancy array
3114 * to a redundancy array and metadata is managed
3115 * externally so we need to be sure that writes
3116 * won't block due to a need to transition
3117 * clean->dirty
3118 * until external management is started.
3119 */
3120 mddev->in_sync = 0;
3121 mddev->safemode_delay = 0;
3122 mddev->safemode = 0;
3123 }
3124
NeilBrowne93f68a2010-06-15 09:36:03 +01003125 list_for_each_entry(rdev, &mddev->disks, same_set) {
3126 char nm[20];
3127 if (rdev->raid_disk < 0)
3128 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003129 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003130 rdev->new_raid_disk = -1;
3131 if (rdev->new_raid_disk == rdev->raid_disk)
3132 continue;
3133 sprintf(nm, "rd%d", rdev->raid_disk);
3134 sysfs_remove_link(&mddev->kobj, nm);
3135 }
3136 list_for_each_entry(rdev, &mddev->disks, same_set) {
3137 if (rdev->raid_disk < 0)
3138 continue;
3139 if (rdev->new_raid_disk == rdev->raid_disk)
3140 continue;
3141 rdev->raid_disk = rdev->new_raid_disk;
3142 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003143 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003144 else {
3145 char nm[20];
3146 sprintf(nm, "rd%d", rdev->raid_disk);
3147 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3148 printk("md: cannot register %s for %s after level change\n",
3149 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003150 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003151 }
3152
3153 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003154 mddev->pers = pers;
3155 mddev->private = priv;
3156 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3157 mddev->level = mddev->new_level;
3158 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003159 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003160 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003161 if (mddev->pers->sync_request == NULL) {
3162 /* this is now an array without redundancy, so
3163 * it must always be in_sync
3164 */
3165 mddev->in_sync = 1;
3166 del_timer_sync(&mddev->safemode_timer);
3167 }
NeilBrown245f46c2009-03-31 14:39:39 +11003168 pers->run(mddev);
3169 mddev_resume(mddev);
3170 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3171 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3172 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003173 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003174 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003175 return rv;
3176}
3177
3178static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003179__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003180
NeilBrownd4dbd022006-06-26 00:27:59 -07003181
3182static ssize_t
3183layout_show(mddev_t *mddev, char *page)
3184{
3185 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003186 if (mddev->reshape_position != MaxSector &&
3187 mddev->layout != mddev->new_layout)
3188 return sprintf(page, "%d (%d)\n",
3189 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003190 return sprintf(page, "%d\n", mddev->layout);
3191}
3192
3193static ssize_t
3194layout_store(mddev_t *mddev, const char *buf, size_t len)
3195{
3196 char *e;
3197 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003198
3199 if (!*buf || (*e && *e != '\n'))
3200 return -EINVAL;
3201
NeilBrownb3546032009-03-31 14:56:41 +11003202 if (mddev->pers) {
3203 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003204 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003205 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003206 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003207 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003208 if (err) {
3209 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003210 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003211 }
NeilBrownb3546032009-03-31 14:56:41 +11003212 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003213 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003214 if (mddev->reshape_position == MaxSector)
3215 mddev->layout = n;
3216 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003217 return len;
3218}
3219static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003220__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003221
3222
NeilBrowneae17012005-11-08 21:39:23 -08003223static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003224raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003225{
NeilBrownbb636542005-11-08 21:39:45 -08003226 if (mddev->raid_disks == 0)
3227 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003228 if (mddev->reshape_position != MaxSector &&
3229 mddev->delta_disks != 0)
3230 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3231 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003232 return sprintf(page, "%d\n", mddev->raid_disks);
3233}
3234
NeilBrownda943b992006-01-06 00:20:54 -08003235static int update_raid_disks(mddev_t *mddev, int raid_disks);
3236
3237static ssize_t
3238raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3239{
NeilBrownda943b992006-01-06 00:20:54 -08003240 char *e;
3241 int rv = 0;
3242 unsigned long n = simple_strtoul(buf, &e, 10);
3243
3244 if (!*buf || (*e && *e != '\n'))
3245 return -EINVAL;
3246
3247 if (mddev->pers)
3248 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003249 else if (mddev->reshape_position != MaxSector) {
3250 int olddisks = mddev->raid_disks - mddev->delta_disks;
3251 mddev->delta_disks = n - olddisks;
3252 mddev->raid_disks = n;
3253 } else
NeilBrownda943b992006-01-06 00:20:54 -08003254 mddev->raid_disks = n;
3255 return rv ? rv : len;
3256}
3257static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003258__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003259
NeilBrown24dd4692005-11-08 21:39:26 -08003260static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003261chunk_size_show(mddev_t *mddev, char *page)
3262{
NeilBrown08a02ec2007-05-09 02:35:38 -07003263 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003264 mddev->chunk_sectors != mddev->new_chunk_sectors)
3265 return sprintf(page, "%d (%d)\n",
3266 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003267 mddev->chunk_sectors << 9);
3268 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003269}
3270
3271static ssize_t
3272chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3273{
NeilBrown3b343802006-01-06 00:20:47 -08003274 char *e;
3275 unsigned long n = simple_strtoul(buf, &e, 10);
3276
NeilBrown3b343802006-01-06 00:20:47 -08003277 if (!*buf || (*e && *e != '\n'))
3278 return -EINVAL;
3279
NeilBrownb3546032009-03-31 14:56:41 +11003280 if (mddev->pers) {
3281 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003282 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003283 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003284 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003285 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003286 if (err) {
3287 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003288 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003289 }
NeilBrownb3546032009-03-31 14:56:41 +11003290 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003291 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003292 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003293 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003294 }
NeilBrown3b343802006-01-06 00:20:47 -08003295 return len;
3296}
3297static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003298__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003299
NeilBrowna94213b2006-06-26 00:28:00 -07003300static ssize_t
3301resync_start_show(mddev_t *mddev, char *page)
3302{
NeilBrownd1a7c502009-03-31 15:24:32 +11003303 if (mddev->recovery_cp == MaxSector)
3304 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003305 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3306}
3307
3308static ssize_t
3309resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3310{
NeilBrowna94213b2006-06-26 00:28:00 -07003311 char *e;
3312 unsigned long long n = simple_strtoull(buf, &e, 10);
3313
3314 if (mddev->pers)
3315 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003316 if (cmd_match(buf, "none"))
3317 n = MaxSector;
3318 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003319 return -EINVAL;
3320
3321 mddev->recovery_cp = n;
3322 return len;
3323}
3324static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003325__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003326
NeilBrown9e653b62006-06-26 00:27:58 -07003327/*
3328 * The array state can be:
3329 *
3330 * clear
3331 * No devices, no size, no level
3332 * Equivalent to STOP_ARRAY ioctl
3333 * inactive
3334 * May have some settings, but array is not active
3335 * all IO results in error
3336 * When written, doesn't tear down array, but just stops it
3337 * suspended (not supported yet)
3338 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003339 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003340 * readonly
3341 * no resync can happen. no superblocks get written.
3342 * write requests fail
3343 * read-auto
3344 * like readonly, but behaves like 'clean' on a write request.
3345 *
3346 * clean - no pending writes, but otherwise active.
3347 * When written to inactive array, starts without resync
3348 * If a write request arrives then
3349 * if metadata is known, mark 'dirty' and switch to 'active'.
3350 * if not known, block and switch to write-pending
3351 * If written to an active array that has pending writes, then fails.
3352 * active
3353 * fully active: IO and resync can be happening.
3354 * When written to inactive array, starts with resync
3355 *
3356 * write-pending
3357 * clean, but writes are blocked waiting for 'active' to be written.
3358 *
3359 * active-idle
3360 * like active, but no writes have been seen for a while (100msec).
3361 *
3362 */
3363enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3364 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003365static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003366 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3367 "write-pending", "active-idle", NULL };
3368
3369static int match_word(const char *word, char **list)
3370{
3371 int n;
3372 for (n=0; list[n]; n++)
3373 if (cmd_match(word, list[n]))
3374 break;
3375 return n;
3376}
3377
3378static ssize_t
3379array_state_show(mddev_t *mddev, char *page)
3380{
3381 enum array_state st = inactive;
3382
3383 if (mddev->pers)
3384 switch(mddev->ro) {
3385 case 1:
3386 st = readonly;
3387 break;
3388 case 2:
3389 st = read_auto;
3390 break;
3391 case 0:
3392 if (mddev->in_sync)
3393 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003394 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003395 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003396 else if (mddev->safemode)
3397 st = active_idle;
3398 else
3399 st = active;
3400 }
3401 else {
3402 if (list_empty(&mddev->disks) &&
3403 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003404 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003405 st = clear;
3406 else
3407 st = inactive;
3408 }
3409 return sprintf(page, "%s\n", array_states[st]);
3410}
3411
Neil Browndf5b20c2008-07-11 22:02:22 +10003412static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003413static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003414static int do_md_run(mddev_t * mddev);
3415static int restart_array(mddev_t *mddev);
3416
3417static ssize_t
3418array_state_store(mddev_t *mddev, const char *buf, size_t len)
3419{
3420 int err = -EINVAL;
3421 enum array_state st = match_word(buf, array_states);
3422 switch(st) {
3423 case bad_word:
3424 break;
3425 case clear:
3426 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003427 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003428 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003429 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003430 break;
3431 case inactive:
3432 /* stopping an active array */
3433 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003434 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003435 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003436 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003437 } else
3438 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003439 break;
3440 case suspended:
3441 break; /* not supported yet */
3442 case readonly:
3443 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003444 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003445 else {
3446 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003447 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003448 err = do_md_run(mddev);
3449 }
3450 break;
3451 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003452 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003453 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003454 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003455 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003456 err = restart_array(mddev);
3457 if (err == 0) {
3458 mddev->ro = 2;
3459 set_disk_ro(mddev->gendisk, 0);
3460 }
NeilBrown9e653b62006-06-26 00:27:58 -07003461 } else {
3462 mddev->ro = 2;
3463 err = do_md_run(mddev);
3464 }
3465 break;
3466 case clean:
3467 if (mddev->pers) {
3468 restart_array(mddev);
3469 spin_lock_irq(&mddev->write_lock);
3470 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003471 if (mddev->in_sync == 0) {
3472 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003473 if (mddev->safemode == 1)
3474 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003475 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003476 }
3477 err = 0;
3478 } else
3479 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003480 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003481 } else
3482 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003483 break;
3484 case active:
3485 if (mddev->pers) {
3486 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003487 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003488 wake_up(&mddev->sb_wait);
3489 err = 0;
3490 } else {
3491 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003492 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003493 err = do_md_run(mddev);
3494 }
3495 break;
3496 case write_pending:
3497 case active_idle:
3498 /* these cannot be set */
3499 break;
3500 }
3501 if (err)
3502 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003503 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003504 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003505 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003506 }
NeilBrown9e653b62006-06-26 00:27:58 -07003507}
NeilBrown80ca3a42006-07-10 04:44:18 -07003508static struct md_sysfs_entry md_array_state =
3509__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003510
NeilBrown6d7ff732006-01-06 00:21:16 -08003511static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003512max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3513 return sprintf(page, "%d\n",
3514 atomic_read(&mddev->max_corr_read_errors));
3515}
3516
3517static ssize_t
3518max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3519{
3520 char *e;
3521 unsigned long n = simple_strtoul(buf, &e, 10);
3522
3523 if (*buf && (*e == 0 || *e == '\n')) {
3524 atomic_set(&mddev->max_corr_read_errors, n);
3525 return len;
3526 }
3527 return -EINVAL;
3528}
3529
3530static struct md_sysfs_entry max_corr_read_errors =
3531__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3532 max_corrected_read_errors_store);
3533
3534static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003535null_show(mddev_t *mddev, char *page)
3536{
3537 return -EINVAL;
3538}
3539
3540static ssize_t
3541new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3542{
3543 /* buf must be %d:%d\n? giving major and minor numbers */
3544 /* The new device is added to the array.
3545 * If the array has a persistent superblock, we read the
3546 * superblock to initialise info and check validity.
3547 * Otherwise, only checking done is that in bind_rdev_to_array,
3548 * which mainly checks size.
3549 */
3550 char *e;
3551 int major = simple_strtoul(buf, &e, 10);
3552 int minor;
3553 dev_t dev;
3554 mdk_rdev_t *rdev;
3555 int err;
3556
3557 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3558 return -EINVAL;
3559 minor = simple_strtoul(e+1, &e, 10);
3560 if (*e && *e != '\n')
3561 return -EINVAL;
3562 dev = MKDEV(major, minor);
3563 if (major != MAJOR(dev) ||
3564 minor != MINOR(dev))
3565 return -EOVERFLOW;
3566
3567
3568 if (mddev->persistent) {
3569 rdev = md_import_device(dev, mddev->major_version,
3570 mddev->minor_version);
3571 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3572 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3573 mdk_rdev_t, same_set);
3574 err = super_types[mddev->major_version]
3575 .load_super(rdev, rdev0, mddev->minor_version);
3576 if (err < 0)
3577 goto out;
3578 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003579 } else if (mddev->external)
3580 rdev = md_import_device(dev, -2, -1);
3581 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003582 rdev = md_import_device(dev, -1, -1);
3583
3584 if (IS_ERR(rdev))
3585 return PTR_ERR(rdev);
3586 err = bind_rdev_to_array(rdev, mddev);
3587 out:
3588 if (err)
3589 export_rdev(rdev);
3590 return err ? err : len;
3591}
3592
3593static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003594__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003595
3596static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003597bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3598{
3599 char *end;
3600 unsigned long chunk, end_chunk;
3601
3602 if (!mddev->bitmap)
3603 goto out;
3604 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3605 while (*buf) {
3606 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3607 if (buf == end) break;
3608 if (*end == '-') { /* range */
3609 buf = end + 1;
3610 end_chunk = simple_strtoul(buf, &end, 0);
3611 if (buf == end) break;
3612 }
3613 if (*end && !isspace(*end)) break;
3614 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003615 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003616 }
3617 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3618out:
3619 return len;
3620}
3621
3622static struct md_sysfs_entry md_bitmap =
3623__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3624
3625static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003626size_show(mddev_t *mddev, char *page)
3627{
Andre Noll58c0fed2009-03-31 14:33:13 +11003628 return sprintf(page, "%llu\n",
3629 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003630}
3631
Andre Nolld71f9f82008-07-11 22:02:22 +10003632static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003633
3634static ssize_t
3635size_store(mddev_t *mddev, const char *buf, size_t len)
3636{
3637 /* If array is inactive, we can reduce the component size, but
3638 * not increase it (except from 0).
3639 * If array is active, we can try an on-line resize
3640 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003641 sector_t sectors;
3642 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003643
Andre Noll58c0fed2009-03-31 14:33:13 +11003644 if (err < 0)
3645 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003646 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003647 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003648 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003649 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003650 if (mddev->dev_sectors == 0 ||
3651 mddev->dev_sectors > sectors)
3652 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003653 else
3654 err = -ENOSPC;
3655 }
3656 return err ? err : len;
3657}
3658
3659static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003660__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003661
NeilBrown8bb93aa2006-01-06 00:20:50 -08003662
3663/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003664 * This is one of
3665 * 'none' for arrays with no metadata (good luck...)
3666 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003667 * or N.M for internally known formats
3668 */
3669static ssize_t
3670metadata_show(mddev_t *mddev, char *page)
3671{
3672 if (mddev->persistent)
3673 return sprintf(page, "%d.%d\n",
3674 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003675 else if (mddev->external)
3676 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003677 else
3678 return sprintf(page, "none\n");
3679}
3680
3681static ssize_t
3682metadata_store(mddev_t *mddev, const char *buf, size_t len)
3683{
3684 int major, minor;
3685 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003686 /* Changing the details of 'external' metadata is
3687 * always permitted. Otherwise there must be
3688 * no devices attached to the array.
3689 */
3690 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3691 ;
3692 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003693 return -EBUSY;
3694
3695 if (cmd_match(buf, "none")) {
3696 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003697 mddev->external = 0;
3698 mddev->major_version = 0;
3699 mddev->minor_version = 90;
3700 return len;
3701 }
3702 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003703 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003704 if (namelen >= sizeof(mddev->metadata_type))
3705 namelen = sizeof(mddev->metadata_type)-1;
3706 strncpy(mddev->metadata_type, buf+9, namelen);
3707 mddev->metadata_type[namelen] = 0;
3708 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3709 mddev->metadata_type[--namelen] = 0;
3710 mddev->persistent = 0;
3711 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003712 mddev->major_version = 0;
3713 mddev->minor_version = 90;
3714 return len;
3715 }
3716 major = simple_strtoul(buf, &e, 10);
3717 if (e==buf || *e != '.')
3718 return -EINVAL;
3719 buf = e+1;
3720 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003721 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003722 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003723 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003724 return -ENOENT;
3725 mddev->major_version = major;
3726 mddev->minor_version = minor;
3727 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003728 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003729 return len;
3730}
3731
3732static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003733__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003734
NeilBrowna35b0d62006-01-06 00:20:49 -08003735static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003736action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003737{
NeilBrown7eec3142005-11-08 21:39:44 -08003738 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003739 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3740 type = "frozen";
3741 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003742 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003743 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3744 type = "reshape";
3745 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003746 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3747 type = "resync";
3748 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3749 type = "check";
3750 else
3751 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003752 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003753 type = "recover";
3754 }
3755 return sprintf(page, "%s\n", type);
3756}
3757
NeilBrown7ebc0be2011-01-14 09:14:33 +11003758static void reap_sync_thread(mddev_t *mddev);
3759
NeilBrown24dd4692005-11-08 21:39:26 -08003760static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003761action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003762{
NeilBrown7eec3142005-11-08 21:39:44 -08003763 if (!mddev->pers || !mddev->pers->sync_request)
3764 return -EINVAL;
3765
NeilBrownb6a9ce62009-05-26 09:41:17 +10003766 if (cmd_match(page, "frozen"))
3767 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3768 else
3769 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3770
3771 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003772 if (mddev->sync_thread) {
3773 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003774 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003775 }
NeilBrown03c902e2006-01-06 00:20:46 -08003776 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3777 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003778 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003779 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003780 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003781 else if (cmd_match(page, "recover")) {
3782 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3783 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3784 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003785 int err;
3786 if (mddev->pers->start_reshape == NULL)
3787 return -EINVAL;
3788 err = mddev->pers->start_reshape(mddev);
3789 if (err)
3790 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003791 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003792 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003793 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003794 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003795 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003796 return -EINVAL;
3797 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3798 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003799 }
NeilBrown03c902e2006-01-06 00:20:46 -08003800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003801 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003802 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003803 return len;
3804}
3805
NeilBrown9d888832005-11-08 21:39:26 -08003806static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003807mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003808{
3809 return sprintf(page, "%llu\n",
3810 (unsigned long long) mddev->resync_mismatches);
3811}
3812
NeilBrown80ca3a42006-07-10 04:44:18 -07003813static struct md_sysfs_entry md_scan_mode =
3814__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003815
NeilBrown96de1e62005-11-08 21:39:39 -08003816
NeilBrown80ca3a42006-07-10 04:44:18 -07003817static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003818
NeilBrown88202a02006-01-06 00:21:36 -08003819static ssize_t
3820sync_min_show(mddev_t *mddev, char *page)
3821{
3822 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3823 mddev->sync_speed_min ? "local": "system");
3824}
3825
3826static ssize_t
3827sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3828{
3829 int min;
3830 char *e;
3831 if (strncmp(buf, "system", 6)==0) {
3832 mddev->sync_speed_min = 0;
3833 return len;
3834 }
3835 min = simple_strtoul(buf, &e, 10);
3836 if (buf == e || (*e && *e != '\n') || min <= 0)
3837 return -EINVAL;
3838 mddev->sync_speed_min = min;
3839 return len;
3840}
3841
3842static struct md_sysfs_entry md_sync_min =
3843__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3844
3845static ssize_t
3846sync_max_show(mddev_t *mddev, char *page)
3847{
3848 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3849 mddev->sync_speed_max ? "local": "system");
3850}
3851
3852static ssize_t
3853sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3854{
3855 int max;
3856 char *e;
3857 if (strncmp(buf, "system", 6)==0) {
3858 mddev->sync_speed_max = 0;
3859 return len;
3860 }
3861 max = simple_strtoul(buf, &e, 10);
3862 if (buf == e || (*e && *e != '\n') || max <= 0)
3863 return -EINVAL;
3864 mddev->sync_speed_max = max;
3865 return len;
3866}
3867
3868static struct md_sysfs_entry md_sync_max =
3869__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3870
Iustin Popd7f3d292007-10-16 23:30:54 -07003871static ssize_t
3872degraded_show(mddev_t *mddev, char *page)
3873{
3874 return sprintf(page, "%d\n", mddev->degraded);
3875}
3876static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003877
3878static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003879sync_force_parallel_show(mddev_t *mddev, char *page)
3880{
3881 return sprintf(page, "%d\n", mddev->parallel_resync);
3882}
3883
3884static ssize_t
3885sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3886{
3887 long n;
3888
3889 if (strict_strtol(buf, 10, &n))
3890 return -EINVAL;
3891
3892 if (n != 0 && n != 1)
3893 return -EINVAL;
3894
3895 mddev->parallel_resync = n;
3896
3897 if (mddev->sync_thread)
3898 wake_up(&resync_wait);
3899
3900 return len;
3901}
3902
3903/* force parallel resync, even with shared block devices */
3904static struct md_sysfs_entry md_sync_force_parallel =
3905__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3906 sync_force_parallel_show, sync_force_parallel_store);
3907
3908static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003909sync_speed_show(mddev_t *mddev, char *page)
3910{
3911 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003912 if (mddev->curr_resync == 0)
3913 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003914 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3915 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003916 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003917 db = resync - mddev->resync_mark_cnt;
3918 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003919}
3920
NeilBrown80ca3a42006-07-10 04:44:18 -07003921static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003922
3923static ssize_t
3924sync_completed_show(mddev_t *mddev, char *page)
3925{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003926 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003927
NeilBrownacb180b2009-04-14 16:28:34 +10003928 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3929 return sprintf(page, "none\n");
3930
NeilBrown88202a02006-01-06 00:21:36 -08003931 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003932 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003933 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003934 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003935
NeilBrownacb180b2009-04-14 16:28:34 +10003936 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003937 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003938}
3939
NeilBrown80ca3a42006-07-10 04:44:18 -07003940static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003941
NeilBrowne464eaf2006-03-27 01:18:14 -08003942static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003943min_sync_show(mddev_t *mddev, char *page)
3944{
3945 return sprintf(page, "%llu\n",
3946 (unsigned long long)mddev->resync_min);
3947}
3948static ssize_t
3949min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3950{
3951 unsigned long long min;
3952 if (strict_strtoull(buf, 10, &min))
3953 return -EINVAL;
3954 if (min > mddev->resync_max)
3955 return -EINVAL;
3956 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3957 return -EBUSY;
3958
3959 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003960 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003961 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003962 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003963 return -EINVAL;
3964 }
3965 mddev->resync_min = min;
3966
3967 return len;
3968}
3969
3970static struct md_sysfs_entry md_min_sync =
3971__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3972
3973static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003974max_sync_show(mddev_t *mddev, char *page)
3975{
3976 if (mddev->resync_max == MaxSector)
3977 return sprintf(page, "max\n");
3978 else
3979 return sprintf(page, "%llu\n",
3980 (unsigned long long)mddev->resync_max);
3981}
3982static ssize_t
3983max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3984{
3985 if (strncmp(buf, "max", 3) == 0)
3986 mddev->resync_max = MaxSector;
3987 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003988 unsigned long long max;
3989 if (strict_strtoull(buf, 10, &max))
3990 return -EINVAL;
3991 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003992 return -EINVAL;
3993 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003994 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003995 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3996 return -EBUSY;
3997
3998 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003999 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004000 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004001 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004002 return -EINVAL;
4003 }
4004 mddev->resync_max = max;
4005 }
4006 wake_up(&mddev->recovery_wait);
4007 return len;
4008}
4009
4010static struct md_sysfs_entry md_max_sync =
4011__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4012
4013static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004014suspend_lo_show(mddev_t *mddev, char *page)
4015{
4016 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4017}
4018
4019static ssize_t
4020suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4021{
4022 char *e;
4023 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004024 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004025
NeilBrownb8d966e2009-07-01 11:14:04 +10004026 if (mddev->pers == NULL ||
4027 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004028 return -EINVAL;
4029 if (buf == e || (*e && *e != '\n'))
4030 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004031
4032 mddev->suspend_lo = new;
4033 if (new >= old)
4034 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004035 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004036 else {
4037 /* Expanding suspended region - need to wait */
4038 mddev->pers->quiesce(mddev, 1);
4039 mddev->pers->quiesce(mddev, 0);
4040 }
4041 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004042}
4043static struct md_sysfs_entry md_suspend_lo =
4044__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4045
4046
4047static ssize_t
4048suspend_hi_show(mddev_t *mddev, char *page)
4049{
4050 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4051}
4052
4053static ssize_t
4054suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4055{
4056 char *e;
4057 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004058 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004059
NeilBrownb8d966e2009-07-01 11:14:04 +10004060 if (mddev->pers == NULL ||
4061 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004062 return -EINVAL;
4063 if (buf == e || (*e && *e != '\n'))
4064 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004065
4066 mddev->suspend_hi = new;
4067 if (new <= old)
4068 /* Shrinking suspended region */
4069 mddev->pers->quiesce(mddev, 2);
4070 else {
4071 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004072 mddev->pers->quiesce(mddev, 1);
4073 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004074 }
4075 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004076}
4077static struct md_sysfs_entry md_suspend_hi =
4078__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4079
NeilBrown08a02ec2007-05-09 02:35:38 -07004080static ssize_t
4081reshape_position_show(mddev_t *mddev, char *page)
4082{
4083 if (mddev->reshape_position != MaxSector)
4084 return sprintf(page, "%llu\n",
4085 (unsigned long long)mddev->reshape_position);
4086 strcpy(page, "none\n");
4087 return 5;
4088}
4089
4090static ssize_t
4091reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4092{
4093 char *e;
4094 unsigned long long new = simple_strtoull(buf, &e, 10);
4095 if (mddev->pers)
4096 return -EBUSY;
4097 if (buf == e || (*e && *e != '\n'))
4098 return -EINVAL;
4099 mddev->reshape_position = new;
4100 mddev->delta_disks = 0;
4101 mddev->new_level = mddev->level;
4102 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004103 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004104 return len;
4105}
4106
4107static struct md_sysfs_entry md_reshape_position =
4108__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4109 reshape_position_store);
4110
Dan Williamsb522adc2009-03-31 15:00:31 +11004111static ssize_t
4112array_size_show(mddev_t *mddev, char *page)
4113{
4114 if (mddev->external_size)
4115 return sprintf(page, "%llu\n",
4116 (unsigned long long)mddev->array_sectors/2);
4117 else
4118 return sprintf(page, "default\n");
4119}
4120
4121static ssize_t
4122array_size_store(mddev_t *mddev, const char *buf, size_t len)
4123{
4124 sector_t sectors;
4125
4126 if (strncmp(buf, "default", 7) == 0) {
4127 if (mddev->pers)
4128 sectors = mddev->pers->size(mddev, 0, 0);
4129 else
4130 sectors = mddev->array_sectors;
4131
4132 mddev->external_size = 0;
4133 } else {
4134 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4135 return -EINVAL;
4136 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004137 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004138
4139 mddev->external_size = 1;
4140 }
4141
4142 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004143 if (mddev->pers) {
4144 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004145 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004146 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004147 return len;
4148}
4149
4150static struct md_sysfs_entry md_array_size =
4151__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4152 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004153
NeilBrowneae17012005-11-08 21:39:23 -08004154static struct attribute *md_default_attrs[] = {
4155 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004156 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004157 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004158 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004159 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004160 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004161 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004162 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004163 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004164 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004165 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004166 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004167 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004168 NULL,
4169};
4170
4171static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004172 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004173 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004174 &md_sync_min.attr,
4175 &md_sync_max.attr,
4176 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004177 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004178 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004179 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004180 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004181 &md_suspend_lo.attr,
4182 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004183 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004184 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004185 NULL,
4186};
NeilBrown411036f2005-11-08 21:39:40 -08004187static struct attribute_group md_redundancy_group = {
4188 .name = NULL,
4189 .attrs = md_redundancy_attrs,
4190};
4191
NeilBrowneae17012005-11-08 21:39:23 -08004192
4193static ssize_t
4194md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4195{
4196 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4197 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004198 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004199
4200 if (!entry->show)
4201 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004202 rv = mddev_lock(mddev);
4203 if (!rv) {
4204 rv = entry->show(mddev, page);
4205 mddev_unlock(mddev);
4206 }
NeilBrown96de1e62005-11-08 21:39:39 -08004207 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004208}
4209
4210static ssize_t
4211md_attr_store(struct kobject *kobj, struct attribute *attr,
4212 const char *page, size_t length)
4213{
4214 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4215 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004216 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004217
4218 if (!entry->store)
4219 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004220 if (!capable(CAP_SYS_ADMIN))
4221 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004222 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004223 if (mddev->hold_active == UNTIL_IOCTL)
4224 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004225 if (!rv) {
4226 rv = entry->store(mddev, page, length);
4227 mddev_unlock(mddev);
4228 }
NeilBrown96de1e62005-11-08 21:39:39 -08004229 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004230}
4231
4232static void md_free(struct kobject *ko)
4233{
4234 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004235
4236 if (mddev->sysfs_state)
4237 sysfs_put(mddev->sysfs_state);
4238
4239 if (mddev->gendisk) {
4240 del_gendisk(mddev->gendisk);
4241 put_disk(mddev->gendisk);
4242 }
4243 if (mddev->queue)
4244 blk_cleanup_queue(mddev->queue);
4245
NeilBrowneae17012005-11-08 21:39:23 -08004246 kfree(mddev);
4247}
4248
Emese Revfy52cf25d2010-01-19 02:58:23 +01004249static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004250 .show = md_attr_show,
4251 .store = md_attr_store,
4252};
4253static struct kobj_type md_ktype = {
4254 .release = md_free,
4255 .sysfs_ops = &md_sysfs_ops,
4256 .default_attrs = md_default_attrs,
4257};
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259int mdp_major = 0;
4260
Dan Williams5fd3a172009-03-04 00:57:25 -07004261static void mddev_delayed_delete(struct work_struct *ws)
4262{
4263 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4264
NeilBrown43a70502009-12-14 12:49:55 +11004265 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004266 kobject_del(&mddev->kobj);
4267 kobject_put(&mddev->kobj);
4268}
4269
NeilBrownefeb53c2009-01-09 08:31:10 +11004270static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004272 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 mddev_t *mddev = mddev_find(dev);
4274 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004275 int partitioned;
4276 int shift;
4277 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004278 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
4280 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004281 return -ENODEV;
4282
4283 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4284 shift = partitioned ? MdpMinorShift : 0;
4285 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Tejun Heoe804ac72010-10-15 15:36:08 +02004287 /* wait for any previous instance of this device to be
4288 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004289 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004290 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004291
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004292 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004293 error = -EEXIST;
4294 if (mddev->gendisk)
4295 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004296
4297 if (name) {
4298 /* Need to ensure that 'name' is not a duplicate.
4299 */
4300 mddev_t *mddev2;
4301 spin_lock(&all_mddevs_lock);
4302
4303 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4304 if (mddev2->gendisk &&
4305 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4306 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004307 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004308 }
4309 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 }
NeilBrown8b765392009-01-09 08:31:08 +11004311
NeilBrown0909dc42009-07-01 12:27:21 +10004312 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004313 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004314 if (!mddev->queue)
4315 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004316 mddev->queue->queuedata = mddev;
4317
NeilBrown409c57f2009-03-31 14:39:39 +11004318 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 disk = alloc_disk(1 << shift);
4321 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004322 blk_cleanup_queue(mddev->queue);
4323 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004324 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004326 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004328 if (name)
4329 strcpy(disk->disk_name, name);
4330 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004332 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 disk->fops = &md_fops;
4335 disk->private_data = mddev;
4336 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004337 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004338 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004339 * remove it now.
4340 */
4341 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 add_disk(disk);
4343 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004344 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4345 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004346 if (error) {
4347 /* This isn't possible, but as kobject_init_and_add is marked
4348 * __must_check, we must do something with the result
4349 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004350 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4351 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004352 error = 0;
4353 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004354 if (mddev->kobj.sd &&
4355 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004356 printk(KERN_DEBUG "pointless warning\n");
Darrick J. Wongbe20e6c2010-11-24 16:40:33 +11004357
4358 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown0909dc42009-07-01 12:27:21 +10004359 abort:
4360 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004361 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004362 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004363 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004364 }
NeilBrownd3374822009-01-09 08:31:10 +11004365 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004366 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004367}
4368
4369static struct kobject *md_probe(dev_t dev, int *part, void *data)
4370{
4371 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return NULL;
4373}
4374
NeilBrownefeb53c2009-01-09 08:31:10 +11004375static int add_named_array(const char *val, struct kernel_param *kp)
4376{
4377 /* val must be "md_*" where * is not all digits.
4378 * We allocate an array with a large free minor number, and
4379 * set the name to val. val must not already be an active name.
4380 */
4381 int len = strlen(val);
4382 char buf[DISK_NAME_LEN];
4383
4384 while (len && val[len-1] == '\n')
4385 len--;
4386 if (len >= DISK_NAME_LEN)
4387 return -E2BIG;
4388 strlcpy(buf, val, len+1);
4389 if (strncmp(buf, "md_", 3) != 0)
4390 return -EINVAL;
4391 return md_alloc(0, buf);
4392}
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394static void md_safemode_timeout(unsigned long data)
4395{
4396 mddev_t *mddev = (mddev_t *) data;
4397
Neil Brown0fd62b82008-06-28 08:31:36 +10004398 if (!atomic_read(&mddev->writes_pending)) {
4399 mddev->safemode = 1;
4400 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004401 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 md_wakeup_thread(mddev->thread);
4404}
4405
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004406static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
NeilBrown390ee602010-06-01 19:37:27 +10004408int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
NeilBrown2604b702006-01-06 00:20:36 -08004410 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004412 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
NeilBrowna757e642005-04-16 15:26:42 -07004414 if (list_empty(&mddev->disks))
4415 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
4418 if (mddev->pers)
4419 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004420 /* Cannot run until previous stop completes properly */
4421 if (mddev->sysfs_active)
4422 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 /*
4425 * Analyze all RAID superblock(s)
4426 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004427 if (!mddev->raid_disks) {
4428 if (!mddev->persistent)
4429 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004430 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
NeilBrownd9d166c2006-01-06 00:20:51 -08004433 if (mddev->level != LEVEL_NONE)
4434 request_module("md-level-%d", mddev->level);
4435 else if (mddev->clevel[0])
4436 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
4438 /*
4439 * Drop all container device buffers, from now on
4440 * the only valid external interface is through the md
4441 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004443 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004444 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 continue;
4446 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004447 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004448
4449 /* perform some consistency tests on the device.
4450 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004451 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004452 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004453 if (rdev->meta_bdev) {
4454 /* Nothing to check */;
4455 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004456 if (mddev->dev_sectors &&
4457 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004458 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004459 printk("md: %s: data overlaps metadata\n",
4460 mdname(mddev));
4461 return -EINVAL;
4462 }
4463 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004464 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004465 > rdev->data_offset) {
4466 printk("md: %s: metadata overlaps data\n",
4467 mdname(mddev));
4468 return -EINVAL;
4469 }
4470 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004471 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 }
4473
NeilBrowna167f662010-10-26 18:31:13 +11004474 if (mddev->bio_set == NULL)
4475 mddev->bio_set = bioset_create(BIO_POOL_SIZE, sizeof(mddev));
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004478 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004479 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004481 if (mddev->level != LEVEL_NONE)
4482 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4483 mddev->level);
4484 else
4485 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4486 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 return -EINVAL;
4488 }
NeilBrown2604b702006-01-06 00:20:36 -08004489 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004491 if (mddev->level != pers->level) {
4492 mddev->level = pers->level;
4493 mddev->new_level = pers->level;
4494 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004495 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
NeilBrownf6705572006-03-27 01:18:11 -08004497 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004498 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004499 /* This personality cannot handle reshaping... */
4500 mddev->pers = NULL;
4501 module_put(pers->owner);
4502 return -EINVAL;
4503 }
4504
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004505 if (pers->sync_request) {
4506 /* Warn if this is a potentially silly
4507 * configuration.
4508 */
4509 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4510 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004511 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004512
4513 list_for_each_entry(rdev, &mddev->disks, same_set)
4514 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004515 if (rdev < rdev2 &&
4516 rdev->bdev->bd_contains ==
4517 rdev2->bdev->bd_contains) {
4518 printk(KERN_WARNING
4519 "%s: WARNING: %s appears to be"
4520 " on the same physical disk as"
4521 " %s.\n",
4522 mdname(mddev),
4523 bdevname(rdev->bdev,b),
4524 bdevname(rdev2->bdev,b2));
4525 warned = 1;
4526 }
4527 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004528
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004529 if (warned)
4530 printk(KERN_WARNING
4531 "True protection against single-disk"
4532 " failure might be compromised.\n");
4533 }
4534
NeilBrown657390d2005-08-26 18:34:16 -07004535 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004536 /* may be over-ridden by personality */
4537 mddev->resync_max_sectors = mddev->dev_sectors;
4538
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004539 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
NeilBrown0f9552b2009-12-30 12:08:50 +11004541 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004542 mddev->ro = 2; /* read-only, but switch on first write */
4543
NeilBrownb15c2e52006-01-06 00:20:16 -08004544 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004545 if (err)
4546 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004547 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4548 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4549 " but 'external_size' not in effect?\n", __func__);
4550 printk(KERN_ERR
4551 "md: invalid array_size %llu > default size %llu\n",
4552 (unsigned long long)mddev->array_sectors / 2,
4553 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4554 err = -EINVAL;
4555 mddev->pers->stop(mddev);
4556 }
4557 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004558 err = bitmap_create(mddev);
4559 if (err) {
4560 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4561 mdname(mddev), err);
4562 mddev->pers->stop(mddev);
4563 }
4564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 module_put(mddev->pers->owner);
4567 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004568 bitmap_destroy(mddev);
4569 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004571 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004572 if (mddev->kobj.sd &&
4573 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004574 printk(KERN_WARNING
4575 "md: cannot register extra attributes for %s\n",
4576 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004577 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004578 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004579 mddev->ro = 0;
4580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004582 atomic_set(&mddev->max_corr_read_errors,
4583 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 mddev->safemode = 0;
4585 mddev->safemode_timer.function = md_safemode_timeout;
4586 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004587 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004589 smp_wmb();
4590 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004591 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004592 if (rdev->raid_disk >= 0) {
4593 char nm[20];
4594 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004595 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004596 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
4599 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4600
NeilBrown850b2b42006-10-03 01:15:46 -07004601 if (mddev->flags)
4602 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
NeilBrown0b8c9de2006-07-10 04:44:15 -07004604 md_wakeup_thread(mddev->thread);
4605 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004606
NeilBrownd7603b72006-01-06 00:20:30 -08004607 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004608 sysfs_notify_dirent_safe(mddev->sysfs_state);
4609 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004610 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return 0;
4612}
NeilBrown390ee602010-06-01 19:37:27 +10004613EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
NeilBrownfe60b012010-03-29 11:10:42 +11004615static int do_md_run(mddev_t *mddev)
4616{
4617 int err;
4618
4619 err = md_run(mddev);
4620 if (err)
4621 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004622 err = bitmap_load(mddev);
4623 if (err) {
4624 bitmap_destroy(mddev);
4625 goto out;
4626 }
NeilBrownfe60b012010-03-29 11:10:42 +11004627 set_capacity(mddev->gendisk, mddev->array_sectors);
4628 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004629 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004630 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4631out:
4632 return err;
4633}
4634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635static int restart_array(mddev_t *mddev)
4636{
4637 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Andre Noll80fab1d2008-07-11 22:02:21 +10004639 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004641 return -ENXIO;
4642 if (!mddev->pers)
4643 return -EINVAL;
4644 if (!mddev->ro)
4645 return -EBUSY;
4646 mddev->safemode = 0;
4647 mddev->ro = 0;
4648 set_disk_ro(disk, 0);
4649 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4650 mdname(mddev));
4651 /* Kick recovery or resync if necessary */
4652 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4653 md_wakeup_thread(mddev->thread);
4654 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004655 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
4658
NeilBrownacc55e22006-06-26 00:27:47 -07004659/* similar to deny_write_access, but accounts for our holding a reference
4660 * to the file ourselves */
4661static int deny_bitmap_write_access(struct file * file)
4662{
4663 struct inode *inode = file->f_mapping->host;
4664
4665 spin_lock(&inode->i_lock);
4666 if (atomic_read(&inode->i_writecount) > 1) {
4667 spin_unlock(&inode->i_lock);
4668 return -ETXTBSY;
4669 }
4670 atomic_set(&inode->i_writecount, -1);
4671 spin_unlock(&inode->i_lock);
4672
4673 return 0;
4674}
4675
NeilBrown43a70502009-12-14 12:49:55 +11004676void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004677{
4678 struct inode *inode = file->f_mapping->host;
4679
4680 spin_lock(&inode->i_lock);
4681 atomic_set(&inode->i_writecount, 1);
4682 spin_unlock(&inode->i_lock);
4683}
4684
NeilBrown6177b472010-03-29 11:37:13 +11004685static void md_clean(mddev_t *mddev)
4686{
4687 mddev->array_sectors = 0;
4688 mddev->external_size = 0;
4689 mddev->dev_sectors = 0;
4690 mddev->raid_disks = 0;
4691 mddev->recovery_cp = 0;
4692 mddev->resync_min = 0;
4693 mddev->resync_max = MaxSector;
4694 mddev->reshape_position = MaxSector;
4695 mddev->external = 0;
4696 mddev->persistent = 0;
4697 mddev->level = LEVEL_NONE;
4698 mddev->clevel[0] = 0;
4699 mddev->flags = 0;
4700 mddev->ro = 0;
4701 mddev->metadata_type[0] = 0;
4702 mddev->chunk_sectors = 0;
4703 mddev->ctime = mddev->utime = 0;
4704 mddev->layout = 0;
4705 mddev->max_disks = 0;
4706 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004707 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004708 mddev->delta_disks = 0;
4709 mddev->new_level = LEVEL_NONE;
4710 mddev->new_layout = 0;
4711 mddev->new_chunk_sectors = 0;
4712 mddev->curr_resync = 0;
4713 mddev->resync_mismatches = 0;
4714 mddev->suspend_lo = mddev->suspend_hi = 0;
4715 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4716 mddev->recovery = 0;
4717 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004718 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004719 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004720 mddev->safemode = 0;
4721 mddev->bitmap_info.offset = 0;
4722 mddev->bitmap_info.default_offset = 0;
4723 mddev->bitmap_info.chunksize = 0;
4724 mddev->bitmap_info.daemon_sleep = 0;
4725 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004726 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004727}
4728
NeilBrowndefad612011-01-14 09:14:33 +11004729static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004730{
4731 if (mddev->sync_thread) {
4732 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4733 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004734 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004735 }
4736
4737 del_timer_sync(&mddev->safemode_timer);
4738
4739 bitmap_flush(mddev);
4740 md_super_wait(mddev);
4741
4742 if (!mddev->in_sync || mddev->flags) {
4743 /* mark array as shutdown cleanly */
4744 mddev->in_sync = 1;
4745 md_update_sb(mddev, 1);
4746 }
4747}
NeilBrowndefad612011-01-14 09:14:33 +11004748
4749void md_stop_writes(mddev_t *mddev)
4750{
4751 mddev_lock(mddev);
4752 __md_stop_writes(mddev);
4753 mddev_unlock(mddev);
4754}
NeilBrown390ee602010-06-01 19:37:27 +10004755EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004756
NeilBrown390ee602010-06-01 19:37:27 +10004757void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004758{
NeilBrown0ca69882011-01-14 09:14:33 +11004759 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004760 mddev->pers->stop(mddev);
4761 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4762 mddev->to_remove = &md_redundancy_group;
4763 module_put(mddev->pers->owner);
4764 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004765 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004766}
NeilBrown390ee602010-06-01 19:37:27 +10004767EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004768
NeilBrowna4bd82d2010-03-29 13:23:10 +11004769static int md_set_readonly(mddev_t *mddev, int is_open)
4770{
4771 int err = 0;
4772 mutex_lock(&mddev->open_mutex);
4773 if (atomic_read(&mddev->openers) > is_open) {
4774 printk("md: %s still in use.\n",mdname(mddev));
4775 err = -EBUSY;
4776 goto out;
4777 }
4778 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004779 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004780
4781 err = -ENXIO;
4782 if (mddev->ro==1)
4783 goto out;
4784 mddev->ro = 1;
4785 set_disk_ro(mddev->gendisk, 1);
4786 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004787 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004788 err = 0;
4789 }
4790out:
4791 mutex_unlock(&mddev->open_mutex);
4792 return err;
4793}
4794
NeilBrown9e653b62006-06-26 00:27:58 -07004795/* mode:
4796 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004797 * 2 - stop but do not disassemble array
4798 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004799static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004802 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
NeilBrownc8c00a62009-08-10 12:50:52 +10004804 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004805 if (atomic_read(&mddev->openers) > is_open ||
4806 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004807 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004808 mutex_unlock(&mddev->open_mutex);
4809 return -EBUSY;
4810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
NeilBrown6e17b022010-08-07 21:41:19 +10004812 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004813 if (mddev->ro)
4814 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004815
NeilBrowndefad612011-01-14 09:14:33 +11004816 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004817 md_stop(mddev);
4818 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004819 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004820
NeilBrowna4bd82d2010-03-29 13:23:10 +11004821 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004822 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004823
NeilBrowna4bd82d2010-03-29 13:23:10 +11004824 list_for_each_entry(rdev, &mddev->disks, same_set)
4825 if (rdev->raid_disk >= 0) {
4826 char nm[20];
4827 sprintf(nm, "rd%d", rdev->raid_disk);
4828 sysfs_remove_link(&mddev->kobj, nm);
4829 }
NeilBrownc4647292009-05-07 12:51:06 +10004830
NeilBrowna4bd82d2010-03-29 13:23:10 +11004831 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004832 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004833 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004834 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004835
NeilBrowna4bd82d2010-03-29 13:23:10 +11004836 if (mddev->ro)
4837 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004838 } else
4839 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 /*
4841 * Free resources if final stop
4842 */
NeilBrown9e653b62006-06-26 00:27:58 -07004843 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4845
NeilBrown978f9462006-02-02 14:28:05 -08004846 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004847 if (mddev->bitmap_info.file) {
4848 restore_bitmap_write_access(mddev->bitmap_info.file);
4849 fput(mddev->bitmap_info.file);
4850 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004851 }
NeilBrownc3d97142009-12-14 12:49:52 +11004852 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 export_array(mddev);
4855
NeilBrown6177b472010-03-29 11:37:13 +11004856 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004857 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004858 if (mddev->hold_active == UNTIL_STOP)
4859 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004860 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004861 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004862 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004863 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004867#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868static void autorun_array(mddev_t *mddev)
4869{
4870 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 int err;
4872
NeilBrowna757e642005-04-16 15:26:42 -07004873 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
4876 printk(KERN_INFO "md: running: ");
4877
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004878 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 char b[BDEVNAME_SIZE];
4880 printk("<%s>", bdevname(rdev->bdev,b));
4881 }
4882 printk("\n");
4883
NeilBrownd710e132008-10-13 11:55:12 +11004884 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 if (err) {
4886 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004887 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 }
4889}
4890
4891/*
4892 * lets try to run arrays based on all disks that have arrived
4893 * until now. (those are in pending_raid_disks)
4894 *
4895 * the method: pick the first pending disk, collect all disks with
4896 * the same UUID, remove all from the pending list and put them into
4897 * the 'same_array' list. Then order this list based on superblock
4898 * update time (freshest comes first), kick out 'old' disks and
4899 * compare superblocks. If everything's fine then run it.
4900 *
4901 * If "unit" is allocated, then bump its reference count
4902 */
4903static void autorun_devices(int part)
4904{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004905 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 mddev_t *mddev;
4907 char b[BDEVNAME_SIZE];
4908
4909 printk(KERN_INFO "md: autorun ...\n");
4910 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004911 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004913 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 rdev0 = list_entry(pending_raid_disks.next,
4915 mdk_rdev_t, same_set);
4916
4917 printk(KERN_INFO "md: considering %s ...\n",
4918 bdevname(rdev0->bdev,b));
4919 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004920 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 if (super_90_load(rdev, rdev0, 0) >= 0) {
4922 printk(KERN_INFO "md: adding %s ...\n",
4923 bdevname(rdev->bdev,b));
4924 list_move(&rdev->same_set, &candidates);
4925 }
4926 /*
4927 * now we have a set of devices, with all of them having
4928 * mostly sane superblocks. It's time to allocate the
4929 * mddev.
4930 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004931 if (part) {
4932 dev = MKDEV(mdp_major,
4933 rdev0->preferred_minor << MdpMinorShift);
4934 unit = MINOR(dev) >> MdpMinorShift;
4935 } else {
4936 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4937 unit = MINOR(dev);
4938 }
4939 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4941 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4942 break;
4943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
4945 md_probe(dev, NULL, NULL);
4946 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004947 if (!mddev || !mddev->gendisk) {
4948 if (mddev)
4949 mddev_put(mddev);
4950 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 "md: cannot allocate memory for md drive.\n");
4952 break;
4953 }
4954 if (mddev_lock(mddev))
4955 printk(KERN_WARNING "md: %s locked, cannot run\n",
4956 mdname(mddev));
4957 else if (mddev->raid_disks || mddev->major_version
4958 || !list_empty(&mddev->disks)) {
4959 printk(KERN_WARNING
4960 "md: %s already running, cannot run %s\n",
4961 mdname(mddev), bdevname(rdev0->bdev,b));
4962 mddev_unlock(mddev);
4963 } else {
4964 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004965 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004966 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 list_del_init(&rdev->same_set);
4968 if (bind_rdev_to_array(rdev, mddev))
4969 export_rdev(rdev);
4970 }
4971 autorun_array(mddev);
4972 mddev_unlock(mddev);
4973 }
4974 /* on success, candidates will be empty, on error
4975 * it won't...
4976 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004977 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004978 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 mddev_put(mddev);
4982 }
4983 printk(KERN_INFO "md: ... autorun DONE.\n");
4984}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004985#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987static int get_version(void __user * arg)
4988{
4989 mdu_version_t ver;
4990
4991 ver.major = MD_MAJOR_VERSION;
4992 ver.minor = MD_MINOR_VERSION;
4993 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4994
4995 if (copy_to_user(arg, &ver, sizeof(ver)))
4996 return -EFAULT;
4997
4998 return 0;
4999}
5000
5001static int get_array_info(mddev_t * mddev, void __user * arg)
5002{
5003 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005004 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
NeilBrowna9f326e2009-09-23 18:06:41 +10005007 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005008 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005010 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 failed++;
5012 else {
5013 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005014 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005015 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 else
5017 spare++;
5018 }
5019 }
5020
5021 info.major_version = mddev->major_version;
5022 info.minor_version = mddev->minor_version;
5023 info.patch_version = MD_PATCHLEVEL_VERSION;
5024 info.ctime = mddev->ctime;
5025 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005026 info.size = mddev->dev_sectors / 2;
5027 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005028 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 info.nr_disks = nr;
5030 info.raid_disks = mddev->raid_disks;
5031 info.md_minor = mddev->md_minor;
5032 info.not_persistent= !mddev->persistent;
5033
5034 info.utime = mddev->utime;
5035 info.state = 0;
5036 if (mddev->in_sync)
5037 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005038 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005039 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005040 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 info.working_disks = working;
5042 info.failed_disks = failed;
5043 info.spare_disks = spare;
5044
5045 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005046 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
5048 if (copy_to_user(arg, &info, sizeof(info)))
5049 return -EFAULT;
5050
5051 return 0;
5052}
5053
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005054static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005055{
5056 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5057 char *ptr, *buf = NULL;
5058 int err = -ENOMEM;
5059
Dan Williamsb5470dc2008-06-27 21:44:04 -07005060 if (md_allow_write(mddev))
5061 file = kmalloc(sizeof(*file), GFP_NOIO);
5062 else
5063 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005064
NeilBrown32a76272005-06-21 17:17:14 -07005065 if (!file)
5066 goto out;
5067
5068 /* bitmap disabled, zero the first byte and copy out */
5069 if (!mddev->bitmap || !mddev->bitmap->file) {
5070 file->pathname[0] = '\0';
5071 goto copy_out;
5072 }
5073
5074 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5075 if (!buf)
5076 goto out;
5077
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005078 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5079 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005080 goto out;
5081
5082 strcpy(file->pathname, ptr);
5083
5084copy_out:
5085 err = 0;
5086 if (copy_to_user(arg, file, sizeof(*file)))
5087 err = -EFAULT;
5088out:
5089 kfree(buf);
5090 kfree(file);
5091 return err;
5092}
5093
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094static int get_disk_info(mddev_t * mddev, void __user * arg)
5095{
5096 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 mdk_rdev_t *rdev;
5098
5099 if (copy_from_user(&info, arg, sizeof(info)))
5100 return -EFAULT;
5101
Andre Noll26ef3792008-07-11 22:02:21 +10005102 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 if (rdev) {
5104 info.major = MAJOR(rdev->bdev->bd_dev);
5105 info.minor = MINOR(rdev->bdev->bd_dev);
5106 info.raid_disk = rdev->raid_disk;
5107 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005108 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005110 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 info.state |= (1<<MD_DISK_ACTIVE);
5112 info.state |= (1<<MD_DISK_SYNC);
5113 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005114 if (test_bit(WriteMostly, &rdev->flags))
5115 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 } else {
5117 info.major = info.minor = 0;
5118 info.raid_disk = -1;
5119 info.state = (1<<MD_DISK_REMOVED);
5120 }
5121
5122 if (copy_to_user(arg, &info, sizeof(info)))
5123 return -EFAULT;
5124
5125 return 0;
5126}
5127
5128static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5129{
5130 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5131 mdk_rdev_t *rdev;
5132 dev_t dev = MKDEV(info->major,info->minor);
5133
5134 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5135 return -EOVERFLOW;
5136
5137 if (!mddev->raid_disks) {
5138 int err;
5139 /* expecting a device which has a superblock */
5140 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5141 if (IS_ERR(rdev)) {
5142 printk(KERN_WARNING
5143 "md: md_import_device returned %ld\n",
5144 PTR_ERR(rdev));
5145 return PTR_ERR(rdev);
5146 }
5147 if (!list_empty(&mddev->disks)) {
5148 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5149 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005150 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 .load_super(rdev, rdev0, mddev->minor_version);
5152 if (err < 0) {
5153 printk(KERN_WARNING
5154 "md: %s has different UUID to %s\n",
5155 bdevname(rdev->bdev,b),
5156 bdevname(rdev0->bdev,b2));
5157 export_rdev(rdev);
5158 return -EINVAL;
5159 }
5160 }
5161 err = bind_rdev_to_array(rdev, mddev);
5162 if (err)
5163 export_rdev(rdev);
5164 return err;
5165 }
5166
5167 /*
5168 * add_new_disk can be used once the array is assembled
5169 * to add "hot spares". They must already have a superblock
5170 * written
5171 */
5172 if (mddev->pers) {
5173 int err;
5174 if (!mddev->pers->hot_add_disk) {
5175 printk(KERN_WARNING
5176 "%s: personality does not support diskops!\n",
5177 mdname(mddev));
5178 return -EINVAL;
5179 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005180 if (mddev->persistent)
5181 rdev = md_import_device(dev, mddev->major_version,
5182 mddev->minor_version);
5183 else
5184 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 if (IS_ERR(rdev)) {
5186 printk(KERN_WARNING
5187 "md: md_import_device returned %ld\n",
5188 PTR_ERR(rdev));
5189 return PTR_ERR(rdev);
5190 }
NeilBrown1a855a02010-12-09 16:36:28 +11005191 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005192 if (!mddev->persistent) {
5193 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005194 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005195 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005196 set_bit(In_sync, &rdev->flags);
5197 } else
NeilBrown41158c72005-06-21 17:17:25 -07005198 rdev->raid_disk = -1;
5199 } else
5200 super_types[mddev->major_version].
5201 validate_super(mddev, rdev);
NeilBrown1a855a02010-12-09 16:36:28 +11005202 if (test_bit(In_sync, &rdev->flags))
5203 rdev->saved_raid_disk = rdev->raid_disk;
5204 else
5205 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005206
NeilBrownb2d444d2005-11-08 21:39:31 -08005207 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005208 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5209 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005210 else
5211 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005212
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 rdev->raid_disk = -1;
5214 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005215 if (!err && !mddev->pers->hot_remove_disk) {
5216 /* If there is hot_add_disk but no hot_remove_disk
5217 * then added disks for geometry changes,
5218 * and should be added immediately.
5219 */
5220 super_types[mddev->major_version].
5221 validate_super(mddev, rdev);
5222 err = mddev->pers->hot_add_disk(mddev, rdev);
5223 if (err)
5224 unbind_rdev_from_array(rdev);
5225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 if (err)
5227 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005228 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005229 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005230
NeilBrown17571282006-12-10 02:20:52 -08005231 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005232 if (mddev->degraded)
5233 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005234 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005235 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 return err;
5237 }
5238
5239 /* otherwise, add_new_disk is only allowed
5240 * for major_version==0 superblocks
5241 */
5242 if (mddev->major_version != 0) {
5243 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5244 mdname(mddev));
5245 return -EINVAL;
5246 }
5247
5248 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5249 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005250 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 if (IS_ERR(rdev)) {
5252 printk(KERN_WARNING
5253 "md: error, md_import_device() returned %ld\n",
5254 PTR_ERR(rdev));
5255 return PTR_ERR(rdev);
5256 }
5257 rdev->desc_nr = info->number;
5258 if (info->raid_disk < mddev->raid_disks)
5259 rdev->raid_disk = info->raid_disk;
5260 else
5261 rdev->raid_disk = -1;
5262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005264 if (info->state & (1<<MD_DISK_SYNC))
5265 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005267 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5268 set_bit(WriteMostly, &rdev->flags);
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (!mddev->persistent) {
5271 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005272 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5273 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005274 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005275 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
NeilBrown2bf071b2006-01-06 00:20:55 -08005277 err = bind_rdev_to_array(rdev, mddev);
5278 if (err) {
5279 export_rdev(rdev);
5280 return err;
5281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 }
5283
5284 return 0;
5285}
5286
5287static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5288{
5289 char b[BDEVNAME_SIZE];
5290 mdk_rdev_t *rdev;
5291
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 rdev = find_rdev(mddev, dev);
5293 if (!rdev)
5294 return -ENXIO;
5295
5296 if (rdev->raid_disk >= 0)
5297 goto busy;
5298
5299 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005300 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005301 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
5303 return 0;
5304busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005305 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 bdevname(rdev->bdev,b), mdname(mddev));
5307 return -EBUSY;
5308}
5309
5310static int hot_add_disk(mddev_t * mddev, dev_t dev)
5311{
5312 char b[BDEVNAME_SIZE];
5313 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 mdk_rdev_t *rdev;
5315
5316 if (!mddev->pers)
5317 return -ENODEV;
5318
5319 if (mddev->major_version != 0) {
5320 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5321 " version-0 superblocks.\n",
5322 mdname(mddev));
5323 return -EINVAL;
5324 }
5325 if (!mddev->pers->hot_add_disk) {
5326 printk(KERN_WARNING
5327 "%s: personality does not support diskops!\n",
5328 mdname(mddev));
5329 return -EINVAL;
5330 }
5331
NeilBrownd710e132008-10-13 11:55:12 +11005332 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 if (IS_ERR(rdev)) {
5334 printk(KERN_WARNING
5335 "md: error, md_import_device() returned %ld\n",
5336 PTR_ERR(rdev));
5337 return -EINVAL;
5338 }
5339
5340 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005341 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005343 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
NeilBrown8190e752009-06-18 08:48:58 +10005345 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
NeilBrownb2d444d2005-11-08 21:39:31 -08005347 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 printk(KERN_WARNING
5349 "md: can not hot-add faulty %s disk to %s!\n",
5350 bdevname(rdev->bdev,b), mdname(mddev));
5351 err = -EINVAL;
5352 goto abort_export;
5353 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005354 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005356 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005357 err = bind_rdev_to_array(rdev, mddev);
5358 if (err)
5359 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 /*
5362 * The rest should better be atomic, we can have disk failures
5363 * noticed in interrupt contexts ...
5364 */
5365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 rdev->raid_disk = -1;
5367
NeilBrown850b2b42006-10-03 01:15:46 -07005368 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
5370 /*
5371 * Kick recovery, maybe this spare has to be added to the
5372 * array immediately.
5373 */
5374 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5375 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005376 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 return 0;
5378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379abort_export:
5380 export_rdev(rdev);
5381 return err;
5382}
5383
NeilBrown32a76272005-06-21 17:17:14 -07005384static int set_bitmap_file(mddev_t *mddev, int fd)
5385{
5386 int err;
5387
NeilBrown36fa3062005-09-09 16:23:45 -07005388 if (mddev->pers) {
5389 if (!mddev->pers->quiesce)
5390 return -EBUSY;
5391 if (mddev->recovery || mddev->sync_thread)
5392 return -EBUSY;
5393 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005394 }
5395
NeilBrown36fa3062005-09-09 16:23:45 -07005396
5397 if (fd >= 0) {
5398 if (mddev->bitmap)
5399 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005400 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005401
NeilBrownc3d97142009-12-14 12:49:52 +11005402 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005403 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5404 mdname(mddev));
5405 return -EBADF;
5406 }
5407
NeilBrownc3d97142009-12-14 12:49:52 +11005408 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005409 if (err) {
5410 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5411 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005412 fput(mddev->bitmap_info.file);
5413 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005414 return err;
5415 }
NeilBrownc3d97142009-12-14 12:49:52 +11005416 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005417 } else if (mddev->bitmap == NULL)
5418 return -ENOENT; /* cannot remove what isn't there */
5419 err = 0;
5420 if (mddev->pers) {
5421 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005422 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005423 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005424 if (!err)
5425 err = bitmap_load(mddev);
5426 }
NeilBrownd7375ab322006-06-26 00:27:43 -07005427 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005428 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005429 fd = -1; /* make sure to put the file */
5430 }
NeilBrown36fa3062005-09-09 16:23:45 -07005431 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005432 }
5433 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005434 if (mddev->bitmap_info.file) {
5435 restore_bitmap_write_access(mddev->bitmap_info.file);
5436 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005437 }
NeilBrownc3d97142009-12-14 12:49:52 +11005438 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005439 }
5440
NeilBrown32a76272005-06-21 17:17:14 -07005441 return err;
5442}
5443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444/*
5445 * set_array_info is used two different ways
5446 * The original usage is when creating a new array.
5447 * In this usage, raid_disks is > 0 and it together with
5448 * level, size, not_persistent,layout,chunksize determine the
5449 * shape of the array.
5450 * This will always create an array with a type-0.90.0 superblock.
5451 * The newer usage is when assembling an array.
5452 * In this case raid_disks will be 0, and the major_version field is
5453 * use to determine which style super-blocks are to be found on the devices.
5454 * The minor and patch _version numbers are also kept incase the
5455 * super_block handler wishes to interpret them.
5456 */
5457static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5458{
5459
5460 if (info->raid_disks == 0) {
5461 /* just setting version number for superblock loading */
5462 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005463 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 super_types[info->major_version].name == NULL) {
5465 /* maybe try to auto-load a module? */
5466 printk(KERN_INFO
5467 "md: superblock version %d not known\n",
5468 info->major_version);
5469 return -EINVAL;
5470 }
5471 mddev->major_version = info->major_version;
5472 mddev->minor_version = info->minor_version;
5473 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005474 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005475 /* ensure mddev_put doesn't delete this now that there
5476 * is some minimal configuration.
5477 */
5478 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 return 0;
5480 }
5481 mddev->major_version = MD_MAJOR_VERSION;
5482 mddev->minor_version = MD_MINOR_VERSION;
5483 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5484 mddev->ctime = get_seconds();
5485
5486 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005487 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005488 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 mddev->raid_disks = info->raid_disks;
5490 /* don't set md_minor, it is determined by which /dev/md* was
5491 * openned
5492 */
5493 if (info->state & (1<<MD_SB_CLEAN))
5494 mddev->recovery_cp = MaxSector;
5495 else
5496 mddev->recovery_cp = 0;
5497 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005498 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499
5500 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005501 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
5503 mddev->max_disks = MD_SB_DISKS;
5504
NeilBrowne6910632008-02-06 01:39:51 -08005505 if (mddev->persistent)
5506 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005507 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
NeilBrownc3d97142009-12-14 12:49:52 +11005509 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5510 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005511
NeilBrownf6705572006-03-27 01:18:11 -08005512 mddev->reshape_position = MaxSector;
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 /*
5515 * Generate a 128 bit UUID
5516 */
5517 get_random_bytes(mddev->uuid, 16);
5518
NeilBrownf6705572006-03-27 01:18:11 -08005519 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005520 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005521 mddev->new_layout = mddev->layout;
5522 mddev->delta_disks = 0;
5523
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 return 0;
5525}
5526
Dan Williams1f403622009-03-31 14:59:03 +11005527void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5528{
Dan Williamsb522adc2009-03-31 15:00:31 +11005529 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5530
5531 if (mddev->external_size)
5532 return;
5533
Dan Williams1f403622009-03-31 14:59:03 +11005534 mddev->array_sectors = array_sectors;
5535}
5536EXPORT_SYMBOL(md_set_array_sectors);
5537
Andre Nolld71f9f82008-07-11 22:02:22 +10005538static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005539{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005540 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005541 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005542 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005543
5544 if (mddev->pers->resize == NULL)
5545 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005546 /* The "num_sectors" is the number of sectors of each device that
5547 * is used. This can only make sense for arrays with redundancy.
5548 * linear and raid0 always use whatever space is available. We can only
5549 * consider changing this number if no resync or reconstruction is
5550 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005551 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005552 * of each device. If num_sectors is zero, we find the largest size
5553 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005554 */
5555 if (mddev->sync_thread)
5556 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005557 if (mddev->bitmap)
5558 /* Sorry, cannot grow a bitmap yet, just remove it,
5559 * grow, and re-add.
5560 */
5561 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005562 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005563 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005564
Andre Nolld71f9f82008-07-11 22:02:22 +10005565 if (fit && (num_sectors == 0 || num_sectors > avail))
5566 num_sectors = avail;
5567 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005568 return -ENOSPC;
5569 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005570 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005571 if (!rv)
5572 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005573 return rv;
5574}
5575
NeilBrownda943b992006-01-06 00:20:54 -08005576static int update_raid_disks(mddev_t *mddev, int raid_disks)
5577{
5578 int rv;
5579 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005580 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005581 return -EINVAL;
5582 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005583 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005584 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005585 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005586 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005587 mddev->delta_disks = raid_disks - mddev->raid_disks;
5588
5589 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005590 if (rv < 0)
5591 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005592 return rv;
5593}
5594
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596/*
5597 * update_array_info is used to change the configuration of an
5598 * on-line array.
5599 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5600 * fields in the info are checked against the array.
5601 * Any differences that cannot be handled will cause an error.
5602 * Normally, only one change can be managed at a time.
5603 */
5604static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5605{
5606 int rv = 0;
5607 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005608 int state = 0;
5609
5610 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005611 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005612 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
5614 if (mddev->major_version != info->major_version ||
5615 mddev->minor_version != info->minor_version ||
5616/* mddev->patch_version != info->patch_version || */
5617 mddev->ctime != info->ctime ||
5618 mddev->level != info->level ||
5619/* mddev->layout != info->layout || */
5620 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005621 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005622 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5623 ((state^info->state) & 0xfffffe00)
5624 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 return -EINVAL;
5626 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005627 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5628 cnt++;
5629 if (mddev->raid_disks != info->raid_disks)
5630 cnt++;
5631 if (mddev->layout != info->layout)
5632 cnt++;
5633 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5634 cnt++;
5635 if (cnt == 0)
5636 return 0;
5637 if (cnt > 1)
5638 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
5640 if (mddev->layout != info->layout) {
5641 /* Change layout
5642 * we don't need to do anything at the md level, the
5643 * personality will take care of it all.
5644 */
NeilBrown50ac1682009-06-18 08:47:55 +10005645 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005647 else {
5648 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005649 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005650 if (rv)
5651 mddev->new_layout = mddev->layout;
5652 return rv;
5653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005655 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005656 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
NeilBrownda943b992006-01-06 00:20:54 -08005658 if (mddev->raid_disks != info->raid_disks)
5659 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
NeilBrown36fa3062005-09-09 16:23:45 -07005661 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5662 if (mddev->pers->quiesce == NULL)
5663 return -EINVAL;
5664 if (mddev->recovery || mddev->sync_thread)
5665 return -EBUSY;
5666 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5667 /* add the bitmap */
5668 if (mddev->bitmap)
5669 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005670 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005671 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005672 mddev->bitmap_info.offset =
5673 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005674 mddev->pers->quiesce(mddev, 1);
5675 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005676 if (!rv)
5677 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005678 if (rv)
5679 bitmap_destroy(mddev);
5680 mddev->pers->quiesce(mddev, 0);
5681 } else {
5682 /* remove the bitmap */
5683 if (!mddev->bitmap)
5684 return -ENOENT;
5685 if (mddev->bitmap->file)
5686 return -EINVAL;
5687 mddev->pers->quiesce(mddev, 1);
5688 bitmap_destroy(mddev);
5689 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005690 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005691 }
5692 }
NeilBrown850b2b42006-10-03 01:15:46 -07005693 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 return rv;
5695}
5696
5697static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5698{
5699 mdk_rdev_t *rdev;
5700
5701 if (mddev->pers == NULL)
5702 return -ENODEV;
5703
5704 rdev = find_rdev(mddev, dev);
5705 if (!rdev)
5706 return -ENODEV;
5707
5708 md_error(mddev, rdev);
5709 return 0;
5710}
5711
Andre Noll2f9618c2008-04-25 18:57:58 +02005712/*
5713 * We have a problem here : there is no easy way to give a CHS
5714 * virtual geometry. We currently pretend that we have a 2 heads
5715 * 4 sectors (with a BIG number of cylinders...). This drives
5716 * dosfs just mad... ;-)
5717 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005718static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5719{
5720 mddev_t *mddev = bdev->bd_disk->private_data;
5721
5722 geo->heads = 2;
5723 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005724 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005725 return 0;
5726}
5727
Al Viroa39907f2008-03-02 10:31:15 -05005728static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 unsigned int cmd, unsigned long arg)
5730{
5731 int err = 0;
5732 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005734 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 if (!capable(CAP_SYS_ADMIN))
5737 return -EACCES;
5738
5739 /*
5740 * Commands dealing with the RAID driver but not any
5741 * particular array:
5742 */
5743 switch (cmd)
5744 {
5745 case RAID_VERSION:
5746 err = get_version(argp);
5747 goto done;
5748
5749 case PRINT_RAID_DEBUG:
5750 err = 0;
5751 md_print_devices();
5752 goto done;
5753
5754#ifndef MODULE
5755 case RAID_AUTORUN:
5756 err = 0;
5757 autostart_arrays(arg);
5758 goto done;
5759#endif
5760 default:;
5761 }
5762
5763 /*
5764 * Commands creating/starting a new array:
5765 */
5766
Al Viroa39907f2008-03-02 10:31:15 -05005767 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769 if (!mddev) {
5770 BUG();
5771 goto abort;
5772 }
5773
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 err = mddev_lock(mddev);
5775 if (err) {
5776 printk(KERN_INFO
5777 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5778 err, cmd);
5779 goto abort;
5780 }
5781
5782 switch (cmd)
5783 {
5784 case SET_ARRAY_INFO:
5785 {
5786 mdu_array_info_t info;
5787 if (!arg)
5788 memset(&info, 0, sizeof(info));
5789 else if (copy_from_user(&info, argp, sizeof(info))) {
5790 err = -EFAULT;
5791 goto abort_unlock;
5792 }
5793 if (mddev->pers) {
5794 err = update_array_info(mddev, &info);
5795 if (err) {
5796 printk(KERN_WARNING "md: couldn't update"
5797 " array info. %d\n", err);
5798 goto abort_unlock;
5799 }
5800 goto done_unlock;
5801 }
5802 if (!list_empty(&mddev->disks)) {
5803 printk(KERN_WARNING
5804 "md: array %s already has disks!\n",
5805 mdname(mddev));
5806 err = -EBUSY;
5807 goto abort_unlock;
5808 }
5809 if (mddev->raid_disks) {
5810 printk(KERN_WARNING
5811 "md: array %s already initialised!\n",
5812 mdname(mddev));
5813 err = -EBUSY;
5814 goto abort_unlock;
5815 }
5816 err = set_array_info(mddev, &info);
5817 if (err) {
5818 printk(KERN_WARNING "md: couldn't set"
5819 " array info. %d\n", err);
5820 goto abort_unlock;
5821 }
5822 }
5823 goto done_unlock;
5824
5825 default:;
5826 }
5827
5828 /*
5829 * Commands querying/configuring an existing array:
5830 */
NeilBrown32a76272005-06-21 17:17:14 -07005831 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005832 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005833 if ((!mddev->raid_disks && !mddev->external)
5834 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5835 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5836 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 err = -ENODEV;
5838 goto abort_unlock;
5839 }
5840
5841 /*
5842 * Commands even a read-only array can execute:
5843 */
5844 switch (cmd)
5845 {
5846 case GET_ARRAY_INFO:
5847 err = get_array_info(mddev, argp);
5848 goto done_unlock;
5849
NeilBrown32a76272005-06-21 17:17:14 -07005850 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005851 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005852 goto done_unlock;
5853
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 case GET_DISK_INFO:
5855 err = get_disk_info(mddev, argp);
5856 goto done_unlock;
5857
5858 case RESTART_ARRAY_RW:
5859 err = restart_array(mddev);
5860 goto done_unlock;
5861
5862 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005863 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 goto done_unlock;
5865
5866 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005867 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 goto done_unlock;
5869
Dan Williamse2218352010-05-12 08:25:37 +10005870 case BLKROSET:
5871 if (get_user(ro, (int __user *)(arg))) {
5872 err = -EFAULT;
5873 goto done_unlock;
5874 }
5875 err = -EINVAL;
5876
5877 /* if the bdev is going readonly the value of mddev->ro
5878 * does not matter, no writes are coming
5879 */
5880 if (ro)
5881 goto done_unlock;
5882
5883 /* are we are already prepared for writes? */
5884 if (mddev->ro != 1)
5885 goto done_unlock;
5886
5887 /* transitioning to readauto need only happen for
5888 * arrays that call md_write_start
5889 */
5890 if (mddev->pers) {
5891 err = restart_array(mddev);
5892 if (err == 0) {
5893 mddev->ro = 2;
5894 set_disk_ro(mddev->gendisk, 0);
5895 }
5896 }
5897 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 }
5899
5900 /*
5901 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005902 * superblock, so we do not allow them on read-only arrays.
5903 * However non-MD ioctls (e.g. get-size) will still come through
5904 * here and hit the 'default' below, so only disallow
5905 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005907 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005908 if (mddev->ro == 2) {
5909 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005910 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005911 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5912 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005913 } else {
5914 err = -EROFS;
5915 goto abort_unlock;
5916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 }
5918
5919 switch (cmd)
5920 {
5921 case ADD_NEW_DISK:
5922 {
5923 mdu_disk_info_t info;
5924 if (copy_from_user(&info, argp, sizeof(info)))
5925 err = -EFAULT;
5926 else
5927 err = add_new_disk(mddev, &info);
5928 goto done_unlock;
5929 }
5930
5931 case HOT_REMOVE_DISK:
5932 err = hot_remove_disk(mddev, new_decode_dev(arg));
5933 goto done_unlock;
5934
5935 case HOT_ADD_DISK:
5936 err = hot_add_disk(mddev, new_decode_dev(arg));
5937 goto done_unlock;
5938
5939 case SET_DISK_FAULTY:
5940 err = set_disk_faulty(mddev, new_decode_dev(arg));
5941 goto done_unlock;
5942
5943 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005944 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 goto done_unlock;
5946
NeilBrown32a76272005-06-21 17:17:14 -07005947 case SET_BITMAP_FILE:
5948 err = set_bitmap_file(mddev, (int)arg);
5949 goto done_unlock;
5950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 err = -EINVAL;
5953 goto abort_unlock;
5954 }
5955
5956done_unlock:
5957abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005958 if (mddev->hold_active == UNTIL_IOCTL &&
5959 err != -EINVAL)
5960 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 mddev_unlock(mddev);
5962
5963 return err;
5964done:
5965 if (err)
5966 MD_BUG();
5967abort:
5968 return err;
5969}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005970#ifdef CONFIG_COMPAT
5971static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5972 unsigned int cmd, unsigned long arg)
5973{
5974 switch (cmd) {
5975 case HOT_REMOVE_DISK:
5976 case HOT_ADD_DISK:
5977 case SET_DISK_FAULTY:
5978 case SET_BITMAP_FILE:
5979 /* These take in integer arg, do not convert */
5980 break;
5981 default:
5982 arg = (unsigned long)compat_ptr(arg);
5983 break;
5984 }
5985
5986 return md_ioctl(bdev, mode, cmd, arg);
5987}
5988#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
Al Viroa39907f2008-03-02 10:31:15 -05005990static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991{
5992 /*
5993 * Succeed if we can lock the mddev, which confirms that
5994 * it isn't being stopped right now.
5995 */
NeilBrownd3374822009-01-09 08:31:10 +11005996 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 int err;
5998
NeilBrownd3374822009-01-09 08:31:10 +11005999 if (mddev->gendisk != bdev->bd_disk) {
6000 /* we are racing with mddev_put which is discarding this
6001 * bd_disk.
6002 */
6003 mddev_put(mddev);
6004 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006005 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006006 /* Then retry the open from the top */
6007 return -ERESTARTSYS;
6008 }
6009 BUG_ON(mddev != bdev->bd_disk->private_data);
6010
NeilBrownc8c00a62009-08-10 12:50:52 +10006011 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 goto out;
6013
6014 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006015 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006016 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006018 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 out:
6020 return err;
6021}
6022
Al Viroa39907f2008-03-02 10:31:15 -05006023static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024{
Al Viroa39907f2008-03-02 10:31:15 -05006025 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006027 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006028 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 mddev_put(mddev);
6030
6031 return 0;
6032}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006033
6034static int md_media_changed(struct gendisk *disk)
6035{
6036 mddev_t *mddev = disk->private_data;
6037
6038 return mddev->changed;
6039}
6040
6041static int md_revalidate(struct gendisk *disk)
6042{
6043 mddev_t *mddev = disk->private_data;
6044
6045 mddev->changed = 0;
6046 return 0;
6047}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006048static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049{
6050 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006051 .open = md_open,
6052 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006053 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006054#ifdef CONFIG_COMPAT
6055 .compat_ioctl = md_compat_ioctl,
6056#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006057 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006058 .media_changed = md_media_changed,
6059 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060};
6061
Adrian Bunk75c96f82005-05-05 16:16:09 -07006062static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063{
6064 mdk_thread_t *thread = arg;
6065
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 /*
6067 * md_thread is a 'system-thread', it's priority should be very
6068 * high. We avoid resource deadlocks individually in each
6069 * raid personality. (RAID5 does preallocation) We also use RR and
6070 * the very same RT priority as kswapd, thus we will never get
6071 * into a priority inversion deadlock.
6072 *
6073 * we definitely have to have equal or higher priority than
6074 * bdflush, otherwise bdflush will deadlock if there are too
6075 * many dirty RAID5 blocks.
6076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
NeilBrown6985c432005-10-19 21:23:47 -07006078 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006079 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
NeilBrown93588e22005-11-15 00:09:12 -08006081 /* We need to wait INTERRUPTIBLE so that
6082 * we don't add to the load-average.
6083 * That means we need to be sure no signals are
6084 * pending
6085 */
6086 if (signal_pending(current))
6087 flush_signals(current);
6088
6089 wait_event_interruptible_timeout
6090 (thread->wqueue,
6091 test_bit(THREAD_WAKEUP, &thread->flags)
6092 || kthread_should_stop(),
6093 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
NeilBrown6c987912011-01-14 09:13:53 +11006095 clear_bit(THREAD_WAKEUP, &thread->flags);
6096 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006097 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006099
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 return 0;
6101}
6102
6103void md_wakeup_thread(mdk_thread_t *thread)
6104{
6105 if (thread) {
6106 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6107 set_bit(THREAD_WAKEUP, &thread->flags);
6108 wake_up(&thread->wqueue);
6109 }
6110}
6111
6112mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6113 const char *name)
6114{
6115 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
NeilBrown9ffae0c2006-01-06 00:20:32 -08006117 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 if (!thread)
6119 return NULL;
6120
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 init_waitqueue_head(&thread->wqueue);
6122
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 thread->run = run;
6124 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006125 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006126 thread->tsk = kthread_run(md_thread, thread,
6127 "%s_%s",
6128 mdname(thread->mddev),
6129 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006130 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 kfree(thread);
6132 return NULL;
6133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 return thread;
6135}
6136
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137void md_unregister_thread(mdk_thread_t *thread)
6138{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006139 if (!thread)
6140 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006141 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006142
6143 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 kfree(thread);
6145}
6146
6147void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6148{
6149 if (!mddev) {
6150 MD_BUG();
6151 return;
6152 }
6153
NeilBrownb2d444d2005-11-08 21:39:31 -08006154 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006156
6157 if (mddev->external)
6158 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006159/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6161 mdname(mddev),
6162 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6163 __builtin_return_address(0),__builtin_return_address(1),
6164 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006165*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006166 if (!mddev->pers)
6167 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 if (!mddev->pers->error_handler)
6169 return;
6170 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006171 if (mddev->degraded)
6172 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006173 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6175 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6176 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006177 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006178 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006179 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180}
6181
6182/* seq_file implementation /proc/mdstat */
6183
6184static void status_unused(struct seq_file *seq)
6185{
6186 int i = 0;
6187 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
6189 seq_printf(seq, "unused devices: ");
6190
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006191 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 char b[BDEVNAME_SIZE];
6193 i++;
6194 seq_printf(seq, "%s ",
6195 bdevname(rdev->bdev,b));
6196 }
6197 if (!i)
6198 seq_printf(seq, "<none>");
6199
6200 seq_printf(seq, "\n");
6201}
6202
6203
6204static void status_resync(struct seq_file *seq, mddev_t * mddev)
6205{
NeilBrowndd71cf62009-05-07 12:49:35 +10006206 sector_t max_sectors, resync, res;
6207 unsigned long dt, db;
6208 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006209 int scale;
6210 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
NeilBrowndd71cf62009-05-07 12:49:35 +10006212 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
6214 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006215 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006217 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
6219 /*
6220 * Should not happen.
6221 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006222 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 MD_BUG();
6224 return;
6225 }
NeilBrown4588b422006-03-27 01:18:04 -08006226 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006227 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006228 * u32, as those are the requirements for sector_div.
6229 * Thus 'scale' must be at least 10
6230 */
6231 scale = 10;
6232 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006233 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006234 scale++;
6235 }
6236 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006237 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006238
6239 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 {
NeilBrown4588b422006-03-27 01:18:04 -08006241 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 seq_printf(seq, "[");
6243 for (i = 0; i < x; i++)
6244 seq_printf(seq, "=");
6245 seq_printf(seq, ">");
6246 for (i = 0; i < y; i++)
6247 seq_printf(seq, ".");
6248 seq_printf(seq, "] ");
6249 }
NeilBrown4588b422006-03-27 01:18:04 -08006250 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006251 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6252 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006253 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6254 "check" :
6255 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6256 "resync" : "recovery"))),
6257 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006258 (unsigned long long) resync/2,
6259 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 * dt: time from mark until now
6263 * db: blocks written from mark until now
6264 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006265 *
6266 * rt is a sector_t, so could be 32bit or 64bit.
6267 * So we divide before multiply in case it is 32bit and close
6268 * to the limit.
6269 * We scale the divisor (db) by 32 to avoid loosing precision
6270 * near the end of resync when the number of remaining sectors
6271 * is close to 'db'.
6272 * We then divide rt by 32 after multiplying by db to compensate.
6273 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 */
6275 dt = ((jiffies - mddev->resync_mark) / HZ);
6276 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006277 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6278 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
NeilBrowndd71cf62009-05-07 12:49:35 +10006280 rt = max_sectors - resync; /* number of remaining sectors */
6281 sector_div(rt, db/32+1);
6282 rt *= dt;
6283 rt >>= 5;
6284
6285 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6286 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
NeilBrownff4e8d92006-07-10 04:44:16 -07006288 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289}
6290
6291static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6292{
6293 struct list_head *tmp;
6294 loff_t l = *pos;
6295 mddev_t *mddev;
6296
6297 if (l >= 0x10000)
6298 return NULL;
6299 if (!l--)
6300 /* header */
6301 return (void*)1;
6302
6303 spin_lock(&all_mddevs_lock);
6304 list_for_each(tmp,&all_mddevs)
6305 if (!l--) {
6306 mddev = list_entry(tmp, mddev_t, all_mddevs);
6307 mddev_get(mddev);
6308 spin_unlock(&all_mddevs_lock);
6309 return mddev;
6310 }
6311 spin_unlock(&all_mddevs_lock);
6312 if (!l--)
6313 return (void*)2;/* tail */
6314 return NULL;
6315}
6316
6317static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6318{
6319 struct list_head *tmp;
6320 mddev_t *next_mddev, *mddev = v;
6321
6322 ++*pos;
6323 if (v == (void*)2)
6324 return NULL;
6325
6326 spin_lock(&all_mddevs_lock);
6327 if (v == (void*)1)
6328 tmp = all_mddevs.next;
6329 else
6330 tmp = mddev->all_mddevs.next;
6331 if (tmp != &all_mddevs)
6332 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6333 else {
6334 next_mddev = (void*)2;
6335 *pos = 0x10000;
6336 }
6337 spin_unlock(&all_mddevs_lock);
6338
6339 if (v != (void*)1)
6340 mddev_put(mddev);
6341 return next_mddev;
6342
6343}
6344
6345static void md_seq_stop(struct seq_file *seq, void *v)
6346{
6347 mddev_t *mddev = v;
6348
6349 if (mddev && v != (void*)1 && v != (void*)2)
6350 mddev_put(mddev);
6351}
6352
NeilBrownd7603b72006-01-06 00:20:30 -08006353struct mdstat_info {
6354 int event;
6355};
6356
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357static int md_seq_show(struct seq_file *seq, void *v)
6358{
6359 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006360 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006362 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006363 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
6365 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006366 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 seq_printf(seq, "Personalities : ");
6368 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006369 list_for_each_entry(pers, &pers_list, list)
6370 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 spin_unlock(&pers_lock);
6373 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006374 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 return 0;
6376 }
6377 if (v == (void*)2) {
6378 status_unused(seq);
6379 return 0;
6380 }
6381
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006382 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006384
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6386 seq_printf(seq, "%s : %sactive", mdname(mddev),
6387 mddev->pers ? "" : "in");
6388 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006389 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006391 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006392 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 seq_printf(seq, " %s", mddev->pers->name);
6394 }
6395
Andre Nolldd8ac332009-03-31 14:33:13 +11006396 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006397 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 char b[BDEVNAME_SIZE];
6399 seq_printf(seq, " %s[%d]",
6400 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006401 if (test_bit(WriteMostly, &rdev->flags))
6402 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006403 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 seq_printf(seq, "(F)");
6405 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006406 } else if (rdev->raid_disk < 0)
6407 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006408 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 }
6410
6411 if (!list_empty(&mddev->disks)) {
6412 if (mddev->pers)
6413 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006414 (unsigned long long)
6415 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 else
6417 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006418 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006420 if (mddev->persistent) {
6421 if (mddev->major_version != 0 ||
6422 mddev->minor_version != 90) {
6423 seq_printf(seq," super %d.%d",
6424 mddev->major_version,
6425 mddev->minor_version);
6426 }
NeilBrowne6910632008-02-06 01:39:51 -08006427 } else if (mddev->external)
6428 seq_printf(seq, " super external:%s",
6429 mddev->metadata_type);
6430 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006431 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
6433 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006434 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006436 if (mddev->pers->sync_request) {
6437 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006438 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006439 seq_printf(seq, "\n ");
6440 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6441 seq_printf(seq, "\tresync=DELAYED\n ");
6442 else if (mddev->recovery_cp < MaxSector)
6443 seq_printf(seq, "\tresync=PENDING\n ");
6444 }
NeilBrown32a76272005-06-21 17:17:14 -07006445 } else
6446 seq_printf(seq, "\n ");
6447
6448 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006449 unsigned long chunk_kb;
6450 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006451 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006452 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006453 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6454 "%lu%s chunk",
6455 bitmap->pages - bitmap->missing_pages,
6456 bitmap->pages,
6457 (bitmap->pages - bitmap->missing_pages)
6458 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006459 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006460 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006461 if (bitmap->file) {
6462 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006463 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006464 }
NeilBrown78d742d2005-06-21 17:17:15 -07006465
NeilBrown32a76272005-06-21 17:17:14 -07006466 seq_printf(seq, "\n");
6467 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 }
6469
6470 seq_printf(seq, "\n");
6471 }
6472 mddev_unlock(mddev);
6473
6474 return 0;
6475}
6476
Jan Engelhardt110518b2009-05-07 12:49:37 +10006477static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 .start = md_seq_start,
6479 .next = md_seq_next,
6480 .stop = md_seq_stop,
6481 .show = md_seq_show,
6482};
6483
6484static int md_seq_open(struct inode *inode, struct file *file)
6485{
6486 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006487 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6488 if (mi == NULL)
6489 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490
6491 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006492 if (error)
6493 kfree(mi);
6494 else {
6495 struct seq_file *p = file->private_data;
6496 p->private = mi;
6497 mi->event = atomic_read(&md_event_count);
6498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 return error;
6500}
6501
NeilBrownd7603b72006-01-06 00:20:30 -08006502static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6503{
6504 struct seq_file *m = filp->private_data;
6505 struct mdstat_info *mi = m->private;
6506 int mask;
6507
6508 poll_wait(filp, &md_event_waiters, wait);
6509
6510 /* always allow read */
6511 mask = POLLIN | POLLRDNORM;
6512
6513 if (mi->event != atomic_read(&md_event_count))
6514 mask |= POLLERR | POLLPRI;
6515 return mask;
6516}
6517
Arjan van de Venfa027c22007-02-12 00:55:33 -08006518static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006519 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 .open = md_seq_open,
6521 .read = seq_read,
6522 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006523 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006524 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525};
6526
NeilBrown2604b702006-01-06 00:20:36 -08006527int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006530 list_add_tail(&p->list, &pers_list);
6531 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 spin_unlock(&pers_lock);
6533 return 0;
6534}
6535
NeilBrown2604b702006-01-06 00:20:36 -08006536int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537{
NeilBrown2604b702006-01-06 00:20:36 -08006538 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006540 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 spin_unlock(&pers_lock);
6542 return 0;
6543}
6544
NeilBrowneea1bf32009-03-31 14:27:02 +11006545static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546{
6547 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006549 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
6551 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006552 rcu_read_lock();
6553 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006555 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6556 (int)part_stat_read(&disk->part0, sectors[1]) -
6557 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006558 /* sync IO will cause sync_io to increase before the disk_stats
6559 * as sync_io is counted when a request starts, and
6560 * disk_stats is counted when it completes.
6561 * So resync activity will cause curr_events to be smaller than
6562 * when there was no such activity.
6563 * non-sync IO will cause disk_stat to increase without
6564 * increasing sync_io so curr_events will (eventually)
6565 * be larger than it was before. Once it becomes
6566 * substantially larger, the test below will cause
6567 * the array to appear non-idle, and resync will slow
6568 * down.
6569 * If there is a lot of outstanding resync activity when
6570 * we set last_event to curr_events, then all that activity
6571 * completing might cause the array to appear non-idle
6572 * and resync will be slowed down even though there might
6573 * not have been non-resync activity. This will only
6574 * happen once though. 'last_events' will soon reflect
6575 * the state where there is little or no outstanding
6576 * resync requests, and further resync activity will
6577 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006578 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006580 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 rdev->last_events = curr_events;
6582 idle = 0;
6583 }
6584 }
NeilBrown4b809912008-07-21 17:05:25 +10006585 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 return idle;
6587}
6588
6589void md_done_sync(mddev_t *mddev, int blocks, int ok)
6590{
6591 /* another "blocks" (512byte) blocks have been synced */
6592 atomic_sub(blocks, &mddev->recovery_active);
6593 wake_up(&mddev->recovery_wait);
6594 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006595 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 md_wakeup_thread(mddev->thread);
6597 // stop recovery, signal do_sync ....
6598 }
6599}
6600
6601
NeilBrown06d91a52005-06-21 17:17:12 -07006602/* md_write_start(mddev, bi)
6603 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006604 * in superblock) before writing, schedule a superblock update
6605 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006606 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006607void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608{
Neil Brown0fd62b82008-06-28 08:31:36 +10006609 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006610 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006611 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006612
NeilBrownf91de922005-11-08 21:39:36 -08006613 BUG_ON(mddev->ro == 1);
6614 if (mddev->ro == 2) {
6615 /* need to switch to read/write */
6616 mddev->ro = 0;
6617 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6618 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006619 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006620 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006621 }
NeilBrown06d91a52005-06-21 17:17:12 -07006622 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006623 if (mddev->safemode == 1)
6624 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006625 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006626 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006627 if (mddev->in_sync) {
6628 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006629 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006630 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006631 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006632 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006633 }
NeilBrowna9701a32005-11-08 21:39:34 -08006634 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006635 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006636 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006637 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006638 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006639 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640}
6641
6642void md_write_end(mddev_t *mddev)
6643{
6644 if (atomic_dec_and_test(&mddev->writes_pending)) {
6645 if (mddev->safemode == 2)
6646 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006647 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6649 }
6650}
6651
NeilBrown2a2275d2007-01-26 00:57:11 -08006652/* md_allow_write(mddev)
6653 * Calling this ensures that the array is marked 'active' so that writes
6654 * may proceed without blocking. It is important to call this before
6655 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6656 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006657 *
6658 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6659 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006660 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006661int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006662{
6663 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006664 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006665 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006666 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006667 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006668 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006669
6670 spin_lock_irq(&mddev->write_lock);
6671 if (mddev->in_sync) {
6672 mddev->in_sync = 0;
6673 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006674 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006675 if (mddev->safemode_delay &&
6676 mddev->safemode == 0)
6677 mddev->safemode = 1;
6678 spin_unlock_irq(&mddev->write_lock);
6679 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006680 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006681 } else
6682 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006683
NeilBrown070dc6d2010-08-30 17:33:34 +10006684 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006685 return -EAGAIN;
6686 else
6687 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006688}
6689EXPORT_SYMBOL_GPL(md_allow_write);
6690
NeilBrownb63d7c22010-06-01 19:37:33 +10006691void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006692{
NeilBrown252ac522010-06-01 19:37:29 +10006693 if (mddev->plug)
6694 mddev->plug->unplug_fn(mddev->plug);
6695}
6696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697#define SYNC_MARKS 10
6698#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006699void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700{
6701 mddev_t *mddev2;
6702 unsigned int currspeed = 0,
6703 window;
NeilBrown57afd892005-06-21 17:17:13 -07006704 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 unsigned long mark[SYNC_MARKS];
6706 sector_t mark_cnt[SYNC_MARKS];
6707 int last_mark,m;
6708 struct list_head *tmp;
6709 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006710 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006711 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006712 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
6714 /* just incase thread restarts... */
6715 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6716 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006717 if (mddev->ro) /* never try to sync a read-only array */
6718 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719
NeilBrown61df9d92006-10-03 01:15:57 -07006720 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6721 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6722 desc = "data-check";
6723 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6724 desc = "requested-resync";
6725 else
6726 desc = "resync";
6727 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6728 desc = "reshape";
6729 else
6730 desc = "recovery";
6731
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 /* we overload curr_resync somewhat here.
6733 * 0 == not engaged in resync at all
6734 * 2 == checking that there is no conflict with another sync
6735 * 1 == like 2, but have yielded to allow conflicting resync to
6736 * commense
6737 * other == active in resync - this many blocks
6738 *
6739 * Before starting a resync we must have set curr_resync to
6740 * 2, and then checked that every "conflicting" array has curr_resync
6741 * less than ours. When we find one that is the same or higher
6742 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6743 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6744 * This will mean we have to start checking from the beginning again.
6745 *
6746 */
6747
6748 do {
6749 mddev->curr_resync = 2;
6750
6751 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006752 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006753 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006754
6755 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006757 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 if (mddev2 == mddev)
6759 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006760 if (!mddev->parallel_resync
6761 && mddev2->curr_resync
6762 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 DEFINE_WAIT(wq);
6764 if (mddev < mddev2 && mddev->curr_resync == 2) {
6765 /* arbitrarily yield */
6766 mddev->curr_resync = 1;
6767 wake_up(&resync_wait);
6768 }
6769 if (mddev > mddev2 && mddev->curr_resync == 1)
6770 /* no need to wait here, we can wait the next
6771 * time 'round when curr_resync == 2
6772 */
6773 continue;
NeilBrown97441972008-09-19 11:49:54 +10006774 /* We need to wait 'interruptible' so as not to
6775 * contribute to the load average, and not to
6776 * be caught by 'softlockup'
6777 */
6778 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006779 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006780 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006781 printk(KERN_INFO "md: delaying %s of %s"
6782 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006784 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006786 if (signal_pending(current))
6787 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 schedule();
6789 finish_wait(&resync_wait, &wq);
6790 goto try_again;
6791 }
6792 finish_wait(&resync_wait, &wq);
6793 }
6794 }
6795 } while (mddev->curr_resync < 2);
6796
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006797 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006798 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006800 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 */
6802 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006803 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006804 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006805 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6806 j = mddev->resync_min;
6807 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006808 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006809
NeilBrownccfcc3c2006-03-27 01:18:09 -08006810 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006811 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006812 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006814 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006815 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006816 rcu_read_lock();
6817 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006818 if (rdev->raid_disk >= 0 &&
6819 !test_bit(Faulty, &rdev->flags) &&
6820 !test_bit(In_sync, &rdev->flags) &&
6821 rdev->recovery_offset < j)
6822 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006823 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
NeilBrown61df9d92006-10-03 01:15:57 -07006826 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6827 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6828 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006829 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006830 "(but not more than %d KB/sec) for %s.\n",
6831 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
NeilBrowneea1bf32009-03-31 14:27:02 +11006833 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006834
NeilBrown57afd892005-06-21 17:17:13 -07006835 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 for (m = 0; m < SYNC_MARKS; m++) {
6837 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006838 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 }
6840 last_mark = 0;
6841 mddev->resync_mark = mark[last_mark];
6842 mddev->resync_mark_cnt = mark_cnt[last_mark];
6843
6844 /*
6845 * Tune reconstruction:
6846 */
6847 window = 32*(PAGE_SIZE/512);
6848 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6849 window/2,(unsigned long long) max_sectors/2);
6850
6851 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 last_check = 0;
6853
6854 if (j>2) {
6855 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006856 "md: resuming %s of %s from checkpoint.\n",
6857 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 mddev->curr_resync = j;
6859 }
NeilBrown75d3da42011-01-14 09:14:34 +11006860 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
6862 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006863 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
NeilBrown57afd892005-06-21 17:17:13 -07006865 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006866
NeilBrown7a91ee12009-05-26 12:57:21 +10006867 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6868 ((mddev->curr_resync > mddev->curr_resync_completed &&
6869 (mddev->curr_resync - mddev->curr_resync_completed)
6870 > (max_sectors >> 4)) ||
6871 (j - mddev->curr_resync_completed)*2
6872 >= mddev->resync_max - mddev->curr_resync_completed
6873 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006874 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11006875 wait_event(mddev->recovery_wait,
6876 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11006877 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10006878 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006879 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006880 }
NeilBrownacb180b2009-04-14 16:28:34 +10006881
NeilBrowne62e58a2009-07-01 13:15:35 +10006882 while (j >= mddev->resync_max && !kthread_should_stop()) {
6883 /* As this condition is controlled by user-space,
6884 * we can block indefinitely, so use '_interruptible'
6885 * to avoid triggering warnings.
6886 */
6887 flush_signals(current); /* just in case */
6888 wait_event_interruptible(mddev->recovery_wait,
6889 mddev->resync_max > j
6890 || kthread_should_stop());
6891 }
NeilBrownacb180b2009-04-14 16:28:34 +10006892
6893 if (kthread_should_stop())
6894 goto interrupted;
6895
NeilBrown57afd892005-06-21 17:17:13 -07006896 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006897 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006898 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006899 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 goto out;
6901 }
NeilBrown57afd892005-06-21 17:17:13 -07006902
6903 if (!skipped) { /* actual IO requested */
6904 io_sectors += sectors;
6905 atomic_add(sectors, &mddev->recovery_active);
6906 }
6907
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 j += sectors;
6909 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006910 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006911 if (last_check == 0)
6912 /* this is the earliers that rebuilt will be
6913 * visible in /proc/mdstat
6914 */
6915 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006916
6917 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 continue;
6919
NeilBrown57afd892005-06-21 17:17:13 -07006920 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
NeilBrowndfc70642008-05-23 13:04:39 -07006922 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 break;
6924
6925 repeat:
6926 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6927 /* step marks */
6928 int next = (last_mark+1) % SYNC_MARKS;
6929
6930 mddev->resync_mark = mark[next];
6931 mddev->resync_mark_cnt = mark_cnt[next];
6932 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006933 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 last_mark = next;
6935 }
6936
6937
NeilBrownc6207272008-02-06 01:39:52 -08006938 if (kthread_should_stop())
6939 goto interrupted;
6940
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 /*
6943 * this loop exits only if either when we are slower than
6944 * the 'hard' speed limit, or the system was IO-idle for
6945 * a jiffy.
6946 * the system might be non-idle CPU-wise, but we only care
6947 * about not overloading the IO subsystem. (things like an
6948 * e2fsck being done on the RAID array should execute fast)
6949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 cond_resched();
6951
NeilBrown57afd892005-06-21 17:17:13 -07006952 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6953 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954
NeilBrown88202a02006-01-06 00:21:36 -08006955 if (currspeed > speed_min(mddev)) {
6956 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006957 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006958 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 goto repeat;
6960 }
6961 }
6962 }
NeilBrown61df9d92006-10-03 01:15:57 -07006963 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 /*
6965 * this also signals 'finished resyncing' to md_stop
6966 */
6967 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6969
6970 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006971 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
NeilBrowndfc70642008-05-23 13:04:39 -07006973 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006974 mddev->curr_resync > 2) {
6975 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6976 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6977 if (mddev->curr_resync >= mddev->recovery_cp) {
6978 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006979 "md: checkpointing %s of %s.\n",
6980 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006981 mddev->recovery_cp = mddev->curr_resync;
6982 }
6983 } else
6984 mddev->recovery_cp = MaxSector;
6985 } else {
6986 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6987 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006988 rcu_read_lock();
6989 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006990 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006991 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006992 !test_bit(Faulty, &rdev->flags) &&
6993 !test_bit(In_sync, &rdev->flags) &&
6994 rdev->recovery_offset < mddev->curr_resync)
6995 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006996 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 }
NeilBrown17571282006-12-10 02:20:52 -08006999 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007002 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7003 /* We completed so min/max setting can be forgotten if used. */
7004 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7005 mddev->resync_min = 0;
7006 mddev->resync_max = MaxSector;
7007 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7008 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 mddev->curr_resync = 0;
7010 wake_up(&resync_wait);
7011 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7012 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007013 return;
7014
7015 interrupted:
7016 /*
7017 * got a signal, exit.
7018 */
7019 printk(KERN_INFO
7020 "md: md_do_sync() got signal ... exiting\n");
7021 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7022 goto out;
7023
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024}
NeilBrown29269552006-03-27 01:18:10 -08007025EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
7027
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007028static int remove_and_add_spares(mddev_t *mddev)
7029{
7030 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007031 int spares = 0;
7032
NeilBrown97e4f422009-03-31 14:33:13 +11007033 mddev->curr_resync_completed = 0;
7034
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007035 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007036 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007037 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007038 (test_bit(Faulty, &rdev->flags) ||
7039 ! test_bit(In_sync, &rdev->flags)) &&
7040 atomic_read(&rdev->nr_pending)==0) {
7041 if (mddev->pers->hot_remove_disk(
7042 mddev, rdev->raid_disk)==0) {
7043 char nm[20];
7044 sprintf(nm,"rd%d", rdev->raid_disk);
7045 sysfs_remove_link(&mddev->kobj, nm);
7046 rdev->raid_disk = -1;
7047 }
7048 }
7049
NeilBrowna8c42c72011-01-31 13:47:13 +11007050 if (mddev->degraded && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007051 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007052 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007053 !test_bit(In_sync, &rdev->flags) &&
7054 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007055 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007056 if (rdev->raid_disk < 0
7057 && !test_bit(Faulty, &rdev->flags)) {
7058 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007059 if (mddev->pers->
7060 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007061 char nm[20];
7062 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007063 if (sysfs_create_link(&mddev->kobj,
7064 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007065 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007066 spares++;
7067 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007068 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007069 } else
7070 break;
7071 }
NeilBrowndfc70642008-05-23 13:04:39 -07007072 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007073 }
7074 return spares;
7075}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007076
7077static void reap_sync_thread(mddev_t *mddev)
7078{
7079 mdk_rdev_t *rdev;
7080
7081 /* resync has finished, collect result */
7082 md_unregister_thread(mddev->sync_thread);
7083 mddev->sync_thread = NULL;
7084 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7085 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7086 /* success...*/
7087 /* activate any spares */
7088 if (mddev->pers->spare_active(mddev))
7089 sysfs_notify(&mddev->kobj, NULL,
7090 "degraded");
7091 }
7092 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7093 mddev->pers->finish_reshape)
7094 mddev->pers->finish_reshape(mddev);
7095 md_update_sb(mddev, 1);
7096
7097 /* if array is no-longer degraded, then any saved_raid_disk
7098 * information must be scrapped
7099 */
7100 if (!mddev->degraded)
7101 list_for_each_entry(rdev, &mddev->disks, same_set)
7102 rdev->saved_raid_disk = -1;
7103
7104 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7105 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7106 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7107 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7108 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7109 /* flag recovery needed just to double check */
7110 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7111 sysfs_notify_dirent_safe(mddev->sysfs_action);
7112 md_new_event(mddev);
7113}
7114
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115/*
7116 * This routine is regularly called by all per-raid-array threads to
7117 * deal with generic issues like resync and super-block update.
7118 * Raid personalities that don't have a thread (linear/raid0) do not
7119 * need this as they never do any recovery or update the superblock.
7120 *
7121 * It does not do any resync itself, but rather "forks" off other threads
7122 * to do that as needed.
7123 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7124 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007125 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 * and wakeups up this thread which will reap the thread and finish up.
7127 * This thread also removes any faulty devices (with nr_pending == 0).
7128 *
7129 * The overall approach is:
7130 * 1/ if the superblock needs updating, update it.
7131 * 2/ If a recovery thread is running, don't do anything else.
7132 * 3/ If recovery has finished, clean up, possibly marking spares active.
7133 * 4/ If there are any faulty devices, remove them.
7134 * 5/ If array is degraded, try to add spares devices
7135 * 6/ If array has spares or is not in-sync, start a resync thread.
7136 */
7137void md_check_recovery(mddev_t *mddev)
7138{
NeilBrown5f404022005-06-21 17:17:16 -07007139 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007140 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
7142 if (mddev->ro)
7143 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007144
7145 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007146 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007147 printk(KERN_INFO "md: %s in immediate safe mode\n",
7148 mdname(mddev));
7149 mddev->safemode = 2;
7150 }
7151 flush_signals(current);
7152 }
7153
NeilBrownc89a8ee2008-08-05 15:54:13 +10007154 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7155 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007157 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007159 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007160 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007161 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7162 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 ))
7164 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007165
NeilBrowndf5b89b2006-03-27 01:18:20 -08007166 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007167 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007168
NeilBrownc89a8ee2008-08-05 15:54:13 +10007169 if (mddev->ro) {
7170 /* Only thing we do on a ro array is remove
7171 * failed devices.
7172 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007173 mdk_rdev_t *rdev;
7174 list_for_each_entry(rdev, &mddev->disks, same_set)
7175 if (rdev->raid_disk >= 0 &&
7176 !test_bit(Blocked, &rdev->flags) &&
7177 test_bit(Faulty, &rdev->flags) &&
7178 atomic_read(&rdev->nr_pending)==0) {
7179 if (mddev->pers->hot_remove_disk(
7180 mddev, rdev->raid_disk)==0) {
7181 char nm[20];
7182 sprintf(nm,"rd%d", rdev->raid_disk);
7183 sysfs_remove_link(&mddev->kobj, nm);
7184 rdev->raid_disk = -1;
7185 }
7186 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007187 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7188 goto unlock;
7189 }
7190
NeilBrown31a59e32008-04-30 00:52:30 -07007191 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007192 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007193 spin_lock_irq(&mddev->write_lock);
7194 if (mddev->safemode &&
7195 !atomic_read(&mddev->writes_pending) &&
7196 !mddev->in_sync &&
7197 mddev->recovery_cp == MaxSector) {
7198 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007199 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007200 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007201 }
7202 if (mddev->safemode == 1)
7203 mddev->safemode = 0;
7204 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007205 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007206 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007207 }
NeilBrownfca4d842005-06-21 17:17:11 -07007208
NeilBrown850b2b42006-10-03 01:15:46 -07007209 if (mddev->flags)
7210 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007211
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7213 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7214 /* resync/recovery still happening */
7215 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7216 goto unlock;
7217 }
7218 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007219 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 goto unlock;
7221 }
Neil Brown72a23c22008-06-28 08:31:41 +10007222 /* Set RUNNING before clearing NEEDED to avoid
7223 * any transients in the value of "sync_action".
7224 */
7225 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7226 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007227 /* Clear some bits that don't mean anything, but
7228 * might be left set
7229 */
NeilBrown24dd4692005-11-08 21:39:26 -08007230 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7231 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007233 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7234 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 /* no recovery is running.
7236 * remove any failed drives, then
7237 * add spares if possible.
7238 * Spare are also removed and re-added, to allow
7239 * the personality to fail the re-add.
7240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007242 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007243 if (mddev->pers->check_reshape == NULL ||
7244 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007245 /* Cannot proceed */
7246 goto unlock;
7247 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007248 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007249 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007250 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7251 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007252 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007253 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007254 } else if (mddev->recovery_cp < MaxSector) {
7255 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007256 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007257 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7258 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007260
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007262 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7263 /* We are adding a device or devices to an array
7264 * which has the bitmap stored on all devices.
7265 * So make sure all bitmap pages get written
7266 */
7267 bitmap_write_all(mddev->bitmap);
7268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 mddev->sync_thread = md_register_thread(md_do_sync,
7270 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007271 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 if (!mddev->sync_thread) {
7273 printk(KERN_ERR "%s: could not start resync"
7274 " thread...\n",
7275 mdname(mddev));
7276 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007277 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7278 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7279 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7280 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7281 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007282 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007284 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007285 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286 }
7287 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007288 if (!mddev->sync_thread) {
7289 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7290 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7291 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007292 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007293 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 mddev_unlock(mddev);
7296 }
7297}
7298
Dan Williams6bfe0b42008-04-30 00:52:32 -07007299void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7300{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007301 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007302 wait_event_timeout(rdev->blocked_wait,
7303 !test_bit(Blocked, &rdev->flags),
7304 msecs_to_jiffies(5000));
7305 rdev_dec_pending(rdev, mddev);
7306}
7307EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7308
Adrian Bunk75c96f82005-05-05 16:16:09 -07007309static int md_notify_reboot(struct notifier_block *this,
7310 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311{
7312 struct list_head *tmp;
7313 mddev_t *mddev;
7314
7315 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7316
7317 printk(KERN_INFO "md: stopping all md devices.\n");
7318
NeilBrown29ac4aa2008-02-06 01:39:58 -08007319 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007320 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007321 /* Force a switch to readonly even array
7322 * appears to still be in use. Hence
7323 * the '100'.
7324 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007325 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007326 mddev_unlock(mddev);
7327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 /*
7329 * certain more exotic SCSI devices are known to be
7330 * volatile wrt too early system reboots. While the
7331 * right place to handle this issue is the given
7332 * driver, we do want to have a safe RAID driver ...
7333 */
7334 mdelay(1000*1);
7335 }
7336 return NOTIFY_DONE;
7337}
7338
Adrian Bunk75c96f82005-05-05 16:16:09 -07007339static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 .notifier_call = md_notify_reboot,
7341 .next = NULL,
7342 .priority = INT_MAX, /* before any real devices */
7343};
7344
7345static void md_geninit(void)
7346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7348
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007349 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350}
7351
Adrian Bunk75c96f82005-05-05 16:16:09 -07007352static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353{
Tejun Heoe804ac72010-10-15 15:36:08 +02007354 int ret = -ENOMEM;
7355
Tejun Heoada609e2011-01-25 14:35:54 +01007356 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02007357 if (!md_wq)
7358 goto err_wq;
7359
7360 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7361 if (!md_misc_wq)
7362 goto err_misc_wq;
7363
7364 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7365 goto err_md;
7366
7367 if ((ret = register_blkdev(0, "mdp")) < 0)
7368 goto err_mdp;
7369 mdp_major = ret;
7370
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007371 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007372 md_probe, NULL, NULL);
7373 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 md_probe, NULL, NULL);
7375
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007377 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378
7379 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381
Tejun Heoe804ac72010-10-15 15:36:08 +02007382err_mdp:
7383 unregister_blkdev(MD_MAJOR, "md");
7384err_md:
7385 destroy_workqueue(md_misc_wq);
7386err_misc_wq:
7387 destroy_workqueue(md_wq);
7388err_wq:
7389 return ret;
7390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
7392#ifndef MODULE
7393
7394/*
7395 * Searches all registered partitions for autorun RAID arrays
7396 * at boot time.
7397 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007398
7399static LIST_HEAD(all_detected_devices);
7400struct detected_devices_node {
7401 struct list_head list;
7402 dev_t dev;
7403};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404
7405void md_autodetect_dev(dev_t dev)
7406{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007407 struct detected_devices_node *node_detected_dev;
7408
7409 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7410 if (node_detected_dev) {
7411 node_detected_dev->dev = dev;
7412 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7413 } else {
7414 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7415 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417}
7418
7419
7420static void autostart_arrays(int part)
7421{
7422 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007423 struct detected_devices_node *node_detected_dev;
7424 dev_t dev;
7425 int i_scanned, i_passed;
7426
7427 i_scanned = 0;
7428 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007429
7430 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7431
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007432 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7433 i_scanned++;
7434 node_detected_dev = list_entry(all_detected_devices.next,
7435 struct detected_devices_node, list);
7436 list_del(&node_detected_dev->list);
7437 dev = node_detected_dev->dev;
7438 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007439 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 if (IS_ERR(rdev))
7441 continue;
7442
NeilBrownb2d444d2005-11-08 21:39:31 -08007443 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 MD_BUG();
7445 continue;
7446 }
NeilBrownd0fae182008-03-04 14:29:31 -08007447 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007449 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007451
7452 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7453 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454
7455 autorun_devices(part);
7456}
7457
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007458#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459
7460static __exit void md_exit(void)
7461{
7462 mddev_t *mddev;
7463 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007464
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007465 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007466 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007468 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 unregister_blkdev(mdp_major, "mdp");
7470 unregister_reboot_notifier(&md_notifier);
7471 unregister_sysctl_table(raid_table_header);
7472 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007473 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007475 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007477 destroy_workqueue(md_misc_wq);
7478 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479}
7480
Dan Williams685784a2007-07-09 11:56:42 -07007481subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482module_exit(md_exit)
7483
NeilBrownf91de922005-11-08 21:39:36 -08007484static int get_ro(char *buffer, struct kernel_param *kp)
7485{
7486 return sprintf(buffer, "%d", start_readonly);
7487}
7488static int set_ro(const char *val, struct kernel_param *kp)
7489{
7490 char *e;
7491 int num = simple_strtoul(val, &e, 10);
7492 if (*val && (*e == '\0' || *e == '\n')) {
7493 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007494 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007495 }
7496 return -EINVAL;
7497}
7498
NeilBrown80ca3a42006-07-10 04:44:18 -07007499module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7500module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007501
NeilBrownefeb53c2009-01-09 08:31:10 +11007502module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007503
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504EXPORT_SYMBOL(register_md_personality);
7505EXPORT_SYMBOL(unregister_md_personality);
7506EXPORT_SYMBOL(md_error);
7507EXPORT_SYMBOL(md_done_sync);
7508EXPORT_SYMBOL(md_write_start);
7509EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510EXPORT_SYMBOL(md_register_thread);
7511EXPORT_SYMBOL(md_unregister_thread);
7512EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513EXPORT_SYMBOL(md_check_recovery);
7514MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007515MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007516MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007517MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);