blob: 7f5ffc51c28e756abeb22b9f8405b1ffdf5fbe9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080041#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070042#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080043#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110044#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
47#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define DEBUG 0
58#define dprintk(x...) ((void)(DEBUG && printk(x)))
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110061static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
NeilBrown2604b702006-01-06 00:20:36 -080064static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_SPINLOCK(pers_lock);
66
Adrian Bunk5e563412006-06-26 00:27:42 -070067static void md_print_devices(void);
68
Bernd Schubert90b08712008-05-23 13:04:38 -070069static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020070static struct workqueue_struct *md_wq;
71static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070072
Adrian Bunk5e563412006-06-26 00:27:42 -070073#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Robert Becker1e509152009-12-14 12:49:58 +110076 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
79 */
80#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070085 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080091 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080096static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700115 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800116 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 },
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800123 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800125 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128static ctl_table raid_dir_table[] = {
129 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .procname = "raid",
131 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700132 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .child = raid_table,
134 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800135 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138static ctl_table raid_root_table[] = {
139 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .procname = "dev",
141 .maxlen = 0,
142 .mode = 0555,
143 .child = raid_dir_table,
144 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800145 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700148static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
NeilBrownf91de922005-11-08 21:39:36 -0800150static int start_readonly;
151
NeilBrowna167f662010-10-26 18:31:13 +1100152/* bio_clone_mddev
153 * like bio_clone, but with a local bio set
154 */
155
156static void mddev_bio_destructor(struct bio *bio)
157{
158 mddev_t *mddev, **mddevp;
159
160 mddevp = (void*)bio;
161 mddev = mddevp[-1];
162
163 bio_free(bio, mddev->bio_set);
164}
165
166struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
167 mddev_t *mddev)
168{
169 struct bio *b;
170 mddev_t **mddevp;
171
172 if (!mddev || !mddev->bio_set)
173 return bio_alloc(gfp_mask, nr_iovecs);
174
175 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
176 mddev->bio_set);
177 if (!b)
178 return NULL;
179 mddevp = (void*)b;
180 mddevp[-1] = mddev;
181 b->bi_destructor = mddev_bio_destructor;
182 return b;
183}
184EXPORT_SYMBOL_GPL(bio_alloc_mddev);
185
186struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
187 mddev_t *mddev)
188{
189 struct bio *b;
190 mddev_t **mddevp;
191
192 if (!mddev || !mddev->bio_set)
193 return bio_clone(bio, gfp_mask);
194
195 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
196 mddev->bio_set);
197 if (!b)
198 return NULL;
199 mddevp = (void*)b;
200 mddevp[-1] = mddev;
201 b->bi_destructor = mddev_bio_destructor;
202 __bio_clone(b, bio);
203 if (bio_integrity(bio)) {
204 int ret;
205
206 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
207
208 if (ret < 0) {
209 bio_put(b);
210 return NULL;
211 }
212 }
213
214 return b;
215}
216EXPORT_SYMBOL_GPL(bio_clone_mddev);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
NeilBrownd7603b72006-01-06 00:20:30 -0800219 * We have a system wide 'event count' that is incremented
220 * on any 'interesting' event, and readers of /proc/mdstat
221 * can use 'poll' or 'select' to find out when the event
222 * count increases.
223 *
224 * Events are:
225 * start array, stop array, error, add device, remove device,
226 * start build, activate spare
227 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800228static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800229static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800230void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800231{
232 atomic_inc(&md_event_count);
233 wake_up(&md_event_waiters);
234}
NeilBrown29269552006-03-27 01:18:10 -0800235EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800236
NeilBrownc331eb02006-05-30 21:27:13 -0700237/* Alternate version that can be called from interrupts
238 * when calling sysfs_notify isn't needed.
239 */
Adrian Bunk05381952006-06-26 00:28:01 -0700240static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700241{
242 atomic_inc(&md_event_count);
243 wake_up(&md_event_waiters);
244}
245
NeilBrownd7603b72006-01-06 00:20:30 -0800246/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * Enables to iterate over all existing md arrays
248 * all_mddevs_lock protects this list.
249 */
250static LIST_HEAD(all_mddevs);
251static DEFINE_SPINLOCK(all_mddevs_lock);
252
253
254/*
255 * iterates through all used mddevs in the system.
256 * We take care to grab the all_mddevs_lock whenever navigating
257 * the list, and to always hold a refcount when unlocked.
258 * Any code which breaks out of this loop while own
259 * a reference to the current mddev and must mddev_put it.
260 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800261#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 \
263 for (({ spin_lock(&all_mddevs_lock); \
264 tmp = all_mddevs.next; \
265 mddev = NULL;}); \
266 ({ if (tmp != &all_mddevs) \
267 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
268 spin_unlock(&all_mddevs_lock); \
269 if (mddev) mddev_put(mddev); \
270 mddev = list_entry(tmp, mddev_t, all_mddevs); \
271 tmp != &all_mddevs;}); \
272 ({ spin_lock(&all_mddevs_lock); \
273 tmp = tmp->next;}) \
274 )
275
276
NeilBrown409c57f2009-03-31 14:39:39 +1100277/* Rather than calling directly into the personality make_request function,
278 * IO requests come here first so that we can check if the device is
279 * being suspended pending a reconfiguration.
280 * We hold a refcount over the call to ->make_request. By the time that
281 * call has finished, the bio has been linked into some internal structure
282 * and so is visible to ->quiesce(), so we don't need the refcount any more.
283 */
284static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
NeilBrown49077322010-03-25 16:20:56 +1100286 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100287 mddev_t *mddev = q->queuedata;
288 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100289 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500290 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100291
NeilBrown0ca69882011-01-14 09:14:33 +1100292 if (mddev == NULL || mddev->pers == NULL
293 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100294 bio_io_error(bio);
295 return 0;
296 }
NeilBrown0ca69882011-01-14 09:14:33 +1100297 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100298 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200299 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100300 DEFINE_WAIT(__wait);
301 for (;;) {
302 prepare_to_wait(&mddev->sb_wait, &__wait,
303 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200304 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100305 break;
306 rcu_read_unlock();
307 schedule();
308 rcu_read_lock();
309 }
310 finish_wait(&mddev->sb_wait, &__wait);
311 }
312 atomic_inc(&mddev->active_io);
313 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100314
Chris Masone91ece52011-02-07 19:21:48 -0500315 /*
316 * save the sectors now since our bio can
317 * go away inside make_request
318 */
319 sectors = bio_sectors(bio);
NeilBrown21a52c62010-04-01 15:02:13 +1100320 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100321
322 cpu = part_stat_lock();
323 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500324 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100325 part_stat_unlock();
326
NeilBrown409c57f2009-03-31 14:39:39 +1100327 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
328 wake_up(&mddev->sb_wait);
329
330 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
NeilBrown9e35b992010-04-06 14:23:02 +1000333/* mddev_suspend makes sure no new requests are submitted
334 * to the device, and that any requests that have been submitted
335 * are completely handled.
336 * Once ->stop is called and completes, the module will be completely
337 * unused.
338 */
NeilBrown390ee602010-06-01 19:37:27 +1000339void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100340{
341 BUG_ON(mddev->suspended);
342 mddev->suspended = 1;
343 synchronize_rcu();
344 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
345 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100346}
NeilBrown390ee602010-06-01 19:37:27 +1000347EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100348
NeilBrown390ee602010-06-01 19:37:27 +1000349void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100350{
351 mddev->suspended = 0;
352 wake_up(&mddev->sb_wait);
353 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500354
355 md_wakeup_thread(mddev->thread);
356 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100357}
NeilBrown390ee602010-06-01 19:37:27 +1000358EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100359
NeilBrown3fa841d2009-09-23 18:10:29 +1000360int mddev_congested(mddev_t *mddev, int bits)
361{
362 return mddev->suspended;
363}
364EXPORT_SYMBOL(mddev_congested);
365
NeilBrowna2826aa2009-12-14 12:49:49 +1100366/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200367 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100368 */
369
Tejun Heoe9c74692010-09-03 11:56:18 +0200370static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100371{
372 mdk_rdev_t *rdev = bio->bi_private;
373 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100374
375 rdev_dec_pending(rdev, mddev);
376
377 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200378 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200379 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100380 }
381 bio_put(bio);
382}
383
NeilBrowna7a07e62010-12-09 16:04:25 +1100384static void md_submit_flush_data(struct work_struct *ws);
385
NeilBrowna035fc32010-12-09 16:17:51 +1100386static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100387{
NeilBrowna035fc32010-12-09 16:17:51 +1100388 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100389 mdk_rdev_t *rdev;
390
NeilBrowna7a07e62010-12-09 16:04:25 +1100391 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
392 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100393 rcu_read_lock();
394 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
395 if (rdev->raid_disk >= 0 &&
396 !test_bit(Faulty, &rdev->flags)) {
397 /* Take two references, one is dropped
398 * when request finishes, one after
399 * we reclaim rcu_read_lock
400 */
401 struct bio *bi;
402 atomic_inc(&rdev->nr_pending);
403 atomic_inc(&rdev->nr_pending);
404 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100405 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200406 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100407 bi->bi_private = rdev;
408 bi->bi_bdev = rdev->bdev;
409 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200410 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100411 rcu_read_lock();
412 rdev_dec_pending(rdev, mddev);
413 }
414 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100415 if (atomic_dec_and_test(&mddev->flush_pending))
416 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100417}
418
Tejun Heoe9c74692010-09-03 11:56:18 +0200419static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100420{
Tejun Heoe9c74692010-09-03 11:56:18 +0200421 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
422 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100423
Tejun Heoe9c74692010-09-03 11:56:18 +0200424 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100425 /* an empty barrier - all done */
426 bio_endio(bio, 0);
427 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200428 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100429 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100430 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431 }
NeilBrown2b74e122010-12-09 15:59:01 +1100432
433 mddev->flush_bio = NULL;
434 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100435}
436
Tejun Heoe9c74692010-09-03 11:56:18 +0200437void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100438{
439 spin_lock_irq(&mddev->write_lock);
440 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200441 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100442 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200443 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 spin_unlock_irq(&mddev->write_lock);
445
NeilBrowna035fc32010-12-09 16:17:51 +1100446 INIT_WORK(&mddev->flush_work, submit_flushes);
447 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100448}
Tejun Heoe9c74692010-09-03 11:56:18 +0200449EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100450
NeilBrown97658cd2011-04-18 18:25:42 +1000451/* Support for plugging.
452 * This mirrors the plugging support in request_queue, but does not
453 * require having a whole queue or request structures.
454 * We allocate an md_plug_cb for each md device and each thread it gets
455 * plugged on. This links tot the private plug_handle structure in the
456 * personality data where we keep a count of the number of outstanding
457 * plugs so other code can see if a plug is active.
458 */
459struct md_plug_cb {
460 struct blk_plug_cb cb;
461 mddev_t *mddev;
462};
463
464static void plugger_unplug(struct blk_plug_cb *cb)
465{
466 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
467 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
468 md_wakeup_thread(mdcb->mddev->thread);
469 kfree(mdcb);
470}
471
472/* Check that an unplug wakeup will come shortly.
473 * If not, wakeup the md thread immediately
474 */
475int mddev_check_plugged(mddev_t *mddev)
476{
477 struct blk_plug *plug = current->plug;
478 struct md_plug_cb *mdcb;
479
480 if (!plug)
481 return 0;
482
483 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
484 if (mdcb->cb.callback == plugger_unplug &&
485 mdcb->mddev == mddev) {
486 /* Already on the list, move to top */
487 if (mdcb != list_first_entry(&plug->cb_list,
488 struct md_plug_cb,
489 cb.list))
490 list_move(&mdcb->cb.list, &plug->cb_list);
491 return 1;
492 }
493 }
494 /* Not currently on the callback list */
495 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
496 if (!mdcb)
497 return 0;
498
499 mdcb->mddev = mddev;
500 mdcb->cb.callback = plugger_unplug;
501 atomic_inc(&mddev->plug_cnt);
502 list_add(&mdcb->cb.list, &plug->cb_list);
503 return 1;
504}
505EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static inline mddev_t *mddev_get(mddev_t *mddev)
508{
509 atomic_inc(&mddev->active);
510 return mddev;
511}
512
Dan Williams5fd3a172009-03-04 00:57:25 -0700513static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static void mddev_put(mddev_t *mddev)
516{
NeilBrowna167f662010-10-26 18:31:13 +1100517 struct bio_set *bs = NULL;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
520 return;
NeilBrownd3374822009-01-09 08:31:10 +1100521 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100522 mddev->ctime == 0 && !mddev->hold_active) {
523 /* Array is not configured at all, and not held active,
524 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100526 bs = mddev->bio_set;
527 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100528 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200529 /* We did a probe so need to clean up. Call
530 * queue_work inside the spinlock so that
531 * flush_workqueue() after mddev_find will
532 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100533 */
534 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200535 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100536 } else
537 kfree(mddev);
538 }
539 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100540 if (bs)
541 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
NeilBrown390ee602010-06-01 19:37:27 +1000544void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100545{
546 mutex_init(&mddev->open_mutex);
547 mutex_init(&mddev->reconfig_mutex);
548 mutex_init(&mddev->bitmap_info.mutex);
549 INIT_LIST_HEAD(&mddev->disks);
550 INIT_LIST_HEAD(&mddev->all_mddevs);
551 init_timer(&mddev->safemode_timer);
552 atomic_set(&mddev->active, 1);
553 atomic_set(&mddev->openers, 0);
554 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000555 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100556 spin_lock_init(&mddev->write_lock);
557 atomic_set(&mddev->flush_pending, 0);
558 init_waitqueue_head(&mddev->sb_wait);
559 init_waitqueue_head(&mddev->recovery_wait);
560 mddev->reshape_position = MaxSector;
561 mddev->resync_min = 0;
562 mddev->resync_max = MaxSector;
563 mddev->level = LEVEL_NONE;
564}
NeilBrown390ee602010-06-01 19:37:27 +1000565EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static mddev_t * mddev_find(dev_t unit)
568{
569 mddev_t *mddev, *new = NULL;
570
NeilBrown8f5f02c2011-02-16 13:58:51 +1100571 if (unit && MAJOR(unit) != MD_MAJOR)
572 unit &= ~((1<<MdpMinorShift)-1);
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 retry:
575 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
NeilBrownefeb53c2009-01-09 08:31:10 +1100577 if (unit) {
578 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
579 if (mddev->unit == unit) {
580 mddev_get(mddev);
581 spin_unlock(&all_mddevs_lock);
582 kfree(new);
583 return mddev;
584 }
585
586 if (new) {
587 list_add(&new->all_mddevs, &all_mddevs);
588 spin_unlock(&all_mddevs_lock);
589 new->hold_active = UNTIL_IOCTL;
590 return new;
591 }
592 } else if (new) {
593 /* find an unused unit number */
594 static int next_minor = 512;
595 int start = next_minor;
596 int is_free = 0;
597 int dev = 0;
598 while (!is_free) {
599 dev = MKDEV(MD_MAJOR, next_minor);
600 next_minor++;
601 if (next_minor > MINORMASK)
602 next_minor = 0;
603 if (next_minor == start) {
604 /* Oh dear, all in use. */
605 spin_unlock(&all_mddevs_lock);
606 kfree(new);
607 return NULL;
608 }
609
610 is_free = 1;
611 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
612 if (mddev->unit == dev) {
613 is_free = 0;
614 break;
615 }
616 }
617 new->unit = dev;
618 new->md_minor = MINOR(dev);
619 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 list_add(&new->all_mddevs, &all_mddevs);
621 spin_unlock(&all_mddevs_lock);
622 return new;
623 }
624 spin_unlock(&all_mddevs_lock);
625
NeilBrown9ffae0c2006-01-06 00:20:32 -0800626 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!new)
628 return NULL;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 new->unit = unit;
631 if (MAJOR(unit) == MD_MAJOR)
632 new->md_minor = MINOR(unit);
633 else
634 new->md_minor = MINOR(unit) >> MdpMinorShift;
635
NeilBrownfafd7fb2010-04-01 15:55:30 +1100636 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 goto retry;
639}
640
641static inline int mddev_lock(mddev_t * mddev)
642{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800643 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Dan Williamsb522adc2009-03-31 15:00:31 +1100646static inline int mddev_is_locked(mddev_t *mddev)
647{
648 return mutex_is_locked(&mddev->reconfig_mutex);
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static inline int mddev_trylock(mddev_t * mddev)
652{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800653 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
NeilBrownb6eb1272010-04-15 10:13:47 +1000656static struct attribute_group md_redundancy_group;
657
NeilBrowna64c8762010-04-14 17:15:37 +1000658static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
NeilBrowna64c8762010-04-14 17:15:37 +1000660 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000661 /* These cannot be removed under reconfig_mutex as
662 * an access to the files will try to take reconfig_mutex
663 * while holding the file unremovable, which leads to
664 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000665 * So hold set sysfs_active while the remove in happeing,
666 * and anything else which might set ->to_remove or my
667 * otherwise change the sysfs namespace will fail with
668 * -EBUSY if sysfs_active is still set.
669 * We set sysfs_active under reconfig_mutex and elsewhere
670 * test it under the same mutex to ensure its correct value
671 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000672 */
NeilBrowna64c8762010-04-14 17:15:37 +1000673 struct attribute_group *to_remove = mddev->to_remove;
674 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000675 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000676 mutex_unlock(&mddev->reconfig_mutex);
677
NeilBrown00bcb4a2010-06-01 19:37:23 +1000678 if (mddev->kobj.sd) {
679 if (to_remove != &md_redundancy_group)
680 sysfs_remove_group(&mddev->kobj, to_remove);
681 if (mddev->pers == NULL ||
682 mddev->pers->sync_request == NULL) {
683 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
684 if (mddev->sysfs_action)
685 sysfs_put(mddev->sysfs_action);
686 mddev->sysfs_action = NULL;
687 }
NeilBrowna64c8762010-04-14 17:15:37 +1000688 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000689 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000690 } else
691 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
NeilBrown005eca52005-08-22 13:11:08 -0700693 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
NeilBrown2989ddb2006-01-06 00:20:43 -0800696static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100698 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100700 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (rdev->desc_nr == nr)
702 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return NULL;
705}
706
707static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 mdk_rdev_t *rdev;
710
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100711 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (rdev->bdev->bd_dev == dev)
713 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return NULL;
716}
717
NeilBrownd9d166c2006-01-06 00:20:51 -0800718static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800719{
720 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800721 list_for_each_entry(pers, &pers_list, list) {
722 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800723 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800724 if (strcmp(pers->name, clevel)==0)
725 return pers;
726 }
NeilBrown2604b702006-01-06 00:20:36 -0800727 return NULL;
728}
729
Andre Nollb73df2d2008-07-11 22:02:23 +1000730/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100731static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100733 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000734 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static int alloc_disk_sb(mdk_rdev_t * rdev)
738{
739 if (rdev->sb_page)
740 MD_BUG();
741
742 rdev->sb_page = alloc_page(GFP_KERNEL);
743 if (!rdev->sb_page) {
744 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000745 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
748 return 0;
749}
750
751static void free_disk_sb(mdk_rdev_t * rdev)
752{
753 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800754 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 rdev->sb_loaded = 0;
756 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000757 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100758 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760}
761
762
NeilBrown6712ecf2007-09-27 12:47:43 +0200763static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700764{
765 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800766 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700767
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700768 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
769 printk("md: super_written gets error=%d, uptodate=%d\n",
770 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
771 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800772 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700773 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700774
NeilBrowna9701a32005-11-08 21:39:34 -0800775 if (atomic_dec_and_test(&mddev->pending_writes))
776 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700777 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700778}
779
780void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
781 sector_t sector, int size, struct page *page)
782{
783 /* write first size bytes of page to sector of rdev
784 * Increment mddev->pending_writes before returning
785 * and decrement it on completion, waking up sb_wait
786 * if zero is reached.
787 * If an error occurred, call md_error
788 */
NeilBrowna167f662010-10-26 18:31:13 +1100789 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700790
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100791 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700792 bio->bi_sector = sector;
793 bio_add_page(bio, page, size, 0);
794 bio->bi_private = rdev;
795 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800796
NeilBrown7bfa19f2005-06-21 17:17:28 -0700797 atomic_inc(&mddev->pending_writes);
Jens Axboe721a9602011-03-09 11:56:30 +0100798 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800799}
800
801void md_super_wait(mddev_t *mddev)
802{
Tejun Heoe9c74692010-09-03 11:56:18 +0200803 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800804 DEFINE_WAIT(wq);
805 for(;;) {
806 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
807 if (atomic_read(&mddev->pending_writes)==0)
808 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800809 schedule();
810 }
811 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700812}
813
NeilBrown6712ecf2007-09-27 12:47:43 +0200814static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
NeilBrown2b193362010-10-27 15:16:40 +1100819int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100820 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
NeilBrowna167f662010-10-26 18:31:13 +1100822 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct completion event;
824 int ret;
825
Jens Axboe721a9602011-03-09 11:56:30 +0100826 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100828 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
829 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100830 if (metadata_op)
831 bio->bi_sector = sector + rdev->sb_start;
832 else
833 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 bio_add_page(bio, page, size, 0);
835 init_completion(&event);
836 bio->bi_private = &event;
837 bio->bi_end_io = bi_complete;
838 submit_bio(rw, bio);
839 wait_for_completion(&event);
840
841 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
842 bio_put(bio);
843 return ret;
844}
NeilBrowna8745db2006-01-06 00:20:34 -0800845EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
NeilBrown0002b272005-09-09 16:23:53 -0700847static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 char b[BDEVNAME_SIZE];
850 if (!rdev->sb_page) {
851 MD_BUG();
852 return -EINVAL;
853 }
854 if (rdev->sb_loaded)
855 return 0;
856
857
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100858 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto fail;
860 rdev->sb_loaded = 1;
861 return 0;
862
863fail:
864 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
865 bdevname(rdev->bdev,b));
866 return -EINVAL;
867}
868
869static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
870{
Andre Noll05710462008-07-11 22:02:20 +1000871 return sb1->set_uuid0 == sb2->set_uuid0 &&
872 sb1->set_uuid1 == sb2->set_uuid1 &&
873 sb1->set_uuid2 == sb2->set_uuid2 &&
874 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
878{
879 int ret;
880 mdp_super_t *tmp1, *tmp2;
881
882 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
883 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
884
885 if (!tmp1 || !tmp2) {
886 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100887 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto abort;
889 }
890
891 *tmp1 = *sb1;
892 *tmp2 = *sb2;
893
894 /*
895 * nr_disks is not constant
896 */
897 tmp1->nr_disks = 0;
898 tmp2->nr_disks = 0;
899
Andre Nollce0c8e02008-07-11 22:02:20 +1000900 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700902 kfree(tmp1);
903 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return ret;
905}
906
NeilBrown4d167f02007-05-09 02:35:37 -0700907
908static u32 md_csum_fold(u32 csum)
909{
910 csum = (csum & 0xffff) + (csum >> 16);
911 return (csum & 0xffff) + (csum >> 16);
912}
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914static unsigned int calc_sb_csum(mdp_super_t * sb)
915{
NeilBrown4d167f02007-05-09 02:35:37 -0700916 u64 newcsum = 0;
917 u32 *sb32 = (u32*)sb;
918 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unsigned int disk_csum, csum;
920
921 disk_csum = sb->sb_csum;
922 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700923
924 for (i = 0; i < MD_SB_BYTES/4 ; i++)
925 newcsum += sb32[i];
926 csum = (newcsum & 0xffffffff) + (newcsum>>32);
927
928
929#ifdef CONFIG_ALPHA
930 /* This used to use csum_partial, which was wrong for several
931 * reasons including that different results are returned on
932 * different architectures. It isn't critical that we get exactly
933 * the same return value as before (we always csum_fold before
934 * testing, and that removes any differences). However as we
935 * know that csum_partial always returned a 16bit value on
936 * alphas, do a fold to maximise conformity to previous behaviour.
937 */
938 sb->sb_csum = md_csum_fold(disk_csum);
939#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return csum;
943}
944
945
946/*
947 * Handle superblock details.
948 * We want to be able to handle multiple superblock formats
949 * so we have a common interface to them all, and an array of
950 * different handlers.
951 * We rely on user-space to write the initial superblock, and support
952 * reading and updating of superblocks.
953 * Interface methods are:
954 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
955 * loads and validates a superblock on dev.
956 * if refdev != NULL, compare superblocks on both devices
957 * Return:
958 * 0 - dev has a superblock that is compatible with refdev
959 * 1 - dev has a superblock that is compatible and newer than refdev
960 * so dev should be used as the refdev in future
961 * -EINVAL superblock incompatible or invalid
962 * -othererror e.g. -EIO
963 *
964 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
965 * Verify that dev is acceptable into mddev.
966 * The first time, mddev->raid_disks will be 0, and data from
967 * dev should be merged in. Subsequent calls check that dev
968 * is new enough. Return 0 or -EINVAL
969 *
970 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
971 * Update the superblock for rdev with data in mddev
972 * This does not write to disc.
973 *
974 */
975
976struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000977 char *name;
978 struct module *owner;
979 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
980 int minor_version);
981 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
982 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
983 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000984 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985};
986
987/*
Andre Noll0894cc32009-06-18 08:49:23 +1000988 * Check that the given mddev has no bitmap.
989 *
990 * This function is called from the run method of all personalities that do not
991 * support bitmaps. It prints an error message and returns non-zero if mddev
992 * has a bitmap. Otherwise, it returns 0.
993 *
994 */
995int md_check_no_bitmap(mddev_t *mddev)
996{
NeilBrownc3d97142009-12-14 12:49:52 +1100997 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000998 return 0;
999 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1000 mdname(mddev), mddev->pers->name);
1001 return 1;
1002}
1003EXPORT_SYMBOL(md_check_no_bitmap);
1004
1005/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * load_super for 0.90.0
1007 */
1008static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1009{
1010 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1011 mdp_super_t *sb;
1012 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /*
Andre Noll0f420352008-07-11 22:02:23 +10001015 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * it's at the end of the disk.
1017 *
1018 * It also happens to be a multiple of 4Kb.
1019 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001020 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
NeilBrown0002b272005-09-09 16:23:53 -07001022 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (ret) return ret;
1024
1025 ret = -EINVAL;
1026
1027 bdevname(rdev->bdev, b);
Namhyung Kim65a06f02011-07-27 11:00:36 +10001028 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (sb->md_magic != MD_SB_MAGIC) {
1031 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1032 b);
1033 goto abort;
1034 }
1035
1036 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001037 sb->minor_version < 90 ||
1038 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1040 sb->major_version, sb->minor_version,
1041 b);
1042 goto abort;
1043 }
1044
1045 if (sb->raid_disks <= 0)
1046 goto abort;
1047
NeilBrown4d167f02007-05-09 02:35:37 -07001048 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1050 b);
1051 goto abort;
1052 }
1053
1054 rdev->preferred_minor = sb->md_minor;
1055 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001056 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (sb->level == LEVEL_MULTIPATH)
1059 rdev->desc_nr = -1;
1060 else
1061 rdev->desc_nr = sb->this_disk.number;
1062
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001063 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001065 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001067 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!uuid_equal(refsb, sb)) {
1069 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1070 b, bdevname(refdev->bdev,b2));
1071 goto abort;
1072 }
1073 if (!sb_equal(refsb, sb)) {
1074 printk(KERN_WARNING "md: %s has same UUID"
1075 " but different superblock to %s\n",
1076 b, bdevname(refdev->bdev, b2));
1077 goto abort;
1078 }
1079 ev1 = md_event(sb);
1080 ev2 = md_event(refsb);
1081 if (ev1 > ev2)
1082 ret = 1;
1083 else
1084 ret = 0;
1085 }
NeilBrown8190e752009-06-18 08:48:58 +10001086 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Andre Nolldd8ac332009-03-31 14:33:13 +11001088 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001089 /* "this cannot possibly happen" ... */
1090 ret = -EINVAL;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 abort:
1093 return ret;
1094}
1095
1096/*
1097 * validate_super for 0.90.0
1098 */
1099static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1100{
1101 mdp_disk_t *desc;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001102 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001103 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrown41158c72005-06-21 17:17:25 -07001105 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001106 clear_bit(Faulty, &rdev->flags);
1107 clear_bit(In_sync, &rdev->flags);
1108 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (mddev->raid_disks == 0) {
1111 mddev->major_version = 0;
1112 mddev->minor_version = sb->minor_version;
1113 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001114 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001115 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 mddev->ctime = sb->ctime;
1117 mddev->utime = sb->utime;
1118 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001119 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 mddev->layout = sb->layout;
1121 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001122 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001123 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001124 mddev->bitmap_info.offset = 0;
1125 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
NeilBrownf6705572006-03-27 01:18:11 -08001127 if (mddev->minor_version >= 91) {
1128 mddev->reshape_position = sb->reshape_position;
1129 mddev->delta_disks = sb->delta_disks;
1130 mddev->new_level = sb->new_level;
1131 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001132 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001133 } else {
1134 mddev->reshape_position = MaxSector;
1135 mddev->delta_disks = 0;
1136 mddev->new_level = mddev->level;
1137 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001138 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001139 }
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (sb->state & (1<<MD_SB_CLEAN))
1142 mddev->recovery_cp = MaxSector;
1143 else {
1144 if (sb->events_hi == sb->cp_events_hi &&
1145 sb->events_lo == sb->cp_events_lo) {
1146 mddev->recovery_cp = sb->recovery_cp;
1147 } else
1148 mddev->recovery_cp = 0;
1149 }
1150
1151 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1152 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1153 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1154 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1155
1156 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001157
1158 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001159 mddev->bitmap_info.file == NULL)
1160 mddev->bitmap_info.offset =
1161 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001162
NeilBrown41158c72005-06-21 17:17:25 -07001163 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001164 /* Insist on good event counter while assembling, except
1165 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001167 if (sb->disks[rdev->desc_nr].state & (
1168 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1169 if (ev1 < mddev->events)
1170 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001171 } else if (mddev->bitmap) {
1172 /* if adding to array with a bitmap, then we can accept an
1173 * older device ... but not too old.
1174 */
NeilBrown41158c72005-06-21 17:17:25 -07001175 if (ev1 < mddev->bitmap->events_cleared)
1176 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001177 } else {
1178 if (ev1 < mddev->events)
1179 /* just a hot-add of a new device, leave raid_disk at -1 */
1180 return 0;
1181 }
NeilBrown41158c72005-06-21 17:17:25 -07001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 desc = sb->disks + rdev->desc_nr;
1185
1186 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001187 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001188 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1189 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001190 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001192 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1193 /* active but not in sync implies recovery up to
1194 * reshape position. We don't know exactly where
1195 * that is, so set to zero for now */
1196 if (mddev->minor_version >= 91) {
1197 rdev->recovery_offset = 0;
1198 rdev->raid_disk = desc->raid_disk;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001201 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1202 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001203 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001204 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
1207
1208/*
1209 * sync_super for 0.90.0
1210 */
1211static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1212{
1213 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 mdk_rdev_t *rdev2;
1215 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 /* make rdev->sb match mddev data..
1219 *
1220 * 1/ zero out disks
1221 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1222 * 3/ any empty disks < next_spare become removed
1223 *
1224 * disks[0] gets initialised to REMOVED because
1225 * we cannot be sure from other fields if it has
1226 * been initialised or not.
1227 */
1228 int i;
1229 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1230
NeilBrown61181562005-09-09 16:24:02 -07001231 rdev->sb_size = MD_SB_BYTES;
1232
Namhyung Kim65a06f02011-07-27 11:00:36 +10001233 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 memset(sb, 0, sizeof(*sb));
1236
1237 sb->md_magic = MD_SB_MAGIC;
1238 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 sb->patch_version = mddev->patch_version;
1240 sb->gvalid_words = 0; /* ignored */
1241 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1242 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1243 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1244 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1245
1246 sb->ctime = mddev->ctime;
1247 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001248 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->raid_disks = mddev->raid_disks;
1250 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001251 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 sb->utime = mddev->utime;
1253 sb->state = 0;
1254 sb->events_hi = (mddev->events>>32);
1255 sb->events_lo = (u32)mddev->events;
1256
NeilBrownf6705572006-03-27 01:18:11 -08001257 if (mddev->reshape_position == MaxSector)
1258 sb->minor_version = 90;
1259 else {
1260 sb->minor_version = 91;
1261 sb->reshape_position = mddev->reshape_position;
1262 sb->new_level = mddev->new_level;
1263 sb->delta_disks = mddev->delta_disks;
1264 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001265 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001266 }
1267 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (mddev->in_sync)
1269 {
1270 sb->recovery_cp = mddev->recovery_cp;
1271 sb->cp_events_hi = (mddev->events>>32);
1272 sb->cp_events_lo = (u32)mddev->events;
1273 if (mddev->recovery_cp == MaxSector)
1274 sb->state = (1<< MD_SB_CLEAN);
1275 } else
1276 sb->recovery_cp = 0;
1277
1278 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001279 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
NeilBrownc3d97142009-12-14 12:49:52 +11001281 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001282 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001285 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001287 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001288 int is_active = test_bit(In_sync, &rdev2->flags);
1289
1290 if (rdev2->raid_disk >= 0 &&
1291 sb->minor_version >= 91)
1292 /* we have nowhere to store the recovery_offset,
1293 * but if it is not below the reshape_position,
1294 * we can piggy-back on that.
1295 */
1296 is_active = 1;
1297 if (rdev2->raid_disk < 0 ||
1298 test_bit(Faulty, &rdev2->flags))
1299 is_active = 0;
1300 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001301 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001303 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001304 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 d = &sb->disks[rdev2->desc_nr];
1306 nr_disks++;
1307 d->number = rdev2->desc_nr;
1308 d->major = MAJOR(rdev2->bdev->bd_dev);
1309 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001310 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 d->raid_disk = rdev2->raid_disk;
1312 else
1313 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001314 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001316 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001318 if (test_bit(In_sync, &rdev2->flags))
1319 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 active++;
1321 working++;
1322 } else {
1323 d->state = 0;
1324 spare++;
1325 working++;
1326 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001327 if (test_bit(WriteMostly, &rdev2->flags))
1328 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* now set the "removed" and "faulty" bits on any missing devices */
1331 for (i=0 ; i < mddev->raid_disks ; i++) {
1332 mdp_disk_t *d = &sb->disks[i];
1333 if (d->state == 0 && d->number == 0) {
1334 d->number = i;
1335 d->raid_disk = i;
1336 d->state = (1<<MD_DISK_REMOVED);
1337 d->state |= (1<<MD_DISK_FAULTY);
1338 failed++;
1339 }
1340 }
1341 sb->nr_disks = nr_disks;
1342 sb->active_disks = active;
1343 sb->working_disks = working;
1344 sb->failed_disks = failed;
1345 sb->spare_disks = spare;
1346
1347 sb->this_disk = sb->disks[rdev->desc_nr];
1348 sb->sb_csum = calc_sb_csum(sb);
1349}
1350
1351/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001352 * rdev_size_change for 0.90.0
1353 */
1354static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001355super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001356{
Andre Noll58c0fed2009-03-31 14:33:13 +11001357 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001358 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001359 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001360 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001361 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001362 if (!num_sectors || num_sectors > rdev->sb_start)
1363 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001364 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365 rdev->sb_page);
1366 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001367 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001368}
1369
1370
1371/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * version 1 superblock
1373 */
1374
NeilBrown1c05b4b2006-10-21 10:24:08 -07001375static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001377 __le32 disk_csum;
1378 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned long long newcsum;
1380 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001381 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int i;
1383
1384 disk_csum = sb->sb_csum;
1385 sb->sb_csum = 0;
1386 newcsum = 0;
1387 for (i=0; size>=4; size -= 4 )
1388 newcsum += le32_to_cpu(*isuper++);
1389
1390 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001391 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1394 sb->sb_csum = disk_csum;
1395 return cpu_to_le32(csum);
1396}
1397
1398static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1399{
1400 struct mdp_superblock_1 *sb;
1401 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001402 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001404 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
Andre Noll0f420352008-07-11 22:02:23 +10001407 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 * It is always aligned to a 4K boundary and
1409 * depeding on minor_version, it can be:
1410 * 0: At least 8K, but less than 12K, from end of device
1411 * 1: At start of device
1412 * 2: 4K from start of device.
1413 */
1414 switch(minor_version) {
1415 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001416 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001417 sb_start -= 8*2;
1418 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001421 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001424 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426 default:
1427 return -EINVAL;
1428 }
Andre Noll0f420352008-07-11 22:02:23 +10001429 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
NeilBrown0002b272005-09-09 16:23:53 -07001431 /* superblock is rarely larger than 1K, but it can be larger,
1432 * and it is safe to read 4k, so we do that
1433 */
1434 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (ret) return ret;
1436
1437
Namhyung Kim65a06f02011-07-27 11:00:36 +10001438 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1441 sb->major_version != cpu_to_le32(1) ||
1442 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001443 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001444 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return -EINVAL;
1446
1447 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1448 printk("md: invalid superblock checksum on %s\n",
1449 bdevname(rdev->bdev,b));
1450 return -EINVAL;
1451 }
1452 if (le64_to_cpu(sb->data_size) < 10) {
1453 printk("md: data_size too small on %s\n",
1454 bdevname(rdev->bdev,b));
1455 return -EINVAL;
1456 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 rdev->preferred_minor = 0xffff;
1459 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001460 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
NeilBrown0002b272005-09-09 16:23:53 -07001462 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001463 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001464 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001465 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1466
1467 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001468 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001469 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001470
NeilBrown31b65a02006-07-10 04:44:14 -07001471 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1472 rdev->desc_nr = -1;
1473 else
1474 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1475
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001476 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001477 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001478 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001480 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1483 sb->level != refsb->level ||
1484 sb->layout != refsb->layout ||
1485 sb->chunksize != refsb->chunksize) {
1486 printk(KERN_WARNING "md: %s has strangely different"
1487 " superblock to %s\n",
1488 bdevname(rdev->bdev,b),
1489 bdevname(refdev->bdev,b2));
1490 return -EINVAL;
1491 }
1492 ev1 = le64_to_cpu(sb->events);
1493 ev2 = le64_to_cpu(refsb->events);
1494
1495 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001496 ret = 1;
1497 else
1498 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
NeilBrowna1801f82008-03-04 14:29:31 -08001500 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001501 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001502 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001504 rdev->sectors = rdev->sb_start;
1505 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001507 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001508 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001509 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001510 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1514{
Namhyung Kim65a06f02011-07-27 11:00:36 +10001515 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001516 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
NeilBrown41158c72005-06-21 17:17:25 -07001518 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001519 clear_bit(Faulty, &rdev->flags);
1520 clear_bit(In_sync, &rdev->flags);
1521 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (mddev->raid_disks == 0) {
1524 mddev->major_version = 1;
1525 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001526 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001527 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1529 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1530 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001531 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 mddev->layout = le32_to_cpu(sb->layout);
1533 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001534 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001535 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001536 mddev->bitmap_info.offset = 0;
1537 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1540 memcpy(mddev->uuid, sb->set_uuid, 16);
1541
1542 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001543
NeilBrown71c08052005-09-09 16:23:51 -07001544 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001545 mddev->bitmap_info.file == NULL )
1546 mddev->bitmap_info.offset =
1547 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001548
NeilBrownf6705572006-03-27 01:18:11 -08001549 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1550 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1551 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1552 mddev->new_level = le32_to_cpu(sb->new_level);
1553 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001554 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001555 } else {
1556 mddev->reshape_position = MaxSector;
1557 mddev->delta_disks = 0;
1558 mddev->new_level = mddev->level;
1559 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001560 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001561 }
1562
NeilBrown41158c72005-06-21 17:17:25 -07001563 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001564 /* Insist of good event counter while assembling, except for
1565 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001567 if (rdev->desc_nr >= 0 &&
1568 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1569 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1570 if (ev1 < mddev->events)
1571 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001572 } else if (mddev->bitmap) {
1573 /* If adding to array with a bitmap, then we can accept an
1574 * older device, but not too old.
1575 */
NeilBrown41158c72005-06-21 17:17:25 -07001576 if (ev1 < mddev->bitmap->events_cleared)
1577 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001578 } else {
1579 if (ev1 < mddev->events)
1580 /* just a hot-add of a new device, leave raid_disk at -1 */
1581 return 0;
1582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (mddev->level != LEVEL_MULTIPATH) {
1584 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001585 if (rdev->desc_nr < 0 ||
1586 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1587 role = 0xffff;
1588 rdev->desc_nr = -1;
1589 } else
1590 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 switch(role) {
1592 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 break;
1594 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001595 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001598 if ((le32_to_cpu(sb->feature_map) &
1599 MD_FEATURE_RECOVERY_OFFSET))
1600 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1601 else
1602 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 rdev->raid_disk = role;
1604 break;
1605 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001606 if (sb->devflags & WriteMostly1)
1607 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001608 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001609 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return 0;
1612}
1613
1614static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1615{
1616 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 mdk_rdev_t *rdev2;
1618 int max_dev, i;
1619 /* make rdev->sb match mddev and rdev data. */
1620
Namhyung Kim65a06f02011-07-27 11:00:36 +10001621 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 sb->feature_map = 0;
1624 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001625 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 memset(sb->pad1, 0, sizeof(sb->pad1));
1627 memset(sb->pad2, 0, sizeof(sb->pad2));
1628 memset(sb->pad3, 0, sizeof(sb->pad3));
1629
1630 sb->utime = cpu_to_le64((__u64)mddev->utime);
1631 sb->events = cpu_to_le64(mddev->events);
1632 if (mddev->in_sync)
1633 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1634 else
1635 sb->resync_offset = cpu_to_le64(0);
1636
NeilBrown1c05b4b2006-10-21 10:24:08 -07001637 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001638
NeilBrownf0ca3402006-02-02 14:28:04 -08001639 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001640 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001641 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001642 sb->level = cpu_to_le32(mddev->level);
1643 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001644
NeilBrownc3d97142009-12-14 12:49:52 +11001645 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1646 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001647 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001648 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001649
1650 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001651 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001652 sb->feature_map |=
1653 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1654 sb->recovery_offset =
1655 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001656 }
1657
NeilBrownf6705572006-03-27 01:18:11 -08001658 if (mddev->reshape_position != MaxSector) {
1659 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1660 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1661 sb->new_layout = cpu_to_le32(mddev->new_layout);
1662 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1663 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001664 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001665 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001668 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (rdev2->desc_nr+1 > max_dev)
1670 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001671
NeilBrown70471da2009-08-03 10:59:57 +10001672 if (max_dev > le32_to_cpu(sb->max_dev)) {
1673 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001674 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001675 rdev->sb_size = max_dev * 2 + 256;
1676 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1677 if (rdev->sb_size & bmask)
1678 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001679 } else
1680 max_dev = le32_to_cpu(sb->max_dev);
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 for (i=0; i<max_dev;i++)
1683 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1684
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001685 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001687 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001689 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001691 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001692 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 else
1694 sb->dev_roles[i] = cpu_to_le16(0xffff);
1695 }
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 sb->sb_csum = calc_sb_1_csum(sb);
1698}
1699
Chris Webb0cd17fe2008-06-28 08:31:46 +10001700static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001701super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001702{
1703 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001704 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001705 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001706 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001707 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001708 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001709 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001710 max_sectors -= rdev->data_offset;
1711 if (!num_sectors || num_sectors > max_sectors)
1712 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001713 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001714 /* minor version 0 with bitmap we can't move */
1715 return 0;
1716 } else {
1717 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001718 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001719 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001720 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001721 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001722 if (!num_sectors || num_sectors > max_sectors)
1723 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001724 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001725 }
Namhyung Kim65a06f02011-07-27 11:00:36 +10001726 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001727 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001728 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001729 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001730 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001731 rdev->sb_page);
1732 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001733 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001734}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Adrian Bunk75c96f82005-05-05 16:16:09 -07001736static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 [0] = {
1738 .name = "0.90.0",
1739 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001740 .load_super = super_90_load,
1741 .validate_super = super_90_validate,
1742 .sync_super = super_90_sync,
1743 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 },
1745 [1] = {
1746 .name = "md-1",
1747 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001748 .load_super = super_1_load,
1749 .validate_super = super_1_validate,
1750 .sync_super = super_1_sync,
1751 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 },
1753};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Jonathan Brassow076f9682011-06-07 17:51:30 -05001755static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev)
1756{
1757 if (mddev->sync_super) {
1758 mddev->sync_super(mddev, rdev);
1759 return;
1760 }
1761
1762 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1763
1764 super_types[mddev->major_version].sync_super(mddev, rdev);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1768{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001769 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
NeilBrown4b809912008-07-21 17:05:25 +10001771 rcu_read_lock();
1772 rdev_for_each_rcu(rdev, mddev1)
1773 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001774 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001775 rdev2->bdev->bd_contains) {
1776 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001777 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001778 }
1779 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return 0;
1781}
1782
1783static LIST_HEAD(pending_raid_disks);
1784
Andre Nollac5e7112009-08-03 10:59:47 +10001785/*
1786 * Try to register data integrity profile for an mddev
1787 *
1788 * This is called when an array is started and after a disk has been kicked
1789 * from the array. It only succeeds if all working and active component devices
1790 * are integrity capable with matching profiles.
1791 */
1792int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001793{
Andre Nollac5e7112009-08-03 10:59:47 +10001794 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001795
Andre Nollac5e7112009-08-03 10:59:47 +10001796 if (list_empty(&mddev->disks))
1797 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001798 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1799 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001800 list_for_each_entry(rdev, &mddev->disks, same_set) {
1801 /* skip spares and non-functional disks */
1802 if (test_bit(Faulty, &rdev->flags))
1803 continue;
1804 if (rdev->raid_disk < 0)
1805 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001806 if (!reference) {
1807 /* Use the first rdev as the reference */
1808 reference = rdev;
1809 continue;
1810 }
1811 /* does this rdev's profile match the reference profile? */
1812 if (blk_integrity_compare(reference->bdev->bd_disk,
1813 rdev->bdev->bd_disk) < 0)
1814 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001815 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001816 if (!reference || !bdev_get_integrity(reference->bdev))
1817 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001818 /*
1819 * All component devices are integrity capable and have matching
1820 * profiles, register the common profile for the md device.
1821 */
1822 if (blk_integrity_register(mddev->gendisk,
1823 bdev_get_integrity(reference->bdev)) != 0) {
1824 printk(KERN_ERR "md: failed to register integrity for %s\n",
1825 mdname(mddev));
1826 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001827 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001828 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1829 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1830 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1831 mdname(mddev));
1832 return -EINVAL;
1833 }
Andre Nollac5e7112009-08-03 10:59:47 +10001834 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001835}
Andre Nollac5e7112009-08-03 10:59:47 +10001836EXPORT_SYMBOL(md_integrity_register);
1837
1838/* Disable data integrity if non-capable/non-matching disk is being added */
1839void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1840{
1841 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1842 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1843
1844 if (!bi_mddev) /* nothing to do */
1845 return;
1846 if (rdev->raid_disk < 0) /* skip spares */
1847 return;
1848 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1849 rdev->bdev->bd_disk) >= 0)
1850 return;
1851 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1852 blk_integrity_unregister(mddev->gendisk);
1853}
1854EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1857{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001858 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001859 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001860 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001861 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if (rdev->mddev) {
1864 MD_BUG();
1865 return -EINVAL;
1866 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001867
1868 /* prevent duplicates */
1869 if (find_rdev(mddev, rdev->bdev->bd_dev))
1870 return -EEXIST;
1871
Andre Nolldd8ac332009-03-31 14:33:13 +11001872 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1873 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1874 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001875 if (mddev->pers) {
1876 /* Cannot change size, so fail
1877 * If mddev->level <= 0, then we don't care
1878 * about aligning sizes (e.g. linear)
1879 */
1880 if (mddev->level > 0)
1881 return -ENOSPC;
1882 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001883 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 /* Verify rdev->desc_nr is unique.
1887 * If it is -1, assign a free number, else
1888 * check number is not in use
1889 */
1890 if (rdev->desc_nr < 0) {
1891 int choice = 0;
1892 if (mddev->pers) choice = mddev->raid_disks;
1893 while (find_rdev_nr(mddev, choice))
1894 choice++;
1895 rdev->desc_nr = choice;
1896 } else {
1897 if (find_rdev_nr(mddev, rdev->desc_nr))
1898 return -EBUSY;
1899 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001900 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1901 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1902 mdname(mddev), mddev->max_disks);
1903 return -EBUSY;
1904 }
NeilBrown19133a42005-11-08 21:39:35 -08001905 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001906 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001907 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001910 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001911
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001912 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001913 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001914
Tejun Heo0762b8b2008-08-25 19:56:12 +09001915 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001916 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1917 /* failure here is OK */;
1918 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001919
NeilBrown4b809912008-07-21 17:05:25 +10001920 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01001921 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001922
1923 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10001924 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001927
1928 fail:
1929 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1930 b, mdname(mddev));
1931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
NeilBrown177a99b2008-02-06 01:39:56 -08001934static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001935{
1936 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1937 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001938 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001939}
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1942{
1943 char b[BDEVNAME_SIZE];
1944 if (!rdev->mddev) {
1945 MD_BUG();
1946 return;
1947 }
Tejun Heo49731ba2011-01-14 18:43:57 +01001948 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001949 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1951 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001952 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001953 sysfs_put(rdev->sysfs_state);
1954 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10001955 kfree(rdev->badblocks.page);
1956 rdev->badblocks.count = 0;
1957 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001958 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001959 * writing to 'remove' to "dev/state". We also need
1960 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001961 */
NeilBrown4b809912008-07-21 17:05:25 +10001962 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001963 INIT_WORK(&rdev->del_work, md_delayed_delete);
1964 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001965 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968/*
1969 * prevent the device from being mounted, repartitioned or
1970 * otherwise reused by a RAID array (or any other kernel
1971 * subsystem), by bd_claiming the device.
1972 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001973static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 int err = 0;
1976 struct block_device *bdev;
1977 char b[BDEVNAME_SIZE];
1978
Tejun Heod4d77622010-11-13 11:55:18 +01001979 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1980 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (IS_ERR(bdev)) {
1982 printk(KERN_ERR "md: could not open %s.\n",
1983 __bdevname(dev, b));
1984 return PTR_ERR(bdev);
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 rdev->bdev = bdev;
1987 return err;
1988}
1989
1990static void unlock_rdev(mdk_rdev_t *rdev)
1991{
1992 struct block_device *bdev = rdev->bdev;
1993 rdev->bdev = NULL;
1994 if (!bdev)
1995 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01001996 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999void md_autodetect_dev(dev_t dev);
2000
2001static void export_rdev(mdk_rdev_t * rdev)
2002{
2003 char b[BDEVNAME_SIZE];
2004 printk(KERN_INFO "md: export_rdev(%s)\n",
2005 bdevname(rdev->bdev,b));
2006 if (rdev->mddev)
2007 MD_BUG();
2008 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002010 if (test_bit(AutoDetected, &rdev->flags))
2011 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012#endif
2013 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002014 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
2017static void kick_rdev_from_array(mdk_rdev_t * rdev)
2018{
2019 unbind_rdev_from_array(rdev);
2020 export_rdev(rdev);
2021}
2022
2023static void export_array(mddev_t *mddev)
2024{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002025 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
NeilBrownd089c6a2008-02-06 01:39:59 -08002027 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (!rdev->mddev) {
2029 MD_BUG();
2030 continue;
2031 }
2032 kick_rdev_from_array(rdev);
2033 }
2034 if (!list_empty(&mddev->disks))
2035 MD_BUG();
2036 mddev->raid_disks = 0;
2037 mddev->major_version = 0;
2038}
2039
2040static void print_desc(mdp_disk_t *desc)
2041{
2042 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2043 desc->major,desc->minor,desc->raid_disk,desc->state);
2044}
2045
Cheng Renquancd2ac932009-01-09 08:31:08 +11002046static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 int i;
2049
2050 printk(KERN_INFO
2051 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2052 sb->major_version, sb->minor_version, sb->patch_version,
2053 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2054 sb->ctime);
2055 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2056 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2057 sb->md_minor, sb->layout, sb->chunk_size);
2058 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2059 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2060 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2061 sb->failed_disks, sb->spare_disks,
2062 sb->sb_csum, (unsigned long)sb->events_lo);
2063
2064 printk(KERN_INFO);
2065 for (i = 0; i < MD_SB_DISKS; i++) {
2066 mdp_disk_t *desc;
2067
2068 desc = sb->disks + i;
2069 if (desc->number || desc->major || desc->minor ||
2070 desc->raid_disk || (desc->state && (desc->state != 4))) {
2071 printk(" D %2d: ", i);
2072 print_desc(desc);
2073 }
2074 }
2075 printk(KERN_INFO "md: THIS: ");
2076 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
Cheng Renquancd2ac932009-01-09 08:31:08 +11002079static void print_sb_1(struct mdp_superblock_1 *sb)
2080{
2081 __u8 *uuid;
2082
2083 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002084 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002085 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002086 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002087 le32_to_cpu(sb->major_version),
2088 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002089 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002090 sb->set_name,
2091 (unsigned long long)le64_to_cpu(sb->ctime)
2092 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2093
2094 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002095 printk(KERN_INFO
2096 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002097 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002098 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002099 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2100 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002101 le32_to_cpu(sb->level),
2102 (unsigned long long)le64_to_cpu(sb->size),
2103 le32_to_cpu(sb->raid_disks),
2104 le32_to_cpu(sb->layout),
2105 le32_to_cpu(sb->chunksize),
2106 (unsigned long long)le64_to_cpu(sb->data_offset),
2107 (unsigned long long)le64_to_cpu(sb->data_size),
2108 (unsigned long long)le64_to_cpu(sb->super_offset),
2109 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2110 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002111 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002112 sb->devflags,
2113 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2114 (unsigned long long)le64_to_cpu(sb->events),
2115 (unsigned long long)le64_to_cpu(sb->resync_offset),
2116 le32_to_cpu(sb->sb_csum),
2117 le32_to_cpu(sb->max_dev)
2118 );
2119}
2120
2121static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002124 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2125 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002126 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2127 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002129 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2130 switch (major_version) {
2131 case 0:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002132 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002133 break;
2134 case 1:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002135 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002136 break;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 } else
2139 printk(KERN_INFO "md: no rdev superblock!\n");
2140}
2141
Adrian Bunk5e563412006-06-26 00:27:42 -07002142static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002144 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 mdk_rdev_t *rdev;
2146 mddev_t *mddev;
2147 char b[BDEVNAME_SIZE];
2148
2149 printk("\n");
2150 printk("md: **********************************\n");
2151 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2152 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002153 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
NeilBrown32a76272005-06-21 17:17:14 -07002155 if (mddev->bitmap)
2156 bitmap_print_sb(mddev->bitmap);
2157 else
2158 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002159 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 printk("<%s>", bdevname(rdev->bdev,b));
2161 printk("\n");
2162
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002163 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002164 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166 printk("md: **********************************\n");
2167 printk("\n");
2168}
2169
2170
NeilBrown42543762006-06-26 00:27:57 -07002171static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
NeilBrown42543762006-06-26 00:27:57 -07002173 /* Update each superblock (in-memory image), but
2174 * if we are allowed to, skip spares which already
2175 * have the right event counter, or have one earlier
2176 * (which would mean they aren't being marked as dirty
2177 * with the rest of the array)
2178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002180 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002181 if (rdev->sb_events == mddev->events ||
2182 (nospares &&
2183 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002184 rdev->sb_events+1 == mddev->events)) {
2185 /* Don't update this superblock */
2186 rdev->sb_loaded = 2;
2187 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002188 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002189 rdev->sb_loaded = 1;
2190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192}
2193
NeilBrown850b2b42006-10-03 01:15:46 -07002194static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002197 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002198 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002201 /* First make sure individual recovery_offsets are correct */
2202 list_for_each_entry(rdev, &mddev->disks, same_set) {
2203 if (rdev->raid_disk >= 0 &&
2204 mddev->delta_disks >= 0 &&
2205 !test_bit(In_sync, &rdev->flags) &&
2206 mddev->curr_resync_completed > rdev->recovery_offset)
2207 rdev->recovery_offset = mddev->curr_resync_completed;
2208
2209 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002210 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002211 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002212 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002213 if (!mddev->external)
2214 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002215 wake_up(&mddev->sb_wait);
2216 return;
2217 }
2218
NeilBrowna9701a32005-11-08 21:39:34 -08002219 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002220
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002221 mddev->utime = get_seconds();
2222
NeilBrown850b2b42006-10-03 01:15:46 -07002223 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2224 force_change = 1;
2225 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2226 /* just a clean<-> dirty transition, possibly leave spares alone,
2227 * though if events isn't the right even/odd, we will have to do
2228 * spares after all
2229 */
2230 nospares = 1;
2231 if (force_change)
2232 nospares = 0;
2233 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002234 /* If the array is degraded, then skipping spares is both
2235 * dangerous and fairly pointless.
2236 * Dangerous because a device that was removed from the array
2237 * might have a event_count that still looks up-to-date,
2238 * so it can be re-added without a resync.
2239 * Pointless because if there are any spares to skip,
2240 * then a recovery will happen and soon that array won't
2241 * be degraded any more and the spare can go back to sleep then.
2242 */
NeilBrown850b2b42006-10-03 01:15:46 -07002243 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002244
NeilBrown06d91a52005-06-21 17:17:12 -07002245 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002246
2247 /* If this is just a dirty<->clean transition, and the array is clean
2248 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002249 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002250 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002251 && mddev->can_decrease_events
2252 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002253 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002254 mddev->can_decrease_events = 0;
2255 } else {
NeilBrown42543762006-06-26 00:27:57 -07002256 /* otherwise we have to go forward and ... */
2257 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002258 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 if (!mddev->events) {
2262 /*
2263 * oops, this 64-bit counter should never wrap.
2264 * Either we are in around ~1 trillion A.C., assuming
2265 * 1 reboot per second, or we have a bug:
2266 */
2267 MD_BUG();
2268 mddev->events --;
2269 }
NeilBrowne6910632008-02-06 01:39:51 -08002270 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002271 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 dprintk(KERN_INFO
2274 "md: updating %s RAID superblock on device (in sync %d)\n",
2275 mdname(mddev),mddev->in_sync);
2276
NeilBrown4ad13662007-07-17 04:06:13 -07002277 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002278 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 char b[BDEVNAME_SIZE];
2280 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002281 if (rdev->sb_loaded != 1)
2282 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002283 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 dprintk("(skipping faulty ");
2285
2286 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002287 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002288 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002289 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002290 rdev->sb_page);
2291 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2292 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002293 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002294 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 } else
2297 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002298 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /* only need to write one superblock... */
2300 break;
2301 }
NeilBrowna9701a32005-11-08 21:39:34 -08002302 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002303 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002304
NeilBrowna9701a32005-11-08 21:39:34 -08002305 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002306 if (mddev->in_sync != sync_req ||
2307 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002308 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002309 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002310 goto repeat;
2311 }
NeilBrown850b2b42006-10-03 01:15:46 -07002312 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002313 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002314 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002315 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2316 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319
Andre Noll7f6ce762008-03-23 18:34:54 +01002320/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002321 * We want to accept with case. For this we use cmd_match.
2322 */
2323static int cmd_match(const char *cmd, const char *str)
2324{
2325 /* See if cmd, written into a sysfs file, matches
2326 * str. They must either be the same, or cmd can
2327 * have a trailing newline
2328 */
2329 while (*cmd && *str && *cmd == *str) {
2330 cmd++;
2331 str++;
2332 }
2333 if (*cmd == '\n')
2334 cmd++;
2335 if (*str || *cmd)
2336 return 0;
2337 return 1;
2338}
2339
NeilBrown86e6ffd2005-11-08 21:39:24 -08002340struct rdev_sysfs_entry {
2341 struct attribute attr;
2342 ssize_t (*show)(mdk_rdev_t *, char *);
2343 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2344};
2345
2346static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002347state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002348{
2349 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002350 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002351
NeilBrownb2d444d2005-11-08 21:39:31 -08002352 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002353 len+= sprintf(page+len, "%sfaulty",sep);
2354 sep = ",";
2355 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002356 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002357 len += sprintf(page+len, "%sin_sync",sep);
2358 sep = ",";
2359 }
NeilBrownf6556752006-06-26 00:28:01 -07002360 if (test_bit(WriteMostly, &rdev->flags)) {
2361 len += sprintf(page+len, "%swrite_mostly",sep);
2362 sep = ",";
2363 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002364 if (test_bit(Blocked, &rdev->flags)) {
2365 len += sprintf(page+len, "%sblocked", sep);
2366 sep = ",";
2367 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002368 if (!test_bit(Faulty, &rdev->flags) &&
2369 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002370 len += sprintf(page+len, "%sspare", sep);
2371 sep = ",";
2372 }
2373 return len+sprintf(page+len, "\n");
2374}
2375
NeilBrown45dc2de2006-06-26 00:27:58 -07002376static ssize_t
2377state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2378{
2379 /* can write
2380 * faulty - simulates and error
2381 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002382 * writemostly - sets write_mostly
2383 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002384 * blocked - sets the Blocked flag
2385 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002386 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002387 */
2388 int err = -EINVAL;
2389 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2390 md_error(rdev->mddev, rdev);
2391 err = 0;
2392 } else if (cmd_match(buf, "remove")) {
2393 if (rdev->raid_disk >= 0)
2394 err = -EBUSY;
2395 else {
2396 mddev_t *mddev = rdev->mddev;
2397 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002398 if (mddev->pers)
2399 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002400 md_new_event(mddev);
2401 err = 0;
2402 }
NeilBrownf6556752006-06-26 00:28:01 -07002403 } else if (cmd_match(buf, "writemostly")) {
2404 set_bit(WriteMostly, &rdev->flags);
2405 err = 0;
2406 } else if (cmd_match(buf, "-writemostly")) {
2407 clear_bit(WriteMostly, &rdev->flags);
2408 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002409 } else if (cmd_match(buf, "blocked")) {
2410 set_bit(Blocked, &rdev->flags);
2411 err = 0;
2412 } else if (cmd_match(buf, "-blocked")) {
2413 clear_bit(Blocked, &rdev->flags);
2414 wake_up(&rdev->blocked_wait);
2415 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2416 md_wakeup_thread(rdev->mddev->thread);
2417
2418 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002419 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2420 set_bit(In_sync, &rdev->flags);
2421 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002422 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002423 if (!err)
2424 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002425 return err ? err : len;
2426}
NeilBrown80ca3a42006-07-10 04:44:18 -07002427static struct rdev_sysfs_entry rdev_state =
2428__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002429
2430static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002431errors_show(mdk_rdev_t *rdev, char *page)
2432{
2433 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2434}
2435
2436static ssize_t
2437errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2438{
2439 char *e;
2440 unsigned long n = simple_strtoul(buf, &e, 10);
2441 if (*buf && (*e == 0 || *e == '\n')) {
2442 atomic_set(&rdev->corrected_errors, n);
2443 return len;
2444 }
2445 return -EINVAL;
2446}
2447static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002448__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002449
NeilBrown014236d2006-01-06 00:20:55 -08002450static ssize_t
2451slot_show(mdk_rdev_t *rdev, char *page)
2452{
2453 if (rdev->raid_disk < 0)
2454 return sprintf(page, "none\n");
2455 else
2456 return sprintf(page, "%d\n", rdev->raid_disk);
2457}
2458
2459static ssize_t
2460slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2461{
2462 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002463 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002464 int slot = simple_strtoul(buf, &e, 10);
2465 if (strncmp(buf, "none", 4)==0)
2466 slot = -1;
2467 else if (e==buf || (*e && *e!= '\n'))
2468 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002469 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002470 /* Setting 'slot' on an active array requires also
2471 * updating the 'rd%d' link, and communicating
2472 * with the personality with ->hot_*_disk.
2473 * For now we only support removing
2474 * failed/spare devices. This normally happens automatically,
2475 * but not when the metadata is externally managed.
2476 */
NeilBrownc303da62008-02-06 01:39:51 -08002477 if (rdev->raid_disk == -1)
2478 return -EEXIST;
2479 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002480 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002481 return -EINVAL;
2482 err = rdev->mddev->pers->
2483 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2484 if (err)
2485 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002486 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002487 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002488 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2489 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002490 } else if (rdev->mddev->pers) {
2491 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002492 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002493 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002494 */
2495
2496 if (rdev->raid_disk != -1)
2497 return -EBUSY;
2498
NeilBrownc6751b22011-02-02 11:57:13 +11002499 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2500 return -EBUSY;
2501
Neil Brown6c2fce22008-06-28 08:31:31 +10002502 if (rdev->mddev->pers->hot_add_disk == NULL)
2503 return -EINVAL;
2504
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002505 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002506 if (rdev2->raid_disk == slot)
2507 return -EEXIST;
2508
NeilBrownba1b41b2011-01-14 09:14:34 +11002509 if (slot >= rdev->mddev->raid_disks &&
2510 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2511 return -ENOSPC;
2512
Neil Brown6c2fce22008-06-28 08:31:31 +10002513 rdev->raid_disk = slot;
2514 if (test_bit(In_sync, &rdev->flags))
2515 rdev->saved_raid_disk = slot;
2516 else
2517 rdev->saved_raid_disk = -1;
2518 err = rdev->mddev->pers->
2519 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002520 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002521 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002522 return err;
Neil Brown52664732008-06-28 08:31:44 +10002523 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002524 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002525 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002526 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002527 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002528 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002529 if (slot >= rdev->mddev->raid_disks &&
2530 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002531 return -ENOSPC;
2532 rdev->raid_disk = slot;
2533 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002534 clear_bit(Faulty, &rdev->flags);
2535 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002536 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002537 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002538 }
NeilBrown014236d2006-01-06 00:20:55 -08002539 return len;
2540}
2541
2542
2543static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002544__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002545
NeilBrown93c8cad2006-01-06 00:20:56 -08002546static ssize_t
2547offset_show(mdk_rdev_t *rdev, char *page)
2548{
Andrew Morton6961ece2006-01-06 00:20:59 -08002549 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002550}
2551
2552static ssize_t
2553offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2554{
2555 char *e;
2556 unsigned long long offset = simple_strtoull(buf, &e, 10);
2557 if (e==buf || (*e && *e != '\n'))
2558 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002559 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002560 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002561 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002562 /* Must set offset before size, so overlap checks
2563 * can be sane */
2564 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002565 rdev->data_offset = offset;
2566 return len;
2567}
2568
2569static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002570__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002571
NeilBrown83303b62006-01-06 00:21:06 -08002572static ssize_t
2573rdev_size_show(mdk_rdev_t *rdev, char *page)
2574{
Andre Nolldd8ac332009-03-31 14:33:13 +11002575 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002576}
2577
NeilBrownc5d79ad2008-02-06 01:39:54 -08002578static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2579{
2580 /* check if two start/length pairs overlap */
2581 if (s1+l1 <= s2)
2582 return 0;
2583 if (s2+l2 <= s1)
2584 return 0;
2585 return 1;
2586}
2587
Dan Williamsb522adc2009-03-31 15:00:31 +11002588static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2589{
2590 unsigned long long blocks;
2591 sector_t new;
2592
2593 if (strict_strtoull(buf, 10, &blocks) < 0)
2594 return -EINVAL;
2595
2596 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2597 return -EINVAL; /* sector conversion overflow */
2598
2599 new = blocks * 2;
2600 if (new != blocks * 2)
2601 return -EINVAL; /* unsigned long long to sector_t overflow */
2602
2603 *sectors = new;
2604 return 0;
2605}
2606
NeilBrown83303b62006-01-06 00:21:06 -08002607static ssize_t
2608rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2609{
NeilBrown27c529b2008-03-04 14:29:33 -08002610 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002611 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002612 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002613
Dan Williamsb522adc2009-03-31 15:00:31 +11002614 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002615 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002616 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002617 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002618 sectors = super_types[my_mddev->major_version].
2619 rdev_size_change(rdev, sectors);
2620 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002621 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002622 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002623 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002624 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002625 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002626 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002627 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002628
Andre Nolldd8ac332009-03-31 14:33:13 +11002629 rdev->sectors = sectors;
2630 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002631 /* need to check that all other rdevs with the same ->bdev
2632 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002633 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002634 * we have to change it back, we will have the lock again.
2635 */
2636 mddev_t *mddev;
2637 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002638 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002639
NeilBrown27c529b2008-03-04 14:29:33 -08002640 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002641 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002642 mdk_rdev_t *rdev2;
2643
2644 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002645 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002646 if (rdev->bdev == rdev2->bdev &&
2647 rdev != rdev2 &&
2648 overlaps(rdev->data_offset, rdev->sectors,
2649 rdev2->data_offset,
2650 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002651 overlap = 1;
2652 break;
2653 }
2654 mddev_unlock(mddev);
2655 if (overlap) {
2656 mddev_put(mddev);
2657 break;
2658 }
2659 }
NeilBrown27c529b2008-03-04 14:29:33 -08002660 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002661 if (overlap) {
2662 /* Someone else could have slipped in a size
2663 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002664 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002665 * safe, and trust userspace not to race with
2666 * itself
2667 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002668 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002669 return -EBUSY;
2670 }
2671 }
NeilBrown83303b62006-01-06 00:21:06 -08002672 return len;
2673}
2674
2675static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002676__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002677
Dan Williams06e3c812009-12-12 21:17:12 -07002678
2679static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2680{
2681 unsigned long long recovery_start = rdev->recovery_offset;
2682
2683 if (test_bit(In_sync, &rdev->flags) ||
2684 recovery_start == MaxSector)
2685 return sprintf(page, "none\n");
2686
2687 return sprintf(page, "%llu\n", recovery_start);
2688}
2689
2690static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2691{
2692 unsigned long long recovery_start;
2693
2694 if (cmd_match(buf, "none"))
2695 recovery_start = MaxSector;
2696 else if (strict_strtoull(buf, 10, &recovery_start))
2697 return -EINVAL;
2698
2699 if (rdev->mddev->pers &&
2700 rdev->raid_disk >= 0)
2701 return -EBUSY;
2702
2703 rdev->recovery_offset = recovery_start;
2704 if (recovery_start == MaxSector)
2705 set_bit(In_sync, &rdev->flags);
2706 else
2707 clear_bit(In_sync, &rdev->flags);
2708 return len;
2709}
2710
2711static struct rdev_sysfs_entry rdev_recovery_start =
2712__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2713
NeilBrown16c791a2011-07-28 11:31:47 +10002714
2715static ssize_t
2716badblocks_show(struct badblocks *bb, char *page, int unack);
2717static ssize_t
2718badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2719
2720static ssize_t bb_show(mdk_rdev_t *rdev, char *page)
2721{
2722 return badblocks_show(&rdev->badblocks, page, 0);
2723}
2724static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2725{
2726 return badblocks_store(&rdev->badblocks, page, len, 0);
2727}
2728static struct rdev_sysfs_entry rdev_bad_blocks =
2729__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2730
2731
2732static ssize_t ubb_show(mdk_rdev_t *rdev, char *page)
2733{
2734 return badblocks_show(&rdev->badblocks, page, 1);
2735}
2736static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2737{
2738 return badblocks_store(&rdev->badblocks, page, len, 1);
2739}
2740static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2741__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2742
NeilBrown86e6ffd2005-11-08 21:39:24 -08002743static struct attribute *rdev_default_attrs[] = {
2744 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002745 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002746 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002747 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002748 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002749 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002750 &rdev_bad_blocks.attr,
2751 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002752 NULL,
2753};
2754static ssize_t
2755rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2756{
2757 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2758 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002759 mddev_t *mddev = rdev->mddev;
2760 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002761
2762 if (!entry->show)
2763 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002764
2765 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2766 if (!rv) {
2767 if (rdev->mddev == NULL)
2768 rv = -EBUSY;
2769 else
2770 rv = entry->show(rdev, page);
2771 mddev_unlock(mddev);
2772 }
2773 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002774}
2775
2776static ssize_t
2777rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2778 const char *page, size_t length)
2779{
2780 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2781 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002782 ssize_t rv;
2783 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002784
2785 if (!entry->store)
2786 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002787 if (!capable(CAP_SYS_ADMIN))
2788 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002789 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002790 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002791 if (rdev->mddev == NULL)
2792 rv = -EBUSY;
2793 else
2794 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002795 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002796 }
2797 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002798}
2799
2800static void rdev_free(struct kobject *ko)
2801{
2802 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2803 kfree(rdev);
2804}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002805static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002806 .show = rdev_attr_show,
2807 .store = rdev_attr_store,
2808};
2809static struct kobj_type rdev_ktype = {
2810 .release = rdev_free,
2811 .sysfs_ops = &rdev_sysfs_ops,
2812 .default_attrs = rdev_default_attrs,
2813};
2814
NeilBrown2230dfe2011-07-28 11:31:46 +10002815int md_rdev_init(mdk_rdev_t *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10002816{
2817 rdev->desc_nr = -1;
2818 rdev->saved_raid_disk = -1;
2819 rdev->raid_disk = -1;
2820 rdev->flags = 0;
2821 rdev->data_offset = 0;
2822 rdev->sb_events = 0;
2823 rdev->last_read_error.tv_sec = 0;
2824 rdev->last_read_error.tv_nsec = 0;
2825 atomic_set(&rdev->nr_pending, 0);
2826 atomic_set(&rdev->read_errors, 0);
2827 atomic_set(&rdev->corrected_errors, 0);
2828
2829 INIT_LIST_HEAD(&rdev->same_set);
2830 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10002831
2832 /* Add space to store bad block list.
2833 * This reserves the space even on arrays where it cannot
2834 * be used - I wonder if that matters
2835 */
2836 rdev->badblocks.count = 0;
2837 rdev->badblocks.shift = 0;
2838 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
2839 seqlock_init(&rdev->badblocks.lock);
2840 if (rdev->badblocks.page == NULL)
2841 return -ENOMEM;
2842
2843 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10002844}
2845EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846/*
2847 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2848 *
2849 * mark the device faulty if:
2850 *
2851 * - the device is nonexistent (zero size)
2852 * - the device has no valid superblock
2853 *
2854 * a faulty rdev _never_ has rdev->sb set.
2855 */
2856static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2857{
2858 char b[BDEVNAME_SIZE];
2859 int err;
2860 mdk_rdev_t *rdev;
2861 sector_t size;
2862
NeilBrown9ffae0c2006-01-06 00:20:32 -08002863 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (!rdev) {
2865 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2866 return ERR_PTR(-ENOMEM);
2867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
NeilBrown2230dfe2011-07-28 11:31:46 +10002869 err = md_rdev_init(rdev);
2870 if (err)
2871 goto abort_free;
2872 err = alloc_disk_sb(rdev);
2873 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto abort_free;
2875
NeilBrownc5d79ad2008-02-06 01:39:54 -08002876 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (err)
2878 goto abort_free;
2879
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002880 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002881
Mike Snitzer77304d22010-11-08 14:39:12 +01002882 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (!size) {
2884 printk(KERN_WARNING
2885 "md: %s has zero or unknown size, marking faulty!\n",
2886 bdevname(rdev->bdev,b));
2887 err = -EINVAL;
2888 goto abort_free;
2889 }
2890
2891 if (super_format >= 0) {
2892 err = super_types[super_format].
2893 load_super(rdev, NULL, super_minor);
2894 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002895 printk(KERN_WARNING
2896 "md: %s does not have a valid v%d.%d "
2897 "superblock, not importing!\n",
2898 bdevname(rdev->bdev,b),
2899 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 goto abort_free;
2901 }
2902 if (err < 0) {
2903 printk(KERN_WARNING
2904 "md: could not read %s's sb, not importing!\n",
2905 bdevname(rdev->bdev,b));
2906 goto abort_free;
2907 }
2908 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return rdev;
2911
2912abort_free:
2913 if (rdev->sb_page) {
2914 if (rdev->bdev)
2915 unlock_rdev(rdev);
2916 free_disk_sb(rdev);
2917 }
NeilBrown2230dfe2011-07-28 11:31:46 +10002918 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 kfree(rdev);
2920 return ERR_PTR(err);
2921}
2922
2923/*
2924 * Check a full RAID array for plausibility
2925 */
2926
2927
NeilBrowna757e642005-04-16 15:26:42 -07002928static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002931 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 char b[BDEVNAME_SIZE];
2933
2934 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002935 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 switch (super_types[mddev->major_version].
2937 load_super(rdev, freshest, mddev->minor_version)) {
2938 case 1:
2939 freshest = rdev;
2940 break;
2941 case 0:
2942 break;
2943 default:
2944 printk( KERN_ERR \
2945 "md: fatal superblock inconsistency in %s"
2946 " -- removing from array\n",
2947 bdevname(rdev->bdev,b));
2948 kick_rdev_from_array(rdev);
2949 }
2950
2951
2952 super_types[mddev->major_version].
2953 validate_super(mddev, freshest);
2954
2955 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002956 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002957 if (mddev->max_disks &&
2958 (rdev->desc_nr >= mddev->max_disks ||
2959 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002960 printk(KERN_WARNING
2961 "md: %s: %s: only %d devices permitted\n",
2962 mdname(mddev), bdevname(rdev->bdev, b),
2963 mddev->max_disks);
2964 kick_rdev_from_array(rdev);
2965 continue;
2966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (rdev != freshest)
2968 if (super_types[mddev->major_version].
2969 validate_super(mddev, rdev)) {
2970 printk(KERN_WARNING "md: kicking non-fresh %s"
2971 " from array!\n",
2972 bdevname(rdev->bdev,b));
2973 kick_rdev_from_array(rdev);
2974 continue;
2975 }
2976 if (mddev->level == LEVEL_MULTIPATH) {
2977 rdev->desc_nr = i++;
2978 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002979 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002980 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002981 rdev->raid_disk = -1;
2982 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
NeilBrown72e02072009-12-14 12:49:55 +11002987/* Read a fixed-point number.
2988 * Numbers in sysfs attributes should be in "standard" units where
2989 * possible, so time should be in seconds.
2990 * However we internally use a a much smaller unit such as
2991 * milliseconds or jiffies.
2992 * This function takes a decimal number with a possible fractional
2993 * component, and produces an integer which is the result of
2994 * multiplying that number by 10^'scale'.
2995 * all without any floating-point arithmetic.
2996 */
2997int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2998{
2999 unsigned long result = 0;
3000 long decimals = -1;
3001 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3002 if (*cp == '.')
3003 decimals = 0;
3004 else if (decimals < scale) {
3005 unsigned int value;
3006 value = *cp - '0';
3007 result = result * 10 + value;
3008 if (decimals >= 0)
3009 decimals++;
3010 }
3011 cp++;
3012 }
3013 if (*cp == '\n')
3014 cp++;
3015 if (*cp)
3016 return -EINVAL;
3017 if (decimals < 0)
3018 decimals = 0;
3019 while (decimals < scale) {
3020 result *= 10;
3021 decimals ++;
3022 }
3023 *res = result;
3024 return 0;
3025}
3026
3027
NeilBrown19052c02008-08-05 15:54:13 +10003028static void md_safemode_timeout(unsigned long data);
3029
NeilBrowneae17012005-11-08 21:39:23 -08003030static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07003031safe_delay_show(mddev_t *mddev, char *page)
3032{
3033 int msec = (mddev->safemode_delay*1000)/HZ;
3034 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3035}
3036static ssize_t
3037safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
3038{
NeilBrown16f17b32006-06-26 00:27:37 -07003039 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003040
NeilBrown72e02072009-12-14 12:49:55 +11003041 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003042 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003043 if (msec == 0)
3044 mddev->safemode_delay = 0;
3045 else {
NeilBrown19052c02008-08-05 15:54:13 +10003046 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003047 mddev->safemode_delay = (msec*HZ)/1000;
3048 if (mddev->safemode_delay == 0)
3049 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003050 if (mddev->safemode_delay < old_delay)
3051 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003052 }
3053 return len;
3054}
3055static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003056__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003057
3058static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003059level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003060{
NeilBrown2604b702006-01-06 00:20:36 -08003061 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003062 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003063 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003064 else if (mddev->clevel[0])
3065 return sprintf(page, "%s\n", mddev->clevel);
3066 else if (mddev->level != LEVEL_NONE)
3067 return sprintf(page, "%d\n", mddev->level);
3068 else
3069 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003070}
3071
NeilBrownd9d166c2006-01-06 00:20:51 -08003072static ssize_t
3073level_store(mddev_t *mddev, const char *buf, size_t len)
3074{
Dan Williamsf2859af2010-05-02 10:04:16 -07003075 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003076 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003077 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003078 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003079 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003080 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003081
3082 if (mddev->pers == NULL) {
3083 if (len == 0)
3084 return 0;
3085 if (len >= sizeof(mddev->clevel))
3086 return -ENOSPC;
3087 strncpy(mddev->clevel, buf, len);
3088 if (mddev->clevel[len-1] == '\n')
3089 len--;
3090 mddev->clevel[len] = 0;
3091 mddev->level = LEVEL_NONE;
3092 return rv;
3093 }
3094
3095 /* request to change the personality. Need to ensure:
3096 * - array is not engaged in resync/recovery/reshape
3097 * - old personality can be suspended
3098 * - new personality will access other array.
3099 */
3100
NeilBrownbb4f1e92010-08-08 21:18:03 +10003101 if (mddev->sync_thread ||
3102 mddev->reshape_position != MaxSector ||
3103 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003104 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003105
3106 if (!mddev->pers->quiesce) {
3107 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3108 mdname(mddev), mddev->pers->name);
3109 return -EINVAL;
3110 }
3111
3112 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003113 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003114 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003115 strncpy(clevel, buf, len);
3116 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003117 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003118 clevel[len] = 0;
3119 if (strict_strtol(clevel, 10, &level))
3120 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003121
Dan Williamsf2859af2010-05-02 10:04:16 -07003122 if (request_module("md-%s", clevel) != 0)
3123 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003124 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003125 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003126 if (!pers || !try_module_get(pers->owner)) {
3127 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003128 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003129 return -EINVAL;
3130 }
3131 spin_unlock(&pers_lock);
3132
3133 if (pers == mddev->pers) {
3134 /* Nothing to do! */
3135 module_put(pers->owner);
3136 return rv;
3137 }
3138 if (!pers->takeover) {
3139 module_put(pers->owner);
3140 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003141 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003142 return -EINVAL;
3143 }
3144
NeilBrowne93f68a2010-06-15 09:36:03 +01003145 list_for_each_entry(rdev, &mddev->disks, same_set)
3146 rdev->new_raid_disk = rdev->raid_disk;
3147
NeilBrown245f46c2009-03-31 14:39:39 +11003148 /* ->takeover must set new_* and/or delta_disks
3149 * if it succeeds, and may set them when it fails.
3150 */
3151 priv = pers->takeover(mddev);
3152 if (IS_ERR(priv)) {
3153 mddev->new_level = mddev->level;
3154 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003155 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003156 mddev->raid_disks -= mddev->delta_disks;
3157 mddev->delta_disks = 0;
3158 module_put(pers->owner);
3159 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003160 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003161 return PTR_ERR(priv);
3162 }
3163
3164 /* Looks like we have a winner */
3165 mddev_suspend(mddev);
3166 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003167
3168 if (mddev->pers->sync_request == NULL &&
3169 pers->sync_request != NULL) {
3170 /* need to add the md_redundancy_group */
3171 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3172 printk(KERN_WARNING
3173 "md: cannot register extra attributes for %s\n",
3174 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003175 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003176 }
3177 if (mddev->pers->sync_request != NULL &&
3178 pers->sync_request == NULL) {
3179 /* need to remove the md_redundancy_group */
3180 if (mddev->to_remove == NULL)
3181 mddev->to_remove = &md_redundancy_group;
3182 }
3183
Trela Maciej54071b32010-03-08 16:02:42 +11003184 if (mddev->pers->sync_request == NULL &&
3185 mddev->external) {
3186 /* We are converting from a no-redundancy array
3187 * to a redundancy array and metadata is managed
3188 * externally so we need to be sure that writes
3189 * won't block due to a need to transition
3190 * clean->dirty
3191 * until external management is started.
3192 */
3193 mddev->in_sync = 0;
3194 mddev->safemode_delay = 0;
3195 mddev->safemode = 0;
3196 }
3197
NeilBrowne93f68a2010-06-15 09:36:03 +01003198 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003199 if (rdev->raid_disk < 0)
3200 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003201 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003202 rdev->new_raid_disk = -1;
3203 if (rdev->new_raid_disk == rdev->raid_disk)
3204 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003205 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003206 }
3207 list_for_each_entry(rdev, &mddev->disks, same_set) {
3208 if (rdev->raid_disk < 0)
3209 continue;
3210 if (rdev->new_raid_disk == rdev->raid_disk)
3211 continue;
3212 rdev->raid_disk = rdev->new_raid_disk;
3213 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003214 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003215 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003216 if (sysfs_link_rdev(mddev, rdev))
3217 printk(KERN_WARNING "md: cannot register rd%d"
3218 " for %s after level change\n",
3219 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003220 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003221 }
3222
3223 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003224 mddev->pers = pers;
3225 mddev->private = priv;
3226 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3227 mddev->level = mddev->new_level;
3228 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003229 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003230 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003231 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003232 if (mddev->pers->sync_request == NULL) {
3233 /* this is now an array without redundancy, so
3234 * it must always be in_sync
3235 */
3236 mddev->in_sync = 1;
3237 del_timer_sync(&mddev->safemode_timer);
3238 }
NeilBrown245f46c2009-03-31 14:39:39 +11003239 pers->run(mddev);
3240 mddev_resume(mddev);
3241 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3242 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3243 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003244 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003245 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003246 return rv;
3247}
3248
3249static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003250__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003251
NeilBrownd4dbd022006-06-26 00:27:59 -07003252
3253static ssize_t
3254layout_show(mddev_t *mddev, char *page)
3255{
3256 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003257 if (mddev->reshape_position != MaxSector &&
3258 mddev->layout != mddev->new_layout)
3259 return sprintf(page, "%d (%d)\n",
3260 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003261 return sprintf(page, "%d\n", mddev->layout);
3262}
3263
3264static ssize_t
3265layout_store(mddev_t *mddev, const char *buf, size_t len)
3266{
3267 char *e;
3268 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003269
3270 if (!*buf || (*e && *e != '\n'))
3271 return -EINVAL;
3272
NeilBrownb3546032009-03-31 14:56:41 +11003273 if (mddev->pers) {
3274 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003275 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003276 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003277 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003278 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003279 if (err) {
3280 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003281 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003282 }
NeilBrownb3546032009-03-31 14:56:41 +11003283 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003284 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003285 if (mddev->reshape_position == MaxSector)
3286 mddev->layout = n;
3287 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003288 return len;
3289}
3290static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003291__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003292
3293
NeilBrowneae17012005-11-08 21:39:23 -08003294static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003295raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003296{
NeilBrownbb636542005-11-08 21:39:45 -08003297 if (mddev->raid_disks == 0)
3298 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003299 if (mddev->reshape_position != MaxSector &&
3300 mddev->delta_disks != 0)
3301 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3302 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003303 return sprintf(page, "%d\n", mddev->raid_disks);
3304}
3305
NeilBrownda943b992006-01-06 00:20:54 -08003306static int update_raid_disks(mddev_t *mddev, int raid_disks);
3307
3308static ssize_t
3309raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3310{
NeilBrownda943b992006-01-06 00:20:54 -08003311 char *e;
3312 int rv = 0;
3313 unsigned long n = simple_strtoul(buf, &e, 10);
3314
3315 if (!*buf || (*e && *e != '\n'))
3316 return -EINVAL;
3317
3318 if (mddev->pers)
3319 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003320 else if (mddev->reshape_position != MaxSector) {
3321 int olddisks = mddev->raid_disks - mddev->delta_disks;
3322 mddev->delta_disks = n - olddisks;
3323 mddev->raid_disks = n;
3324 } else
NeilBrownda943b992006-01-06 00:20:54 -08003325 mddev->raid_disks = n;
3326 return rv ? rv : len;
3327}
3328static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003329__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003330
NeilBrown24dd4692005-11-08 21:39:26 -08003331static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003332chunk_size_show(mddev_t *mddev, char *page)
3333{
NeilBrown08a02ec2007-05-09 02:35:38 -07003334 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003335 mddev->chunk_sectors != mddev->new_chunk_sectors)
3336 return sprintf(page, "%d (%d)\n",
3337 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003338 mddev->chunk_sectors << 9);
3339 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003340}
3341
3342static ssize_t
3343chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3344{
NeilBrown3b343802006-01-06 00:20:47 -08003345 char *e;
3346 unsigned long n = simple_strtoul(buf, &e, 10);
3347
NeilBrown3b343802006-01-06 00:20:47 -08003348 if (!*buf || (*e && *e != '\n'))
3349 return -EINVAL;
3350
NeilBrownb3546032009-03-31 14:56:41 +11003351 if (mddev->pers) {
3352 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003353 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003354 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003355 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003356 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003357 if (err) {
3358 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003359 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003360 }
NeilBrownb3546032009-03-31 14:56:41 +11003361 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003362 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003363 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003364 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003365 }
NeilBrown3b343802006-01-06 00:20:47 -08003366 return len;
3367}
3368static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003369__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003370
NeilBrowna94213b2006-06-26 00:28:00 -07003371static ssize_t
3372resync_start_show(mddev_t *mddev, char *page)
3373{
NeilBrownd1a7c502009-03-31 15:24:32 +11003374 if (mddev->recovery_cp == MaxSector)
3375 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003376 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3377}
3378
3379static ssize_t
3380resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3381{
NeilBrowna94213b2006-06-26 00:28:00 -07003382 char *e;
3383 unsigned long long n = simple_strtoull(buf, &e, 10);
3384
NeilBrownb0986362011-05-11 15:52:21 +10003385 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003386 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003387 if (cmd_match(buf, "none"))
3388 n = MaxSector;
3389 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003390 return -EINVAL;
3391
3392 mddev->recovery_cp = n;
3393 return len;
3394}
3395static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003396__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003397
NeilBrown9e653b62006-06-26 00:27:58 -07003398/*
3399 * The array state can be:
3400 *
3401 * clear
3402 * No devices, no size, no level
3403 * Equivalent to STOP_ARRAY ioctl
3404 * inactive
3405 * May have some settings, but array is not active
3406 * all IO results in error
3407 * When written, doesn't tear down array, but just stops it
3408 * suspended (not supported yet)
3409 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003410 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003411 * readonly
3412 * no resync can happen. no superblocks get written.
3413 * write requests fail
3414 * read-auto
3415 * like readonly, but behaves like 'clean' on a write request.
3416 *
3417 * clean - no pending writes, but otherwise active.
3418 * When written to inactive array, starts without resync
3419 * If a write request arrives then
3420 * if metadata is known, mark 'dirty' and switch to 'active'.
3421 * if not known, block and switch to write-pending
3422 * If written to an active array that has pending writes, then fails.
3423 * active
3424 * fully active: IO and resync can be happening.
3425 * When written to inactive array, starts with resync
3426 *
3427 * write-pending
3428 * clean, but writes are blocked waiting for 'active' to be written.
3429 *
3430 * active-idle
3431 * like active, but no writes have been seen for a while (100msec).
3432 *
3433 */
3434enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3435 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003436static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003437 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3438 "write-pending", "active-idle", NULL };
3439
3440static int match_word(const char *word, char **list)
3441{
3442 int n;
3443 for (n=0; list[n]; n++)
3444 if (cmd_match(word, list[n]))
3445 break;
3446 return n;
3447}
3448
3449static ssize_t
3450array_state_show(mddev_t *mddev, char *page)
3451{
3452 enum array_state st = inactive;
3453
3454 if (mddev->pers)
3455 switch(mddev->ro) {
3456 case 1:
3457 st = readonly;
3458 break;
3459 case 2:
3460 st = read_auto;
3461 break;
3462 case 0:
3463 if (mddev->in_sync)
3464 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003465 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003466 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003467 else if (mddev->safemode)
3468 st = active_idle;
3469 else
3470 st = active;
3471 }
3472 else {
3473 if (list_empty(&mddev->disks) &&
3474 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003475 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003476 st = clear;
3477 else
3478 st = inactive;
3479 }
3480 return sprintf(page, "%s\n", array_states[st]);
3481}
3482
Neil Browndf5b20c2008-07-11 22:02:22 +10003483static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003484static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003485static int do_md_run(mddev_t * mddev);
3486static int restart_array(mddev_t *mddev);
3487
3488static ssize_t
3489array_state_store(mddev_t *mddev, const char *buf, size_t len)
3490{
3491 int err = -EINVAL;
3492 enum array_state st = match_word(buf, array_states);
3493 switch(st) {
3494 case bad_word:
3495 break;
3496 case clear:
3497 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003498 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003499 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003500 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003501 break;
3502 case inactive:
3503 /* stopping an active array */
3504 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003505 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003506 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003507 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003508 } else
3509 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003510 break;
3511 case suspended:
3512 break; /* not supported yet */
3513 case readonly:
3514 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003515 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003516 else {
3517 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003518 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003519 err = do_md_run(mddev);
3520 }
3521 break;
3522 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003523 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003524 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003525 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003526 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003527 err = restart_array(mddev);
3528 if (err == 0) {
3529 mddev->ro = 2;
3530 set_disk_ro(mddev->gendisk, 0);
3531 }
NeilBrown9e653b62006-06-26 00:27:58 -07003532 } else {
3533 mddev->ro = 2;
3534 err = do_md_run(mddev);
3535 }
3536 break;
3537 case clean:
3538 if (mddev->pers) {
3539 restart_array(mddev);
3540 spin_lock_irq(&mddev->write_lock);
3541 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003542 if (mddev->in_sync == 0) {
3543 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003544 if (mddev->safemode == 1)
3545 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003546 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003547 }
3548 err = 0;
3549 } else
3550 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003551 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003552 } else
3553 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003554 break;
3555 case active:
3556 if (mddev->pers) {
3557 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003558 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003559 wake_up(&mddev->sb_wait);
3560 err = 0;
3561 } else {
3562 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003563 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003564 err = do_md_run(mddev);
3565 }
3566 break;
3567 case write_pending:
3568 case active_idle:
3569 /* these cannot be set */
3570 break;
3571 }
3572 if (err)
3573 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003574 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003575 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003576 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003577 }
NeilBrown9e653b62006-06-26 00:27:58 -07003578}
NeilBrown80ca3a42006-07-10 04:44:18 -07003579static struct md_sysfs_entry md_array_state =
3580__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003581
NeilBrown6d7ff732006-01-06 00:21:16 -08003582static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003583max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3584 return sprintf(page, "%d\n",
3585 atomic_read(&mddev->max_corr_read_errors));
3586}
3587
3588static ssize_t
3589max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3590{
3591 char *e;
3592 unsigned long n = simple_strtoul(buf, &e, 10);
3593
3594 if (*buf && (*e == 0 || *e == '\n')) {
3595 atomic_set(&mddev->max_corr_read_errors, n);
3596 return len;
3597 }
3598 return -EINVAL;
3599}
3600
3601static struct md_sysfs_entry max_corr_read_errors =
3602__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3603 max_corrected_read_errors_store);
3604
3605static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003606null_show(mddev_t *mddev, char *page)
3607{
3608 return -EINVAL;
3609}
3610
3611static ssize_t
3612new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3613{
3614 /* buf must be %d:%d\n? giving major and minor numbers */
3615 /* The new device is added to the array.
3616 * If the array has a persistent superblock, we read the
3617 * superblock to initialise info and check validity.
3618 * Otherwise, only checking done is that in bind_rdev_to_array,
3619 * which mainly checks size.
3620 */
3621 char *e;
3622 int major = simple_strtoul(buf, &e, 10);
3623 int minor;
3624 dev_t dev;
3625 mdk_rdev_t *rdev;
3626 int err;
3627
3628 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3629 return -EINVAL;
3630 minor = simple_strtoul(e+1, &e, 10);
3631 if (*e && *e != '\n')
3632 return -EINVAL;
3633 dev = MKDEV(major, minor);
3634 if (major != MAJOR(dev) ||
3635 minor != MINOR(dev))
3636 return -EOVERFLOW;
3637
3638
3639 if (mddev->persistent) {
3640 rdev = md_import_device(dev, mddev->major_version,
3641 mddev->minor_version);
3642 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3643 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3644 mdk_rdev_t, same_set);
3645 err = super_types[mddev->major_version]
3646 .load_super(rdev, rdev0, mddev->minor_version);
3647 if (err < 0)
3648 goto out;
3649 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003650 } else if (mddev->external)
3651 rdev = md_import_device(dev, -2, -1);
3652 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003653 rdev = md_import_device(dev, -1, -1);
3654
3655 if (IS_ERR(rdev))
3656 return PTR_ERR(rdev);
3657 err = bind_rdev_to_array(rdev, mddev);
3658 out:
3659 if (err)
3660 export_rdev(rdev);
3661 return err ? err : len;
3662}
3663
3664static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003665__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003666
3667static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003668bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3669{
3670 char *end;
3671 unsigned long chunk, end_chunk;
3672
3673 if (!mddev->bitmap)
3674 goto out;
3675 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3676 while (*buf) {
3677 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3678 if (buf == end) break;
3679 if (*end == '-') { /* range */
3680 buf = end + 1;
3681 end_chunk = simple_strtoul(buf, &end, 0);
3682 if (buf == end) break;
3683 }
3684 if (*end && !isspace(*end)) break;
3685 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003686 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003687 }
3688 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3689out:
3690 return len;
3691}
3692
3693static struct md_sysfs_entry md_bitmap =
3694__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3695
3696static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003697size_show(mddev_t *mddev, char *page)
3698{
Andre Noll58c0fed2009-03-31 14:33:13 +11003699 return sprintf(page, "%llu\n",
3700 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003701}
3702
Andre Nolld71f9f82008-07-11 22:02:22 +10003703static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003704
3705static ssize_t
3706size_store(mddev_t *mddev, const char *buf, size_t len)
3707{
3708 /* If array is inactive, we can reduce the component size, but
3709 * not increase it (except from 0).
3710 * If array is active, we can try an on-line resize
3711 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003712 sector_t sectors;
3713 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003714
Andre Noll58c0fed2009-03-31 14:33:13 +11003715 if (err < 0)
3716 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003717 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003718 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003719 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003720 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003721 if (mddev->dev_sectors == 0 ||
3722 mddev->dev_sectors > sectors)
3723 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003724 else
3725 err = -ENOSPC;
3726 }
3727 return err ? err : len;
3728}
3729
3730static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003731__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003732
NeilBrown8bb93aa2006-01-06 00:20:50 -08003733
3734/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003735 * This is one of
3736 * 'none' for arrays with no metadata (good luck...)
3737 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003738 * or N.M for internally known formats
3739 */
3740static ssize_t
3741metadata_show(mddev_t *mddev, char *page)
3742{
3743 if (mddev->persistent)
3744 return sprintf(page, "%d.%d\n",
3745 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003746 else if (mddev->external)
3747 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003748 else
3749 return sprintf(page, "none\n");
3750}
3751
3752static ssize_t
3753metadata_store(mddev_t *mddev, const char *buf, size_t len)
3754{
3755 int major, minor;
3756 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003757 /* Changing the details of 'external' metadata is
3758 * always permitted. Otherwise there must be
3759 * no devices attached to the array.
3760 */
3761 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3762 ;
3763 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003764 return -EBUSY;
3765
3766 if (cmd_match(buf, "none")) {
3767 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003768 mddev->external = 0;
3769 mddev->major_version = 0;
3770 mddev->minor_version = 90;
3771 return len;
3772 }
3773 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003774 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003775 if (namelen >= sizeof(mddev->metadata_type))
3776 namelen = sizeof(mddev->metadata_type)-1;
3777 strncpy(mddev->metadata_type, buf+9, namelen);
3778 mddev->metadata_type[namelen] = 0;
3779 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3780 mddev->metadata_type[--namelen] = 0;
3781 mddev->persistent = 0;
3782 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003783 mddev->major_version = 0;
3784 mddev->minor_version = 90;
3785 return len;
3786 }
3787 major = simple_strtoul(buf, &e, 10);
3788 if (e==buf || *e != '.')
3789 return -EINVAL;
3790 buf = e+1;
3791 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003792 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003793 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003794 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003795 return -ENOENT;
3796 mddev->major_version = major;
3797 mddev->minor_version = minor;
3798 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003799 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003800 return len;
3801}
3802
3803static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003804__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003805
NeilBrowna35b0d62006-01-06 00:20:49 -08003806static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003807action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003808{
NeilBrown7eec3142005-11-08 21:39:44 -08003809 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003810 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3811 type = "frozen";
3812 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003813 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003814 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3815 type = "reshape";
3816 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003817 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3818 type = "resync";
3819 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3820 type = "check";
3821 else
3822 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003823 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003824 type = "recover";
3825 }
3826 return sprintf(page, "%s\n", type);
3827}
3828
NeilBrown7ebc0be2011-01-14 09:14:33 +11003829static void reap_sync_thread(mddev_t *mddev);
3830
NeilBrown24dd4692005-11-08 21:39:26 -08003831static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003832action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003833{
NeilBrown7eec3142005-11-08 21:39:44 -08003834 if (!mddev->pers || !mddev->pers->sync_request)
3835 return -EINVAL;
3836
NeilBrownb6a9ce62009-05-26 09:41:17 +10003837 if (cmd_match(page, "frozen"))
3838 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3839 else
3840 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3841
3842 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003843 if (mddev->sync_thread) {
3844 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003845 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003846 }
NeilBrown03c902e2006-01-06 00:20:46 -08003847 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3848 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003849 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003850 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003851 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003852 else if (cmd_match(page, "recover")) {
3853 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3854 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3855 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003856 int err;
3857 if (mddev->pers->start_reshape == NULL)
3858 return -EINVAL;
3859 err = mddev->pers->start_reshape(mddev);
3860 if (err)
3861 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003862 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003863 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003864 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003865 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003866 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003867 return -EINVAL;
3868 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3869 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003870 }
NeilBrown03c902e2006-01-06 00:20:46 -08003871 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003872 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003873 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003874 return len;
3875}
3876
NeilBrown9d888832005-11-08 21:39:26 -08003877static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003878mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003879{
3880 return sprintf(page, "%llu\n",
3881 (unsigned long long) mddev->resync_mismatches);
3882}
3883
NeilBrown80ca3a42006-07-10 04:44:18 -07003884static struct md_sysfs_entry md_scan_mode =
3885__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003886
NeilBrown96de1e62005-11-08 21:39:39 -08003887
NeilBrown80ca3a42006-07-10 04:44:18 -07003888static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003889
NeilBrown88202a02006-01-06 00:21:36 -08003890static ssize_t
3891sync_min_show(mddev_t *mddev, char *page)
3892{
3893 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3894 mddev->sync_speed_min ? "local": "system");
3895}
3896
3897static ssize_t
3898sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3899{
3900 int min;
3901 char *e;
3902 if (strncmp(buf, "system", 6)==0) {
3903 mddev->sync_speed_min = 0;
3904 return len;
3905 }
3906 min = simple_strtoul(buf, &e, 10);
3907 if (buf == e || (*e && *e != '\n') || min <= 0)
3908 return -EINVAL;
3909 mddev->sync_speed_min = min;
3910 return len;
3911}
3912
3913static struct md_sysfs_entry md_sync_min =
3914__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3915
3916static ssize_t
3917sync_max_show(mddev_t *mddev, char *page)
3918{
3919 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3920 mddev->sync_speed_max ? "local": "system");
3921}
3922
3923static ssize_t
3924sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3925{
3926 int max;
3927 char *e;
3928 if (strncmp(buf, "system", 6)==0) {
3929 mddev->sync_speed_max = 0;
3930 return len;
3931 }
3932 max = simple_strtoul(buf, &e, 10);
3933 if (buf == e || (*e && *e != '\n') || max <= 0)
3934 return -EINVAL;
3935 mddev->sync_speed_max = max;
3936 return len;
3937}
3938
3939static struct md_sysfs_entry md_sync_max =
3940__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3941
Iustin Popd7f3d292007-10-16 23:30:54 -07003942static ssize_t
3943degraded_show(mddev_t *mddev, char *page)
3944{
3945 return sprintf(page, "%d\n", mddev->degraded);
3946}
3947static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003948
3949static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003950sync_force_parallel_show(mddev_t *mddev, char *page)
3951{
3952 return sprintf(page, "%d\n", mddev->parallel_resync);
3953}
3954
3955static ssize_t
3956sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3957{
3958 long n;
3959
3960 if (strict_strtol(buf, 10, &n))
3961 return -EINVAL;
3962
3963 if (n != 0 && n != 1)
3964 return -EINVAL;
3965
3966 mddev->parallel_resync = n;
3967
3968 if (mddev->sync_thread)
3969 wake_up(&resync_wait);
3970
3971 return len;
3972}
3973
3974/* force parallel resync, even with shared block devices */
3975static struct md_sysfs_entry md_sync_force_parallel =
3976__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3977 sync_force_parallel_show, sync_force_parallel_store);
3978
3979static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003980sync_speed_show(mddev_t *mddev, char *page)
3981{
3982 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003983 if (mddev->curr_resync == 0)
3984 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003985 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3986 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003987 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003988 db = resync - mddev->resync_mark_cnt;
3989 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003990}
3991
NeilBrown80ca3a42006-07-10 04:44:18 -07003992static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003993
3994static ssize_t
3995sync_completed_show(mddev_t *mddev, char *page)
3996{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11003997 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003998
NeilBrownacb180b2009-04-14 16:28:34 +10003999 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4000 return sprintf(page, "none\n");
4001
NeilBrown88202a02006-01-06 00:21:36 -08004002 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004003 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004004 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004005 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004006
NeilBrownacb180b2009-04-14 16:28:34 +10004007 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004008 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004009}
4010
NeilBrown80ca3a42006-07-10 04:44:18 -07004011static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004012
NeilBrowne464eaf2006-03-27 01:18:14 -08004013static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10004014min_sync_show(mddev_t *mddev, char *page)
4015{
4016 return sprintf(page, "%llu\n",
4017 (unsigned long long)mddev->resync_min);
4018}
4019static ssize_t
4020min_sync_store(mddev_t *mddev, const char *buf, size_t len)
4021{
4022 unsigned long long min;
4023 if (strict_strtoull(buf, 10, &min))
4024 return -EINVAL;
4025 if (min > mddev->resync_max)
4026 return -EINVAL;
4027 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4028 return -EBUSY;
4029
4030 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004031 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004032 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004033 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004034 return -EINVAL;
4035 }
4036 mddev->resync_min = min;
4037
4038 return len;
4039}
4040
4041static struct md_sysfs_entry md_min_sync =
4042__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4043
4044static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08004045max_sync_show(mddev_t *mddev, char *page)
4046{
4047 if (mddev->resync_max == MaxSector)
4048 return sprintf(page, "max\n");
4049 else
4050 return sprintf(page, "%llu\n",
4051 (unsigned long long)mddev->resync_max);
4052}
4053static ssize_t
4054max_sync_store(mddev_t *mddev, const char *buf, size_t len)
4055{
4056 if (strncmp(buf, "max", 3) == 0)
4057 mddev->resync_max = MaxSector;
4058 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004059 unsigned long long max;
4060 if (strict_strtoull(buf, 10, &max))
4061 return -EINVAL;
4062 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004063 return -EINVAL;
4064 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004065 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004066 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4067 return -EBUSY;
4068
4069 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004070 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004071 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004072 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004073 return -EINVAL;
4074 }
4075 mddev->resync_max = max;
4076 }
4077 wake_up(&mddev->recovery_wait);
4078 return len;
4079}
4080
4081static struct md_sysfs_entry md_max_sync =
4082__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4083
4084static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004085suspend_lo_show(mddev_t *mddev, char *page)
4086{
4087 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4088}
4089
4090static ssize_t
4091suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4092{
4093 char *e;
4094 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004095 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004096
NeilBrownb8d966e2009-07-01 11:14:04 +10004097 if (mddev->pers == NULL ||
4098 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004099 return -EINVAL;
4100 if (buf == e || (*e && *e != '\n'))
4101 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004102
4103 mddev->suspend_lo = new;
4104 if (new >= old)
4105 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004106 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004107 else {
4108 /* Expanding suspended region - need to wait */
4109 mddev->pers->quiesce(mddev, 1);
4110 mddev->pers->quiesce(mddev, 0);
4111 }
4112 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004113}
4114static struct md_sysfs_entry md_suspend_lo =
4115__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4116
4117
4118static ssize_t
4119suspend_hi_show(mddev_t *mddev, char *page)
4120{
4121 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4122}
4123
4124static ssize_t
4125suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4126{
4127 char *e;
4128 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004129 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004130
NeilBrownb8d966e2009-07-01 11:14:04 +10004131 if (mddev->pers == NULL ||
4132 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004133 return -EINVAL;
4134 if (buf == e || (*e && *e != '\n'))
4135 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004136
4137 mddev->suspend_hi = new;
4138 if (new <= old)
4139 /* Shrinking suspended region */
4140 mddev->pers->quiesce(mddev, 2);
4141 else {
4142 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004143 mddev->pers->quiesce(mddev, 1);
4144 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004145 }
4146 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004147}
4148static struct md_sysfs_entry md_suspend_hi =
4149__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4150
NeilBrown08a02ec2007-05-09 02:35:38 -07004151static ssize_t
4152reshape_position_show(mddev_t *mddev, char *page)
4153{
4154 if (mddev->reshape_position != MaxSector)
4155 return sprintf(page, "%llu\n",
4156 (unsigned long long)mddev->reshape_position);
4157 strcpy(page, "none\n");
4158 return 5;
4159}
4160
4161static ssize_t
4162reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4163{
4164 char *e;
4165 unsigned long long new = simple_strtoull(buf, &e, 10);
4166 if (mddev->pers)
4167 return -EBUSY;
4168 if (buf == e || (*e && *e != '\n'))
4169 return -EINVAL;
4170 mddev->reshape_position = new;
4171 mddev->delta_disks = 0;
4172 mddev->new_level = mddev->level;
4173 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004174 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004175 return len;
4176}
4177
4178static struct md_sysfs_entry md_reshape_position =
4179__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4180 reshape_position_store);
4181
Dan Williamsb522adc2009-03-31 15:00:31 +11004182static ssize_t
4183array_size_show(mddev_t *mddev, char *page)
4184{
4185 if (mddev->external_size)
4186 return sprintf(page, "%llu\n",
4187 (unsigned long long)mddev->array_sectors/2);
4188 else
4189 return sprintf(page, "default\n");
4190}
4191
4192static ssize_t
4193array_size_store(mddev_t *mddev, const char *buf, size_t len)
4194{
4195 sector_t sectors;
4196
4197 if (strncmp(buf, "default", 7) == 0) {
4198 if (mddev->pers)
4199 sectors = mddev->pers->size(mddev, 0, 0);
4200 else
4201 sectors = mddev->array_sectors;
4202
4203 mddev->external_size = 0;
4204 } else {
4205 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4206 return -EINVAL;
4207 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004208 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004209
4210 mddev->external_size = 1;
4211 }
4212
4213 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004214 if (mddev->pers) {
4215 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004216 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004217 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004218 return len;
4219}
4220
4221static struct md_sysfs_entry md_array_size =
4222__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4223 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004224
NeilBrowneae17012005-11-08 21:39:23 -08004225static struct attribute *md_default_attrs[] = {
4226 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004227 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004228 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004229 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004230 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004231 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004232 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004233 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004234 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004235 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004236 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004237 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004238 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004239 NULL,
4240};
4241
4242static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004243 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004244 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004245 &md_sync_min.attr,
4246 &md_sync_max.attr,
4247 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004248 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004249 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004250 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004251 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004252 &md_suspend_lo.attr,
4253 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004254 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004255 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004256 NULL,
4257};
NeilBrown411036f2005-11-08 21:39:40 -08004258static struct attribute_group md_redundancy_group = {
4259 .name = NULL,
4260 .attrs = md_redundancy_attrs,
4261};
4262
NeilBrowneae17012005-11-08 21:39:23 -08004263
4264static ssize_t
4265md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4266{
4267 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4268 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004269 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004270
4271 if (!entry->show)
4272 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004273 rv = mddev_lock(mddev);
4274 if (!rv) {
4275 rv = entry->show(mddev, page);
4276 mddev_unlock(mddev);
4277 }
NeilBrown96de1e62005-11-08 21:39:39 -08004278 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004279}
4280
4281static ssize_t
4282md_attr_store(struct kobject *kobj, struct attribute *attr,
4283 const char *page, size_t length)
4284{
4285 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4286 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004287 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004288
4289 if (!entry->store)
4290 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004291 if (!capable(CAP_SYS_ADMIN))
4292 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004293 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004294 if (mddev->hold_active == UNTIL_IOCTL)
4295 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004296 if (!rv) {
4297 rv = entry->store(mddev, page, length);
4298 mddev_unlock(mddev);
4299 }
NeilBrown96de1e62005-11-08 21:39:39 -08004300 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004301}
4302
4303static void md_free(struct kobject *ko)
4304{
4305 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004306
4307 if (mddev->sysfs_state)
4308 sysfs_put(mddev->sysfs_state);
4309
4310 if (mddev->gendisk) {
4311 del_gendisk(mddev->gendisk);
4312 put_disk(mddev->gendisk);
4313 }
4314 if (mddev->queue)
4315 blk_cleanup_queue(mddev->queue);
4316
NeilBrowneae17012005-11-08 21:39:23 -08004317 kfree(mddev);
4318}
4319
Emese Revfy52cf25d2010-01-19 02:58:23 +01004320static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004321 .show = md_attr_show,
4322 .store = md_attr_store,
4323};
4324static struct kobj_type md_ktype = {
4325 .release = md_free,
4326 .sysfs_ops = &md_sysfs_ops,
4327 .default_attrs = md_default_attrs,
4328};
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330int mdp_major = 0;
4331
Dan Williams5fd3a172009-03-04 00:57:25 -07004332static void mddev_delayed_delete(struct work_struct *ws)
4333{
4334 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4335
NeilBrown43a70502009-12-14 12:49:55 +11004336 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004337 kobject_del(&mddev->kobj);
4338 kobject_put(&mddev->kobj);
4339}
4340
NeilBrownefeb53c2009-01-09 08:31:10 +11004341static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004343 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 mddev_t *mddev = mddev_find(dev);
4345 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004346 int partitioned;
4347 int shift;
4348 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004349 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004352 return -ENODEV;
4353
4354 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4355 shift = partitioned ? MdpMinorShift : 0;
4356 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Tejun Heoe804ac72010-10-15 15:36:08 +02004358 /* wait for any previous instance of this device to be
4359 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004360 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004361 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004362
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004363 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004364 error = -EEXIST;
4365 if (mddev->gendisk)
4366 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004367
4368 if (name) {
4369 /* Need to ensure that 'name' is not a duplicate.
4370 */
4371 mddev_t *mddev2;
4372 spin_lock(&all_mddevs_lock);
4373
4374 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4375 if (mddev2->gendisk &&
4376 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4377 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004378 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004379 }
4380 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 }
NeilBrown8b765392009-01-09 08:31:08 +11004382
NeilBrown0909dc42009-07-01 12:27:21 +10004383 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004384 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004385 if (!mddev->queue)
4386 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004387 mddev->queue->queuedata = mddev;
4388
NeilBrown409c57f2009-03-31 14:39:39 +11004389 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 disk = alloc_disk(1 << shift);
4392 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004393 blk_cleanup_queue(mddev->queue);
4394 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004395 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004397 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004399 if (name)
4400 strcpy(disk->disk_name, name);
4401 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004403 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 disk->fops = &md_fops;
4406 disk->private_data = mddev;
4407 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004408 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004409 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004410 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004411 * remove it now.
4412 */
4413 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004415 /* As soon as we call add_disk(), another thread could get
4416 * through to md_open, so make sure it doesn't get too far
4417 */
4418 mutex_lock(&mddev->open_mutex);
4419 add_disk(disk);
4420
Tejun Heoed9e1982008-08-25 19:56:05 +09004421 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4422 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004423 if (error) {
4424 /* This isn't possible, but as kobject_init_and_add is marked
4425 * __must_check, we must do something with the result
4426 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004427 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4428 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004429 error = 0;
4430 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004431 if (mddev->kobj.sd &&
4432 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004433 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004434 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004435 abort:
4436 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004437 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004438 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004439 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004440 }
NeilBrownd3374822009-01-09 08:31:10 +11004441 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004442 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004443}
4444
4445static struct kobject *md_probe(dev_t dev, int *part, void *data)
4446{
4447 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return NULL;
4449}
4450
NeilBrownefeb53c2009-01-09 08:31:10 +11004451static int add_named_array(const char *val, struct kernel_param *kp)
4452{
4453 /* val must be "md_*" where * is not all digits.
4454 * We allocate an array with a large free minor number, and
4455 * set the name to val. val must not already be an active name.
4456 */
4457 int len = strlen(val);
4458 char buf[DISK_NAME_LEN];
4459
4460 while (len && val[len-1] == '\n')
4461 len--;
4462 if (len >= DISK_NAME_LEN)
4463 return -E2BIG;
4464 strlcpy(buf, val, len+1);
4465 if (strncmp(buf, "md_", 3) != 0)
4466 return -EINVAL;
4467 return md_alloc(0, buf);
4468}
4469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470static void md_safemode_timeout(unsigned long data)
4471{
4472 mddev_t *mddev = (mddev_t *) data;
4473
Neil Brown0fd62b82008-06-28 08:31:36 +10004474 if (!atomic_read(&mddev->writes_pending)) {
4475 mddev->safemode = 1;
4476 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004477 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 md_wakeup_thread(mddev->thread);
4480}
4481
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004482static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
NeilBrown390ee602010-06-01 19:37:27 +10004484int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485{
NeilBrown2604b702006-01-06 00:20:36 -08004486 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004488 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
NeilBrowna757e642005-04-16 15:26:42 -07004490 if (list_empty(&mddev->disks))
4491 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
4494 if (mddev->pers)
4495 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004496 /* Cannot run until previous stop completes properly */
4497 if (mddev->sysfs_active)
4498 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /*
4501 * Analyze all RAID superblock(s)
4502 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004503 if (!mddev->raid_disks) {
4504 if (!mddev->persistent)
4505 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004506 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
NeilBrownd9d166c2006-01-06 00:20:51 -08004509 if (mddev->level != LEVEL_NONE)
4510 request_module("md-level-%d", mddev->level);
4511 else if (mddev->clevel[0])
4512 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
4514 /*
4515 * Drop all container device buffers, from now on
4516 * the only valid external interface is through the md
4517 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004519 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004520 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 continue;
4522 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004523 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004524
4525 /* perform some consistency tests on the device.
4526 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004527 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004528 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004529 if (rdev->meta_bdev) {
4530 /* Nothing to check */;
4531 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004532 if (mddev->dev_sectors &&
4533 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004534 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004535 printk("md: %s: data overlaps metadata\n",
4536 mdname(mddev));
4537 return -EINVAL;
4538 }
4539 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004540 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004541 > rdev->data_offset) {
4542 printk("md: %s: metadata overlaps data\n",
4543 mdname(mddev));
4544 return -EINVAL;
4545 }
4546 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004547 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 }
4549
NeilBrowna167f662010-10-26 18:31:13 +11004550 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004551 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
4552 sizeof(mddev_t *));
NeilBrowna167f662010-10-26 18:31:13 +11004553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004555 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004556 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004558 if (mddev->level != LEVEL_NONE)
4559 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4560 mddev->level);
4561 else
4562 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4563 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return -EINVAL;
4565 }
NeilBrown2604b702006-01-06 00:20:36 -08004566 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004568 if (mddev->level != pers->level) {
4569 mddev->level = pers->level;
4570 mddev->new_level = pers->level;
4571 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004572 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
NeilBrownf6705572006-03-27 01:18:11 -08004574 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004575 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004576 /* This personality cannot handle reshaping... */
4577 mddev->pers = NULL;
4578 module_put(pers->owner);
4579 return -EINVAL;
4580 }
4581
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004582 if (pers->sync_request) {
4583 /* Warn if this is a potentially silly
4584 * configuration.
4585 */
4586 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4587 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004588 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004589
4590 list_for_each_entry(rdev, &mddev->disks, same_set)
4591 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004592 if (rdev < rdev2 &&
4593 rdev->bdev->bd_contains ==
4594 rdev2->bdev->bd_contains) {
4595 printk(KERN_WARNING
4596 "%s: WARNING: %s appears to be"
4597 " on the same physical disk as"
4598 " %s.\n",
4599 mdname(mddev),
4600 bdevname(rdev->bdev,b),
4601 bdevname(rdev2->bdev,b2));
4602 warned = 1;
4603 }
4604 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004605
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004606 if (warned)
4607 printk(KERN_WARNING
4608 "True protection against single-disk"
4609 " failure might be compromised.\n");
4610 }
4611
NeilBrown657390d2005-08-26 18:34:16 -07004612 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004613 /* may be over-ridden by personality */
4614 mddev->resync_max_sectors = mddev->dev_sectors;
4615
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004616 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
NeilBrown0f9552b2009-12-30 12:08:50 +11004618 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004619 mddev->ro = 2; /* read-only, but switch on first write */
4620
NeilBrownb15c2e52006-01-06 00:20:16 -08004621 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004622 if (err)
4623 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004624 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4625 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4626 " but 'external_size' not in effect?\n", __func__);
4627 printk(KERN_ERR
4628 "md: invalid array_size %llu > default size %llu\n",
4629 (unsigned long long)mddev->array_sectors / 2,
4630 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4631 err = -EINVAL;
4632 mddev->pers->stop(mddev);
4633 }
4634 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004635 err = bitmap_create(mddev);
4636 if (err) {
4637 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4638 mdname(mddev), err);
4639 mddev->pers->stop(mddev);
4640 }
4641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 module_put(mddev->pers->owner);
4644 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004645 bitmap_destroy(mddev);
4646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004648 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004649 if (mddev->kobj.sd &&
4650 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004651 printk(KERN_WARNING
4652 "md: cannot register extra attributes for %s\n",
4653 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004654 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004655 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004656 mddev->ro = 0;
4657
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004659 atomic_set(&mddev->max_corr_read_errors,
4660 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 mddev->safemode = 0;
4662 mddev->safemode_timer.function = md_safemode_timeout;
4663 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004664 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004666 smp_wmb();
4667 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004668 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004669 if (rdev->raid_disk >= 0)
4670 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004671 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4674
NeilBrown850b2b42006-10-03 01:15:46 -07004675 if (mddev->flags)
4676 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
NeilBrownd7603b72006-01-06 00:20:30 -08004678 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004679 sysfs_notify_dirent_safe(mddev->sysfs_state);
4680 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004681 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 return 0;
4683}
NeilBrown390ee602010-06-01 19:37:27 +10004684EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
NeilBrownfe60b012010-03-29 11:10:42 +11004686static int do_md_run(mddev_t *mddev)
4687{
4688 int err;
4689
4690 err = md_run(mddev);
4691 if (err)
4692 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004693 err = bitmap_load(mddev);
4694 if (err) {
4695 bitmap_destroy(mddev);
4696 goto out;
4697 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004698
4699 md_wakeup_thread(mddev->thread);
4700 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4701
NeilBrownfe60b012010-03-29 11:10:42 +11004702 set_capacity(mddev->gendisk, mddev->array_sectors);
4703 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004704 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004705 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4706out:
4707 return err;
4708}
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710static int restart_array(mddev_t *mddev)
4711{
4712 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Andre Noll80fab1d2008-07-11 22:02:21 +10004714 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004716 return -ENXIO;
4717 if (!mddev->pers)
4718 return -EINVAL;
4719 if (!mddev->ro)
4720 return -EBUSY;
4721 mddev->safemode = 0;
4722 mddev->ro = 0;
4723 set_disk_ro(disk, 0);
4724 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4725 mdname(mddev));
4726 /* Kick recovery or resync if necessary */
4727 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4728 md_wakeup_thread(mddev->thread);
4729 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004730 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004731 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
4733
NeilBrownacc55e22006-06-26 00:27:47 -07004734/* similar to deny_write_access, but accounts for our holding a reference
4735 * to the file ourselves */
4736static int deny_bitmap_write_access(struct file * file)
4737{
4738 struct inode *inode = file->f_mapping->host;
4739
4740 spin_lock(&inode->i_lock);
4741 if (atomic_read(&inode->i_writecount) > 1) {
4742 spin_unlock(&inode->i_lock);
4743 return -ETXTBSY;
4744 }
4745 atomic_set(&inode->i_writecount, -1);
4746 spin_unlock(&inode->i_lock);
4747
4748 return 0;
4749}
4750
NeilBrown43a70502009-12-14 12:49:55 +11004751void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004752{
4753 struct inode *inode = file->f_mapping->host;
4754
4755 spin_lock(&inode->i_lock);
4756 atomic_set(&inode->i_writecount, 1);
4757 spin_unlock(&inode->i_lock);
4758}
4759
NeilBrown6177b472010-03-29 11:37:13 +11004760static void md_clean(mddev_t *mddev)
4761{
4762 mddev->array_sectors = 0;
4763 mddev->external_size = 0;
4764 mddev->dev_sectors = 0;
4765 mddev->raid_disks = 0;
4766 mddev->recovery_cp = 0;
4767 mddev->resync_min = 0;
4768 mddev->resync_max = MaxSector;
4769 mddev->reshape_position = MaxSector;
4770 mddev->external = 0;
4771 mddev->persistent = 0;
4772 mddev->level = LEVEL_NONE;
4773 mddev->clevel[0] = 0;
4774 mddev->flags = 0;
4775 mddev->ro = 0;
4776 mddev->metadata_type[0] = 0;
4777 mddev->chunk_sectors = 0;
4778 mddev->ctime = mddev->utime = 0;
4779 mddev->layout = 0;
4780 mddev->max_disks = 0;
4781 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004782 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004783 mddev->delta_disks = 0;
4784 mddev->new_level = LEVEL_NONE;
4785 mddev->new_layout = 0;
4786 mddev->new_chunk_sectors = 0;
4787 mddev->curr_resync = 0;
4788 mddev->resync_mismatches = 0;
4789 mddev->suspend_lo = mddev->suspend_hi = 0;
4790 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4791 mddev->recovery = 0;
4792 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004793 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004794 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004795 mddev->safemode = 0;
4796 mddev->bitmap_info.offset = 0;
4797 mddev->bitmap_info.default_offset = 0;
4798 mddev->bitmap_info.chunksize = 0;
4799 mddev->bitmap_info.daemon_sleep = 0;
4800 mddev->bitmap_info.max_write_behind = 0;
4801}
4802
NeilBrowndefad612011-01-14 09:14:33 +11004803static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004804{
4805 if (mddev->sync_thread) {
4806 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4807 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004808 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004809 }
4810
4811 del_timer_sync(&mddev->safemode_timer);
4812
4813 bitmap_flush(mddev);
4814 md_super_wait(mddev);
4815
4816 if (!mddev->in_sync || mddev->flags) {
4817 /* mark array as shutdown cleanly */
4818 mddev->in_sync = 1;
4819 md_update_sb(mddev, 1);
4820 }
4821}
NeilBrowndefad612011-01-14 09:14:33 +11004822
4823void md_stop_writes(mddev_t *mddev)
4824{
4825 mddev_lock(mddev);
4826 __md_stop_writes(mddev);
4827 mddev_unlock(mddev);
4828}
NeilBrown390ee602010-06-01 19:37:27 +10004829EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004830
NeilBrown390ee602010-06-01 19:37:27 +10004831void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004832{
NeilBrown0ca69882011-01-14 09:14:33 +11004833 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004834 mddev->pers->stop(mddev);
4835 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4836 mddev->to_remove = &md_redundancy_group;
4837 module_put(mddev->pers->owner);
4838 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004839 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004840}
NeilBrown390ee602010-06-01 19:37:27 +10004841EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004842
NeilBrowna4bd82d2010-03-29 13:23:10 +11004843static int md_set_readonly(mddev_t *mddev, int is_open)
4844{
4845 int err = 0;
4846 mutex_lock(&mddev->open_mutex);
4847 if (atomic_read(&mddev->openers) > is_open) {
4848 printk("md: %s still in use.\n",mdname(mddev));
4849 err = -EBUSY;
4850 goto out;
4851 }
4852 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004853 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004854
4855 err = -ENXIO;
4856 if (mddev->ro==1)
4857 goto out;
4858 mddev->ro = 1;
4859 set_disk_ro(mddev->gendisk, 1);
4860 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004861 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004862 err = 0;
4863 }
4864out:
4865 mutex_unlock(&mddev->open_mutex);
4866 return err;
4867}
4868
NeilBrown9e653b62006-06-26 00:27:58 -07004869/* mode:
4870 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004871 * 2 - stop but do not disassemble array
4872 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004873static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004876 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
NeilBrownc8c00a62009-08-10 12:50:52 +10004878 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004879 if (atomic_read(&mddev->openers) > is_open ||
4880 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004881 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004882 mutex_unlock(&mddev->open_mutex);
4883 return -EBUSY;
4884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
NeilBrown6e17b022010-08-07 21:41:19 +10004886 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004887 if (mddev->ro)
4888 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004889
NeilBrowndefad612011-01-14 09:14:33 +11004890 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004891 md_stop(mddev);
4892 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004893 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004894
NeilBrowna4bd82d2010-03-29 13:23:10 +11004895 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004896 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004897
NeilBrowna4bd82d2010-03-29 13:23:10 +11004898 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004899 if (rdev->raid_disk >= 0)
4900 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10004901
NeilBrowna4bd82d2010-03-29 13:23:10 +11004902 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004903 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004904 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004905 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004906
NeilBrowna4bd82d2010-03-29 13:23:10 +11004907 if (mddev->ro)
4908 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004909 } else
4910 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 /*
4912 * Free resources if final stop
4913 */
NeilBrown9e653b62006-06-26 00:27:58 -07004914 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4916
NeilBrown978f9462006-02-02 14:28:05 -08004917 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004918 if (mddev->bitmap_info.file) {
4919 restore_bitmap_write_access(mddev->bitmap_info.file);
4920 fput(mddev->bitmap_info.file);
4921 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004922 }
NeilBrownc3d97142009-12-14 12:49:52 +11004923 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 export_array(mddev);
4926
NeilBrown6177b472010-03-29 11:37:13 +11004927 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004928 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004929 if (mddev->hold_active == UNTIL_STOP)
4930 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004931 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004932 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004933 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004934 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004938#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939static void autorun_array(mddev_t *mddev)
4940{
4941 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 int err;
4943
NeilBrowna757e642005-04-16 15:26:42 -07004944 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947 printk(KERN_INFO "md: running: ");
4948
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004949 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 char b[BDEVNAME_SIZE];
4951 printk("<%s>", bdevname(rdev->bdev,b));
4952 }
4953 printk("\n");
4954
NeilBrownd710e132008-10-13 11:55:12 +11004955 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 if (err) {
4957 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004958 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
4960}
4961
4962/*
4963 * lets try to run arrays based on all disks that have arrived
4964 * until now. (those are in pending_raid_disks)
4965 *
4966 * the method: pick the first pending disk, collect all disks with
4967 * the same UUID, remove all from the pending list and put them into
4968 * the 'same_array' list. Then order this list based on superblock
4969 * update time (freshest comes first), kick out 'old' disks and
4970 * compare superblocks. If everything's fine then run it.
4971 *
4972 * If "unit" is allocated, then bump its reference count
4973 */
4974static void autorun_devices(int part)
4975{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004976 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 mddev_t *mddev;
4978 char b[BDEVNAME_SIZE];
4979
4980 printk(KERN_INFO "md: autorun ...\n");
4981 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004982 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004984 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 rdev0 = list_entry(pending_raid_disks.next,
4986 mdk_rdev_t, same_set);
4987
4988 printk(KERN_INFO "md: considering %s ...\n",
4989 bdevname(rdev0->bdev,b));
4990 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004991 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 if (super_90_load(rdev, rdev0, 0) >= 0) {
4993 printk(KERN_INFO "md: adding %s ...\n",
4994 bdevname(rdev->bdev,b));
4995 list_move(&rdev->same_set, &candidates);
4996 }
4997 /*
4998 * now we have a set of devices, with all of them having
4999 * mostly sane superblocks. It's time to allocate the
5000 * mddev.
5001 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005002 if (part) {
5003 dev = MKDEV(mdp_major,
5004 rdev0->preferred_minor << MdpMinorShift);
5005 unit = MINOR(dev) >> MdpMinorShift;
5006 } else {
5007 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5008 unit = MINOR(dev);
5009 }
5010 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5012 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5013 break;
5014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 md_probe(dev, NULL, NULL);
5017 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005018 if (!mddev || !mddev->gendisk) {
5019 if (mddev)
5020 mddev_put(mddev);
5021 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 "md: cannot allocate memory for md drive.\n");
5023 break;
5024 }
5025 if (mddev_lock(mddev))
5026 printk(KERN_WARNING "md: %s locked, cannot run\n",
5027 mdname(mddev));
5028 else if (mddev->raid_disks || mddev->major_version
5029 || !list_empty(&mddev->disks)) {
5030 printk(KERN_WARNING
5031 "md: %s already running, cannot run %s\n",
5032 mdname(mddev), bdevname(rdev0->bdev,b));
5033 mddev_unlock(mddev);
5034 } else {
5035 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005036 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005037 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 list_del_init(&rdev->same_set);
5039 if (bind_rdev_to_array(rdev, mddev))
5040 export_rdev(rdev);
5041 }
5042 autorun_array(mddev);
5043 mddev_unlock(mddev);
5044 }
5045 /* on success, candidates will be empty, on error
5046 * it won't...
5047 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005048 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005049 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 mddev_put(mddev);
5053 }
5054 printk(KERN_INFO "md: ... autorun DONE.\n");
5055}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005056#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058static int get_version(void __user * arg)
5059{
5060 mdu_version_t ver;
5061
5062 ver.major = MD_MAJOR_VERSION;
5063 ver.minor = MD_MINOR_VERSION;
5064 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5065
5066 if (copy_to_user(arg, &ver, sizeof(ver)))
5067 return -EFAULT;
5068
5069 return 0;
5070}
5071
5072static int get_array_info(mddev_t * mddev, void __user * arg)
5073{
5074 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005075 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
NeilBrowna9f326e2009-09-23 18:06:41 +10005078 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005079 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005081 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 failed++;
5083 else {
5084 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005085 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005086 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 else
5088 spare++;
5089 }
5090 }
5091
5092 info.major_version = mddev->major_version;
5093 info.minor_version = mddev->minor_version;
5094 info.patch_version = MD_PATCHLEVEL_VERSION;
5095 info.ctime = mddev->ctime;
5096 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005097 info.size = mddev->dev_sectors / 2;
5098 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005099 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 info.nr_disks = nr;
5101 info.raid_disks = mddev->raid_disks;
5102 info.md_minor = mddev->md_minor;
5103 info.not_persistent= !mddev->persistent;
5104
5105 info.utime = mddev->utime;
5106 info.state = 0;
5107 if (mddev->in_sync)
5108 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005109 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005110 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005111 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 info.working_disks = working;
5113 info.failed_disks = failed;
5114 info.spare_disks = spare;
5115
5116 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005117 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
5119 if (copy_to_user(arg, &info, sizeof(info)))
5120 return -EFAULT;
5121
5122 return 0;
5123}
5124
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005125static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005126{
5127 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5128 char *ptr, *buf = NULL;
5129 int err = -ENOMEM;
5130
Dan Williamsb5470dc2008-06-27 21:44:04 -07005131 if (md_allow_write(mddev))
5132 file = kmalloc(sizeof(*file), GFP_NOIO);
5133 else
5134 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005135
NeilBrown32a76272005-06-21 17:17:14 -07005136 if (!file)
5137 goto out;
5138
5139 /* bitmap disabled, zero the first byte and copy out */
5140 if (!mddev->bitmap || !mddev->bitmap->file) {
5141 file->pathname[0] = '\0';
5142 goto copy_out;
5143 }
5144
5145 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5146 if (!buf)
5147 goto out;
5148
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005149 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5150 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005151 goto out;
5152
5153 strcpy(file->pathname, ptr);
5154
5155copy_out:
5156 err = 0;
5157 if (copy_to_user(arg, file, sizeof(*file)))
5158 err = -EFAULT;
5159out:
5160 kfree(buf);
5161 kfree(file);
5162 return err;
5163}
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165static int get_disk_info(mddev_t * mddev, void __user * arg)
5166{
5167 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 mdk_rdev_t *rdev;
5169
5170 if (copy_from_user(&info, arg, sizeof(info)))
5171 return -EFAULT;
5172
Andre Noll26ef3792008-07-11 22:02:21 +10005173 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 if (rdev) {
5175 info.major = MAJOR(rdev->bdev->bd_dev);
5176 info.minor = MINOR(rdev->bdev->bd_dev);
5177 info.raid_disk = rdev->raid_disk;
5178 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005179 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005181 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 info.state |= (1<<MD_DISK_ACTIVE);
5183 info.state |= (1<<MD_DISK_SYNC);
5184 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005185 if (test_bit(WriteMostly, &rdev->flags))
5186 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 } else {
5188 info.major = info.minor = 0;
5189 info.raid_disk = -1;
5190 info.state = (1<<MD_DISK_REMOVED);
5191 }
5192
5193 if (copy_to_user(arg, &info, sizeof(info)))
5194 return -EFAULT;
5195
5196 return 0;
5197}
5198
5199static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5200{
5201 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5202 mdk_rdev_t *rdev;
5203 dev_t dev = MKDEV(info->major,info->minor);
5204
5205 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5206 return -EOVERFLOW;
5207
5208 if (!mddev->raid_disks) {
5209 int err;
5210 /* expecting a device which has a superblock */
5211 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5212 if (IS_ERR(rdev)) {
5213 printk(KERN_WARNING
5214 "md: md_import_device returned %ld\n",
5215 PTR_ERR(rdev));
5216 return PTR_ERR(rdev);
5217 }
5218 if (!list_empty(&mddev->disks)) {
5219 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5220 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005221 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 .load_super(rdev, rdev0, mddev->minor_version);
5223 if (err < 0) {
5224 printk(KERN_WARNING
5225 "md: %s has different UUID to %s\n",
5226 bdevname(rdev->bdev,b),
5227 bdevname(rdev0->bdev,b2));
5228 export_rdev(rdev);
5229 return -EINVAL;
5230 }
5231 }
5232 err = bind_rdev_to_array(rdev, mddev);
5233 if (err)
5234 export_rdev(rdev);
5235 return err;
5236 }
5237
5238 /*
5239 * add_new_disk can be used once the array is assembled
5240 * to add "hot spares". They must already have a superblock
5241 * written
5242 */
5243 if (mddev->pers) {
5244 int err;
5245 if (!mddev->pers->hot_add_disk) {
5246 printk(KERN_WARNING
5247 "%s: personality does not support diskops!\n",
5248 mdname(mddev));
5249 return -EINVAL;
5250 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005251 if (mddev->persistent)
5252 rdev = md_import_device(dev, mddev->major_version,
5253 mddev->minor_version);
5254 else
5255 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 if (IS_ERR(rdev)) {
5257 printk(KERN_WARNING
5258 "md: md_import_device returned %ld\n",
5259 PTR_ERR(rdev));
5260 return PTR_ERR(rdev);
5261 }
NeilBrown1a855a02010-12-09 16:36:28 +11005262 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005263 if (!mddev->persistent) {
5264 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005265 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005266 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005267 set_bit(In_sync, &rdev->flags);
5268 } else
NeilBrown41158c72005-06-21 17:17:25 -07005269 rdev->raid_disk = -1;
5270 } else
5271 super_types[mddev->major_version].
5272 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005273 if ((info->state & (1<<MD_DISK_SYNC)) &&
5274 (!test_bit(In_sync, &rdev->flags) ||
5275 rdev->raid_disk != info->raid_disk)) {
5276 /* This was a hot-add request, but events doesn't
5277 * match, so reject it.
5278 */
5279 export_rdev(rdev);
5280 return -EINVAL;
5281 }
5282
NeilBrown1a855a02010-12-09 16:36:28 +11005283 if (test_bit(In_sync, &rdev->flags))
5284 rdev->saved_raid_disk = rdev->raid_disk;
5285 else
5286 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005287
NeilBrownb2d444d2005-11-08 21:39:31 -08005288 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005289 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5290 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005291 else
5292 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 rdev->raid_disk = -1;
5295 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005296 if (!err && !mddev->pers->hot_remove_disk) {
5297 /* If there is hot_add_disk but no hot_remove_disk
5298 * then added disks for geometry changes,
5299 * and should be added immediately.
5300 */
5301 super_types[mddev->major_version].
5302 validate_super(mddev, rdev);
5303 err = mddev->pers->hot_add_disk(mddev, rdev);
5304 if (err)
5305 unbind_rdev_from_array(rdev);
5306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 if (err)
5308 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005309 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005310 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005311
NeilBrown17571282006-12-10 02:20:52 -08005312 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005313 if (mddev->degraded)
5314 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005315 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005316 if (!err)
5317 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005318 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 return err;
5320 }
5321
5322 /* otherwise, add_new_disk is only allowed
5323 * for major_version==0 superblocks
5324 */
5325 if (mddev->major_version != 0) {
5326 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5327 mdname(mddev));
5328 return -EINVAL;
5329 }
5330
5331 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5332 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005333 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 if (IS_ERR(rdev)) {
5335 printk(KERN_WARNING
5336 "md: error, md_import_device() returned %ld\n",
5337 PTR_ERR(rdev));
5338 return PTR_ERR(rdev);
5339 }
5340 rdev->desc_nr = info->number;
5341 if (info->raid_disk < mddev->raid_disks)
5342 rdev->raid_disk = info->raid_disk;
5343 else
5344 rdev->raid_disk = -1;
5345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005347 if (info->state & (1<<MD_DISK_SYNC))
5348 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005350 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5351 set_bit(WriteMostly, &rdev->flags);
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 if (!mddev->persistent) {
5354 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005355 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5356 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005357 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005358 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
NeilBrown2bf071b2006-01-06 00:20:55 -08005360 err = bind_rdev_to_array(rdev, mddev);
5361 if (err) {
5362 export_rdev(rdev);
5363 return err;
5364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 }
5366
5367 return 0;
5368}
5369
5370static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5371{
5372 char b[BDEVNAME_SIZE];
5373 mdk_rdev_t *rdev;
5374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 rdev = find_rdev(mddev, dev);
5376 if (!rdev)
5377 return -ENXIO;
5378
5379 if (rdev->raid_disk >= 0)
5380 goto busy;
5381
5382 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005383 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005384 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
5386 return 0;
5387busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005388 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 bdevname(rdev->bdev,b), mdname(mddev));
5390 return -EBUSY;
5391}
5392
5393static int hot_add_disk(mddev_t * mddev, dev_t dev)
5394{
5395 char b[BDEVNAME_SIZE];
5396 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 mdk_rdev_t *rdev;
5398
5399 if (!mddev->pers)
5400 return -ENODEV;
5401
5402 if (mddev->major_version != 0) {
5403 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5404 " version-0 superblocks.\n",
5405 mdname(mddev));
5406 return -EINVAL;
5407 }
5408 if (!mddev->pers->hot_add_disk) {
5409 printk(KERN_WARNING
5410 "%s: personality does not support diskops!\n",
5411 mdname(mddev));
5412 return -EINVAL;
5413 }
5414
NeilBrownd710e132008-10-13 11:55:12 +11005415 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (IS_ERR(rdev)) {
5417 printk(KERN_WARNING
5418 "md: error, md_import_device() returned %ld\n",
5419 PTR_ERR(rdev));
5420 return -EINVAL;
5421 }
5422
5423 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005424 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005426 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
NeilBrown8190e752009-06-18 08:48:58 +10005428 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
NeilBrownb2d444d2005-11-08 21:39:31 -08005430 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 printk(KERN_WARNING
5432 "md: can not hot-add faulty %s disk to %s!\n",
5433 bdevname(rdev->bdev,b), mdname(mddev));
5434 err = -EINVAL;
5435 goto abort_export;
5436 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005437 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005439 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005440 err = bind_rdev_to_array(rdev, mddev);
5441 if (err)
5442 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
5444 /*
5445 * The rest should better be atomic, we can have disk failures
5446 * noticed in interrupt contexts ...
5447 */
5448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 rdev->raid_disk = -1;
5450
NeilBrown850b2b42006-10-03 01:15:46 -07005451 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
5453 /*
5454 * Kick recovery, maybe this spare has to be added to the
5455 * array immediately.
5456 */
5457 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5458 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005459 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 return 0;
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462abort_export:
5463 export_rdev(rdev);
5464 return err;
5465}
5466
NeilBrown32a76272005-06-21 17:17:14 -07005467static int set_bitmap_file(mddev_t *mddev, int fd)
5468{
5469 int err;
5470
NeilBrown36fa3062005-09-09 16:23:45 -07005471 if (mddev->pers) {
5472 if (!mddev->pers->quiesce)
5473 return -EBUSY;
5474 if (mddev->recovery || mddev->sync_thread)
5475 return -EBUSY;
5476 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005477 }
5478
NeilBrown36fa3062005-09-09 16:23:45 -07005479
5480 if (fd >= 0) {
5481 if (mddev->bitmap)
5482 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005483 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005484
NeilBrownc3d97142009-12-14 12:49:52 +11005485 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005486 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5487 mdname(mddev));
5488 return -EBADF;
5489 }
5490
NeilBrownc3d97142009-12-14 12:49:52 +11005491 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005492 if (err) {
5493 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5494 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005495 fput(mddev->bitmap_info.file);
5496 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005497 return err;
5498 }
NeilBrownc3d97142009-12-14 12:49:52 +11005499 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005500 } else if (mddev->bitmap == NULL)
5501 return -ENOENT; /* cannot remove what isn't there */
5502 err = 0;
5503 if (mddev->pers) {
5504 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005505 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005506 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005507 if (!err)
5508 err = bitmap_load(mddev);
5509 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005510 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005511 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005512 fd = -1; /* make sure to put the file */
5513 }
NeilBrown36fa3062005-09-09 16:23:45 -07005514 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005515 }
5516 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005517 if (mddev->bitmap_info.file) {
5518 restore_bitmap_write_access(mddev->bitmap_info.file);
5519 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005520 }
NeilBrownc3d97142009-12-14 12:49:52 +11005521 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005522 }
5523
NeilBrown32a76272005-06-21 17:17:14 -07005524 return err;
5525}
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527/*
5528 * set_array_info is used two different ways
5529 * The original usage is when creating a new array.
5530 * In this usage, raid_disks is > 0 and it together with
5531 * level, size, not_persistent,layout,chunksize determine the
5532 * shape of the array.
5533 * This will always create an array with a type-0.90.0 superblock.
5534 * The newer usage is when assembling an array.
5535 * In this case raid_disks will be 0, and the major_version field is
5536 * use to determine which style super-blocks are to be found on the devices.
5537 * The minor and patch _version numbers are also kept incase the
5538 * super_block handler wishes to interpret them.
5539 */
5540static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5541{
5542
5543 if (info->raid_disks == 0) {
5544 /* just setting version number for superblock loading */
5545 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005546 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 super_types[info->major_version].name == NULL) {
5548 /* maybe try to auto-load a module? */
5549 printk(KERN_INFO
5550 "md: superblock version %d not known\n",
5551 info->major_version);
5552 return -EINVAL;
5553 }
5554 mddev->major_version = info->major_version;
5555 mddev->minor_version = info->minor_version;
5556 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005557 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005558 /* ensure mddev_put doesn't delete this now that there
5559 * is some minimal configuration.
5560 */
5561 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 return 0;
5563 }
5564 mddev->major_version = MD_MAJOR_VERSION;
5565 mddev->minor_version = MD_MINOR_VERSION;
5566 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5567 mddev->ctime = get_seconds();
5568
5569 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005570 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005571 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 mddev->raid_disks = info->raid_disks;
5573 /* don't set md_minor, it is determined by which /dev/md* was
5574 * openned
5575 */
5576 if (info->state & (1<<MD_SB_CLEAN))
5577 mddev->recovery_cp = MaxSector;
5578 else
5579 mddev->recovery_cp = 0;
5580 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005581 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005584 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
5586 mddev->max_disks = MD_SB_DISKS;
5587
NeilBrowne6910632008-02-06 01:39:51 -08005588 if (mddev->persistent)
5589 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005590 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
NeilBrownc3d97142009-12-14 12:49:52 +11005592 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5593 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005594
NeilBrownf6705572006-03-27 01:18:11 -08005595 mddev->reshape_position = MaxSector;
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 /*
5598 * Generate a 128 bit UUID
5599 */
5600 get_random_bytes(mddev->uuid, 16);
5601
NeilBrownf6705572006-03-27 01:18:11 -08005602 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005603 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005604 mddev->new_layout = mddev->layout;
5605 mddev->delta_disks = 0;
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 return 0;
5608}
5609
Dan Williams1f403622009-03-31 14:59:03 +11005610void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5611{
Dan Williamsb522adc2009-03-31 15:00:31 +11005612 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5613
5614 if (mddev->external_size)
5615 return;
5616
Dan Williams1f403622009-03-31 14:59:03 +11005617 mddev->array_sectors = array_sectors;
5618}
5619EXPORT_SYMBOL(md_set_array_sectors);
5620
Andre Nolld71f9f82008-07-11 22:02:22 +10005621static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005622{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005623 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005624 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005625 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005626
5627 if (mddev->pers->resize == NULL)
5628 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005629 /* The "num_sectors" is the number of sectors of each device that
5630 * is used. This can only make sense for arrays with redundancy.
5631 * linear and raid0 always use whatever space is available. We can only
5632 * consider changing this number if no resync or reconstruction is
5633 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005634 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005635 * of each device. If num_sectors is zero, we find the largest size
5636 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005637 */
5638 if (mddev->sync_thread)
5639 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005640 if (mddev->bitmap)
5641 /* Sorry, cannot grow a bitmap yet, just remove it,
5642 * grow, and re-add.
5643 */
5644 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005645 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005646 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005647
Andre Nolld71f9f82008-07-11 22:02:22 +10005648 if (fit && (num_sectors == 0 || num_sectors > avail))
5649 num_sectors = avail;
5650 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005651 return -ENOSPC;
5652 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005653 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005654 if (!rv)
5655 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005656 return rv;
5657}
5658
NeilBrownda943b992006-01-06 00:20:54 -08005659static int update_raid_disks(mddev_t *mddev, int raid_disks)
5660{
5661 int rv;
5662 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005663 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005664 return -EINVAL;
5665 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005666 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005667 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005668 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005669 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005670 mddev->delta_disks = raid_disks - mddev->raid_disks;
5671
5672 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005673 if (rv < 0)
5674 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005675 return rv;
5676}
5677
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679/*
5680 * update_array_info is used to change the configuration of an
5681 * on-line array.
5682 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5683 * fields in the info are checked against the array.
5684 * Any differences that cannot be handled will cause an error.
5685 * Normally, only one change can be managed at a time.
5686 */
5687static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5688{
5689 int rv = 0;
5690 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005691 int state = 0;
5692
5693 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005694 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005695 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 if (mddev->major_version != info->major_version ||
5698 mddev->minor_version != info->minor_version ||
5699/* mddev->patch_version != info->patch_version || */
5700 mddev->ctime != info->ctime ||
5701 mddev->level != info->level ||
5702/* mddev->layout != info->layout || */
5703 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005704 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005705 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5706 ((state^info->state) & 0xfffffe00)
5707 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 return -EINVAL;
5709 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005710 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5711 cnt++;
5712 if (mddev->raid_disks != info->raid_disks)
5713 cnt++;
5714 if (mddev->layout != info->layout)
5715 cnt++;
5716 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5717 cnt++;
5718 if (cnt == 0)
5719 return 0;
5720 if (cnt > 1)
5721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 if (mddev->layout != info->layout) {
5724 /* Change layout
5725 * we don't need to do anything at the md level, the
5726 * personality will take care of it all.
5727 */
NeilBrown50ac1682009-06-18 08:47:55 +10005728 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005730 else {
5731 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005732 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005733 if (rv)
5734 mddev->new_layout = mddev->layout;
5735 return rv;
5736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005738 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005739 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740
NeilBrownda943b992006-01-06 00:20:54 -08005741 if (mddev->raid_disks != info->raid_disks)
5742 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
NeilBrown36fa3062005-09-09 16:23:45 -07005744 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5745 if (mddev->pers->quiesce == NULL)
5746 return -EINVAL;
5747 if (mddev->recovery || mddev->sync_thread)
5748 return -EBUSY;
5749 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5750 /* add the bitmap */
5751 if (mddev->bitmap)
5752 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005753 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005754 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005755 mddev->bitmap_info.offset =
5756 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005757 mddev->pers->quiesce(mddev, 1);
5758 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005759 if (!rv)
5760 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005761 if (rv)
5762 bitmap_destroy(mddev);
5763 mddev->pers->quiesce(mddev, 0);
5764 } else {
5765 /* remove the bitmap */
5766 if (!mddev->bitmap)
5767 return -ENOENT;
5768 if (mddev->bitmap->file)
5769 return -EINVAL;
5770 mddev->pers->quiesce(mddev, 1);
5771 bitmap_destroy(mddev);
5772 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005773 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005774 }
5775 }
NeilBrown850b2b42006-10-03 01:15:46 -07005776 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 return rv;
5778}
5779
5780static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5781{
5782 mdk_rdev_t *rdev;
5783
5784 if (mddev->pers == NULL)
5785 return -ENODEV;
5786
5787 rdev = find_rdev(mddev, dev);
5788 if (!rdev)
5789 return -ENODEV;
5790
5791 md_error(mddev, rdev);
5792 return 0;
5793}
5794
Andre Noll2f9618c2008-04-25 18:57:58 +02005795/*
5796 * We have a problem here : there is no easy way to give a CHS
5797 * virtual geometry. We currently pretend that we have a 2 heads
5798 * 4 sectors (with a BIG number of cylinders...). This drives
5799 * dosfs just mad... ;-)
5800 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005801static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5802{
5803 mddev_t *mddev = bdev->bd_disk->private_data;
5804
5805 geo->heads = 2;
5806 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005807 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005808 return 0;
5809}
5810
Al Viroa39907f2008-03-02 10:31:15 -05005811static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 unsigned int cmd, unsigned long arg)
5813{
5814 int err = 0;
5815 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005817 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
5819 if (!capable(CAP_SYS_ADMIN))
5820 return -EACCES;
5821
5822 /*
5823 * Commands dealing with the RAID driver but not any
5824 * particular array:
5825 */
5826 switch (cmd)
5827 {
5828 case RAID_VERSION:
5829 err = get_version(argp);
5830 goto done;
5831
5832 case PRINT_RAID_DEBUG:
5833 err = 0;
5834 md_print_devices();
5835 goto done;
5836
5837#ifndef MODULE
5838 case RAID_AUTORUN:
5839 err = 0;
5840 autostart_arrays(arg);
5841 goto done;
5842#endif
5843 default:;
5844 }
5845
5846 /*
5847 * Commands creating/starting a new array:
5848 */
5849
Al Viroa39907f2008-03-02 10:31:15 -05005850 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
5852 if (!mddev) {
5853 BUG();
5854 goto abort;
5855 }
5856
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 err = mddev_lock(mddev);
5858 if (err) {
5859 printk(KERN_INFO
5860 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5861 err, cmd);
5862 goto abort;
5863 }
5864
5865 switch (cmd)
5866 {
5867 case SET_ARRAY_INFO:
5868 {
5869 mdu_array_info_t info;
5870 if (!arg)
5871 memset(&info, 0, sizeof(info));
5872 else if (copy_from_user(&info, argp, sizeof(info))) {
5873 err = -EFAULT;
5874 goto abort_unlock;
5875 }
5876 if (mddev->pers) {
5877 err = update_array_info(mddev, &info);
5878 if (err) {
5879 printk(KERN_WARNING "md: couldn't update"
5880 " array info. %d\n", err);
5881 goto abort_unlock;
5882 }
5883 goto done_unlock;
5884 }
5885 if (!list_empty(&mddev->disks)) {
5886 printk(KERN_WARNING
5887 "md: array %s already has disks!\n",
5888 mdname(mddev));
5889 err = -EBUSY;
5890 goto abort_unlock;
5891 }
5892 if (mddev->raid_disks) {
5893 printk(KERN_WARNING
5894 "md: array %s already initialised!\n",
5895 mdname(mddev));
5896 err = -EBUSY;
5897 goto abort_unlock;
5898 }
5899 err = set_array_info(mddev, &info);
5900 if (err) {
5901 printk(KERN_WARNING "md: couldn't set"
5902 " array info. %d\n", err);
5903 goto abort_unlock;
5904 }
5905 }
5906 goto done_unlock;
5907
5908 default:;
5909 }
5910
5911 /*
5912 * Commands querying/configuring an existing array:
5913 */
NeilBrown32a76272005-06-21 17:17:14 -07005914 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005915 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005916 if ((!mddev->raid_disks && !mddev->external)
5917 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5918 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5919 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 err = -ENODEV;
5921 goto abort_unlock;
5922 }
5923
5924 /*
5925 * Commands even a read-only array can execute:
5926 */
5927 switch (cmd)
5928 {
5929 case GET_ARRAY_INFO:
5930 err = get_array_info(mddev, argp);
5931 goto done_unlock;
5932
NeilBrown32a76272005-06-21 17:17:14 -07005933 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005934 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005935 goto done_unlock;
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 case GET_DISK_INFO:
5938 err = get_disk_info(mddev, argp);
5939 goto done_unlock;
5940
5941 case RESTART_ARRAY_RW:
5942 err = restart_array(mddev);
5943 goto done_unlock;
5944
5945 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005946 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 goto done_unlock;
5948
5949 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005950 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 goto done_unlock;
5952
Dan Williamse2218352010-05-12 08:25:37 +10005953 case BLKROSET:
5954 if (get_user(ro, (int __user *)(arg))) {
5955 err = -EFAULT;
5956 goto done_unlock;
5957 }
5958 err = -EINVAL;
5959
5960 /* if the bdev is going readonly the value of mddev->ro
5961 * does not matter, no writes are coming
5962 */
5963 if (ro)
5964 goto done_unlock;
5965
5966 /* are we are already prepared for writes? */
5967 if (mddev->ro != 1)
5968 goto done_unlock;
5969
5970 /* transitioning to readauto need only happen for
5971 * arrays that call md_write_start
5972 */
5973 if (mddev->pers) {
5974 err = restart_array(mddev);
5975 if (err == 0) {
5976 mddev->ro = 2;
5977 set_disk_ro(mddev->gendisk, 0);
5978 }
5979 }
5980 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 }
5982
5983 /*
5984 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005985 * superblock, so we do not allow them on read-only arrays.
5986 * However non-MD ioctls (e.g. get-size) will still come through
5987 * here and hit the 'default' below, so only disallow
5988 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005990 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005991 if (mddev->ro == 2) {
5992 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005993 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005994 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5995 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005996 } else {
5997 err = -EROFS;
5998 goto abort_unlock;
5999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 }
6001
6002 switch (cmd)
6003 {
6004 case ADD_NEW_DISK:
6005 {
6006 mdu_disk_info_t info;
6007 if (copy_from_user(&info, argp, sizeof(info)))
6008 err = -EFAULT;
6009 else
6010 err = add_new_disk(mddev, &info);
6011 goto done_unlock;
6012 }
6013
6014 case HOT_REMOVE_DISK:
6015 err = hot_remove_disk(mddev, new_decode_dev(arg));
6016 goto done_unlock;
6017
6018 case HOT_ADD_DISK:
6019 err = hot_add_disk(mddev, new_decode_dev(arg));
6020 goto done_unlock;
6021
6022 case SET_DISK_FAULTY:
6023 err = set_disk_faulty(mddev, new_decode_dev(arg));
6024 goto done_unlock;
6025
6026 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006027 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 goto done_unlock;
6029
NeilBrown32a76272005-06-21 17:17:14 -07006030 case SET_BITMAP_FILE:
6031 err = set_bitmap_file(mddev, (int)arg);
6032 goto done_unlock;
6033
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 err = -EINVAL;
6036 goto abort_unlock;
6037 }
6038
6039done_unlock:
6040abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006041 if (mddev->hold_active == UNTIL_IOCTL &&
6042 err != -EINVAL)
6043 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 mddev_unlock(mddev);
6045
6046 return err;
6047done:
6048 if (err)
6049 MD_BUG();
6050abort:
6051 return err;
6052}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006053#ifdef CONFIG_COMPAT
6054static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6055 unsigned int cmd, unsigned long arg)
6056{
6057 switch (cmd) {
6058 case HOT_REMOVE_DISK:
6059 case HOT_ADD_DISK:
6060 case SET_DISK_FAULTY:
6061 case SET_BITMAP_FILE:
6062 /* These take in integer arg, do not convert */
6063 break;
6064 default:
6065 arg = (unsigned long)compat_ptr(arg);
6066 break;
6067 }
6068
6069 return md_ioctl(bdev, mode, cmd, arg);
6070}
6071#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Al Viroa39907f2008-03-02 10:31:15 -05006073static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074{
6075 /*
6076 * Succeed if we can lock the mddev, which confirms that
6077 * it isn't being stopped right now.
6078 */
NeilBrownd3374822009-01-09 08:31:10 +11006079 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 int err;
6081
NeilBrownd3374822009-01-09 08:31:10 +11006082 if (mddev->gendisk != bdev->bd_disk) {
6083 /* we are racing with mddev_put which is discarding this
6084 * bd_disk.
6085 */
6086 mddev_put(mddev);
6087 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006088 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006089 /* Then retry the open from the top */
6090 return -ERESTARTSYS;
6091 }
6092 BUG_ON(mddev != bdev->bd_disk->private_data);
6093
NeilBrownc8c00a62009-08-10 12:50:52 +10006094 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 goto out;
6096
6097 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006098 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006099 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006101 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 out:
6103 return err;
6104}
6105
Al Viroa39907f2008-03-02 10:31:15 -05006106static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107{
Al Viroa39907f2008-03-02 10:31:15 -05006108 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006110 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006111 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 mddev_put(mddev);
6113
6114 return 0;
6115}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006116
6117static int md_media_changed(struct gendisk *disk)
6118{
6119 mddev_t *mddev = disk->private_data;
6120
6121 return mddev->changed;
6122}
6123
6124static int md_revalidate(struct gendisk *disk)
6125{
6126 mddev_t *mddev = disk->private_data;
6127
6128 mddev->changed = 0;
6129 return 0;
6130}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006131static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132{
6133 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006134 .open = md_open,
6135 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006136 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006137#ifdef CONFIG_COMPAT
6138 .compat_ioctl = md_compat_ioctl,
6139#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006140 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006141 .media_changed = md_media_changed,
6142 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143};
6144
Adrian Bunk75c96f82005-05-05 16:16:09 -07006145static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146{
6147 mdk_thread_t *thread = arg;
6148
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 /*
6150 * md_thread is a 'system-thread', it's priority should be very
6151 * high. We avoid resource deadlocks individually in each
6152 * raid personality. (RAID5 does preallocation) We also use RR and
6153 * the very same RT priority as kswapd, thus we will never get
6154 * into a priority inversion deadlock.
6155 *
6156 * we definitely have to have equal or higher priority than
6157 * bdflush, otherwise bdflush will deadlock if there are too
6158 * many dirty RAID5 blocks.
6159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
NeilBrown6985c432005-10-19 21:23:47 -07006161 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006162 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
NeilBrown93588e22005-11-15 00:09:12 -08006164 /* We need to wait INTERRUPTIBLE so that
6165 * we don't add to the load-average.
6166 * That means we need to be sure no signals are
6167 * pending
6168 */
6169 if (signal_pending(current))
6170 flush_signals(current);
6171
6172 wait_event_interruptible_timeout
6173 (thread->wqueue,
6174 test_bit(THREAD_WAKEUP, &thread->flags)
6175 || kthread_should_stop(),
6176 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
NeilBrown6c987912011-01-14 09:13:53 +11006178 clear_bit(THREAD_WAKEUP, &thread->flags);
6179 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006180 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 return 0;
6184}
6185
6186void md_wakeup_thread(mdk_thread_t *thread)
6187{
6188 if (thread) {
6189 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6190 set_bit(THREAD_WAKEUP, &thread->flags);
6191 wake_up(&thread->wqueue);
6192 }
6193}
6194
6195mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6196 const char *name)
6197{
6198 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
NeilBrown9ffae0c2006-01-06 00:20:32 -08006200 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 if (!thread)
6202 return NULL;
6203
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 init_waitqueue_head(&thread->wqueue);
6205
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 thread->run = run;
6207 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006208 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006209 thread->tsk = kthread_run(md_thread, thread,
6210 "%s_%s",
6211 mdname(thread->mddev),
6212 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006213 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 kfree(thread);
6215 return NULL;
6216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 return thread;
6218}
6219
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220void md_unregister_thread(mdk_thread_t *thread)
6221{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006222 if (!thread)
6223 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006224 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006225
6226 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 kfree(thread);
6228}
6229
6230void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6231{
6232 if (!mddev) {
6233 MD_BUG();
6234 return;
6235 }
6236
NeilBrownb2d444d2005-11-08 21:39:31 -08006237 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006239
6240 if (mddev->external)
6241 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006242/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6244 mdname(mddev),
6245 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6246 __builtin_return_address(0),__builtin_return_address(1),
6247 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006248*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006249 if (!mddev->pers)
6250 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 if (!mddev->pers->error_handler)
6252 return;
6253 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006254 if (mddev->degraded)
6255 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006256 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6258 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6259 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006260 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006261 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006262 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263}
6264
6265/* seq_file implementation /proc/mdstat */
6266
6267static void status_unused(struct seq_file *seq)
6268{
6269 int i = 0;
6270 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
6272 seq_printf(seq, "unused devices: ");
6273
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006274 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 char b[BDEVNAME_SIZE];
6276 i++;
6277 seq_printf(seq, "%s ",
6278 bdevname(rdev->bdev,b));
6279 }
6280 if (!i)
6281 seq_printf(seq, "<none>");
6282
6283 seq_printf(seq, "\n");
6284}
6285
6286
6287static void status_resync(struct seq_file *seq, mddev_t * mddev)
6288{
NeilBrowndd71cf62009-05-07 12:49:35 +10006289 sector_t max_sectors, resync, res;
6290 unsigned long dt, db;
6291 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006292 int scale;
6293 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
NeilBrowndd71cf62009-05-07 12:49:35 +10006295 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
6297 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006298 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006300 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301
6302 /*
6303 * Should not happen.
6304 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006305 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 MD_BUG();
6307 return;
6308 }
NeilBrown4588b422006-03-27 01:18:04 -08006309 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006310 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006311 * u32, as those are the requirements for sector_div.
6312 * Thus 'scale' must be at least 10
6313 */
6314 scale = 10;
6315 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006316 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006317 scale++;
6318 }
6319 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006320 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006321
6322 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 {
NeilBrown4588b422006-03-27 01:18:04 -08006324 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 seq_printf(seq, "[");
6326 for (i = 0; i < x; i++)
6327 seq_printf(seq, "=");
6328 seq_printf(seq, ">");
6329 for (i = 0; i < y; i++)
6330 seq_printf(seq, ".");
6331 seq_printf(seq, "] ");
6332 }
NeilBrown4588b422006-03-27 01:18:04 -08006333 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006334 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6335 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006336 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6337 "check" :
6338 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6339 "resync" : "recovery"))),
6340 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006341 (unsigned long long) resync/2,
6342 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
6344 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 * dt: time from mark until now
6346 * db: blocks written from mark until now
6347 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006348 *
6349 * rt is a sector_t, so could be 32bit or 64bit.
6350 * So we divide before multiply in case it is 32bit and close
6351 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006352 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006353 * near the end of resync when the number of remaining sectors
6354 * is close to 'db'.
6355 * We then divide rt by 32 after multiplying by db to compensate.
6356 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 */
6358 dt = ((jiffies - mddev->resync_mark) / HZ);
6359 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006360 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6361 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
NeilBrowndd71cf62009-05-07 12:49:35 +10006363 rt = max_sectors - resync; /* number of remaining sectors */
6364 sector_div(rt, db/32+1);
6365 rt *= dt;
6366 rt >>= 5;
6367
6368 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6369 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
NeilBrownff4e8d92006-07-10 04:44:16 -07006371 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372}
6373
6374static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6375{
6376 struct list_head *tmp;
6377 loff_t l = *pos;
6378 mddev_t *mddev;
6379
6380 if (l >= 0x10000)
6381 return NULL;
6382 if (!l--)
6383 /* header */
6384 return (void*)1;
6385
6386 spin_lock(&all_mddevs_lock);
6387 list_for_each(tmp,&all_mddevs)
6388 if (!l--) {
6389 mddev = list_entry(tmp, mddev_t, all_mddevs);
6390 mddev_get(mddev);
6391 spin_unlock(&all_mddevs_lock);
6392 return mddev;
6393 }
6394 spin_unlock(&all_mddevs_lock);
6395 if (!l--)
6396 return (void*)2;/* tail */
6397 return NULL;
6398}
6399
6400static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6401{
6402 struct list_head *tmp;
6403 mddev_t *next_mddev, *mddev = v;
6404
6405 ++*pos;
6406 if (v == (void*)2)
6407 return NULL;
6408
6409 spin_lock(&all_mddevs_lock);
6410 if (v == (void*)1)
6411 tmp = all_mddevs.next;
6412 else
6413 tmp = mddev->all_mddevs.next;
6414 if (tmp != &all_mddevs)
6415 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6416 else {
6417 next_mddev = (void*)2;
6418 *pos = 0x10000;
6419 }
6420 spin_unlock(&all_mddevs_lock);
6421
6422 if (v != (void*)1)
6423 mddev_put(mddev);
6424 return next_mddev;
6425
6426}
6427
6428static void md_seq_stop(struct seq_file *seq, void *v)
6429{
6430 mddev_t *mddev = v;
6431
6432 if (mddev && v != (void*)1 && v != (void*)2)
6433 mddev_put(mddev);
6434}
6435
NeilBrownd7603b72006-01-06 00:20:30 -08006436struct mdstat_info {
6437 int event;
6438};
6439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440static int md_seq_show(struct seq_file *seq, void *v)
6441{
6442 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006443 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006445 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006446 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
6448 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006449 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 seq_printf(seq, "Personalities : ");
6451 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006452 list_for_each_entry(pers, &pers_list, list)
6453 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
6455 spin_unlock(&pers_lock);
6456 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006457 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 return 0;
6459 }
6460 if (v == (void*)2) {
6461 status_unused(seq);
6462 return 0;
6463 }
6464
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006465 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006467
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6469 seq_printf(seq, "%s : %sactive", mdname(mddev),
6470 mddev->pers ? "" : "in");
6471 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006472 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006474 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006475 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 seq_printf(seq, " %s", mddev->pers->name);
6477 }
6478
Andre Nolldd8ac332009-03-31 14:33:13 +11006479 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006480 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 char b[BDEVNAME_SIZE];
6482 seq_printf(seq, " %s[%d]",
6483 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006484 if (test_bit(WriteMostly, &rdev->flags))
6485 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006486 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 seq_printf(seq, "(F)");
6488 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006489 } else if (rdev->raid_disk < 0)
6490 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006491 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 }
6493
6494 if (!list_empty(&mddev->disks)) {
6495 if (mddev->pers)
6496 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006497 (unsigned long long)
6498 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 else
6500 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006501 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006503 if (mddev->persistent) {
6504 if (mddev->major_version != 0 ||
6505 mddev->minor_version != 90) {
6506 seq_printf(seq," super %d.%d",
6507 mddev->major_version,
6508 mddev->minor_version);
6509 }
NeilBrowne6910632008-02-06 01:39:51 -08006510 } else if (mddev->external)
6511 seq_printf(seq, " super external:%s",
6512 mddev->metadata_type);
6513 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006514 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515
6516 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006517 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006519 if (mddev->pers->sync_request) {
6520 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006521 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006522 seq_printf(seq, "\n ");
6523 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6524 seq_printf(seq, "\tresync=DELAYED\n ");
6525 else if (mddev->recovery_cp < MaxSector)
6526 seq_printf(seq, "\tresync=PENDING\n ");
6527 }
NeilBrown32a76272005-06-21 17:17:14 -07006528 } else
6529 seq_printf(seq, "\n ");
6530
6531 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006532 unsigned long chunk_kb;
6533 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006534 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006535 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006536 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6537 "%lu%s chunk",
6538 bitmap->pages - bitmap->missing_pages,
6539 bitmap->pages,
6540 (bitmap->pages - bitmap->missing_pages)
6541 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006542 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006543 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006544 if (bitmap->file) {
6545 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006546 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006547 }
NeilBrown78d742d2005-06-21 17:17:15 -07006548
NeilBrown32a76272005-06-21 17:17:14 -07006549 seq_printf(seq, "\n");
6550 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 }
6552
6553 seq_printf(seq, "\n");
6554 }
6555 mddev_unlock(mddev);
6556
6557 return 0;
6558}
6559
Jan Engelhardt110518b2009-05-07 12:49:37 +10006560static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 .start = md_seq_start,
6562 .next = md_seq_next,
6563 .stop = md_seq_stop,
6564 .show = md_seq_show,
6565};
6566
6567static int md_seq_open(struct inode *inode, struct file *file)
6568{
6569 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006570 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6571 if (mi == NULL)
6572 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573
6574 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006575 if (error)
6576 kfree(mi);
6577 else {
6578 struct seq_file *p = file->private_data;
6579 p->private = mi;
6580 mi->event = atomic_read(&md_event_count);
6581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 return error;
6583}
6584
NeilBrownd7603b72006-01-06 00:20:30 -08006585static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6586{
6587 struct seq_file *m = filp->private_data;
6588 struct mdstat_info *mi = m->private;
6589 int mask;
6590
6591 poll_wait(filp, &md_event_waiters, wait);
6592
6593 /* always allow read */
6594 mask = POLLIN | POLLRDNORM;
6595
6596 if (mi->event != atomic_read(&md_event_count))
6597 mask |= POLLERR | POLLPRI;
6598 return mask;
6599}
6600
Arjan van de Venfa027c22007-02-12 00:55:33 -08006601static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006602 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 .open = md_seq_open,
6604 .read = seq_read,
6605 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006606 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006607 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608};
6609
NeilBrown2604b702006-01-06 00:20:36 -08006610int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006613 list_add_tail(&p->list, &pers_list);
6614 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 spin_unlock(&pers_lock);
6616 return 0;
6617}
6618
NeilBrown2604b702006-01-06 00:20:36 -08006619int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620{
NeilBrown2604b702006-01-06 00:20:36 -08006621 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006623 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 spin_unlock(&pers_lock);
6625 return 0;
6626}
6627
NeilBrowneea1bf32009-03-31 14:27:02 +11006628static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629{
6630 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006632 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006635 rcu_read_lock();
6636 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006638 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6639 (int)part_stat_read(&disk->part0, sectors[1]) -
6640 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006641 /* sync IO will cause sync_io to increase before the disk_stats
6642 * as sync_io is counted when a request starts, and
6643 * disk_stats is counted when it completes.
6644 * So resync activity will cause curr_events to be smaller than
6645 * when there was no such activity.
6646 * non-sync IO will cause disk_stat to increase without
6647 * increasing sync_io so curr_events will (eventually)
6648 * be larger than it was before. Once it becomes
6649 * substantially larger, the test below will cause
6650 * the array to appear non-idle, and resync will slow
6651 * down.
6652 * If there is a lot of outstanding resync activity when
6653 * we set last_event to curr_events, then all that activity
6654 * completing might cause the array to appear non-idle
6655 * and resync will be slowed down even though there might
6656 * not have been non-resync activity. This will only
6657 * happen once though. 'last_events' will soon reflect
6658 * the state where there is little or no outstanding
6659 * resync requests, and further resync activity will
6660 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006663 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 rdev->last_events = curr_events;
6665 idle = 0;
6666 }
6667 }
NeilBrown4b809912008-07-21 17:05:25 +10006668 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 return idle;
6670}
6671
6672void md_done_sync(mddev_t *mddev, int blocks, int ok)
6673{
6674 /* another "blocks" (512byte) blocks have been synced */
6675 atomic_sub(blocks, &mddev->recovery_active);
6676 wake_up(&mddev->recovery_wait);
6677 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006678 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 md_wakeup_thread(mddev->thread);
6680 // stop recovery, signal do_sync ....
6681 }
6682}
6683
6684
NeilBrown06d91a52005-06-21 17:17:12 -07006685/* md_write_start(mddev, bi)
6686 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006687 * in superblock) before writing, schedule a superblock update
6688 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006689 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006690void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691{
Neil Brown0fd62b82008-06-28 08:31:36 +10006692 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006693 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006694 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006695
NeilBrownf91de922005-11-08 21:39:36 -08006696 BUG_ON(mddev->ro == 1);
6697 if (mddev->ro == 2) {
6698 /* need to switch to read/write */
6699 mddev->ro = 0;
6700 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6701 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006702 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006703 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006704 }
NeilBrown06d91a52005-06-21 17:17:12 -07006705 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006706 if (mddev->safemode == 1)
6707 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006708 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006709 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006710 if (mddev->in_sync) {
6711 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006712 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006713 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006714 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006715 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006716 }
NeilBrowna9701a32005-11-08 21:39:34 -08006717 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006718 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006719 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006720 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006721 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006722 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723}
6724
6725void md_write_end(mddev_t *mddev)
6726{
6727 if (atomic_dec_and_test(&mddev->writes_pending)) {
6728 if (mddev->safemode == 2)
6729 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006730 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6732 }
6733}
6734
NeilBrown2a2275d2007-01-26 00:57:11 -08006735/* md_allow_write(mddev)
6736 * Calling this ensures that the array is marked 'active' so that writes
6737 * may proceed without blocking. It is important to call this before
6738 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6739 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006740 *
6741 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6742 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006743 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006744int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006745{
6746 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006747 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006748 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006749 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006750 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006751 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006752
6753 spin_lock_irq(&mddev->write_lock);
6754 if (mddev->in_sync) {
6755 mddev->in_sync = 0;
6756 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006757 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006758 if (mddev->safemode_delay &&
6759 mddev->safemode == 0)
6760 mddev->safemode = 1;
6761 spin_unlock_irq(&mddev->write_lock);
6762 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006763 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006764 } else
6765 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006766
NeilBrown070dc6d2010-08-30 17:33:34 +10006767 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006768 return -EAGAIN;
6769 else
6770 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006771}
6772EXPORT_SYMBOL_GPL(md_allow_write);
6773
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774#define SYNC_MARKS 10
6775#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006776void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777{
6778 mddev_t *mddev2;
6779 unsigned int currspeed = 0,
6780 window;
NeilBrown57afd892005-06-21 17:17:13 -07006781 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 unsigned long mark[SYNC_MARKS];
6783 sector_t mark_cnt[SYNC_MARKS];
6784 int last_mark,m;
6785 struct list_head *tmp;
6786 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006787 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006788 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006789 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 /* just incase thread restarts... */
6792 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6793 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006794 if (mddev->ro) /* never try to sync a read-only array */
6795 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
NeilBrown61df9d92006-10-03 01:15:57 -07006797 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6798 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6799 desc = "data-check";
6800 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6801 desc = "requested-resync";
6802 else
6803 desc = "resync";
6804 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6805 desc = "reshape";
6806 else
6807 desc = "recovery";
6808
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 /* we overload curr_resync somewhat here.
6810 * 0 == not engaged in resync at all
6811 * 2 == checking that there is no conflict with another sync
6812 * 1 == like 2, but have yielded to allow conflicting resync to
6813 * commense
6814 * other == active in resync - this many blocks
6815 *
6816 * Before starting a resync we must have set curr_resync to
6817 * 2, and then checked that every "conflicting" array has curr_resync
6818 * less than ours. When we find one that is the same or higher
6819 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6820 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6821 * This will mean we have to start checking from the beginning again.
6822 *
6823 */
6824
6825 do {
6826 mddev->curr_resync = 2;
6827
6828 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006829 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006830 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006831
6832 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006834 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 if (mddev2 == mddev)
6836 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006837 if (!mddev->parallel_resync
6838 && mddev2->curr_resync
6839 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 DEFINE_WAIT(wq);
6841 if (mddev < mddev2 && mddev->curr_resync == 2) {
6842 /* arbitrarily yield */
6843 mddev->curr_resync = 1;
6844 wake_up(&resync_wait);
6845 }
6846 if (mddev > mddev2 && mddev->curr_resync == 1)
6847 /* no need to wait here, we can wait the next
6848 * time 'round when curr_resync == 2
6849 */
6850 continue;
NeilBrown97441972008-09-19 11:49:54 +10006851 /* We need to wait 'interruptible' so as not to
6852 * contribute to the load average, and not to
6853 * be caught by 'softlockup'
6854 */
6855 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006856 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006857 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006858 printk(KERN_INFO "md: delaying %s of %s"
6859 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006861 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006863 if (signal_pending(current))
6864 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 schedule();
6866 finish_wait(&resync_wait, &wq);
6867 goto try_again;
6868 }
6869 finish_wait(&resync_wait, &wq);
6870 }
6871 }
6872 } while (mddev->curr_resync < 2);
6873
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006874 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006875 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006877 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 */
6879 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006880 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006881 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006882 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6883 j = mddev->resync_min;
6884 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006885 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006886
NeilBrownccfcc3c2006-03-27 01:18:09 -08006887 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006888 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006889 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006891 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006892 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006893 rcu_read_lock();
6894 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006895 if (rdev->raid_disk >= 0 &&
6896 !test_bit(Faulty, &rdev->flags) &&
6897 !test_bit(In_sync, &rdev->flags) &&
6898 rdev->recovery_offset < j)
6899 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006900 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
NeilBrown61df9d92006-10-03 01:15:57 -07006903 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6904 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6905 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006906 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006907 "(but not more than %d KB/sec) for %s.\n",
6908 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
NeilBrowneea1bf32009-03-31 14:27:02 +11006910 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006911
NeilBrown57afd892005-06-21 17:17:13 -07006912 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 for (m = 0; m < SYNC_MARKS; m++) {
6914 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006915 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 }
6917 last_mark = 0;
6918 mddev->resync_mark = mark[last_mark];
6919 mddev->resync_mark_cnt = mark_cnt[last_mark];
6920
6921 /*
6922 * Tune reconstruction:
6923 */
6924 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05006925 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
6926 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927
6928 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 last_check = 0;
6930
6931 if (j>2) {
6932 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006933 "md: resuming %s of %s from checkpoint.\n",
6934 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 mddev->curr_resync = j;
6936 }
NeilBrown75d3da42011-01-14 09:14:34 +11006937 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
6939 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006940 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
NeilBrown57afd892005-06-21 17:17:13 -07006942 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006943
NeilBrown7a91ee12009-05-26 12:57:21 +10006944 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6945 ((mddev->curr_resync > mddev->curr_resync_completed &&
6946 (mddev->curr_resync - mddev->curr_resync_completed)
6947 > (max_sectors >> 4)) ||
6948 (j - mddev->curr_resync_completed)*2
6949 >= mddev->resync_max - mddev->curr_resync_completed
6950 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006951 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11006952 wait_event(mddev->recovery_wait,
6953 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11006954 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10006955 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006956 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006957 }
NeilBrownacb180b2009-04-14 16:28:34 +10006958
NeilBrowne62e58a2009-07-01 13:15:35 +10006959 while (j >= mddev->resync_max && !kthread_should_stop()) {
6960 /* As this condition is controlled by user-space,
6961 * we can block indefinitely, so use '_interruptible'
6962 * to avoid triggering warnings.
6963 */
6964 flush_signals(current); /* just in case */
6965 wait_event_interruptible(mddev->recovery_wait,
6966 mddev->resync_max > j
6967 || kthread_should_stop());
6968 }
NeilBrownacb180b2009-04-14 16:28:34 +10006969
6970 if (kthread_should_stop())
6971 goto interrupted;
6972
NeilBrown57afd892005-06-21 17:17:13 -07006973 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006974 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006975 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006976 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 goto out;
6978 }
NeilBrown57afd892005-06-21 17:17:13 -07006979
6980 if (!skipped) { /* actual IO requested */
6981 io_sectors += sectors;
6982 atomic_add(sectors, &mddev->recovery_active);
6983 }
6984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 j += sectors;
6986 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006987 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006988 if (last_check == 0)
6989 /* this is the earliers that rebuilt will be
6990 * visible in /proc/mdstat
6991 */
6992 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006993
6994 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 continue;
6996
NeilBrown57afd892005-06-21 17:17:13 -07006997 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
NeilBrowndfc70642008-05-23 13:04:39 -07006999 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 break;
7001
7002 repeat:
7003 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7004 /* step marks */
7005 int next = (last_mark+1) % SYNC_MARKS;
7006
7007 mddev->resync_mark = mark[next];
7008 mddev->resync_mark_cnt = mark_cnt[next];
7009 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007010 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 last_mark = next;
7012 }
7013
7014
NeilBrownc6207272008-02-06 01:39:52 -08007015 if (kthread_should_stop())
7016 goto interrupted;
7017
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018
7019 /*
7020 * this loop exits only if either when we are slower than
7021 * the 'hard' speed limit, or the system was IO-idle for
7022 * a jiffy.
7023 * the system might be non-idle CPU-wise, but we only care
7024 * about not overloading the IO subsystem. (things like an
7025 * e2fsck being done on the RAID array should execute fast)
7026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 cond_resched();
7028
NeilBrown57afd892005-06-21 17:17:13 -07007029 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7030 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031
NeilBrown88202a02006-01-06 00:21:36 -08007032 if (currspeed > speed_min(mddev)) {
7033 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007034 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007035 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 goto repeat;
7037 }
7038 }
7039 }
NeilBrown61df9d92006-10-03 01:15:57 -07007040 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 /*
7042 * this also signals 'finished resyncing' to md_stop
7043 */
7044 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7046
7047 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007048 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
NeilBrowndfc70642008-05-23 13:04:39 -07007050 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007051 mddev->curr_resync > 2) {
7052 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7053 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7054 if (mddev->curr_resync >= mddev->recovery_cp) {
7055 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007056 "md: checkpointing %s of %s.\n",
7057 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007058 mddev->recovery_cp = mddev->curr_resync;
7059 }
7060 } else
7061 mddev->recovery_cp = MaxSector;
7062 } else {
7063 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7064 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007065 rcu_read_lock();
7066 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007067 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007068 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007069 !test_bit(Faulty, &rdev->flags) &&
7070 !test_bit(In_sync, &rdev->flags) &&
7071 rdev->recovery_offset < mddev->curr_resync)
7072 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007073 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 }
NeilBrown17571282006-12-10 02:20:52 -08007076 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007079 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7080 /* We completed so min/max setting can be forgotten if used. */
7081 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7082 mddev->resync_min = 0;
7083 mddev->resync_max = MaxSector;
7084 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7085 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 mddev->curr_resync = 0;
7087 wake_up(&resync_wait);
7088 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7089 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007090 return;
7091
7092 interrupted:
7093 /*
7094 * got a signal, exit.
7095 */
7096 printk(KERN_INFO
7097 "md: md_do_sync() got signal ... exiting\n");
7098 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7099 goto out;
7100
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101}
NeilBrown29269552006-03-27 01:18:10 -08007102EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007104static int remove_and_add_spares(mddev_t *mddev)
7105{
7106 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007107 int spares = 0;
7108
NeilBrown97e4f422009-03-31 14:33:13 +11007109 mddev->curr_resync_completed = 0;
7110
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007111 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007112 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007113 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007114 (test_bit(Faulty, &rdev->flags) ||
7115 ! test_bit(In_sync, &rdev->flags)) &&
7116 atomic_read(&rdev->nr_pending)==0) {
7117 if (mddev->pers->hot_remove_disk(
7118 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007119 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007120 rdev->raid_disk = -1;
7121 }
7122 }
7123
NeilBrown53890422011-07-27 11:00:36 +10007124 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007125 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007126 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007127 !test_bit(In_sync, &rdev->flags) &&
NeilBrown42742152011-06-28 16:59:42 +10007128 !test_bit(Faulty, &rdev->flags) &&
Dan Williamse5427132008-07-28 23:28:06 -07007129 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007130 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007131 if (rdev->raid_disk < 0
7132 && !test_bit(Faulty, &rdev->flags)) {
7133 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007134 if (mddev->pers->
7135 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007136 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007137 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007138 spares++;
7139 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007140 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007141 } else
7142 break;
7143 }
NeilBrowndfc70642008-05-23 13:04:39 -07007144 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007145 }
7146 return spares;
7147}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007148
7149static void reap_sync_thread(mddev_t *mddev)
7150{
7151 mdk_rdev_t *rdev;
7152
7153 /* resync has finished, collect result */
7154 md_unregister_thread(mddev->sync_thread);
7155 mddev->sync_thread = NULL;
7156 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7157 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7158 /* success...*/
7159 /* activate any spares */
7160 if (mddev->pers->spare_active(mddev))
7161 sysfs_notify(&mddev->kobj, NULL,
7162 "degraded");
7163 }
7164 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7165 mddev->pers->finish_reshape)
7166 mddev->pers->finish_reshape(mddev);
7167 md_update_sb(mddev, 1);
7168
7169 /* if array is no-longer degraded, then any saved_raid_disk
7170 * information must be scrapped
7171 */
7172 if (!mddev->degraded)
7173 list_for_each_entry(rdev, &mddev->disks, same_set)
7174 rdev->saved_raid_disk = -1;
7175
7176 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7177 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7178 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7179 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7180 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7181 /* flag recovery needed just to double check */
7182 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7183 sysfs_notify_dirent_safe(mddev->sysfs_action);
7184 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007185 if (mddev->event_work.func)
7186 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007187}
7188
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189/*
7190 * This routine is regularly called by all per-raid-array threads to
7191 * deal with generic issues like resync and super-block update.
7192 * Raid personalities that don't have a thread (linear/raid0) do not
7193 * need this as they never do any recovery or update the superblock.
7194 *
7195 * It does not do any resync itself, but rather "forks" off other threads
7196 * to do that as needed.
7197 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7198 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007199 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 * and wakeups up this thread which will reap the thread and finish up.
7201 * This thread also removes any faulty devices (with nr_pending == 0).
7202 *
7203 * The overall approach is:
7204 * 1/ if the superblock needs updating, update it.
7205 * 2/ If a recovery thread is running, don't do anything else.
7206 * 3/ If recovery has finished, clean up, possibly marking spares active.
7207 * 4/ If there are any faulty devices, remove them.
7208 * 5/ If array is degraded, try to add spares devices
7209 * 6/ If array has spares or is not in-sync, start a resync thread.
7210 */
7211void md_check_recovery(mddev_t *mddev)
7212{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007213 if (mddev->suspended)
7214 return;
7215
NeilBrown5f404022005-06-21 17:17:16 -07007216 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007217 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218
NeilBrownfca4d842005-06-21 17:17:11 -07007219 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007220 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007221 printk(KERN_INFO "md: %s in immediate safe mode\n",
7222 mdname(mddev));
7223 mddev->safemode = 2;
7224 }
7225 flush_signals(current);
7226 }
7227
NeilBrownc89a8ee2008-08-05 15:54:13 +10007228 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007231 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007233 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007234 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007235 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7236 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 ))
7238 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007239
NeilBrowndf5b89b2006-03-27 01:18:20 -08007240 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007241 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007242
NeilBrownc89a8ee2008-08-05 15:54:13 +10007243 if (mddev->ro) {
7244 /* Only thing we do on a ro array is remove
7245 * failed devices.
7246 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007247 mdk_rdev_t *rdev;
7248 list_for_each_entry(rdev, &mddev->disks, same_set)
7249 if (rdev->raid_disk >= 0 &&
7250 !test_bit(Blocked, &rdev->flags) &&
7251 test_bit(Faulty, &rdev->flags) &&
7252 atomic_read(&rdev->nr_pending)==0) {
7253 if (mddev->pers->hot_remove_disk(
7254 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007255 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007256 rdev->raid_disk = -1;
7257 }
7258 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007259 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7260 goto unlock;
7261 }
7262
NeilBrown31a59e32008-04-30 00:52:30 -07007263 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007264 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007265 spin_lock_irq(&mddev->write_lock);
7266 if (mddev->safemode &&
7267 !atomic_read(&mddev->writes_pending) &&
7268 !mddev->in_sync &&
7269 mddev->recovery_cp == MaxSector) {
7270 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007271 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007272 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007273 }
7274 if (mddev->safemode == 1)
7275 mddev->safemode = 0;
7276 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007277 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007278 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007279 }
NeilBrownfca4d842005-06-21 17:17:11 -07007280
NeilBrown850b2b42006-10-03 01:15:46 -07007281 if (mddev->flags)
7282 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007283
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7285 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7286 /* resync/recovery still happening */
7287 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7288 goto unlock;
7289 }
7290 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007291 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 goto unlock;
7293 }
Neil Brown72a23c22008-06-28 08:31:41 +10007294 /* Set RUNNING before clearing NEEDED to avoid
7295 * any transients in the value of "sync_action".
7296 */
7297 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7298 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007299 /* Clear some bits that don't mean anything, but
7300 * might be left set
7301 */
NeilBrown24dd4692005-11-08 21:39:26 -08007302 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7303 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007305 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7306 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 /* no recovery is running.
7308 * remove any failed drives, then
7309 * add spares if possible.
7310 * Spare are also removed and re-added, to allow
7311 * the personality to fail the re-add.
7312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007314 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007315 if (mddev->pers->check_reshape == NULL ||
7316 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007317 /* Cannot proceed */
7318 goto unlock;
7319 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007320 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007321 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007322 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7323 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007324 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007325 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007326 } else if (mddev->recovery_cp < MaxSector) {
7327 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007328 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007329 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7330 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007332
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007334 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7335 /* We are adding a device or devices to an array
7336 * which has the bitmap stored on all devices.
7337 * So make sure all bitmap pages get written
7338 */
7339 bitmap_write_all(mddev->bitmap);
7340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 mddev->sync_thread = md_register_thread(md_do_sync,
7342 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007343 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 if (!mddev->sync_thread) {
7345 printk(KERN_ERR "%s: could not start resync"
7346 " thread...\n",
7347 mdname(mddev));
7348 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007349 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7350 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7351 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7352 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7353 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007354 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007356 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007357 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 }
7359 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007360 if (!mddev->sync_thread) {
7361 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7362 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7363 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007364 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007365 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 mddev_unlock(mddev);
7368 }
7369}
7370
Dan Williams6bfe0b42008-04-30 00:52:32 -07007371void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7372{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007373 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007374 wait_event_timeout(rdev->blocked_wait,
7375 !test_bit(Blocked, &rdev->flags),
7376 msecs_to_jiffies(5000));
7377 rdev_dec_pending(rdev, mddev);
7378}
7379EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7380
NeilBrown2230dfe2011-07-28 11:31:46 +10007381
7382/* Bad block management.
7383 * We can record which blocks on each device are 'bad' and so just
7384 * fail those blocks, or that stripe, rather than the whole device.
7385 * Entries in the bad-block table are 64bits wide. This comprises:
7386 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7387 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7388 * A 'shift' can be set so that larger blocks are tracked and
7389 * consequently larger devices can be covered.
7390 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7391 *
7392 * Locking of the bad-block table uses a seqlock so md_is_badblock
7393 * might need to retry if it is very unlucky.
7394 * We will sometimes want to check for bad blocks in a bi_end_io function,
7395 * so we use the write_seqlock_irq variant.
7396 *
7397 * When looking for a bad block we specify a range and want to
7398 * know if any block in the range is bad. So we binary-search
7399 * to the last range that starts at-or-before the given endpoint,
7400 * (or "before the sector after the target range")
7401 * then see if it ends after the given start.
7402 * We return
7403 * 0 if there are no known bad blocks in the range
7404 * 1 if there are known bad block which are all acknowledged
7405 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7406 * plus the start/length of the first bad section we overlap.
7407 */
7408int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7409 sector_t *first_bad, int *bad_sectors)
7410{
7411 int hi;
7412 int lo = 0;
7413 u64 *p = bb->page;
7414 int rv = 0;
7415 sector_t target = s + sectors;
7416 unsigned seq;
7417
7418 if (bb->shift > 0) {
7419 /* round the start down, and the end up */
7420 s >>= bb->shift;
7421 target += (1<<bb->shift) - 1;
7422 target >>= bb->shift;
7423 sectors = target - s;
7424 }
7425 /* 'target' is now the first block after the bad range */
7426
7427retry:
7428 seq = read_seqbegin(&bb->lock);
7429
7430 hi = bb->count;
7431
7432 /* Binary search between lo and hi for 'target'
7433 * i.e. for the last range that starts before 'target'
7434 */
7435 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7436 * are known not to be the last range before target.
7437 * VARIANT: hi-lo is the number of possible
7438 * ranges, and decreases until it reaches 1
7439 */
7440 while (hi - lo > 1) {
7441 int mid = (lo + hi) / 2;
7442 sector_t a = BB_OFFSET(p[mid]);
7443 if (a < target)
7444 /* This could still be the one, earlier ranges
7445 * could not. */
7446 lo = mid;
7447 else
7448 /* This and later ranges are definitely out. */
7449 hi = mid;
7450 }
7451 /* 'lo' might be the last that started before target, but 'hi' isn't */
7452 if (hi > lo) {
7453 /* need to check all range that end after 's' to see if
7454 * any are unacknowledged.
7455 */
7456 while (lo >= 0 &&
7457 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7458 if (BB_OFFSET(p[lo]) < target) {
7459 /* starts before the end, and finishes after
7460 * the start, so they must overlap
7461 */
7462 if (rv != -1 && BB_ACK(p[lo]))
7463 rv = 1;
7464 else
7465 rv = -1;
7466 *first_bad = BB_OFFSET(p[lo]);
7467 *bad_sectors = BB_LEN(p[lo]);
7468 }
7469 lo--;
7470 }
7471 }
7472
7473 if (read_seqretry(&bb->lock, seq))
7474 goto retry;
7475
7476 return rv;
7477}
7478EXPORT_SYMBOL_GPL(md_is_badblock);
7479
7480/*
7481 * Add a range of bad blocks to the table.
7482 * This might extend the table, or might contract it
7483 * if two adjacent ranges can be merged.
7484 * We binary-search to find the 'insertion' point, then
7485 * decide how best to handle it.
7486 */
7487static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7488 int acknowledged)
7489{
7490 u64 *p;
7491 int lo, hi;
7492 int rv = 1;
7493
7494 if (bb->shift < 0)
7495 /* badblocks are disabled */
7496 return 0;
7497
7498 if (bb->shift) {
7499 /* round the start down, and the end up */
7500 sector_t next = s + sectors;
7501 s >>= bb->shift;
7502 next += (1<<bb->shift) - 1;
7503 next >>= bb->shift;
7504 sectors = next - s;
7505 }
7506
7507 write_seqlock_irq(&bb->lock);
7508
7509 p = bb->page;
7510 lo = 0;
7511 hi = bb->count;
7512 /* Find the last range that starts at-or-before 's' */
7513 while (hi - lo > 1) {
7514 int mid = (lo + hi) / 2;
7515 sector_t a = BB_OFFSET(p[mid]);
7516 if (a <= s)
7517 lo = mid;
7518 else
7519 hi = mid;
7520 }
7521 if (hi > lo && BB_OFFSET(p[lo]) > s)
7522 hi = lo;
7523
7524 if (hi > lo) {
7525 /* we found a range that might merge with the start
7526 * of our new range
7527 */
7528 sector_t a = BB_OFFSET(p[lo]);
7529 sector_t e = a + BB_LEN(p[lo]);
7530 int ack = BB_ACK(p[lo]);
7531 if (e >= s) {
7532 /* Yes, we can merge with a previous range */
7533 if (s == a && s + sectors >= e)
7534 /* new range covers old */
7535 ack = acknowledged;
7536 else
7537 ack = ack && acknowledged;
7538
7539 if (e < s + sectors)
7540 e = s + sectors;
7541 if (e - a <= BB_MAX_LEN) {
7542 p[lo] = BB_MAKE(a, e-a, ack);
7543 s = e;
7544 } else {
7545 /* does not all fit in one range,
7546 * make p[lo] maximal
7547 */
7548 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7549 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7550 s = a + BB_MAX_LEN;
7551 }
7552 sectors = e - s;
7553 }
7554 }
7555 if (sectors && hi < bb->count) {
7556 /* 'hi' points to the first range that starts after 's'.
7557 * Maybe we can merge with the start of that range */
7558 sector_t a = BB_OFFSET(p[hi]);
7559 sector_t e = a + BB_LEN(p[hi]);
7560 int ack = BB_ACK(p[hi]);
7561 if (a <= s + sectors) {
7562 /* merging is possible */
7563 if (e <= s + sectors) {
7564 /* full overlap */
7565 e = s + sectors;
7566 ack = acknowledged;
7567 } else
7568 ack = ack && acknowledged;
7569
7570 a = s;
7571 if (e - a <= BB_MAX_LEN) {
7572 p[hi] = BB_MAKE(a, e-a, ack);
7573 s = e;
7574 } else {
7575 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7576 s = a + BB_MAX_LEN;
7577 }
7578 sectors = e - s;
7579 lo = hi;
7580 hi++;
7581 }
7582 }
7583 if (sectors == 0 && hi < bb->count) {
7584 /* we might be able to combine lo and hi */
7585 /* Note: 's' is at the end of 'lo' */
7586 sector_t a = BB_OFFSET(p[hi]);
7587 int lolen = BB_LEN(p[lo]);
7588 int hilen = BB_LEN(p[hi]);
7589 int newlen = lolen + hilen - (s - a);
7590 if (s >= a && newlen < BB_MAX_LEN) {
7591 /* yes, we can combine them */
7592 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7593 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7594 memmove(p + hi, p + hi + 1,
7595 (bb->count - hi - 1) * 8);
7596 bb->count--;
7597 }
7598 }
7599 while (sectors) {
7600 /* didn't merge (it all).
7601 * Need to add a range just before 'hi' */
7602 if (bb->count >= MD_MAX_BADBLOCKS) {
7603 /* No room for more */
7604 rv = 0;
7605 break;
7606 } else {
7607 int this_sectors = sectors;
7608 memmove(p + hi + 1, p + hi,
7609 (bb->count - hi) * 8);
7610 bb->count++;
7611
7612 if (this_sectors > BB_MAX_LEN)
7613 this_sectors = BB_MAX_LEN;
7614 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7615 sectors -= this_sectors;
7616 s += this_sectors;
7617 }
7618 }
7619
7620 bb->changed = 1;
7621 write_sequnlock_irq(&bb->lock);
7622
7623 return rv;
7624}
7625
7626int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors,
7627 int acknowledged)
7628{
7629 int rv = md_set_badblocks(&rdev->badblocks,
7630 s + rdev->data_offset, sectors, acknowledged);
7631 if (rv) {
7632 /* Make sure they get written out promptly */
7633 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7634 md_wakeup_thread(rdev->mddev->thread);
7635 }
7636 return rv;
7637}
7638EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7639
7640/*
7641 * Remove a range of bad blocks from the table.
7642 * This may involve extending the table if we spilt a region,
7643 * but it must not fail. So if the table becomes full, we just
7644 * drop the remove request.
7645 */
7646static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7647{
7648 u64 *p;
7649 int lo, hi;
7650 sector_t target = s + sectors;
7651 int rv = 0;
7652
7653 if (bb->shift > 0) {
7654 /* When clearing we round the start up and the end down.
7655 * This should not matter as the shift should align with
7656 * the block size and no rounding should ever be needed.
7657 * However it is better the think a block is bad when it
7658 * isn't than to think a block is not bad when it is.
7659 */
7660 s += (1<<bb->shift) - 1;
7661 s >>= bb->shift;
7662 target >>= bb->shift;
7663 sectors = target - s;
7664 }
7665
7666 write_seqlock_irq(&bb->lock);
7667
7668 p = bb->page;
7669 lo = 0;
7670 hi = bb->count;
7671 /* Find the last range that starts before 'target' */
7672 while (hi - lo > 1) {
7673 int mid = (lo + hi) / 2;
7674 sector_t a = BB_OFFSET(p[mid]);
7675 if (a < target)
7676 lo = mid;
7677 else
7678 hi = mid;
7679 }
7680 if (hi > lo) {
7681 /* p[lo] is the last range that could overlap the
7682 * current range. Earlier ranges could also overlap,
7683 * but only this one can overlap the end of the range.
7684 */
7685 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7686 /* Partial overlap, leave the tail of this range */
7687 int ack = BB_ACK(p[lo]);
7688 sector_t a = BB_OFFSET(p[lo]);
7689 sector_t end = a + BB_LEN(p[lo]);
7690
7691 if (a < s) {
7692 /* we need to split this range */
7693 if (bb->count >= MD_MAX_BADBLOCKS) {
7694 rv = 0;
7695 goto out;
7696 }
7697 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7698 bb->count++;
7699 p[lo] = BB_MAKE(a, s-a, ack);
7700 lo++;
7701 }
7702 p[lo] = BB_MAKE(target, end - target, ack);
7703 /* there is no longer an overlap */
7704 hi = lo;
7705 lo--;
7706 }
7707 while (lo >= 0 &&
7708 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7709 /* This range does overlap */
7710 if (BB_OFFSET(p[lo]) < s) {
7711 /* Keep the early parts of this range. */
7712 int ack = BB_ACK(p[lo]);
7713 sector_t start = BB_OFFSET(p[lo]);
7714 p[lo] = BB_MAKE(start, s - start, ack);
7715 /* now low doesn't overlap, so.. */
7716 break;
7717 }
7718 lo--;
7719 }
7720 /* 'lo' is strictly before, 'hi' is strictly after,
7721 * anything between needs to be discarded
7722 */
7723 if (hi - lo > 1) {
7724 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7725 bb->count -= (hi - lo - 1);
7726 }
7727 }
7728
7729 bb->changed = 1;
7730out:
7731 write_sequnlock_irq(&bb->lock);
7732 return rv;
7733}
7734
7735int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors)
7736{
7737 return md_clear_badblocks(&rdev->badblocks,
7738 s + rdev->data_offset,
7739 sectors);
7740}
7741EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7742
7743/*
7744 * Acknowledge all bad blocks in a list.
7745 * This only succeeds if ->changed is clear. It is used by
7746 * in-kernel metadata updates
7747 */
7748void md_ack_all_badblocks(struct badblocks *bb)
7749{
7750 if (bb->page == NULL || bb->changed)
7751 /* no point even trying */
7752 return;
7753 write_seqlock_irq(&bb->lock);
7754
7755 if (bb->changed == 0) {
7756 u64 *p = bb->page;
7757 int i;
7758 for (i = 0; i < bb->count ; i++) {
7759 if (!BB_ACK(p[i])) {
7760 sector_t start = BB_OFFSET(p[i]);
7761 int len = BB_LEN(p[i]);
7762 p[i] = BB_MAKE(start, len, 1);
7763 }
7764 }
7765 }
7766 write_sequnlock_irq(&bb->lock);
7767}
7768EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7769
NeilBrown16c791a2011-07-28 11:31:47 +10007770/* sysfs access to bad-blocks list.
7771 * We present two files.
7772 * 'bad-blocks' lists sector numbers and lengths of ranges that
7773 * are recorded as bad. The list is truncated to fit within
7774 * the one-page limit of sysfs.
7775 * Writing "sector length" to this file adds an acknowledged
7776 * bad block list.
7777 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7778 * been acknowledged. Writing to this file adds bad blocks
7779 * without acknowledging them. This is largely for testing.
7780 */
7781
7782static ssize_t
7783badblocks_show(struct badblocks *bb, char *page, int unack)
7784{
7785 size_t len;
7786 int i;
7787 u64 *p = bb->page;
7788 unsigned seq;
7789
7790 if (bb->shift < 0)
7791 return 0;
7792
7793retry:
7794 seq = read_seqbegin(&bb->lock);
7795
7796 len = 0;
7797 i = 0;
7798
7799 while (len < PAGE_SIZE && i < bb->count) {
7800 sector_t s = BB_OFFSET(p[i]);
7801 unsigned int length = BB_LEN(p[i]);
7802 int ack = BB_ACK(p[i]);
7803 i++;
7804
7805 if (unack && ack)
7806 continue;
7807
7808 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
7809 (unsigned long long)s << bb->shift,
7810 length << bb->shift);
7811 }
7812
7813 if (read_seqretry(&bb->lock, seq))
7814 goto retry;
7815
7816 return len;
7817}
7818
7819#define DO_DEBUG 1
7820
7821static ssize_t
7822badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
7823{
7824 unsigned long long sector;
7825 int length;
7826 char newline;
7827#ifdef DO_DEBUG
7828 /* Allow clearing via sysfs *only* for testing/debugging.
7829 * Normally only a successful write may clear a badblock
7830 */
7831 int clear = 0;
7832 if (page[0] == '-') {
7833 clear = 1;
7834 page++;
7835 }
7836#endif /* DO_DEBUG */
7837
7838 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
7839 case 3:
7840 if (newline != '\n')
7841 return -EINVAL;
7842 case 2:
7843 if (length <= 0)
7844 return -EINVAL;
7845 break;
7846 default:
7847 return -EINVAL;
7848 }
7849
7850#ifdef DO_DEBUG
7851 if (clear) {
7852 md_clear_badblocks(bb, sector, length);
7853 return len;
7854 }
7855#endif /* DO_DEBUG */
7856 if (md_set_badblocks(bb, sector, length, !unack))
7857 return len;
7858 else
7859 return -ENOSPC;
7860}
7861
Adrian Bunk75c96f82005-05-05 16:16:09 -07007862static int md_notify_reboot(struct notifier_block *this,
7863 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007864{
7865 struct list_head *tmp;
7866 mddev_t *mddev;
7867
7868 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7869
7870 printk(KERN_INFO "md: stopping all md devices.\n");
7871
NeilBrown29ac4aa2008-02-06 01:39:58 -08007872 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007873 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007874 /* Force a switch to readonly even array
7875 * appears to still be in use. Hence
7876 * the '100'.
7877 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007878 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007879 mddev_unlock(mddev);
7880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 /*
7882 * certain more exotic SCSI devices are known to be
7883 * volatile wrt too early system reboots. While the
7884 * right place to handle this issue is the given
7885 * driver, we do want to have a safe RAID driver ...
7886 */
7887 mdelay(1000*1);
7888 }
7889 return NOTIFY_DONE;
7890}
7891
Adrian Bunk75c96f82005-05-05 16:16:09 -07007892static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893 .notifier_call = md_notify_reboot,
7894 .next = NULL,
7895 .priority = INT_MAX, /* before any real devices */
7896};
7897
7898static void md_geninit(void)
7899{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7901
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007902 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007903}
7904
Adrian Bunk75c96f82005-05-05 16:16:09 -07007905static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906{
Tejun Heoe804ac72010-10-15 15:36:08 +02007907 int ret = -ENOMEM;
7908
Tejun Heoada609e2011-01-25 14:35:54 +01007909 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02007910 if (!md_wq)
7911 goto err_wq;
7912
7913 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7914 if (!md_misc_wq)
7915 goto err_misc_wq;
7916
7917 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7918 goto err_md;
7919
7920 if ((ret = register_blkdev(0, "mdp")) < 0)
7921 goto err_mdp;
7922 mdp_major = ret;
7923
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007924 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007925 md_probe, NULL, NULL);
7926 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 md_probe, NULL, NULL);
7928
Linus Torvalds1da177e2005-04-16 15:20:36 -07007929 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007930 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007931
7932 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
Tejun Heoe804ac72010-10-15 15:36:08 +02007935err_mdp:
7936 unregister_blkdev(MD_MAJOR, "md");
7937err_md:
7938 destroy_workqueue(md_misc_wq);
7939err_misc_wq:
7940 destroy_workqueue(md_wq);
7941err_wq:
7942 return ret;
7943}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944
7945#ifndef MODULE
7946
7947/*
7948 * Searches all registered partitions for autorun RAID arrays
7949 * at boot time.
7950 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007951
7952static LIST_HEAD(all_detected_devices);
7953struct detected_devices_node {
7954 struct list_head list;
7955 dev_t dev;
7956};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957
7958void md_autodetect_dev(dev_t dev)
7959{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007960 struct detected_devices_node *node_detected_dev;
7961
7962 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7963 if (node_detected_dev) {
7964 node_detected_dev->dev = dev;
7965 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7966 } else {
7967 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7968 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970}
7971
7972
7973static void autostart_arrays(int part)
7974{
7975 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007976 struct detected_devices_node *node_detected_dev;
7977 dev_t dev;
7978 int i_scanned, i_passed;
7979
7980 i_scanned = 0;
7981 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007982
7983 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7984
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007985 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7986 i_scanned++;
7987 node_detected_dev = list_entry(all_detected_devices.next,
7988 struct detected_devices_node, list);
7989 list_del(&node_detected_dev->list);
7990 dev = node_detected_dev->dev;
7991 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007992 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 if (IS_ERR(rdev))
7994 continue;
7995
NeilBrownb2d444d2005-11-08 21:39:31 -08007996 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 MD_BUG();
7998 continue;
7999 }
NeilBrownd0fae182008-03-04 14:29:31 -08008000 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008001 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008002 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008004
8005 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8006 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007
8008 autorun_devices(part);
8009}
8010
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008011#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008012
8013static __exit void md_exit(void)
8014{
8015 mddev_t *mddev;
8016 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008017
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008018 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008019 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008021 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 unregister_blkdev(mdp_major, "mdp");
8023 unregister_reboot_notifier(&md_notifier);
8024 unregister_sysctl_table(raid_table_header);
8025 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008026 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008028 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008030 destroy_workqueue(md_misc_wq);
8031 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032}
8033
Dan Williams685784a2007-07-09 11:56:42 -07008034subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035module_exit(md_exit)
8036
NeilBrownf91de922005-11-08 21:39:36 -08008037static int get_ro(char *buffer, struct kernel_param *kp)
8038{
8039 return sprintf(buffer, "%d", start_readonly);
8040}
8041static int set_ro(const char *val, struct kernel_param *kp)
8042{
8043 char *e;
8044 int num = simple_strtoul(val, &e, 10);
8045 if (*val && (*e == '\0' || *e == '\n')) {
8046 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008047 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008048 }
8049 return -EINVAL;
8050}
8051
NeilBrown80ca3a42006-07-10 04:44:18 -07008052module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8053module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008054
NeilBrownefeb53c2009-01-09 08:31:10 +11008055module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008056
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057EXPORT_SYMBOL(register_md_personality);
8058EXPORT_SYMBOL(unregister_md_personality);
8059EXPORT_SYMBOL(md_error);
8060EXPORT_SYMBOL(md_done_sync);
8061EXPORT_SYMBOL(md_write_start);
8062EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063EXPORT_SYMBOL(md_register_thread);
8064EXPORT_SYMBOL(md_unregister_thread);
8065EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066EXPORT_SYMBOL(md_check_recovery);
8067MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008068MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008069MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008070MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);