blob: 7ae3c5a18001b437e3d43ef593e5342b8db1cfce [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 }
NeilBrown2699b672011-07-28 11:31:47 +1000760 if (rdev->bb_page) {
761 put_page(rdev->bb_page);
762 rdev->bb_page = NULL;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766
NeilBrown6712ecf2007-09-27 12:47:43 +0200767static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700768{
769 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800770 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700771
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700772 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
773 printk("md: super_written gets error=%d, uptodate=%d\n",
774 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
775 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800776 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700777 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700778
NeilBrowna9701a32005-11-08 21:39:34 -0800779 if (atomic_dec_and_test(&mddev->pending_writes))
780 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700781 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700782}
783
784void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
785 sector_t sector, int size, struct page *page)
786{
787 /* write first size bytes of page to sector of rdev
788 * Increment mddev->pending_writes before returning
789 * and decrement it on completion, waking up sb_wait
790 * if zero is reached.
791 * If an error occurred, call md_error
792 */
NeilBrowna167f662010-10-26 18:31:13 +1100793 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700794
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100795 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700796 bio->bi_sector = sector;
797 bio_add_page(bio, page, size, 0);
798 bio->bi_private = rdev;
799 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800800
NeilBrown7bfa19f2005-06-21 17:17:28 -0700801 atomic_inc(&mddev->pending_writes);
Jens Axboe721a9602011-03-09 11:56:30 +0100802 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800803}
804
805void md_super_wait(mddev_t *mddev)
806{
Tejun Heoe9c74692010-09-03 11:56:18 +0200807 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800808 DEFINE_WAIT(wq);
809 for(;;) {
810 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
811 if (atomic_read(&mddev->pending_writes)==0)
812 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800813 schedule();
814 }
815 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700816}
817
NeilBrown6712ecf2007-09-27 12:47:43 +0200818static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
NeilBrown2b193362010-10-27 15:16:40 +1100823int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100824 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
NeilBrowna167f662010-10-26 18:31:13 +1100826 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct completion event;
828 int ret;
829
Jens Axboe721a9602011-03-09 11:56:30 +0100830 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100832 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
833 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100834 if (metadata_op)
835 bio->bi_sector = sector + rdev->sb_start;
836 else
837 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 bio_add_page(bio, page, size, 0);
839 init_completion(&event);
840 bio->bi_private = &event;
841 bio->bi_end_io = bi_complete;
842 submit_bio(rw, bio);
843 wait_for_completion(&event);
844
845 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
846 bio_put(bio);
847 return ret;
848}
NeilBrowna8745db2006-01-06 00:20:34 -0800849EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
NeilBrown0002b272005-09-09 16:23:53 -0700851static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 char b[BDEVNAME_SIZE];
854 if (!rdev->sb_page) {
855 MD_BUG();
856 return -EINVAL;
857 }
858 if (rdev->sb_loaded)
859 return 0;
860
861
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100862 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 goto fail;
864 rdev->sb_loaded = 1;
865 return 0;
866
867fail:
868 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
869 bdevname(rdev->bdev,b));
870 return -EINVAL;
871}
872
873static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
874{
Andre Noll05710462008-07-11 22:02:20 +1000875 return sb1->set_uuid0 == sb2->set_uuid0 &&
876 sb1->set_uuid1 == sb2->set_uuid1 &&
877 sb1->set_uuid2 == sb2->set_uuid2 &&
878 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
882{
883 int ret;
884 mdp_super_t *tmp1, *tmp2;
885
886 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
887 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
888
889 if (!tmp1 || !tmp2) {
890 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100891 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto abort;
893 }
894
895 *tmp1 = *sb1;
896 *tmp2 = *sb2;
897
898 /*
899 * nr_disks is not constant
900 */
901 tmp1->nr_disks = 0;
902 tmp2->nr_disks = 0;
903
Andre Nollce0c8e02008-07-11 22:02:20 +1000904 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700906 kfree(tmp1);
907 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return ret;
909}
910
NeilBrown4d167f02007-05-09 02:35:37 -0700911
912static u32 md_csum_fold(u32 csum)
913{
914 csum = (csum & 0xffff) + (csum >> 16);
915 return (csum & 0xffff) + (csum >> 16);
916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918static unsigned int calc_sb_csum(mdp_super_t * sb)
919{
NeilBrown4d167f02007-05-09 02:35:37 -0700920 u64 newcsum = 0;
921 u32 *sb32 = (u32*)sb;
922 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned int disk_csum, csum;
924
925 disk_csum = sb->sb_csum;
926 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700927
928 for (i = 0; i < MD_SB_BYTES/4 ; i++)
929 newcsum += sb32[i];
930 csum = (newcsum & 0xffffffff) + (newcsum>>32);
931
932
933#ifdef CONFIG_ALPHA
934 /* This used to use csum_partial, which was wrong for several
935 * reasons including that different results are returned on
936 * different architectures. It isn't critical that we get exactly
937 * the same return value as before (we always csum_fold before
938 * testing, and that removes any differences). However as we
939 * know that csum_partial always returned a 16bit value on
940 * alphas, do a fold to maximise conformity to previous behaviour.
941 */
942 sb->sb_csum = md_csum_fold(disk_csum);
943#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700945#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return csum;
947}
948
949
950/*
951 * Handle superblock details.
952 * We want to be able to handle multiple superblock formats
953 * so we have a common interface to them all, and an array of
954 * different handlers.
955 * We rely on user-space to write the initial superblock, and support
956 * reading and updating of superblocks.
957 * Interface methods are:
958 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
959 * loads and validates a superblock on dev.
960 * if refdev != NULL, compare superblocks on both devices
961 * Return:
962 * 0 - dev has a superblock that is compatible with refdev
963 * 1 - dev has a superblock that is compatible and newer than refdev
964 * so dev should be used as the refdev in future
965 * -EINVAL superblock incompatible or invalid
966 * -othererror e.g. -EIO
967 *
968 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
969 * Verify that dev is acceptable into mddev.
970 * The first time, mddev->raid_disks will be 0, and data from
971 * dev should be merged in. Subsequent calls check that dev
972 * is new enough. Return 0 or -EINVAL
973 *
974 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
975 * Update the superblock for rdev with data in mddev
976 * This does not write to disc.
977 *
978 */
979
980struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000981 char *name;
982 struct module *owner;
983 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
984 int minor_version);
985 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
986 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
987 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000988 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989};
990
991/*
Andre Noll0894cc32009-06-18 08:49:23 +1000992 * Check that the given mddev has no bitmap.
993 *
994 * This function is called from the run method of all personalities that do not
995 * support bitmaps. It prints an error message and returns non-zero if mddev
996 * has a bitmap. Otherwise, it returns 0.
997 *
998 */
999int md_check_no_bitmap(mddev_t *mddev)
1000{
NeilBrownc3d97142009-12-14 12:49:52 +11001001 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001002 return 0;
1003 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1004 mdname(mddev), mddev->pers->name);
1005 return 1;
1006}
1007EXPORT_SYMBOL(md_check_no_bitmap);
1008
1009/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * load_super for 0.90.0
1011 */
1012static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1013{
1014 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1015 mdp_super_t *sb;
1016 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /*
Andre Noll0f420352008-07-11 22:02:23 +10001019 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * it's at the end of the disk.
1021 *
1022 * It also happens to be a multiple of 4Kb.
1023 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001024 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
NeilBrown0002b272005-09-09 16:23:53 -07001026 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (ret) return ret;
1028
1029 ret = -EINVAL;
1030
1031 bdevname(rdev->bdev, b);
Namhyung Kim65a06f02011-07-27 11:00:36 +10001032 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (sb->md_magic != MD_SB_MAGIC) {
1035 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1036 b);
1037 goto abort;
1038 }
1039
1040 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001041 sb->minor_version < 90 ||
1042 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1044 sb->major_version, sb->minor_version,
1045 b);
1046 goto abort;
1047 }
1048
1049 if (sb->raid_disks <= 0)
1050 goto abort;
1051
NeilBrown4d167f02007-05-09 02:35:37 -07001052 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1054 b);
1055 goto abort;
1056 }
1057
1058 rdev->preferred_minor = sb->md_minor;
1059 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001060 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f383072011-07-28 11:31:47 +10001061 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 if (sb->level == LEVEL_MULTIPATH)
1064 rdev->desc_nr = -1;
1065 else
1066 rdev->desc_nr = sb->this_disk.number;
1067
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001068 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001070 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001072 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!uuid_equal(refsb, sb)) {
1074 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1075 b, bdevname(refdev->bdev,b2));
1076 goto abort;
1077 }
1078 if (!sb_equal(refsb, sb)) {
1079 printk(KERN_WARNING "md: %s has same UUID"
1080 " but different superblock to %s\n",
1081 b, bdevname(refdev->bdev, b2));
1082 goto abort;
1083 }
1084 ev1 = md_event(sb);
1085 ev2 = md_event(refsb);
1086 if (ev1 > ev2)
1087 ret = 1;
1088 else
1089 ret = 0;
1090 }
NeilBrown8190e752009-06-18 08:48:58 +10001091 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Andre Nolldd8ac332009-03-31 14:33:13 +11001093 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001094 /* "this cannot possibly happen" ... */
1095 ret = -EINVAL;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 abort:
1098 return ret;
1099}
1100
1101/*
1102 * validate_super for 0.90.0
1103 */
1104static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1105{
1106 mdp_disk_t *desc;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001107 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001108 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
NeilBrown41158c72005-06-21 17:17:25 -07001110 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001111 clear_bit(Faulty, &rdev->flags);
1112 clear_bit(In_sync, &rdev->flags);
1113 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (mddev->raid_disks == 0) {
1116 mddev->major_version = 0;
1117 mddev->minor_version = sb->minor_version;
1118 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001119 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001120 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 mddev->ctime = sb->ctime;
1122 mddev->utime = sb->utime;
1123 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001124 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 mddev->layout = sb->layout;
1126 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001127 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001128 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001129 mddev->bitmap_info.offset = 0;
1130 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
NeilBrownf6705572006-03-27 01:18:11 -08001132 if (mddev->minor_version >= 91) {
1133 mddev->reshape_position = sb->reshape_position;
1134 mddev->delta_disks = sb->delta_disks;
1135 mddev->new_level = sb->new_level;
1136 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001137 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001138 } else {
1139 mddev->reshape_position = MaxSector;
1140 mddev->delta_disks = 0;
1141 mddev->new_level = mddev->level;
1142 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001143 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001144 }
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (sb->state & (1<<MD_SB_CLEAN))
1147 mddev->recovery_cp = MaxSector;
1148 else {
1149 if (sb->events_hi == sb->cp_events_hi &&
1150 sb->events_lo == sb->cp_events_lo) {
1151 mddev->recovery_cp = sb->recovery_cp;
1152 } else
1153 mddev->recovery_cp = 0;
1154 }
1155
1156 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1157 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1158 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1159 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1160
1161 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001162
1163 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001164 mddev->bitmap_info.file == NULL)
1165 mddev->bitmap_info.offset =
1166 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001167
NeilBrown41158c72005-06-21 17:17:25 -07001168 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001169 /* Insist on good event counter while assembling, except
1170 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001172 if (sb->disks[rdev->desc_nr].state & (
1173 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1174 if (ev1 < mddev->events)
1175 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001176 } else if (mddev->bitmap) {
1177 /* if adding to array with a bitmap, then we can accept an
1178 * older device ... but not too old.
1179 */
NeilBrown41158c72005-06-21 17:17:25 -07001180 if (ev1 < mddev->bitmap->events_cleared)
1181 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001182 } else {
1183 if (ev1 < mddev->events)
1184 /* just a hot-add of a new device, leave raid_disk at -1 */
1185 return 0;
1186 }
NeilBrown41158c72005-06-21 17:17:25 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 desc = sb->disks + rdev->desc_nr;
1190
1191 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001192 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001193 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1194 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001195 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001197 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1198 /* active but not in sync implies recovery up to
1199 * reshape position. We don't know exactly where
1200 * that is, so set to zero for now */
1201 if (mddev->minor_version >= 91) {
1202 rdev->recovery_offset = 0;
1203 rdev->raid_disk = desc->raid_disk;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001206 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1207 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001208 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001209 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return 0;
1211}
1212
1213/*
1214 * sync_super for 0.90.0
1215 */
1216static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1217{
1218 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 mdk_rdev_t *rdev2;
1220 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /* make rdev->sb match mddev data..
1224 *
1225 * 1/ zero out disks
1226 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1227 * 3/ any empty disks < next_spare become removed
1228 *
1229 * disks[0] gets initialised to REMOVED because
1230 * we cannot be sure from other fields if it has
1231 * been initialised or not.
1232 */
1233 int i;
1234 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1235
NeilBrown61181562005-09-09 16:24:02 -07001236 rdev->sb_size = MD_SB_BYTES;
1237
Namhyung Kim65a06f02011-07-27 11:00:36 +10001238 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 memset(sb, 0, sizeof(*sb));
1241
1242 sb->md_magic = MD_SB_MAGIC;
1243 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sb->patch_version = mddev->patch_version;
1245 sb->gvalid_words = 0; /* ignored */
1246 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1247 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1248 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1249 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1250
1251 sb->ctime = mddev->ctime;
1252 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001253 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 sb->raid_disks = mddev->raid_disks;
1255 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001256 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 sb->utime = mddev->utime;
1258 sb->state = 0;
1259 sb->events_hi = (mddev->events>>32);
1260 sb->events_lo = (u32)mddev->events;
1261
NeilBrownf6705572006-03-27 01:18:11 -08001262 if (mddev->reshape_position == MaxSector)
1263 sb->minor_version = 90;
1264 else {
1265 sb->minor_version = 91;
1266 sb->reshape_position = mddev->reshape_position;
1267 sb->new_level = mddev->new_level;
1268 sb->delta_disks = mddev->delta_disks;
1269 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001270 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001271 }
1272 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (mddev->in_sync)
1274 {
1275 sb->recovery_cp = mddev->recovery_cp;
1276 sb->cp_events_hi = (mddev->events>>32);
1277 sb->cp_events_lo = (u32)mddev->events;
1278 if (mddev->recovery_cp == MaxSector)
1279 sb->state = (1<< MD_SB_CLEAN);
1280 } else
1281 sb->recovery_cp = 0;
1282
1283 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001284 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
NeilBrownc3d97142009-12-14 12:49:52 +11001286 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001287 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001290 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001292 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001293 int is_active = test_bit(In_sync, &rdev2->flags);
1294
1295 if (rdev2->raid_disk >= 0 &&
1296 sb->minor_version >= 91)
1297 /* we have nowhere to store the recovery_offset,
1298 * but if it is not below the reshape_position,
1299 * we can piggy-back on that.
1300 */
1301 is_active = 1;
1302 if (rdev2->raid_disk < 0 ||
1303 test_bit(Faulty, &rdev2->flags))
1304 is_active = 0;
1305 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001306 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001308 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001309 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 d = &sb->disks[rdev2->desc_nr];
1311 nr_disks++;
1312 d->number = rdev2->desc_nr;
1313 d->major = MAJOR(rdev2->bdev->bd_dev);
1314 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001315 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 d->raid_disk = rdev2->raid_disk;
1317 else
1318 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001319 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001321 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001323 if (test_bit(In_sync, &rdev2->flags))
1324 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 active++;
1326 working++;
1327 } else {
1328 d->state = 0;
1329 spare++;
1330 working++;
1331 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001332 if (test_bit(WriteMostly, &rdev2->flags))
1333 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /* now set the "removed" and "faulty" bits on any missing devices */
1336 for (i=0 ; i < mddev->raid_disks ; i++) {
1337 mdp_disk_t *d = &sb->disks[i];
1338 if (d->state == 0 && d->number == 0) {
1339 d->number = i;
1340 d->raid_disk = i;
1341 d->state = (1<<MD_DISK_REMOVED);
1342 d->state |= (1<<MD_DISK_FAULTY);
1343 failed++;
1344 }
1345 }
1346 sb->nr_disks = nr_disks;
1347 sb->active_disks = active;
1348 sb->working_disks = working;
1349 sb->failed_disks = failed;
1350 sb->spare_disks = spare;
1351
1352 sb->this_disk = sb->disks[rdev->desc_nr];
1353 sb->sb_csum = calc_sb_csum(sb);
1354}
1355
1356/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001357 * rdev_size_change for 0.90.0
1358 */
1359static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001360super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001361{
Andre Noll58c0fed2009-03-31 14:33:13 +11001362 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001363 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001364 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001366 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001367 if (!num_sectors || num_sectors > rdev->sb_start)
1368 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001369 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001370 rdev->sb_page);
1371 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001372 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001373}
1374
1375
1376/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * version 1 superblock
1378 */
1379
NeilBrown1c05b4b2006-10-21 10:24:08 -07001380static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001382 __le32 disk_csum;
1383 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 unsigned long long newcsum;
1385 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001386 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 int i;
1388
1389 disk_csum = sb->sb_csum;
1390 sb->sb_csum = 0;
1391 newcsum = 0;
1392 for (i=0; size>=4; size -= 4 )
1393 newcsum += le32_to_cpu(*isuper++);
1394
1395 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001396 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1399 sb->sb_csum = disk_csum;
1400 return cpu_to_le32(csum);
1401}
1402
NeilBrown2699b672011-07-28 11:31:47 +10001403static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1404 int acknowledged);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1406{
1407 struct mdp_superblock_1 *sb;
1408 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001409 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001411 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /*
Andre Noll0f420352008-07-11 22:02:23 +10001414 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * It is always aligned to a 4K boundary and
1416 * depeding on minor_version, it can be:
1417 * 0: At least 8K, but less than 12K, from end of device
1418 * 1: At start of device
1419 * 2: 4K from start of device.
1420 */
1421 switch(minor_version) {
1422 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001423 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001424 sb_start -= 8*2;
1425 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001428 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 break;
1430 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001431 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433 default:
1434 return -EINVAL;
1435 }
Andre Noll0f420352008-07-11 22:02:23 +10001436 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
NeilBrown0002b272005-09-09 16:23:53 -07001438 /* superblock is rarely larger than 1K, but it can be larger,
1439 * and it is safe to read 4k, so we do that
1440 */
1441 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (ret) return ret;
1443
1444
Namhyung Kim65a06f02011-07-27 11:00:36 +10001445 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1448 sb->major_version != cpu_to_le32(1) ||
1449 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001450 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001451 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return -EINVAL;
1453
1454 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1455 printk("md: invalid superblock checksum on %s\n",
1456 bdevname(rdev->bdev,b));
1457 return -EINVAL;
1458 }
1459 if (le64_to_cpu(sb->data_size) < 10) {
1460 printk("md: data_size too small on %s\n",
1461 bdevname(rdev->bdev,b));
1462 return -EINVAL;
1463 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 rdev->preferred_minor = 0xffff;
1466 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001467 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
NeilBrown0002b272005-09-09 16:23:53 -07001469 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001470 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001471 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001472 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1473
1474 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001475 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001476 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001477
NeilBrown31b65a02006-07-10 04:44:14 -07001478 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1479 rdev->desc_nr = -1;
1480 else
1481 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1482
NeilBrown2699b672011-07-28 11:31:47 +10001483 if (!rdev->bb_page) {
1484 rdev->bb_page = alloc_page(GFP_KERNEL);
1485 if (!rdev->bb_page)
1486 return -ENOMEM;
1487 }
1488 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1489 rdev->badblocks.count == 0) {
1490 /* need to load the bad block list.
1491 * Currently we limit it to one page.
1492 */
1493 s32 offset;
1494 sector_t bb_sector;
1495 u64 *bbp;
1496 int i;
1497 int sectors = le16_to_cpu(sb->bblog_size);
1498 if (sectors > (PAGE_SIZE / 512))
1499 return -EINVAL;
1500 offset = le32_to_cpu(sb->bblog_offset);
1501 if (offset == 0)
1502 return -EINVAL;
1503 bb_sector = (long long)offset;
1504 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1505 rdev->bb_page, READ, true))
1506 return -EIO;
1507 bbp = (u64 *)page_address(rdev->bb_page);
1508 rdev->badblocks.shift = sb->bblog_shift;
1509 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1510 u64 bb = le64_to_cpu(*bbp);
1511 int count = bb & (0x3ff);
1512 u64 sector = bb >> 10;
1513 sector <<= sb->bblog_shift;
1514 count <<= sb->bblog_shift;
1515 if (bb + 1 == 0)
1516 break;
1517 if (md_set_badblocks(&rdev->badblocks,
1518 sector, count, 1) == 0)
1519 return -EINVAL;
1520 }
1521 } else if (sb->bblog_offset == 0)
1522 rdev->badblocks.shift = -1;
1523
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001524 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001525 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001526 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001528 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1531 sb->level != refsb->level ||
1532 sb->layout != refsb->layout ||
1533 sb->chunksize != refsb->chunksize) {
1534 printk(KERN_WARNING "md: %s has strangely different"
1535 " superblock to %s\n",
1536 bdevname(rdev->bdev,b),
1537 bdevname(refdev->bdev,b2));
1538 return -EINVAL;
1539 }
1540 ev1 = le64_to_cpu(sb->events);
1541 ev2 = le64_to_cpu(refsb->events);
1542
1543 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001544 ret = 1;
1545 else
1546 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
NeilBrowna1801f82008-03-04 14:29:31 -08001548 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001549 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001550 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001552 rdev->sectors = rdev->sb_start;
1553 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001555 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001556 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001557 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
1561static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1562{
Namhyung Kim65a06f02011-07-27 11:00:36 +10001563 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001564 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
NeilBrown41158c72005-06-21 17:17:25 -07001566 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001567 clear_bit(Faulty, &rdev->flags);
1568 clear_bit(In_sync, &rdev->flags);
1569 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (mddev->raid_disks == 0) {
1572 mddev->major_version = 1;
1573 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001574 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001575 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1577 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1578 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001579 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 mddev->layout = le32_to_cpu(sb->layout);
1581 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001582 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001583 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001584 mddev->bitmap_info.offset = 0;
1585 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1588 memcpy(mddev->uuid, sb->set_uuid, 16);
1589
1590 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001591
NeilBrown71c08052005-09-09 16:23:51 -07001592 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001593 mddev->bitmap_info.file == NULL )
1594 mddev->bitmap_info.offset =
1595 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001596
NeilBrownf6705572006-03-27 01:18:11 -08001597 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1598 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1599 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1600 mddev->new_level = le32_to_cpu(sb->new_level);
1601 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001602 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001603 } else {
1604 mddev->reshape_position = MaxSector;
1605 mddev->delta_disks = 0;
1606 mddev->new_level = mddev->level;
1607 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001608 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001609 }
1610
NeilBrown41158c72005-06-21 17:17:25 -07001611 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001612 /* Insist of good event counter while assembling, except for
1613 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001615 if (rdev->desc_nr >= 0 &&
1616 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1617 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1618 if (ev1 < mddev->events)
1619 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001620 } else if (mddev->bitmap) {
1621 /* If adding to array with a bitmap, then we can accept an
1622 * older device, but not too old.
1623 */
NeilBrown41158c72005-06-21 17:17:25 -07001624 if (ev1 < mddev->bitmap->events_cleared)
1625 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001626 } else {
1627 if (ev1 < mddev->events)
1628 /* just a hot-add of a new device, leave raid_disk at -1 */
1629 return 0;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (mddev->level != LEVEL_MULTIPATH) {
1632 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001633 if (rdev->desc_nr < 0 ||
1634 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1635 role = 0xffff;
1636 rdev->desc_nr = -1;
1637 } else
1638 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 switch(role) {
1640 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 break;
1642 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001643 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 break;
1645 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001646 if ((le32_to_cpu(sb->feature_map) &
1647 MD_FEATURE_RECOVERY_OFFSET))
1648 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1649 else
1650 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 rdev->raid_disk = role;
1652 break;
1653 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001654 if (sb->devflags & WriteMostly1)
1655 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001656 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001657 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return 0;
1660}
1661
1662static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1663{
1664 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 mdk_rdev_t *rdev2;
1666 int max_dev, i;
1667 /* make rdev->sb match mddev and rdev data. */
1668
Namhyung Kim65a06f02011-07-27 11:00:36 +10001669 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 sb->feature_map = 0;
1672 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001673 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 memset(sb->pad3, 0, sizeof(sb->pad3));
1676
1677 sb->utime = cpu_to_le64((__u64)mddev->utime);
1678 sb->events = cpu_to_le64(mddev->events);
1679 if (mddev->in_sync)
1680 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1681 else
1682 sb->resync_offset = cpu_to_le64(0);
1683
NeilBrown1c05b4b2006-10-21 10:24:08 -07001684 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001685
NeilBrownf0ca3402006-02-02 14:28:04 -08001686 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001687 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001688 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001689 sb->level = cpu_to_le32(mddev->level);
1690 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001691
NeilBrownc3d97142009-12-14 12:49:52 +11001692 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1693 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001694 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001695 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001696
1697 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001698 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001699 sb->feature_map |=
1700 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1701 sb->recovery_offset =
1702 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001703 }
1704
NeilBrownf6705572006-03-27 01:18:11 -08001705 if (mddev->reshape_position != MaxSector) {
1706 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1707 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1708 sb->new_layout = cpu_to_le32(mddev->new_layout);
1709 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1710 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001711 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001712 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001713
NeilBrown2699b672011-07-28 11:31:47 +10001714 if (rdev->badblocks.count == 0)
1715 /* Nothing to do for bad blocks*/ ;
1716 else if (sb->bblog_offset == 0)
1717 /* Cannot record bad blocks on this device */
1718 md_error(mddev, rdev);
1719 else {
1720 struct badblocks *bb = &rdev->badblocks;
1721 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1722 u64 *p = bb->page;
1723 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1724 if (bb->changed) {
1725 unsigned seq;
1726
1727retry:
1728 seq = read_seqbegin(&bb->lock);
1729
1730 memset(bbp, 0xff, PAGE_SIZE);
1731
1732 for (i = 0 ; i < bb->count ; i++) {
1733 u64 internal_bb = *p++;
1734 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1735 | BB_LEN(internal_bb));
1736 *bbp++ = cpu_to_le64(store_bb);
1737 }
1738 if (read_seqretry(&bb->lock, seq))
1739 goto retry;
1740
1741 bb->sector = (rdev->sb_start +
1742 (int)le32_to_cpu(sb->bblog_offset));
1743 bb->size = le16_to_cpu(sb->bblog_size);
1744 bb->changed = 0;
1745 }
1746 }
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001749 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (rdev2->desc_nr+1 > max_dev)
1751 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001752
NeilBrown70471da2009-08-03 10:59:57 +10001753 if (max_dev > le32_to_cpu(sb->max_dev)) {
1754 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001755 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001756 rdev->sb_size = max_dev * 2 + 256;
1757 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1758 if (rdev->sb_size & bmask)
1759 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001760 } else
1761 max_dev = le32_to_cpu(sb->max_dev);
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 for (i=0; i<max_dev;i++)
1764 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1765
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001766 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001768 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001770 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001772 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001773 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 else
1775 sb->dev_roles[i] = cpu_to_le16(0xffff);
1776 }
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 sb->sb_csum = calc_sb_1_csum(sb);
1779}
1780
Chris Webb0cd17fe2008-06-28 08:31:46 +10001781static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001782super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001783{
1784 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001785 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001786 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001787 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001788 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001789 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001790 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001791 max_sectors -= rdev->data_offset;
1792 if (!num_sectors || num_sectors > max_sectors)
1793 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001794 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001795 /* minor version 0 with bitmap we can't move */
1796 return 0;
1797 } else {
1798 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001799 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001800 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001801 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001802 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001803 if (!num_sectors || num_sectors > max_sectors)
1804 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001805 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001806 }
Namhyung Kim65a06f02011-07-27 11:00:36 +10001807 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001808 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001809 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001810 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001811 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001812 rdev->sb_page);
1813 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001814 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001815}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Adrian Bunk75c96f82005-05-05 16:16:09 -07001817static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 [0] = {
1819 .name = "0.90.0",
1820 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001821 .load_super = super_90_load,
1822 .validate_super = super_90_validate,
1823 .sync_super = super_90_sync,
1824 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 },
1826 [1] = {
1827 .name = "md-1",
1828 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001829 .load_super = super_1_load,
1830 .validate_super = super_1_validate,
1831 .sync_super = super_1_sync,
1832 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 },
1834};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Jonathan Brassow076f9682011-06-07 17:51:30 -05001836static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev)
1837{
1838 if (mddev->sync_super) {
1839 mddev->sync_super(mddev, rdev);
1840 return;
1841 }
1842
1843 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1844
1845 super_types[mddev->major_version].sync_super(mddev, rdev);
1846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1849{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001850 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
NeilBrown4b809912008-07-21 17:05:25 +10001852 rcu_read_lock();
1853 rdev_for_each_rcu(rdev, mddev1)
1854 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001855 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001856 rdev2->bdev->bd_contains) {
1857 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001858 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001859 }
1860 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return 0;
1862}
1863
1864static LIST_HEAD(pending_raid_disks);
1865
Andre Nollac5e7112009-08-03 10:59:47 +10001866/*
1867 * Try to register data integrity profile for an mddev
1868 *
1869 * This is called when an array is started and after a disk has been kicked
1870 * from the array. It only succeeds if all working and active component devices
1871 * are integrity capable with matching profiles.
1872 */
1873int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001874{
Andre Nollac5e7112009-08-03 10:59:47 +10001875 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001876
Andre Nollac5e7112009-08-03 10:59:47 +10001877 if (list_empty(&mddev->disks))
1878 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001879 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1880 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001881 list_for_each_entry(rdev, &mddev->disks, same_set) {
1882 /* skip spares and non-functional disks */
1883 if (test_bit(Faulty, &rdev->flags))
1884 continue;
1885 if (rdev->raid_disk < 0)
1886 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001887 if (!reference) {
1888 /* Use the first rdev as the reference */
1889 reference = rdev;
1890 continue;
1891 }
1892 /* does this rdev's profile match the reference profile? */
1893 if (blk_integrity_compare(reference->bdev->bd_disk,
1894 rdev->bdev->bd_disk) < 0)
1895 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001896 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001897 if (!reference || !bdev_get_integrity(reference->bdev))
1898 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001899 /*
1900 * All component devices are integrity capable and have matching
1901 * profiles, register the common profile for the md device.
1902 */
1903 if (blk_integrity_register(mddev->gendisk,
1904 bdev_get_integrity(reference->bdev)) != 0) {
1905 printk(KERN_ERR "md: failed to register integrity for %s\n",
1906 mdname(mddev));
1907 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001908 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001909 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1910 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1911 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1912 mdname(mddev));
1913 return -EINVAL;
1914 }
Andre Nollac5e7112009-08-03 10:59:47 +10001915 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001916}
Andre Nollac5e7112009-08-03 10:59:47 +10001917EXPORT_SYMBOL(md_integrity_register);
1918
1919/* Disable data integrity if non-capable/non-matching disk is being added */
1920void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1921{
1922 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1923 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1924
1925 if (!bi_mddev) /* nothing to do */
1926 return;
1927 if (rdev->raid_disk < 0) /* skip spares */
1928 return;
1929 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1930 rdev->bdev->bd_disk) >= 0)
1931 return;
1932 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1933 blk_integrity_unregister(mddev->gendisk);
1934}
1935EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1938{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001939 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001940 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001941 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001942 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 if (rdev->mddev) {
1945 MD_BUG();
1946 return -EINVAL;
1947 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001948
1949 /* prevent duplicates */
1950 if (find_rdev(mddev, rdev->bdev->bd_dev))
1951 return -EEXIST;
1952
Andre Nolldd8ac332009-03-31 14:33:13 +11001953 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1954 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1955 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001956 if (mddev->pers) {
1957 /* Cannot change size, so fail
1958 * If mddev->level <= 0, then we don't care
1959 * about aligning sizes (e.g. linear)
1960 */
1961 if (mddev->level > 0)
1962 return -ENOSPC;
1963 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001964 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* Verify rdev->desc_nr is unique.
1968 * If it is -1, assign a free number, else
1969 * check number is not in use
1970 */
1971 if (rdev->desc_nr < 0) {
1972 int choice = 0;
1973 if (mddev->pers) choice = mddev->raid_disks;
1974 while (find_rdev_nr(mddev, choice))
1975 choice++;
1976 rdev->desc_nr = choice;
1977 } else {
1978 if (find_rdev_nr(mddev, rdev->desc_nr))
1979 return -EBUSY;
1980 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001981 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1982 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1983 mdname(mddev), mddev->max_disks);
1984 return -EBUSY;
1985 }
NeilBrown19133a42005-11-08 21:39:35 -08001986 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001987 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001988 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001991 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001992
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001993 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001994 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001995
Tejun Heo0762b8b2008-08-25 19:56:12 +09001996 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001997 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1998 /* failure here is OK */;
1999 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002000
NeilBrown4b809912008-07-21 17:05:25 +10002001 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002002 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002003
2004 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002005 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002008
2009 fail:
2010 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2011 b, mdname(mddev));
2012 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
NeilBrown177a99b2008-02-06 01:39:56 -08002015static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002016{
2017 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
2018 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002019 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002020}
2021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022static void unbind_rdev_from_array(mdk_rdev_t * rdev)
2023{
2024 char b[BDEVNAME_SIZE];
2025 if (!rdev->mddev) {
2026 MD_BUG();
2027 return;
2028 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002029 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002030 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2032 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002033 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002034 sysfs_put(rdev->sysfs_state);
2035 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002036 kfree(rdev->badblocks.page);
2037 rdev->badblocks.count = 0;
2038 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002039 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002040 * writing to 'remove' to "dev/state". We also need
2041 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002042 */
NeilBrown4b809912008-07-21 17:05:25 +10002043 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002044 INIT_WORK(&rdev->del_work, md_delayed_delete);
2045 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002046 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049/*
2050 * prevent the device from being mounted, repartitioned or
2051 * otherwise reused by a RAID array (or any other kernel
2052 * subsystem), by bd_claiming the device.
2053 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002054static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int err = 0;
2057 struct block_device *bdev;
2058 char b[BDEVNAME_SIZE];
2059
Tejun Heod4d77622010-11-13 11:55:18 +01002060 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2061 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (IS_ERR(bdev)) {
2063 printk(KERN_ERR "md: could not open %s.\n",
2064 __bdevname(dev, b));
2065 return PTR_ERR(bdev);
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 rdev->bdev = bdev;
2068 return err;
2069}
2070
2071static void unlock_rdev(mdk_rdev_t *rdev)
2072{
2073 struct block_device *bdev = rdev->bdev;
2074 rdev->bdev = NULL;
2075 if (!bdev)
2076 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002077 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080void md_autodetect_dev(dev_t dev);
2081
2082static void export_rdev(mdk_rdev_t * rdev)
2083{
2084 char b[BDEVNAME_SIZE];
2085 printk(KERN_INFO "md: export_rdev(%s)\n",
2086 bdevname(rdev->bdev,b));
2087 if (rdev->mddev)
2088 MD_BUG();
2089 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002091 if (test_bit(AutoDetected, &rdev->flags))
2092 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093#endif
2094 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002095 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098static void kick_rdev_from_array(mdk_rdev_t * rdev)
2099{
2100 unbind_rdev_from_array(rdev);
2101 export_rdev(rdev);
2102}
2103
2104static void export_array(mddev_t *mddev)
2105{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002106 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
NeilBrownd089c6a2008-02-06 01:39:59 -08002108 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (!rdev->mddev) {
2110 MD_BUG();
2111 continue;
2112 }
2113 kick_rdev_from_array(rdev);
2114 }
2115 if (!list_empty(&mddev->disks))
2116 MD_BUG();
2117 mddev->raid_disks = 0;
2118 mddev->major_version = 0;
2119}
2120
2121static void print_desc(mdp_disk_t *desc)
2122{
2123 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2124 desc->major,desc->minor,desc->raid_disk,desc->state);
2125}
2126
Cheng Renquancd2ac932009-01-09 08:31:08 +11002127static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 int i;
2130
2131 printk(KERN_INFO
2132 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2133 sb->major_version, sb->minor_version, sb->patch_version,
2134 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2135 sb->ctime);
2136 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2137 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2138 sb->md_minor, sb->layout, sb->chunk_size);
2139 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2140 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2141 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2142 sb->failed_disks, sb->spare_disks,
2143 sb->sb_csum, (unsigned long)sb->events_lo);
2144
2145 printk(KERN_INFO);
2146 for (i = 0; i < MD_SB_DISKS; i++) {
2147 mdp_disk_t *desc;
2148
2149 desc = sb->disks + i;
2150 if (desc->number || desc->major || desc->minor ||
2151 desc->raid_disk || (desc->state && (desc->state != 4))) {
2152 printk(" D %2d: ", i);
2153 print_desc(desc);
2154 }
2155 }
2156 printk(KERN_INFO "md: THIS: ");
2157 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Cheng Renquancd2ac932009-01-09 08:31:08 +11002160static void print_sb_1(struct mdp_superblock_1 *sb)
2161{
2162 __u8 *uuid;
2163
2164 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002165 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002166 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002167 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002168 le32_to_cpu(sb->major_version),
2169 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002170 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002171 sb->set_name,
2172 (unsigned long long)le64_to_cpu(sb->ctime)
2173 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2174
2175 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002176 printk(KERN_INFO
2177 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002178 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002179 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002180 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2181 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002182 le32_to_cpu(sb->level),
2183 (unsigned long long)le64_to_cpu(sb->size),
2184 le32_to_cpu(sb->raid_disks),
2185 le32_to_cpu(sb->layout),
2186 le32_to_cpu(sb->chunksize),
2187 (unsigned long long)le64_to_cpu(sb->data_offset),
2188 (unsigned long long)le64_to_cpu(sb->data_size),
2189 (unsigned long long)le64_to_cpu(sb->super_offset),
2190 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2191 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002192 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002193 sb->devflags,
2194 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2195 (unsigned long long)le64_to_cpu(sb->events),
2196 (unsigned long long)le64_to_cpu(sb->resync_offset),
2197 le32_to_cpu(sb->sb_csum),
2198 le32_to_cpu(sb->max_dev)
2199 );
2200}
2201
2202static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002205 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2206 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002207 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2208 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002210 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2211 switch (major_version) {
2212 case 0:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002213 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002214 break;
2215 case 1:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002216 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002217 break;
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 } else
2220 printk(KERN_INFO "md: no rdev superblock!\n");
2221}
2222
Adrian Bunk5e563412006-06-26 00:27:42 -07002223static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002225 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 mdk_rdev_t *rdev;
2227 mddev_t *mddev;
2228 char b[BDEVNAME_SIZE];
2229
2230 printk("\n");
2231 printk("md: **********************************\n");
2232 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2233 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002234 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
NeilBrown32a76272005-06-21 17:17:14 -07002236 if (mddev->bitmap)
2237 bitmap_print_sb(mddev->bitmap);
2238 else
2239 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002240 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 printk("<%s>", bdevname(rdev->bdev,b));
2242 printk("\n");
2243
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002244 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002245 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 printk("md: **********************************\n");
2248 printk("\n");
2249}
2250
2251
NeilBrown42543762006-06-26 00:27:57 -07002252static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
NeilBrown42543762006-06-26 00:27:57 -07002254 /* Update each superblock (in-memory image), but
2255 * if we are allowed to, skip spares which already
2256 * have the right event counter, or have one earlier
2257 * (which would mean they aren't being marked as dirty
2258 * with the rest of the array)
2259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002261 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002262 if (rdev->sb_events == mddev->events ||
2263 (nospares &&
2264 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002265 rdev->sb_events+1 == mddev->events)) {
2266 /* Don't update this superblock */
2267 rdev->sb_loaded = 2;
2268 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002269 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002270 rdev->sb_loaded = 1;
2271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273}
2274
NeilBrown850b2b42006-10-03 01:15:46 -07002275static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002278 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002279 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002280 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002283 /* First make sure individual recovery_offsets are correct */
2284 list_for_each_entry(rdev, &mddev->disks, same_set) {
2285 if (rdev->raid_disk >= 0 &&
2286 mddev->delta_disks >= 0 &&
2287 !test_bit(In_sync, &rdev->flags) &&
2288 mddev->curr_resync_completed > rdev->recovery_offset)
2289 rdev->recovery_offset = mddev->curr_resync_completed;
2290
2291 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002292 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002293 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002294 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002295 if (!mddev->external)
2296 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002297 wake_up(&mddev->sb_wait);
2298 return;
2299 }
2300
NeilBrowna9701a32005-11-08 21:39:34 -08002301 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002302
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002303 mddev->utime = get_seconds();
2304
NeilBrown850b2b42006-10-03 01:15:46 -07002305 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2306 force_change = 1;
2307 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2308 /* just a clean<-> dirty transition, possibly leave spares alone,
2309 * though if events isn't the right even/odd, we will have to do
2310 * spares after all
2311 */
2312 nospares = 1;
2313 if (force_change)
2314 nospares = 0;
2315 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002316 /* If the array is degraded, then skipping spares is both
2317 * dangerous and fairly pointless.
2318 * Dangerous because a device that was removed from the array
2319 * might have a event_count that still looks up-to-date,
2320 * so it can be re-added without a resync.
2321 * Pointless because if there are any spares to skip,
2322 * then a recovery will happen and soon that array won't
2323 * be degraded any more and the spare can go back to sleep then.
2324 */
NeilBrown850b2b42006-10-03 01:15:46 -07002325 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002326
NeilBrown06d91a52005-06-21 17:17:12 -07002327 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002328
2329 /* If this is just a dirty<->clean transition, and the array is clean
2330 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002331 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002332 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002333 && mddev->can_decrease_events
2334 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002335 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002336 mddev->can_decrease_events = 0;
2337 } else {
NeilBrown42543762006-06-26 00:27:57 -07002338 /* otherwise we have to go forward and ... */
2339 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002340 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 if (!mddev->events) {
2344 /*
2345 * oops, this 64-bit counter should never wrap.
2346 * Either we are in around ~1 trillion A.C., assuming
2347 * 1 reboot per second, or we have a bug:
2348 */
2349 MD_BUG();
2350 mddev->events --;
2351 }
NeilBrown2699b672011-07-28 11:31:47 +10002352
2353 list_for_each_entry(rdev, &mddev->disks, same_set)
2354 if (rdev->badblocks.changed)
2355 any_badblocks_changed++;
2356
NeilBrowne6910632008-02-06 01:39:51 -08002357 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002358 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 dprintk(KERN_INFO
2361 "md: updating %s RAID superblock on device (in sync %d)\n",
2362 mdname(mddev),mddev->in_sync);
2363
NeilBrown4ad13662007-07-17 04:06:13 -07002364 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002365 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 char b[BDEVNAME_SIZE];
2367 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002368 if (rdev->sb_loaded != 1)
2369 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002370 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 dprintk("(skipping faulty ");
2372
2373 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002374 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002375 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002376 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002377 rdev->sb_page);
2378 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2379 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002380 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002381 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002382 if (rdev->badblocks.size) {
2383 md_super_write(mddev, rdev,
2384 rdev->badblocks.sector,
2385 rdev->badblocks.size << 9,
2386 rdev->bb_page);
2387 rdev->badblocks.size = 0;
2388 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 } else
2391 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002392 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 /* only need to write one superblock... */
2394 break;
2395 }
NeilBrowna9701a32005-11-08 21:39:34 -08002396 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002397 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002398
NeilBrowna9701a32005-11-08 21:39:34 -08002399 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002400 if (mddev->in_sync != sync_req ||
2401 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002402 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002403 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002404 goto repeat;
2405 }
NeilBrown850b2b42006-10-03 01:15:46 -07002406 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002407 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002408 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002409 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2410 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002411
NeilBrown2699b672011-07-28 11:31:47 +10002412 if (any_badblocks_changed)
2413 list_for_each_entry(rdev, &mddev->disks, same_set)
2414 md_ack_all_badblocks(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Andre Noll7f6ce762008-03-23 18:34:54 +01002417/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002418 * We want to accept with case. For this we use cmd_match.
2419 */
2420static int cmd_match(const char *cmd, const char *str)
2421{
2422 /* See if cmd, written into a sysfs file, matches
2423 * str. They must either be the same, or cmd can
2424 * have a trailing newline
2425 */
2426 while (*cmd && *str && *cmd == *str) {
2427 cmd++;
2428 str++;
2429 }
2430 if (*cmd == '\n')
2431 cmd++;
2432 if (*str || *cmd)
2433 return 0;
2434 return 1;
2435}
2436
NeilBrown86e6ffd2005-11-08 21:39:24 -08002437struct rdev_sysfs_entry {
2438 struct attribute attr;
2439 ssize_t (*show)(mdk_rdev_t *, char *);
2440 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2441};
2442
2443static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002444state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002445{
2446 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002447 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002448
NeilBrownb2d444d2005-11-08 21:39:31 -08002449 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002450 len+= sprintf(page+len, "%sfaulty",sep);
2451 sep = ",";
2452 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002453 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002454 len += sprintf(page+len, "%sin_sync",sep);
2455 sep = ",";
2456 }
NeilBrownf6556752006-06-26 00:28:01 -07002457 if (test_bit(WriteMostly, &rdev->flags)) {
2458 len += sprintf(page+len, "%swrite_mostly",sep);
2459 sep = ",";
2460 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002461 if (test_bit(Blocked, &rdev->flags)) {
2462 len += sprintf(page+len, "%sblocked", sep);
2463 sep = ",";
2464 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002465 if (!test_bit(Faulty, &rdev->flags) &&
2466 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002467 len += sprintf(page+len, "%sspare", sep);
2468 sep = ",";
2469 }
2470 return len+sprintf(page+len, "\n");
2471}
2472
NeilBrown45dc2de2006-06-26 00:27:58 -07002473static ssize_t
2474state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2475{
2476 /* can write
2477 * faulty - simulates and error
2478 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002479 * writemostly - sets write_mostly
2480 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002481 * blocked - sets the Blocked flag
2482 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002483 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002484 */
2485 int err = -EINVAL;
2486 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2487 md_error(rdev->mddev, rdev);
2488 err = 0;
2489 } else if (cmd_match(buf, "remove")) {
2490 if (rdev->raid_disk >= 0)
2491 err = -EBUSY;
2492 else {
2493 mddev_t *mddev = rdev->mddev;
2494 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002495 if (mddev->pers)
2496 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002497 md_new_event(mddev);
2498 err = 0;
2499 }
NeilBrownf6556752006-06-26 00:28:01 -07002500 } else if (cmd_match(buf, "writemostly")) {
2501 set_bit(WriteMostly, &rdev->flags);
2502 err = 0;
2503 } else if (cmd_match(buf, "-writemostly")) {
2504 clear_bit(WriteMostly, &rdev->flags);
2505 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002506 } else if (cmd_match(buf, "blocked")) {
2507 set_bit(Blocked, &rdev->flags);
2508 err = 0;
2509 } else if (cmd_match(buf, "-blocked")) {
2510 clear_bit(Blocked, &rdev->flags);
2511 wake_up(&rdev->blocked_wait);
2512 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2513 md_wakeup_thread(rdev->mddev->thread);
2514
2515 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002516 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2517 set_bit(In_sync, &rdev->flags);
2518 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002519 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002520 if (!err)
2521 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002522 return err ? err : len;
2523}
NeilBrown80ca3a42006-07-10 04:44:18 -07002524static struct rdev_sysfs_entry rdev_state =
2525__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002526
2527static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002528errors_show(mdk_rdev_t *rdev, char *page)
2529{
2530 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2531}
2532
2533static ssize_t
2534errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2535{
2536 char *e;
2537 unsigned long n = simple_strtoul(buf, &e, 10);
2538 if (*buf && (*e == 0 || *e == '\n')) {
2539 atomic_set(&rdev->corrected_errors, n);
2540 return len;
2541 }
2542 return -EINVAL;
2543}
2544static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002545__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002546
NeilBrown014236d2006-01-06 00:20:55 -08002547static ssize_t
2548slot_show(mdk_rdev_t *rdev, char *page)
2549{
2550 if (rdev->raid_disk < 0)
2551 return sprintf(page, "none\n");
2552 else
2553 return sprintf(page, "%d\n", rdev->raid_disk);
2554}
2555
2556static ssize_t
2557slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2558{
2559 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002560 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002561 int slot = simple_strtoul(buf, &e, 10);
2562 if (strncmp(buf, "none", 4)==0)
2563 slot = -1;
2564 else if (e==buf || (*e && *e!= '\n'))
2565 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002566 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002567 /* Setting 'slot' on an active array requires also
2568 * updating the 'rd%d' link, and communicating
2569 * with the personality with ->hot_*_disk.
2570 * For now we only support removing
2571 * failed/spare devices. This normally happens automatically,
2572 * but not when the metadata is externally managed.
2573 */
NeilBrownc303da62008-02-06 01:39:51 -08002574 if (rdev->raid_disk == -1)
2575 return -EEXIST;
2576 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002577 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002578 return -EINVAL;
2579 err = rdev->mddev->pers->
2580 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2581 if (err)
2582 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002583 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002584 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002585 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2586 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002587 } else if (rdev->mddev->pers) {
2588 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002589 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002590 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002591 */
2592
2593 if (rdev->raid_disk != -1)
2594 return -EBUSY;
2595
NeilBrownc6751b22011-02-02 11:57:13 +11002596 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2597 return -EBUSY;
2598
Neil Brown6c2fce22008-06-28 08:31:31 +10002599 if (rdev->mddev->pers->hot_add_disk == NULL)
2600 return -EINVAL;
2601
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002602 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002603 if (rdev2->raid_disk == slot)
2604 return -EEXIST;
2605
NeilBrownba1b41b2011-01-14 09:14:34 +11002606 if (slot >= rdev->mddev->raid_disks &&
2607 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2608 return -ENOSPC;
2609
Neil Brown6c2fce22008-06-28 08:31:31 +10002610 rdev->raid_disk = slot;
2611 if (test_bit(In_sync, &rdev->flags))
2612 rdev->saved_raid_disk = slot;
2613 else
2614 rdev->saved_raid_disk = -1;
2615 err = rdev->mddev->pers->
2616 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002617 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002618 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002619 return err;
Neil Brown52664732008-06-28 08:31:44 +10002620 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002621 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002622 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002623 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002624 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002625 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002626 if (slot >= rdev->mddev->raid_disks &&
2627 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002628 return -ENOSPC;
2629 rdev->raid_disk = slot;
2630 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002631 clear_bit(Faulty, &rdev->flags);
2632 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002633 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002634 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002635 }
NeilBrown014236d2006-01-06 00:20:55 -08002636 return len;
2637}
2638
2639
2640static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002641__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002642
NeilBrown93c8cad2006-01-06 00:20:56 -08002643static ssize_t
2644offset_show(mdk_rdev_t *rdev, char *page)
2645{
Andrew Morton6961ece2006-01-06 00:20:59 -08002646 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002647}
2648
2649static ssize_t
2650offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2651{
2652 char *e;
2653 unsigned long long offset = simple_strtoull(buf, &e, 10);
2654 if (e==buf || (*e && *e != '\n'))
2655 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002656 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002657 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002658 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002659 /* Must set offset before size, so overlap checks
2660 * can be sane */
2661 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002662 rdev->data_offset = offset;
2663 return len;
2664}
2665
2666static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002667__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002668
NeilBrown83303b62006-01-06 00:21:06 -08002669static ssize_t
2670rdev_size_show(mdk_rdev_t *rdev, char *page)
2671{
Andre Nolldd8ac332009-03-31 14:33:13 +11002672 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002673}
2674
NeilBrownc5d79ad2008-02-06 01:39:54 -08002675static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2676{
2677 /* check if two start/length pairs overlap */
2678 if (s1+l1 <= s2)
2679 return 0;
2680 if (s2+l2 <= s1)
2681 return 0;
2682 return 1;
2683}
2684
Dan Williamsb522adc2009-03-31 15:00:31 +11002685static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2686{
2687 unsigned long long blocks;
2688 sector_t new;
2689
2690 if (strict_strtoull(buf, 10, &blocks) < 0)
2691 return -EINVAL;
2692
2693 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2694 return -EINVAL; /* sector conversion overflow */
2695
2696 new = blocks * 2;
2697 if (new != blocks * 2)
2698 return -EINVAL; /* unsigned long long to sector_t overflow */
2699
2700 *sectors = new;
2701 return 0;
2702}
2703
NeilBrown83303b62006-01-06 00:21:06 -08002704static ssize_t
2705rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2706{
NeilBrown27c529b2008-03-04 14:29:33 -08002707 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002708 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002709 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002710
Dan Williamsb522adc2009-03-31 15:00:31 +11002711 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002712 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002713 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002714 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002715 sectors = super_types[my_mddev->major_version].
2716 rdev_size_change(rdev, sectors);
2717 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002718 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002719 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002720 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002721 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002722 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002723 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002724 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002725
Andre Nolldd8ac332009-03-31 14:33:13 +11002726 rdev->sectors = sectors;
2727 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002728 /* need to check that all other rdevs with the same ->bdev
2729 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002730 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002731 * we have to change it back, we will have the lock again.
2732 */
2733 mddev_t *mddev;
2734 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002735 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002736
NeilBrown27c529b2008-03-04 14:29:33 -08002737 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002738 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002739 mdk_rdev_t *rdev2;
2740
2741 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002742 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002743 if (rdev->bdev == rdev2->bdev &&
2744 rdev != rdev2 &&
2745 overlaps(rdev->data_offset, rdev->sectors,
2746 rdev2->data_offset,
2747 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002748 overlap = 1;
2749 break;
2750 }
2751 mddev_unlock(mddev);
2752 if (overlap) {
2753 mddev_put(mddev);
2754 break;
2755 }
2756 }
NeilBrown27c529b2008-03-04 14:29:33 -08002757 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002758 if (overlap) {
2759 /* Someone else could have slipped in a size
2760 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002761 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002762 * safe, and trust userspace not to race with
2763 * itself
2764 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002765 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002766 return -EBUSY;
2767 }
2768 }
NeilBrown83303b62006-01-06 00:21:06 -08002769 return len;
2770}
2771
2772static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002773__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002774
Dan Williams06e3c812009-12-12 21:17:12 -07002775
2776static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2777{
2778 unsigned long long recovery_start = rdev->recovery_offset;
2779
2780 if (test_bit(In_sync, &rdev->flags) ||
2781 recovery_start == MaxSector)
2782 return sprintf(page, "none\n");
2783
2784 return sprintf(page, "%llu\n", recovery_start);
2785}
2786
2787static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2788{
2789 unsigned long long recovery_start;
2790
2791 if (cmd_match(buf, "none"))
2792 recovery_start = MaxSector;
2793 else if (strict_strtoull(buf, 10, &recovery_start))
2794 return -EINVAL;
2795
2796 if (rdev->mddev->pers &&
2797 rdev->raid_disk >= 0)
2798 return -EBUSY;
2799
2800 rdev->recovery_offset = recovery_start;
2801 if (recovery_start == MaxSector)
2802 set_bit(In_sync, &rdev->flags);
2803 else
2804 clear_bit(In_sync, &rdev->flags);
2805 return len;
2806}
2807
2808static struct rdev_sysfs_entry rdev_recovery_start =
2809__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2810
NeilBrown16c791a2011-07-28 11:31:47 +10002811
2812static ssize_t
2813badblocks_show(struct badblocks *bb, char *page, int unack);
2814static ssize_t
2815badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2816
2817static ssize_t bb_show(mdk_rdev_t *rdev, char *page)
2818{
2819 return badblocks_show(&rdev->badblocks, page, 0);
2820}
2821static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2822{
2823 return badblocks_store(&rdev->badblocks, page, len, 0);
2824}
2825static struct rdev_sysfs_entry rdev_bad_blocks =
2826__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2827
2828
2829static ssize_t ubb_show(mdk_rdev_t *rdev, char *page)
2830{
2831 return badblocks_show(&rdev->badblocks, page, 1);
2832}
2833static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2834{
2835 return badblocks_store(&rdev->badblocks, page, len, 1);
2836}
2837static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2838__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2839
NeilBrown86e6ffd2005-11-08 21:39:24 -08002840static struct attribute *rdev_default_attrs[] = {
2841 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002842 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002843 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002844 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002845 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002846 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002847 &rdev_bad_blocks.attr,
2848 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002849 NULL,
2850};
2851static ssize_t
2852rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2853{
2854 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2855 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002856 mddev_t *mddev = rdev->mddev;
2857 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002858
2859 if (!entry->show)
2860 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002861
2862 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2863 if (!rv) {
2864 if (rdev->mddev == NULL)
2865 rv = -EBUSY;
2866 else
2867 rv = entry->show(rdev, page);
2868 mddev_unlock(mddev);
2869 }
2870 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002871}
2872
2873static ssize_t
2874rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2875 const char *page, size_t length)
2876{
2877 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2878 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002879 ssize_t rv;
2880 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002881
2882 if (!entry->store)
2883 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002884 if (!capable(CAP_SYS_ADMIN))
2885 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002886 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002887 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002888 if (rdev->mddev == NULL)
2889 rv = -EBUSY;
2890 else
2891 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002892 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002893 }
2894 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002895}
2896
2897static void rdev_free(struct kobject *ko)
2898{
2899 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2900 kfree(rdev);
2901}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002902static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002903 .show = rdev_attr_show,
2904 .store = rdev_attr_store,
2905};
2906static struct kobj_type rdev_ktype = {
2907 .release = rdev_free,
2908 .sysfs_ops = &rdev_sysfs_ops,
2909 .default_attrs = rdev_default_attrs,
2910};
2911
NeilBrown2230dfe2011-07-28 11:31:46 +10002912int md_rdev_init(mdk_rdev_t *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10002913{
2914 rdev->desc_nr = -1;
2915 rdev->saved_raid_disk = -1;
2916 rdev->raid_disk = -1;
2917 rdev->flags = 0;
2918 rdev->data_offset = 0;
2919 rdev->sb_events = 0;
2920 rdev->last_read_error.tv_sec = 0;
2921 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002922 rdev->sb_loaded = 0;
2923 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10002924 atomic_set(&rdev->nr_pending, 0);
2925 atomic_set(&rdev->read_errors, 0);
2926 atomic_set(&rdev->corrected_errors, 0);
2927
2928 INIT_LIST_HEAD(&rdev->same_set);
2929 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10002930
2931 /* Add space to store bad block list.
2932 * This reserves the space even on arrays where it cannot
2933 * be used - I wonder if that matters
2934 */
2935 rdev->badblocks.count = 0;
2936 rdev->badblocks.shift = 0;
2937 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
2938 seqlock_init(&rdev->badblocks.lock);
2939 if (rdev->badblocks.page == NULL)
2940 return -ENOMEM;
2941
2942 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10002943}
2944EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945/*
2946 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2947 *
2948 * mark the device faulty if:
2949 *
2950 * - the device is nonexistent (zero size)
2951 * - the device has no valid superblock
2952 *
2953 * a faulty rdev _never_ has rdev->sb set.
2954 */
2955static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2956{
2957 char b[BDEVNAME_SIZE];
2958 int err;
2959 mdk_rdev_t *rdev;
2960 sector_t size;
2961
NeilBrown9ffae0c2006-01-06 00:20:32 -08002962 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (!rdev) {
2964 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2965 return ERR_PTR(-ENOMEM);
2966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
NeilBrown2230dfe2011-07-28 11:31:46 +10002968 err = md_rdev_init(rdev);
2969 if (err)
2970 goto abort_free;
2971 err = alloc_disk_sb(rdev);
2972 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 goto abort_free;
2974
NeilBrownc5d79ad2008-02-06 01:39:54 -08002975 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (err)
2977 goto abort_free;
2978
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002979 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002980
Mike Snitzer77304d22010-11-08 14:39:12 +01002981 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 if (!size) {
2983 printk(KERN_WARNING
2984 "md: %s has zero or unknown size, marking faulty!\n",
2985 bdevname(rdev->bdev,b));
2986 err = -EINVAL;
2987 goto abort_free;
2988 }
2989
2990 if (super_format >= 0) {
2991 err = super_types[super_format].
2992 load_super(rdev, NULL, super_minor);
2993 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002994 printk(KERN_WARNING
2995 "md: %s does not have a valid v%d.%d "
2996 "superblock, not importing!\n",
2997 bdevname(rdev->bdev,b),
2998 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 goto abort_free;
3000 }
3001 if (err < 0) {
3002 printk(KERN_WARNING
3003 "md: could not read %s's sb, not importing!\n",
3004 bdevname(rdev->bdev,b));
3005 goto abort_free;
3006 }
3007 }
NeilBrown9f2f383072011-07-28 11:31:47 +10003008 if (super_format == -1)
3009 /* hot-add for 0.90, or non-persistent: so no badblocks */
3010 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return rdev;
3013
3014abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003015 if (rdev->bdev)
3016 unlock_rdev(rdev);
3017 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003018 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 kfree(rdev);
3020 return ERR_PTR(err);
3021}
3022
3023/*
3024 * Check a full RAID array for plausibility
3025 */
3026
3027
NeilBrowna757e642005-04-16 15:26:42 -07003028static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
3030 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003031 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 char b[BDEVNAME_SIZE];
3033
3034 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003035 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 switch (super_types[mddev->major_version].
3037 load_super(rdev, freshest, mddev->minor_version)) {
3038 case 1:
3039 freshest = rdev;
3040 break;
3041 case 0:
3042 break;
3043 default:
3044 printk( KERN_ERR \
3045 "md: fatal superblock inconsistency in %s"
3046 " -- removing from array\n",
3047 bdevname(rdev->bdev,b));
3048 kick_rdev_from_array(rdev);
3049 }
3050
3051
3052 super_types[mddev->major_version].
3053 validate_super(mddev, freshest);
3054
3055 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003056 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003057 if (mddev->max_disks &&
3058 (rdev->desc_nr >= mddev->max_disks ||
3059 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003060 printk(KERN_WARNING
3061 "md: %s: %s: only %d devices permitted\n",
3062 mdname(mddev), bdevname(rdev->bdev, b),
3063 mddev->max_disks);
3064 kick_rdev_from_array(rdev);
3065 continue;
3066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 if (rdev != freshest)
3068 if (super_types[mddev->major_version].
3069 validate_super(mddev, rdev)) {
3070 printk(KERN_WARNING "md: kicking non-fresh %s"
3071 " from array!\n",
3072 bdevname(rdev->bdev,b));
3073 kick_rdev_from_array(rdev);
3074 continue;
3075 }
3076 if (mddev->level == LEVEL_MULTIPATH) {
3077 rdev->desc_nr = i++;
3078 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003079 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003080 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003081 rdev->raid_disk = -1;
3082 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
NeilBrown72e02072009-12-14 12:49:55 +11003087/* Read a fixed-point number.
3088 * Numbers in sysfs attributes should be in "standard" units where
3089 * possible, so time should be in seconds.
3090 * However we internally use a a much smaller unit such as
3091 * milliseconds or jiffies.
3092 * This function takes a decimal number with a possible fractional
3093 * component, and produces an integer which is the result of
3094 * multiplying that number by 10^'scale'.
3095 * all without any floating-point arithmetic.
3096 */
3097int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3098{
3099 unsigned long result = 0;
3100 long decimals = -1;
3101 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3102 if (*cp == '.')
3103 decimals = 0;
3104 else if (decimals < scale) {
3105 unsigned int value;
3106 value = *cp - '0';
3107 result = result * 10 + value;
3108 if (decimals >= 0)
3109 decimals++;
3110 }
3111 cp++;
3112 }
3113 if (*cp == '\n')
3114 cp++;
3115 if (*cp)
3116 return -EINVAL;
3117 if (decimals < 0)
3118 decimals = 0;
3119 while (decimals < scale) {
3120 result *= 10;
3121 decimals ++;
3122 }
3123 *res = result;
3124 return 0;
3125}
3126
3127
NeilBrown19052c02008-08-05 15:54:13 +10003128static void md_safemode_timeout(unsigned long data);
3129
NeilBrowneae17012005-11-08 21:39:23 -08003130static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07003131safe_delay_show(mddev_t *mddev, char *page)
3132{
3133 int msec = (mddev->safemode_delay*1000)/HZ;
3134 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3135}
3136static ssize_t
3137safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
3138{
NeilBrown16f17b32006-06-26 00:27:37 -07003139 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003140
NeilBrown72e02072009-12-14 12:49:55 +11003141 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003142 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003143 if (msec == 0)
3144 mddev->safemode_delay = 0;
3145 else {
NeilBrown19052c02008-08-05 15:54:13 +10003146 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003147 mddev->safemode_delay = (msec*HZ)/1000;
3148 if (mddev->safemode_delay == 0)
3149 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003150 if (mddev->safemode_delay < old_delay)
3151 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003152 }
3153 return len;
3154}
3155static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003156__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003157
3158static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003159level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003160{
NeilBrown2604b702006-01-06 00:20:36 -08003161 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003162 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003163 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003164 else if (mddev->clevel[0])
3165 return sprintf(page, "%s\n", mddev->clevel);
3166 else if (mddev->level != LEVEL_NONE)
3167 return sprintf(page, "%d\n", mddev->level);
3168 else
3169 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003170}
3171
NeilBrownd9d166c2006-01-06 00:20:51 -08003172static ssize_t
3173level_store(mddev_t *mddev, const char *buf, size_t len)
3174{
Dan Williamsf2859af2010-05-02 10:04:16 -07003175 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003176 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003177 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003178 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003179 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003180 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003181
3182 if (mddev->pers == NULL) {
3183 if (len == 0)
3184 return 0;
3185 if (len >= sizeof(mddev->clevel))
3186 return -ENOSPC;
3187 strncpy(mddev->clevel, buf, len);
3188 if (mddev->clevel[len-1] == '\n')
3189 len--;
3190 mddev->clevel[len] = 0;
3191 mddev->level = LEVEL_NONE;
3192 return rv;
3193 }
3194
3195 /* request to change the personality. Need to ensure:
3196 * - array is not engaged in resync/recovery/reshape
3197 * - old personality can be suspended
3198 * - new personality will access other array.
3199 */
3200
NeilBrownbb4f1e92010-08-08 21:18:03 +10003201 if (mddev->sync_thread ||
3202 mddev->reshape_position != MaxSector ||
3203 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003204 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003205
3206 if (!mddev->pers->quiesce) {
3207 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3208 mdname(mddev), mddev->pers->name);
3209 return -EINVAL;
3210 }
3211
3212 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003213 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003214 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003215 strncpy(clevel, buf, len);
3216 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003217 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003218 clevel[len] = 0;
3219 if (strict_strtol(clevel, 10, &level))
3220 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003221
Dan Williamsf2859af2010-05-02 10:04:16 -07003222 if (request_module("md-%s", clevel) != 0)
3223 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003224 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003225 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003226 if (!pers || !try_module_get(pers->owner)) {
3227 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003228 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003229 return -EINVAL;
3230 }
3231 spin_unlock(&pers_lock);
3232
3233 if (pers == mddev->pers) {
3234 /* Nothing to do! */
3235 module_put(pers->owner);
3236 return rv;
3237 }
3238 if (!pers->takeover) {
3239 module_put(pers->owner);
3240 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003241 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003242 return -EINVAL;
3243 }
3244
NeilBrowne93f68a2010-06-15 09:36:03 +01003245 list_for_each_entry(rdev, &mddev->disks, same_set)
3246 rdev->new_raid_disk = rdev->raid_disk;
3247
NeilBrown245f46c2009-03-31 14:39:39 +11003248 /* ->takeover must set new_* and/or delta_disks
3249 * if it succeeds, and may set them when it fails.
3250 */
3251 priv = pers->takeover(mddev);
3252 if (IS_ERR(priv)) {
3253 mddev->new_level = mddev->level;
3254 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003255 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003256 mddev->raid_disks -= mddev->delta_disks;
3257 mddev->delta_disks = 0;
3258 module_put(pers->owner);
3259 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003260 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003261 return PTR_ERR(priv);
3262 }
3263
3264 /* Looks like we have a winner */
3265 mddev_suspend(mddev);
3266 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003267
3268 if (mddev->pers->sync_request == NULL &&
3269 pers->sync_request != NULL) {
3270 /* need to add the md_redundancy_group */
3271 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3272 printk(KERN_WARNING
3273 "md: cannot register extra attributes for %s\n",
3274 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003275 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003276 }
3277 if (mddev->pers->sync_request != NULL &&
3278 pers->sync_request == NULL) {
3279 /* need to remove the md_redundancy_group */
3280 if (mddev->to_remove == NULL)
3281 mddev->to_remove = &md_redundancy_group;
3282 }
3283
Trela Maciej54071b32010-03-08 16:02:42 +11003284 if (mddev->pers->sync_request == NULL &&
3285 mddev->external) {
3286 /* We are converting from a no-redundancy array
3287 * to a redundancy array and metadata is managed
3288 * externally so we need to be sure that writes
3289 * won't block due to a need to transition
3290 * clean->dirty
3291 * until external management is started.
3292 */
3293 mddev->in_sync = 0;
3294 mddev->safemode_delay = 0;
3295 mddev->safemode = 0;
3296 }
3297
NeilBrowne93f68a2010-06-15 09:36:03 +01003298 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003299 if (rdev->raid_disk < 0)
3300 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003301 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003302 rdev->new_raid_disk = -1;
3303 if (rdev->new_raid_disk == rdev->raid_disk)
3304 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003305 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003306 }
3307 list_for_each_entry(rdev, &mddev->disks, same_set) {
3308 if (rdev->raid_disk < 0)
3309 continue;
3310 if (rdev->new_raid_disk == rdev->raid_disk)
3311 continue;
3312 rdev->raid_disk = rdev->new_raid_disk;
3313 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003314 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003315 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003316 if (sysfs_link_rdev(mddev, rdev))
3317 printk(KERN_WARNING "md: cannot register rd%d"
3318 " for %s after level change\n",
3319 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003320 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003321 }
3322
3323 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003324 mddev->pers = pers;
3325 mddev->private = priv;
3326 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3327 mddev->level = mddev->new_level;
3328 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003329 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003330 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003331 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003332 if (mddev->pers->sync_request == NULL) {
3333 /* this is now an array without redundancy, so
3334 * it must always be in_sync
3335 */
3336 mddev->in_sync = 1;
3337 del_timer_sync(&mddev->safemode_timer);
3338 }
NeilBrown245f46c2009-03-31 14:39:39 +11003339 pers->run(mddev);
3340 mddev_resume(mddev);
3341 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3342 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3343 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003344 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003345 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003346 return rv;
3347}
3348
3349static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003350__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003351
NeilBrownd4dbd022006-06-26 00:27:59 -07003352
3353static ssize_t
3354layout_show(mddev_t *mddev, char *page)
3355{
3356 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003357 if (mddev->reshape_position != MaxSector &&
3358 mddev->layout != mddev->new_layout)
3359 return sprintf(page, "%d (%d)\n",
3360 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003361 return sprintf(page, "%d\n", mddev->layout);
3362}
3363
3364static ssize_t
3365layout_store(mddev_t *mddev, const char *buf, size_t len)
3366{
3367 char *e;
3368 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003369
3370 if (!*buf || (*e && *e != '\n'))
3371 return -EINVAL;
3372
NeilBrownb3546032009-03-31 14:56:41 +11003373 if (mddev->pers) {
3374 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003375 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003376 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003377 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003378 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003379 if (err) {
3380 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003381 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003382 }
NeilBrownb3546032009-03-31 14:56:41 +11003383 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003384 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003385 if (mddev->reshape_position == MaxSector)
3386 mddev->layout = n;
3387 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003388 return len;
3389}
3390static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003391__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003392
3393
NeilBrowneae17012005-11-08 21:39:23 -08003394static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003395raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003396{
NeilBrownbb636542005-11-08 21:39:45 -08003397 if (mddev->raid_disks == 0)
3398 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003399 if (mddev->reshape_position != MaxSector &&
3400 mddev->delta_disks != 0)
3401 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3402 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003403 return sprintf(page, "%d\n", mddev->raid_disks);
3404}
3405
NeilBrownda943b92006-01-06 00:20:54 -08003406static int update_raid_disks(mddev_t *mddev, int raid_disks);
3407
3408static ssize_t
3409raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3410{
NeilBrownda943b92006-01-06 00:20:54 -08003411 char *e;
3412 int rv = 0;
3413 unsigned long n = simple_strtoul(buf, &e, 10);
3414
3415 if (!*buf || (*e && *e != '\n'))
3416 return -EINVAL;
3417
3418 if (mddev->pers)
3419 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003420 else if (mddev->reshape_position != MaxSector) {
3421 int olddisks = mddev->raid_disks - mddev->delta_disks;
3422 mddev->delta_disks = n - olddisks;
3423 mddev->raid_disks = n;
3424 } else
NeilBrownda943b92006-01-06 00:20:54 -08003425 mddev->raid_disks = n;
3426 return rv ? rv : len;
3427}
3428static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003429__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003430
NeilBrown24dd4692005-11-08 21:39:26 -08003431static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003432chunk_size_show(mddev_t *mddev, char *page)
3433{
NeilBrown08a02ec2007-05-09 02:35:38 -07003434 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003435 mddev->chunk_sectors != mddev->new_chunk_sectors)
3436 return sprintf(page, "%d (%d)\n",
3437 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003438 mddev->chunk_sectors << 9);
3439 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003440}
3441
3442static ssize_t
3443chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3444{
NeilBrown3b343802006-01-06 00:20:47 -08003445 char *e;
3446 unsigned long n = simple_strtoul(buf, &e, 10);
3447
NeilBrown3b343802006-01-06 00:20:47 -08003448 if (!*buf || (*e && *e != '\n'))
3449 return -EINVAL;
3450
NeilBrownb3546032009-03-31 14:56:41 +11003451 if (mddev->pers) {
3452 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003453 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003454 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003455 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003456 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003457 if (err) {
3458 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003459 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003460 }
NeilBrownb3546032009-03-31 14:56:41 +11003461 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003462 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003463 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003464 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003465 }
NeilBrown3b343802006-01-06 00:20:47 -08003466 return len;
3467}
3468static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003469__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003470
NeilBrowna94213b2006-06-26 00:28:00 -07003471static ssize_t
3472resync_start_show(mddev_t *mddev, char *page)
3473{
NeilBrownd1a7c502009-03-31 15:24:32 +11003474 if (mddev->recovery_cp == MaxSector)
3475 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003476 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3477}
3478
3479static ssize_t
3480resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3481{
NeilBrowna94213b2006-06-26 00:28:00 -07003482 char *e;
3483 unsigned long long n = simple_strtoull(buf, &e, 10);
3484
NeilBrownb0986362011-05-11 15:52:21 +10003485 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003486 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003487 if (cmd_match(buf, "none"))
3488 n = MaxSector;
3489 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003490 return -EINVAL;
3491
3492 mddev->recovery_cp = n;
3493 return len;
3494}
3495static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003496__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003497
NeilBrown9e653b62006-06-26 00:27:58 -07003498/*
3499 * The array state can be:
3500 *
3501 * clear
3502 * No devices, no size, no level
3503 * Equivalent to STOP_ARRAY ioctl
3504 * inactive
3505 * May have some settings, but array is not active
3506 * all IO results in error
3507 * When written, doesn't tear down array, but just stops it
3508 * suspended (not supported yet)
3509 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003510 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003511 * readonly
3512 * no resync can happen. no superblocks get written.
3513 * write requests fail
3514 * read-auto
3515 * like readonly, but behaves like 'clean' on a write request.
3516 *
3517 * clean - no pending writes, but otherwise active.
3518 * When written to inactive array, starts without resync
3519 * If a write request arrives then
3520 * if metadata is known, mark 'dirty' and switch to 'active'.
3521 * if not known, block and switch to write-pending
3522 * If written to an active array that has pending writes, then fails.
3523 * active
3524 * fully active: IO and resync can be happening.
3525 * When written to inactive array, starts with resync
3526 *
3527 * write-pending
3528 * clean, but writes are blocked waiting for 'active' to be written.
3529 *
3530 * active-idle
3531 * like active, but no writes have been seen for a while (100msec).
3532 *
3533 */
3534enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3535 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003536static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003537 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3538 "write-pending", "active-idle", NULL };
3539
3540static int match_word(const char *word, char **list)
3541{
3542 int n;
3543 for (n=0; list[n]; n++)
3544 if (cmd_match(word, list[n]))
3545 break;
3546 return n;
3547}
3548
3549static ssize_t
3550array_state_show(mddev_t *mddev, char *page)
3551{
3552 enum array_state st = inactive;
3553
3554 if (mddev->pers)
3555 switch(mddev->ro) {
3556 case 1:
3557 st = readonly;
3558 break;
3559 case 2:
3560 st = read_auto;
3561 break;
3562 case 0:
3563 if (mddev->in_sync)
3564 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003565 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003566 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003567 else if (mddev->safemode)
3568 st = active_idle;
3569 else
3570 st = active;
3571 }
3572 else {
3573 if (list_empty(&mddev->disks) &&
3574 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003575 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003576 st = clear;
3577 else
3578 st = inactive;
3579 }
3580 return sprintf(page, "%s\n", array_states[st]);
3581}
3582
Neil Browndf5b20c2008-07-11 22:02:22 +10003583static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003584static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003585static int do_md_run(mddev_t * mddev);
3586static int restart_array(mddev_t *mddev);
3587
3588static ssize_t
3589array_state_store(mddev_t *mddev, const char *buf, size_t len)
3590{
3591 int err = -EINVAL;
3592 enum array_state st = match_word(buf, array_states);
3593 switch(st) {
3594 case bad_word:
3595 break;
3596 case clear:
3597 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003598 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003599 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003600 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003601 break;
3602 case inactive:
3603 /* stopping an active array */
3604 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003605 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003606 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003607 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003608 } else
3609 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003610 break;
3611 case suspended:
3612 break; /* not supported yet */
3613 case readonly:
3614 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003615 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003616 else {
3617 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003618 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003619 err = do_md_run(mddev);
3620 }
3621 break;
3622 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003623 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003624 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003625 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003626 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003627 err = restart_array(mddev);
3628 if (err == 0) {
3629 mddev->ro = 2;
3630 set_disk_ro(mddev->gendisk, 0);
3631 }
NeilBrown9e653b62006-06-26 00:27:58 -07003632 } else {
3633 mddev->ro = 2;
3634 err = do_md_run(mddev);
3635 }
3636 break;
3637 case clean:
3638 if (mddev->pers) {
3639 restart_array(mddev);
3640 spin_lock_irq(&mddev->write_lock);
3641 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003642 if (mddev->in_sync == 0) {
3643 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003644 if (mddev->safemode == 1)
3645 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003646 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003647 }
3648 err = 0;
3649 } else
3650 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003651 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003652 } else
3653 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003654 break;
3655 case active:
3656 if (mddev->pers) {
3657 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003658 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003659 wake_up(&mddev->sb_wait);
3660 err = 0;
3661 } else {
3662 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003663 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003664 err = do_md_run(mddev);
3665 }
3666 break;
3667 case write_pending:
3668 case active_idle:
3669 /* these cannot be set */
3670 break;
3671 }
3672 if (err)
3673 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003674 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003675 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003676 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003677 }
NeilBrown9e653b62006-06-26 00:27:58 -07003678}
NeilBrown80ca3a42006-07-10 04:44:18 -07003679static struct md_sysfs_entry md_array_state =
3680__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003681
NeilBrown6d7ff732006-01-06 00:21:16 -08003682static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003683max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3684 return sprintf(page, "%d\n",
3685 atomic_read(&mddev->max_corr_read_errors));
3686}
3687
3688static ssize_t
3689max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3690{
3691 char *e;
3692 unsigned long n = simple_strtoul(buf, &e, 10);
3693
3694 if (*buf && (*e == 0 || *e == '\n')) {
3695 atomic_set(&mddev->max_corr_read_errors, n);
3696 return len;
3697 }
3698 return -EINVAL;
3699}
3700
3701static struct md_sysfs_entry max_corr_read_errors =
3702__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3703 max_corrected_read_errors_store);
3704
3705static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003706null_show(mddev_t *mddev, char *page)
3707{
3708 return -EINVAL;
3709}
3710
3711static ssize_t
3712new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3713{
3714 /* buf must be %d:%d\n? giving major and minor numbers */
3715 /* The new device is added to the array.
3716 * If the array has a persistent superblock, we read the
3717 * superblock to initialise info and check validity.
3718 * Otherwise, only checking done is that in bind_rdev_to_array,
3719 * which mainly checks size.
3720 */
3721 char *e;
3722 int major = simple_strtoul(buf, &e, 10);
3723 int minor;
3724 dev_t dev;
3725 mdk_rdev_t *rdev;
3726 int err;
3727
3728 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3729 return -EINVAL;
3730 minor = simple_strtoul(e+1, &e, 10);
3731 if (*e && *e != '\n')
3732 return -EINVAL;
3733 dev = MKDEV(major, minor);
3734 if (major != MAJOR(dev) ||
3735 minor != MINOR(dev))
3736 return -EOVERFLOW;
3737
3738
3739 if (mddev->persistent) {
3740 rdev = md_import_device(dev, mddev->major_version,
3741 mddev->minor_version);
3742 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3743 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3744 mdk_rdev_t, same_set);
3745 err = super_types[mddev->major_version]
3746 .load_super(rdev, rdev0, mddev->minor_version);
3747 if (err < 0)
3748 goto out;
3749 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003750 } else if (mddev->external)
3751 rdev = md_import_device(dev, -2, -1);
3752 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003753 rdev = md_import_device(dev, -1, -1);
3754
3755 if (IS_ERR(rdev))
3756 return PTR_ERR(rdev);
3757 err = bind_rdev_to_array(rdev, mddev);
3758 out:
3759 if (err)
3760 export_rdev(rdev);
3761 return err ? err : len;
3762}
3763
3764static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003765__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003766
3767static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003768bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3769{
3770 char *end;
3771 unsigned long chunk, end_chunk;
3772
3773 if (!mddev->bitmap)
3774 goto out;
3775 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3776 while (*buf) {
3777 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3778 if (buf == end) break;
3779 if (*end == '-') { /* range */
3780 buf = end + 1;
3781 end_chunk = simple_strtoul(buf, &end, 0);
3782 if (buf == end) break;
3783 }
3784 if (*end && !isspace(*end)) break;
3785 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003786 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003787 }
3788 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3789out:
3790 return len;
3791}
3792
3793static struct md_sysfs_entry md_bitmap =
3794__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3795
3796static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003797size_show(mddev_t *mddev, char *page)
3798{
Andre Noll58c0fed2009-03-31 14:33:13 +11003799 return sprintf(page, "%llu\n",
3800 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003801}
3802
Andre Nolld71f9f82008-07-11 22:02:22 +10003803static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003804
3805static ssize_t
3806size_store(mddev_t *mddev, const char *buf, size_t len)
3807{
3808 /* If array is inactive, we can reduce the component size, but
3809 * not increase it (except from 0).
3810 * If array is active, we can try an on-line resize
3811 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003812 sector_t sectors;
3813 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003814
Andre Noll58c0fed2009-03-31 14:33:13 +11003815 if (err < 0)
3816 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003817 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003818 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003819 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003820 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003821 if (mddev->dev_sectors == 0 ||
3822 mddev->dev_sectors > sectors)
3823 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003824 else
3825 err = -ENOSPC;
3826 }
3827 return err ? err : len;
3828}
3829
3830static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003831__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003832
NeilBrown8bb93aa2006-01-06 00:20:50 -08003833
3834/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003835 * This is one of
3836 * 'none' for arrays with no metadata (good luck...)
3837 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003838 * or N.M for internally known formats
3839 */
3840static ssize_t
3841metadata_show(mddev_t *mddev, char *page)
3842{
3843 if (mddev->persistent)
3844 return sprintf(page, "%d.%d\n",
3845 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003846 else if (mddev->external)
3847 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003848 else
3849 return sprintf(page, "none\n");
3850}
3851
3852static ssize_t
3853metadata_store(mddev_t *mddev, const char *buf, size_t len)
3854{
3855 int major, minor;
3856 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003857 /* Changing the details of 'external' metadata is
3858 * always permitted. Otherwise there must be
3859 * no devices attached to the array.
3860 */
3861 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3862 ;
3863 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003864 return -EBUSY;
3865
3866 if (cmd_match(buf, "none")) {
3867 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003868 mddev->external = 0;
3869 mddev->major_version = 0;
3870 mddev->minor_version = 90;
3871 return len;
3872 }
3873 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003874 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003875 if (namelen >= sizeof(mddev->metadata_type))
3876 namelen = sizeof(mddev->metadata_type)-1;
3877 strncpy(mddev->metadata_type, buf+9, namelen);
3878 mddev->metadata_type[namelen] = 0;
3879 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3880 mddev->metadata_type[--namelen] = 0;
3881 mddev->persistent = 0;
3882 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003883 mddev->major_version = 0;
3884 mddev->minor_version = 90;
3885 return len;
3886 }
3887 major = simple_strtoul(buf, &e, 10);
3888 if (e==buf || *e != '.')
3889 return -EINVAL;
3890 buf = e+1;
3891 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003892 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003893 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003894 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003895 return -ENOENT;
3896 mddev->major_version = major;
3897 mddev->minor_version = minor;
3898 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003899 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003900 return len;
3901}
3902
3903static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003904__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003905
NeilBrowna35b0d62006-01-06 00:20:49 -08003906static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003907action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003908{
NeilBrown7eec3142005-11-08 21:39:44 -08003909 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003910 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3911 type = "frozen";
3912 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003913 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003914 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3915 type = "reshape";
3916 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003917 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3918 type = "resync";
3919 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3920 type = "check";
3921 else
3922 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003923 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003924 type = "recover";
3925 }
3926 return sprintf(page, "%s\n", type);
3927}
3928
NeilBrown7ebc0be2011-01-14 09:14:33 +11003929static void reap_sync_thread(mddev_t *mddev);
3930
NeilBrown24dd4692005-11-08 21:39:26 -08003931static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003932action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003933{
NeilBrown7eec3142005-11-08 21:39:44 -08003934 if (!mddev->pers || !mddev->pers->sync_request)
3935 return -EINVAL;
3936
NeilBrownb6a9ce62009-05-26 09:41:17 +10003937 if (cmd_match(page, "frozen"))
3938 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3939 else
3940 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3941
3942 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003943 if (mddev->sync_thread) {
3944 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11003945 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08003946 }
NeilBrown03c902e2006-01-06 00:20:46 -08003947 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3948 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003949 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003950 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003951 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003952 else if (cmd_match(page, "recover")) {
3953 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3954 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3955 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003956 int err;
3957 if (mddev->pers->start_reshape == NULL)
3958 return -EINVAL;
3959 err = mddev->pers->start_reshape(mddev);
3960 if (err)
3961 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003962 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003963 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003964 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003965 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003966 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003967 return -EINVAL;
3968 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3969 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003970 }
NeilBrown03c902e2006-01-06 00:20:46 -08003971 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003972 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003973 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003974 return len;
3975}
3976
NeilBrown9d888832005-11-08 21:39:26 -08003977static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003978mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003979{
3980 return sprintf(page, "%llu\n",
3981 (unsigned long long) mddev->resync_mismatches);
3982}
3983
NeilBrown80ca3a42006-07-10 04:44:18 -07003984static struct md_sysfs_entry md_scan_mode =
3985__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003986
NeilBrown96de1e62005-11-08 21:39:39 -08003987
NeilBrown80ca3a42006-07-10 04:44:18 -07003988static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003989
NeilBrown88202a02006-01-06 00:21:36 -08003990static ssize_t
3991sync_min_show(mddev_t *mddev, char *page)
3992{
3993 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3994 mddev->sync_speed_min ? "local": "system");
3995}
3996
3997static ssize_t
3998sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3999{
4000 int min;
4001 char *e;
4002 if (strncmp(buf, "system", 6)==0) {
4003 mddev->sync_speed_min = 0;
4004 return len;
4005 }
4006 min = simple_strtoul(buf, &e, 10);
4007 if (buf == e || (*e && *e != '\n') || min <= 0)
4008 return -EINVAL;
4009 mddev->sync_speed_min = min;
4010 return len;
4011}
4012
4013static struct md_sysfs_entry md_sync_min =
4014__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4015
4016static ssize_t
4017sync_max_show(mddev_t *mddev, char *page)
4018{
4019 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4020 mddev->sync_speed_max ? "local": "system");
4021}
4022
4023static ssize_t
4024sync_max_store(mddev_t *mddev, const char *buf, size_t len)
4025{
4026 int max;
4027 char *e;
4028 if (strncmp(buf, "system", 6)==0) {
4029 mddev->sync_speed_max = 0;
4030 return len;
4031 }
4032 max = simple_strtoul(buf, &e, 10);
4033 if (buf == e || (*e && *e != '\n') || max <= 0)
4034 return -EINVAL;
4035 mddev->sync_speed_max = max;
4036 return len;
4037}
4038
4039static struct md_sysfs_entry md_sync_max =
4040__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4041
Iustin Popd7f3d292007-10-16 23:30:54 -07004042static ssize_t
4043degraded_show(mddev_t *mddev, char *page)
4044{
4045 return sprintf(page, "%d\n", mddev->degraded);
4046}
4047static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004048
4049static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07004050sync_force_parallel_show(mddev_t *mddev, char *page)
4051{
4052 return sprintf(page, "%d\n", mddev->parallel_resync);
4053}
4054
4055static ssize_t
4056sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
4057{
4058 long n;
4059
4060 if (strict_strtol(buf, 10, &n))
4061 return -EINVAL;
4062
4063 if (n != 0 && n != 1)
4064 return -EINVAL;
4065
4066 mddev->parallel_resync = n;
4067
4068 if (mddev->sync_thread)
4069 wake_up(&resync_wait);
4070
4071 return len;
4072}
4073
4074/* force parallel resync, even with shared block devices */
4075static struct md_sysfs_entry md_sync_force_parallel =
4076__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4077 sync_force_parallel_show, sync_force_parallel_store);
4078
4079static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08004080sync_speed_show(mddev_t *mddev, char *page)
4081{
4082 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004083 if (mddev->curr_resync == 0)
4084 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004085 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4086 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004087 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004088 db = resync - mddev->resync_mark_cnt;
4089 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004090}
4091
NeilBrown80ca3a42006-07-10 04:44:18 -07004092static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004093
4094static ssize_t
4095sync_completed_show(mddev_t *mddev, char *page)
4096{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004097 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004098
NeilBrownacb180b2009-04-14 16:28:34 +10004099 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4100 return sprintf(page, "none\n");
4101
NeilBrown88202a02006-01-06 00:21:36 -08004102 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004103 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004104 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004105 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004106
NeilBrownacb180b2009-04-14 16:28:34 +10004107 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004108 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004109}
4110
NeilBrown80ca3a42006-07-10 04:44:18 -07004111static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004112
NeilBrowne464eaf2006-03-27 01:18:14 -08004113static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10004114min_sync_show(mddev_t *mddev, char *page)
4115{
4116 return sprintf(page, "%llu\n",
4117 (unsigned long long)mddev->resync_min);
4118}
4119static ssize_t
4120min_sync_store(mddev_t *mddev, const char *buf, size_t len)
4121{
4122 unsigned long long min;
4123 if (strict_strtoull(buf, 10, &min))
4124 return -EINVAL;
4125 if (min > mddev->resync_max)
4126 return -EINVAL;
4127 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4128 return -EBUSY;
4129
4130 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004131 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004132 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004133 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004134 return -EINVAL;
4135 }
4136 mddev->resync_min = min;
4137
4138 return len;
4139}
4140
4141static struct md_sysfs_entry md_min_sync =
4142__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4143
4144static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08004145max_sync_show(mddev_t *mddev, char *page)
4146{
4147 if (mddev->resync_max == MaxSector)
4148 return sprintf(page, "max\n");
4149 else
4150 return sprintf(page, "%llu\n",
4151 (unsigned long long)mddev->resync_max);
4152}
4153static ssize_t
4154max_sync_store(mddev_t *mddev, const char *buf, size_t len)
4155{
4156 if (strncmp(buf, "max", 3) == 0)
4157 mddev->resync_max = MaxSector;
4158 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004159 unsigned long long max;
4160 if (strict_strtoull(buf, 10, &max))
4161 return -EINVAL;
4162 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004163 return -EINVAL;
4164 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004165 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004166 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4167 return -EBUSY;
4168
4169 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004170 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004171 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004172 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004173 return -EINVAL;
4174 }
4175 mddev->resync_max = max;
4176 }
4177 wake_up(&mddev->recovery_wait);
4178 return len;
4179}
4180
4181static struct md_sysfs_entry md_max_sync =
4182__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4183
4184static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004185suspend_lo_show(mddev_t *mddev, char *page)
4186{
4187 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4188}
4189
4190static ssize_t
4191suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4192{
4193 char *e;
4194 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004195 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004196
NeilBrownb8d966e2009-07-01 11:14:04 +10004197 if (mddev->pers == NULL ||
4198 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004199 return -EINVAL;
4200 if (buf == e || (*e && *e != '\n'))
4201 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004202
4203 mddev->suspend_lo = new;
4204 if (new >= old)
4205 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004206 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004207 else {
4208 /* Expanding suspended region - need to wait */
4209 mddev->pers->quiesce(mddev, 1);
4210 mddev->pers->quiesce(mddev, 0);
4211 }
4212 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004213}
4214static struct md_sysfs_entry md_suspend_lo =
4215__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4216
4217
4218static ssize_t
4219suspend_hi_show(mddev_t *mddev, char *page)
4220{
4221 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4222}
4223
4224static ssize_t
4225suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4226{
4227 char *e;
4228 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004229 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004230
NeilBrownb8d966e2009-07-01 11:14:04 +10004231 if (mddev->pers == NULL ||
4232 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004233 return -EINVAL;
4234 if (buf == e || (*e && *e != '\n'))
4235 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004236
4237 mddev->suspend_hi = new;
4238 if (new <= old)
4239 /* Shrinking suspended region */
4240 mddev->pers->quiesce(mddev, 2);
4241 else {
4242 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004243 mddev->pers->quiesce(mddev, 1);
4244 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004245 }
4246 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004247}
4248static struct md_sysfs_entry md_suspend_hi =
4249__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4250
NeilBrown08a02ec2007-05-09 02:35:38 -07004251static ssize_t
4252reshape_position_show(mddev_t *mddev, char *page)
4253{
4254 if (mddev->reshape_position != MaxSector)
4255 return sprintf(page, "%llu\n",
4256 (unsigned long long)mddev->reshape_position);
4257 strcpy(page, "none\n");
4258 return 5;
4259}
4260
4261static ssize_t
4262reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4263{
4264 char *e;
4265 unsigned long long new = simple_strtoull(buf, &e, 10);
4266 if (mddev->pers)
4267 return -EBUSY;
4268 if (buf == e || (*e && *e != '\n'))
4269 return -EINVAL;
4270 mddev->reshape_position = new;
4271 mddev->delta_disks = 0;
4272 mddev->new_level = mddev->level;
4273 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004274 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004275 return len;
4276}
4277
4278static struct md_sysfs_entry md_reshape_position =
4279__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4280 reshape_position_store);
4281
Dan Williamsb522adc2009-03-31 15:00:31 +11004282static ssize_t
4283array_size_show(mddev_t *mddev, char *page)
4284{
4285 if (mddev->external_size)
4286 return sprintf(page, "%llu\n",
4287 (unsigned long long)mddev->array_sectors/2);
4288 else
4289 return sprintf(page, "default\n");
4290}
4291
4292static ssize_t
4293array_size_store(mddev_t *mddev, const char *buf, size_t len)
4294{
4295 sector_t sectors;
4296
4297 if (strncmp(buf, "default", 7) == 0) {
4298 if (mddev->pers)
4299 sectors = mddev->pers->size(mddev, 0, 0);
4300 else
4301 sectors = mddev->array_sectors;
4302
4303 mddev->external_size = 0;
4304 } else {
4305 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4306 return -EINVAL;
4307 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004308 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004309
4310 mddev->external_size = 1;
4311 }
4312
4313 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004314 if (mddev->pers) {
4315 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004316 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004317 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004318 return len;
4319}
4320
4321static struct md_sysfs_entry md_array_size =
4322__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4323 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004324
NeilBrowneae17012005-11-08 21:39:23 -08004325static struct attribute *md_default_attrs[] = {
4326 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004327 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004328 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004329 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004330 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004331 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004332 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004333 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004334 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004335 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004336 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004337 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004338 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004339 NULL,
4340};
4341
4342static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004343 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004344 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004345 &md_sync_min.attr,
4346 &md_sync_max.attr,
4347 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004348 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004349 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004350 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004351 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004352 &md_suspend_lo.attr,
4353 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004354 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004355 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004356 NULL,
4357};
NeilBrown411036f2005-11-08 21:39:40 -08004358static struct attribute_group md_redundancy_group = {
4359 .name = NULL,
4360 .attrs = md_redundancy_attrs,
4361};
4362
NeilBrowneae17012005-11-08 21:39:23 -08004363
4364static ssize_t
4365md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4366{
4367 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4368 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004369 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004370
4371 if (!entry->show)
4372 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004373 rv = mddev_lock(mddev);
4374 if (!rv) {
4375 rv = entry->show(mddev, page);
4376 mddev_unlock(mddev);
4377 }
NeilBrown96de1e62005-11-08 21:39:39 -08004378 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004379}
4380
4381static ssize_t
4382md_attr_store(struct kobject *kobj, struct attribute *attr,
4383 const char *page, size_t length)
4384{
4385 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4386 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004387 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004388
4389 if (!entry->store)
4390 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004391 if (!capable(CAP_SYS_ADMIN))
4392 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004393 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004394 if (mddev->hold_active == UNTIL_IOCTL)
4395 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004396 if (!rv) {
4397 rv = entry->store(mddev, page, length);
4398 mddev_unlock(mddev);
4399 }
NeilBrown96de1e62005-11-08 21:39:39 -08004400 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004401}
4402
4403static void md_free(struct kobject *ko)
4404{
4405 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004406
4407 if (mddev->sysfs_state)
4408 sysfs_put(mddev->sysfs_state);
4409
4410 if (mddev->gendisk) {
4411 del_gendisk(mddev->gendisk);
4412 put_disk(mddev->gendisk);
4413 }
4414 if (mddev->queue)
4415 blk_cleanup_queue(mddev->queue);
4416
NeilBrowneae17012005-11-08 21:39:23 -08004417 kfree(mddev);
4418}
4419
Emese Revfy52cf25d2010-01-19 02:58:23 +01004420static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004421 .show = md_attr_show,
4422 .store = md_attr_store,
4423};
4424static struct kobj_type md_ktype = {
4425 .release = md_free,
4426 .sysfs_ops = &md_sysfs_ops,
4427 .default_attrs = md_default_attrs,
4428};
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430int mdp_major = 0;
4431
Dan Williams5fd3a172009-03-04 00:57:25 -07004432static void mddev_delayed_delete(struct work_struct *ws)
4433{
4434 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4435
NeilBrown43a70502009-12-14 12:49:55 +11004436 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004437 kobject_del(&mddev->kobj);
4438 kobject_put(&mddev->kobj);
4439}
4440
NeilBrownefeb53c2009-01-09 08:31:10 +11004441static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004443 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 mddev_t *mddev = mddev_find(dev);
4445 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004446 int partitioned;
4447 int shift;
4448 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004449 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
4451 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004452 return -ENODEV;
4453
4454 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4455 shift = partitioned ? MdpMinorShift : 0;
4456 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Tejun Heoe804ac72010-10-15 15:36:08 +02004458 /* wait for any previous instance of this device to be
4459 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004460 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004461 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004462
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004463 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004464 error = -EEXIST;
4465 if (mddev->gendisk)
4466 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004467
4468 if (name) {
4469 /* Need to ensure that 'name' is not a duplicate.
4470 */
4471 mddev_t *mddev2;
4472 spin_lock(&all_mddevs_lock);
4473
4474 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4475 if (mddev2->gendisk &&
4476 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4477 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004478 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004479 }
4480 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
NeilBrown8b765392009-01-09 08:31:08 +11004482
NeilBrown0909dc42009-07-01 12:27:21 +10004483 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004484 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004485 if (!mddev->queue)
4486 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004487 mddev->queue->queuedata = mddev;
4488
NeilBrown409c57f2009-03-31 14:39:39 +11004489 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 disk = alloc_disk(1 << shift);
4492 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004493 blk_cleanup_queue(mddev->queue);
4494 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004495 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004497 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004499 if (name)
4500 strcpy(disk->disk_name, name);
4501 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004503 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 disk->fops = &md_fops;
4506 disk->private_data = mddev;
4507 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004508 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004509 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004510 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004511 * remove it now.
4512 */
4513 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004515 /* As soon as we call add_disk(), another thread could get
4516 * through to md_open, so make sure it doesn't get too far
4517 */
4518 mutex_lock(&mddev->open_mutex);
4519 add_disk(disk);
4520
Tejun Heoed9e1982008-08-25 19:56:05 +09004521 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4522 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004523 if (error) {
4524 /* This isn't possible, but as kobject_init_and_add is marked
4525 * __must_check, we must do something with the result
4526 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004527 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4528 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004529 error = 0;
4530 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004531 if (mddev->kobj.sd &&
4532 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004533 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004534 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004535 abort:
4536 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004537 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004538 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004539 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004540 }
NeilBrownd3374822009-01-09 08:31:10 +11004541 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004542 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004543}
4544
4545static struct kobject *md_probe(dev_t dev, int *part, void *data)
4546{
4547 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 return NULL;
4549}
4550
NeilBrownefeb53c2009-01-09 08:31:10 +11004551static int add_named_array(const char *val, struct kernel_param *kp)
4552{
4553 /* val must be "md_*" where * is not all digits.
4554 * We allocate an array with a large free minor number, and
4555 * set the name to val. val must not already be an active name.
4556 */
4557 int len = strlen(val);
4558 char buf[DISK_NAME_LEN];
4559
4560 while (len && val[len-1] == '\n')
4561 len--;
4562 if (len >= DISK_NAME_LEN)
4563 return -E2BIG;
4564 strlcpy(buf, val, len+1);
4565 if (strncmp(buf, "md_", 3) != 0)
4566 return -EINVAL;
4567 return md_alloc(0, buf);
4568}
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570static void md_safemode_timeout(unsigned long data)
4571{
4572 mddev_t *mddev = (mddev_t *) data;
4573
Neil Brown0fd62b82008-06-28 08:31:36 +10004574 if (!atomic_read(&mddev->writes_pending)) {
4575 mddev->safemode = 1;
4576 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004577 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 md_wakeup_thread(mddev->thread);
4580}
4581
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004582static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
NeilBrown390ee602010-06-01 19:37:27 +10004584int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
NeilBrown2604b702006-01-06 00:20:36 -08004586 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004588 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
NeilBrowna757e642005-04-16 15:26:42 -07004590 if (list_empty(&mddev->disks))
4591 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
4594 if (mddev->pers)
4595 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004596 /* Cannot run until previous stop completes properly */
4597 if (mddev->sysfs_active)
4598 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 /*
4601 * Analyze all RAID superblock(s)
4602 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004603 if (!mddev->raid_disks) {
4604 if (!mddev->persistent)
4605 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004606 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
NeilBrownd9d166c2006-01-06 00:20:51 -08004609 if (mddev->level != LEVEL_NONE)
4610 request_module("md-level-%d", mddev->level);
4611 else if (mddev->clevel[0])
4612 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 /*
4615 * Drop all container device buffers, from now on
4616 * the only valid external interface is through the md
4617 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004619 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004620 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 continue;
4622 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004623 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004624
4625 /* perform some consistency tests on the device.
4626 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004627 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004628 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004629 if (rdev->meta_bdev) {
4630 /* Nothing to check */;
4631 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004632 if (mddev->dev_sectors &&
4633 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004634 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004635 printk("md: %s: data overlaps metadata\n",
4636 mdname(mddev));
4637 return -EINVAL;
4638 }
4639 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004640 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004641 > rdev->data_offset) {
4642 printk("md: %s: metadata overlaps data\n",
4643 mdname(mddev));
4644 return -EINVAL;
4645 }
4646 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004647 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
4649
NeilBrowna167f662010-10-26 18:31:13 +11004650 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004651 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
4652 sizeof(mddev_t *));
NeilBrowna167f662010-10-26 18:31:13 +11004653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004655 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004656 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004658 if (mddev->level != LEVEL_NONE)
4659 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4660 mddev->level);
4661 else
4662 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4663 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return -EINVAL;
4665 }
NeilBrown2604b702006-01-06 00:20:36 -08004666 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004668 if (mddev->level != pers->level) {
4669 mddev->level = pers->level;
4670 mddev->new_level = pers->level;
4671 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004672 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
NeilBrownf6705572006-03-27 01:18:11 -08004674 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004675 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004676 /* This personality cannot handle reshaping... */
4677 mddev->pers = NULL;
4678 module_put(pers->owner);
4679 return -EINVAL;
4680 }
4681
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004682 if (pers->sync_request) {
4683 /* Warn if this is a potentially silly
4684 * configuration.
4685 */
4686 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4687 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004688 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004689
4690 list_for_each_entry(rdev, &mddev->disks, same_set)
4691 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004692 if (rdev < rdev2 &&
4693 rdev->bdev->bd_contains ==
4694 rdev2->bdev->bd_contains) {
4695 printk(KERN_WARNING
4696 "%s: WARNING: %s appears to be"
4697 " on the same physical disk as"
4698 " %s.\n",
4699 mdname(mddev),
4700 bdevname(rdev->bdev,b),
4701 bdevname(rdev2->bdev,b2));
4702 warned = 1;
4703 }
4704 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004705
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004706 if (warned)
4707 printk(KERN_WARNING
4708 "True protection against single-disk"
4709 " failure might be compromised.\n");
4710 }
4711
NeilBrown657390d2005-08-26 18:34:16 -07004712 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004713 /* may be over-ridden by personality */
4714 mddev->resync_max_sectors = mddev->dev_sectors;
4715
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004716 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
NeilBrown0f9552b2009-12-30 12:08:50 +11004718 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004719 mddev->ro = 2; /* read-only, but switch on first write */
4720
NeilBrownb15c2e52006-01-06 00:20:16 -08004721 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004722 if (err)
4723 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004724 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4725 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4726 " but 'external_size' not in effect?\n", __func__);
4727 printk(KERN_ERR
4728 "md: invalid array_size %llu > default size %llu\n",
4729 (unsigned long long)mddev->array_sectors / 2,
4730 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4731 err = -EINVAL;
4732 mddev->pers->stop(mddev);
4733 }
4734 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004735 err = bitmap_create(mddev);
4736 if (err) {
4737 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4738 mdname(mddev), err);
4739 mddev->pers->stop(mddev);
4740 }
4741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 module_put(mddev->pers->owner);
4744 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004745 bitmap_destroy(mddev);
4746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004748 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004749 if (mddev->kobj.sd &&
4750 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004751 printk(KERN_WARNING
4752 "md: cannot register extra attributes for %s\n",
4753 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004754 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004755 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004756 mddev->ro = 0;
4757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004759 atomic_set(&mddev->max_corr_read_errors,
4760 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 mddev->safemode = 0;
4762 mddev->safemode_timer.function = md_safemode_timeout;
4763 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004764 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004766 smp_wmb();
4767 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004768 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004769 if (rdev->raid_disk >= 0)
4770 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004771 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4774
NeilBrown850b2b42006-10-03 01:15:46 -07004775 if (mddev->flags)
4776 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
NeilBrownd7603b72006-01-06 00:20:30 -08004778 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004779 sysfs_notify_dirent_safe(mddev->sysfs_state);
4780 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004781 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 return 0;
4783}
NeilBrown390ee602010-06-01 19:37:27 +10004784EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
NeilBrownfe60b012010-03-29 11:10:42 +11004786static int do_md_run(mddev_t *mddev)
4787{
4788 int err;
4789
4790 err = md_run(mddev);
4791 if (err)
4792 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004793 err = bitmap_load(mddev);
4794 if (err) {
4795 bitmap_destroy(mddev);
4796 goto out;
4797 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004798
4799 md_wakeup_thread(mddev->thread);
4800 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4801
NeilBrownfe60b012010-03-29 11:10:42 +11004802 set_capacity(mddev->gendisk, mddev->array_sectors);
4803 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004804 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004805 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4806out:
4807 return err;
4808}
4809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810static int restart_array(mddev_t *mddev)
4811{
4812 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Andre Noll80fab1d2008-07-11 22:02:21 +10004814 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004816 return -ENXIO;
4817 if (!mddev->pers)
4818 return -EINVAL;
4819 if (!mddev->ro)
4820 return -EBUSY;
4821 mddev->safemode = 0;
4822 mddev->ro = 0;
4823 set_disk_ro(disk, 0);
4824 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4825 mdname(mddev));
4826 /* Kick recovery or resync if necessary */
4827 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4828 md_wakeup_thread(mddev->thread);
4829 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004830 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832}
4833
NeilBrownacc55e22006-06-26 00:27:47 -07004834/* similar to deny_write_access, but accounts for our holding a reference
4835 * to the file ourselves */
4836static int deny_bitmap_write_access(struct file * file)
4837{
4838 struct inode *inode = file->f_mapping->host;
4839
4840 spin_lock(&inode->i_lock);
4841 if (atomic_read(&inode->i_writecount) > 1) {
4842 spin_unlock(&inode->i_lock);
4843 return -ETXTBSY;
4844 }
4845 atomic_set(&inode->i_writecount, -1);
4846 spin_unlock(&inode->i_lock);
4847
4848 return 0;
4849}
4850
NeilBrown43a70502009-12-14 12:49:55 +11004851void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004852{
4853 struct inode *inode = file->f_mapping->host;
4854
4855 spin_lock(&inode->i_lock);
4856 atomic_set(&inode->i_writecount, 1);
4857 spin_unlock(&inode->i_lock);
4858}
4859
NeilBrown6177b472010-03-29 11:37:13 +11004860static void md_clean(mddev_t *mddev)
4861{
4862 mddev->array_sectors = 0;
4863 mddev->external_size = 0;
4864 mddev->dev_sectors = 0;
4865 mddev->raid_disks = 0;
4866 mddev->recovery_cp = 0;
4867 mddev->resync_min = 0;
4868 mddev->resync_max = MaxSector;
4869 mddev->reshape_position = MaxSector;
4870 mddev->external = 0;
4871 mddev->persistent = 0;
4872 mddev->level = LEVEL_NONE;
4873 mddev->clevel[0] = 0;
4874 mddev->flags = 0;
4875 mddev->ro = 0;
4876 mddev->metadata_type[0] = 0;
4877 mddev->chunk_sectors = 0;
4878 mddev->ctime = mddev->utime = 0;
4879 mddev->layout = 0;
4880 mddev->max_disks = 0;
4881 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004882 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004883 mddev->delta_disks = 0;
4884 mddev->new_level = LEVEL_NONE;
4885 mddev->new_layout = 0;
4886 mddev->new_chunk_sectors = 0;
4887 mddev->curr_resync = 0;
4888 mddev->resync_mismatches = 0;
4889 mddev->suspend_lo = mddev->suspend_hi = 0;
4890 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4891 mddev->recovery = 0;
4892 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004893 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004894 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004895 mddev->safemode = 0;
4896 mddev->bitmap_info.offset = 0;
4897 mddev->bitmap_info.default_offset = 0;
4898 mddev->bitmap_info.chunksize = 0;
4899 mddev->bitmap_info.daemon_sleep = 0;
4900 mddev->bitmap_info.max_write_behind = 0;
4901}
4902
NeilBrowndefad612011-01-14 09:14:33 +11004903static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004904{
4905 if (mddev->sync_thread) {
4906 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4907 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004908 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004909 }
4910
4911 del_timer_sync(&mddev->safemode_timer);
4912
4913 bitmap_flush(mddev);
4914 md_super_wait(mddev);
4915
4916 if (!mddev->in_sync || mddev->flags) {
4917 /* mark array as shutdown cleanly */
4918 mddev->in_sync = 1;
4919 md_update_sb(mddev, 1);
4920 }
4921}
NeilBrowndefad612011-01-14 09:14:33 +11004922
4923void md_stop_writes(mddev_t *mddev)
4924{
4925 mddev_lock(mddev);
4926 __md_stop_writes(mddev);
4927 mddev_unlock(mddev);
4928}
NeilBrown390ee602010-06-01 19:37:27 +10004929EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004930
NeilBrown390ee602010-06-01 19:37:27 +10004931void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004932{
NeilBrown0ca69882011-01-14 09:14:33 +11004933 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004934 mddev->pers->stop(mddev);
4935 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4936 mddev->to_remove = &md_redundancy_group;
4937 module_put(mddev->pers->owner);
4938 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004939 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004940}
NeilBrown390ee602010-06-01 19:37:27 +10004941EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004942
NeilBrowna4bd82d2010-03-29 13:23:10 +11004943static int md_set_readonly(mddev_t *mddev, int is_open)
4944{
4945 int err = 0;
4946 mutex_lock(&mddev->open_mutex);
4947 if (atomic_read(&mddev->openers) > is_open) {
4948 printk("md: %s still in use.\n",mdname(mddev));
4949 err = -EBUSY;
4950 goto out;
4951 }
4952 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11004953 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004954
4955 err = -ENXIO;
4956 if (mddev->ro==1)
4957 goto out;
4958 mddev->ro = 1;
4959 set_disk_ro(mddev->gendisk, 1);
4960 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004961 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004962 err = 0;
4963 }
4964out:
4965 mutex_unlock(&mddev->open_mutex);
4966 return err;
4967}
4968
NeilBrown9e653b62006-06-26 00:27:58 -07004969/* mode:
4970 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004971 * 2 - stop but do not disassemble array
4972 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004973static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004976 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
NeilBrownc8c00a62009-08-10 12:50:52 +10004978 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004979 if (atomic_read(&mddev->openers) > is_open ||
4980 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004981 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004982 mutex_unlock(&mddev->open_mutex);
4983 return -EBUSY;
4984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
NeilBrown6e17b022010-08-07 21:41:19 +10004986 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004987 if (mddev->ro)
4988 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004989
NeilBrowndefad612011-01-14 09:14:33 +11004990 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004991 md_stop(mddev);
4992 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004993 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004994
NeilBrowna4bd82d2010-03-29 13:23:10 +11004995 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004996 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004997
NeilBrowna4bd82d2010-03-29 13:23:10 +11004998 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004999 if (rdev->raid_disk >= 0)
5000 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005001
NeilBrowna4bd82d2010-03-29 13:23:10 +11005002 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005003 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005004 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005005 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005006
NeilBrowna4bd82d2010-03-29 13:23:10 +11005007 if (mddev->ro)
5008 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005009 } else
5010 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 /*
5012 * Free resources if final stop
5013 */
NeilBrown9e653b62006-06-26 00:27:58 -07005014 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5016
NeilBrown978f9462006-02-02 14:28:05 -08005017 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005018 if (mddev->bitmap_info.file) {
5019 restore_bitmap_write_access(mddev->bitmap_info.file);
5020 fput(mddev->bitmap_info.file);
5021 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005022 }
NeilBrownc3d97142009-12-14 12:49:52 +11005023 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 export_array(mddev);
5026
NeilBrown6177b472010-03-29 11:37:13 +11005027 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005028 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005029 if (mddev->hold_active == UNTIL_STOP)
5030 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005031 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005032 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005033 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005034 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036}
5037
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005038#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039static void autorun_array(mddev_t *mddev)
5040{
5041 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 int err;
5043
NeilBrowna757e642005-04-16 15:26:42 -07005044 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
5047 printk(KERN_INFO "md: running: ");
5048
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005049 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 char b[BDEVNAME_SIZE];
5051 printk("<%s>", bdevname(rdev->bdev,b));
5052 }
5053 printk("\n");
5054
NeilBrownd710e132008-10-13 11:55:12 +11005055 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 if (err) {
5057 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005058 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
5060}
5061
5062/*
5063 * lets try to run arrays based on all disks that have arrived
5064 * until now. (those are in pending_raid_disks)
5065 *
5066 * the method: pick the first pending disk, collect all disks with
5067 * the same UUID, remove all from the pending list and put them into
5068 * the 'same_array' list. Then order this list based on superblock
5069 * update time (freshest comes first), kick out 'old' disks and
5070 * compare superblocks. If everything's fine then run it.
5071 *
5072 * If "unit" is allocated, then bump its reference count
5073 */
5074static void autorun_devices(int part)
5075{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005076 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 mddev_t *mddev;
5078 char b[BDEVNAME_SIZE];
5079
5080 printk(KERN_INFO "md: autorun ...\n");
5081 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005082 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005084 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 rdev0 = list_entry(pending_raid_disks.next,
5086 mdk_rdev_t, same_set);
5087
5088 printk(KERN_INFO "md: considering %s ...\n",
5089 bdevname(rdev0->bdev,b));
5090 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005091 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 if (super_90_load(rdev, rdev0, 0) >= 0) {
5093 printk(KERN_INFO "md: adding %s ...\n",
5094 bdevname(rdev->bdev,b));
5095 list_move(&rdev->same_set, &candidates);
5096 }
5097 /*
5098 * now we have a set of devices, with all of them having
5099 * mostly sane superblocks. It's time to allocate the
5100 * mddev.
5101 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005102 if (part) {
5103 dev = MKDEV(mdp_major,
5104 rdev0->preferred_minor << MdpMinorShift);
5105 unit = MINOR(dev) >> MdpMinorShift;
5106 } else {
5107 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5108 unit = MINOR(dev);
5109 }
5110 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5112 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5113 break;
5114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
5116 md_probe(dev, NULL, NULL);
5117 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005118 if (!mddev || !mddev->gendisk) {
5119 if (mddev)
5120 mddev_put(mddev);
5121 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 "md: cannot allocate memory for md drive.\n");
5123 break;
5124 }
5125 if (mddev_lock(mddev))
5126 printk(KERN_WARNING "md: %s locked, cannot run\n",
5127 mdname(mddev));
5128 else if (mddev->raid_disks || mddev->major_version
5129 || !list_empty(&mddev->disks)) {
5130 printk(KERN_WARNING
5131 "md: %s already running, cannot run %s\n",
5132 mdname(mddev), bdevname(rdev0->bdev,b));
5133 mddev_unlock(mddev);
5134 } else {
5135 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005136 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005137 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 list_del_init(&rdev->same_set);
5139 if (bind_rdev_to_array(rdev, mddev))
5140 export_rdev(rdev);
5141 }
5142 autorun_array(mddev);
5143 mddev_unlock(mddev);
5144 }
5145 /* on success, candidates will be empty, on error
5146 * it won't...
5147 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005148 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005149 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 mddev_put(mddev);
5153 }
5154 printk(KERN_INFO "md: ... autorun DONE.\n");
5155}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005156#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158static int get_version(void __user * arg)
5159{
5160 mdu_version_t ver;
5161
5162 ver.major = MD_MAJOR_VERSION;
5163 ver.minor = MD_MINOR_VERSION;
5164 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5165
5166 if (copy_to_user(arg, &ver, sizeof(ver)))
5167 return -EFAULT;
5168
5169 return 0;
5170}
5171
5172static int get_array_info(mddev_t * mddev, void __user * arg)
5173{
5174 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005175 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
NeilBrowna9f326e2009-09-23 18:06:41 +10005178 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005179 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005181 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 failed++;
5183 else {
5184 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005185 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005186 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 else
5188 spare++;
5189 }
5190 }
5191
5192 info.major_version = mddev->major_version;
5193 info.minor_version = mddev->minor_version;
5194 info.patch_version = MD_PATCHLEVEL_VERSION;
5195 info.ctime = mddev->ctime;
5196 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005197 info.size = mddev->dev_sectors / 2;
5198 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005199 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 info.nr_disks = nr;
5201 info.raid_disks = mddev->raid_disks;
5202 info.md_minor = mddev->md_minor;
5203 info.not_persistent= !mddev->persistent;
5204
5205 info.utime = mddev->utime;
5206 info.state = 0;
5207 if (mddev->in_sync)
5208 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005209 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005210 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005211 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 info.working_disks = working;
5213 info.failed_disks = failed;
5214 info.spare_disks = spare;
5215
5216 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005217 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
5219 if (copy_to_user(arg, &info, sizeof(info)))
5220 return -EFAULT;
5221
5222 return 0;
5223}
5224
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005225static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005226{
5227 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5228 char *ptr, *buf = NULL;
5229 int err = -ENOMEM;
5230
Dan Williamsb5470dc2008-06-27 21:44:04 -07005231 if (md_allow_write(mddev))
5232 file = kmalloc(sizeof(*file), GFP_NOIO);
5233 else
5234 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005235
NeilBrown32a76272005-06-21 17:17:14 -07005236 if (!file)
5237 goto out;
5238
5239 /* bitmap disabled, zero the first byte and copy out */
5240 if (!mddev->bitmap || !mddev->bitmap->file) {
5241 file->pathname[0] = '\0';
5242 goto copy_out;
5243 }
5244
5245 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5246 if (!buf)
5247 goto out;
5248
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005249 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5250 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005251 goto out;
5252
5253 strcpy(file->pathname, ptr);
5254
5255copy_out:
5256 err = 0;
5257 if (copy_to_user(arg, file, sizeof(*file)))
5258 err = -EFAULT;
5259out:
5260 kfree(buf);
5261 kfree(file);
5262 return err;
5263}
5264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265static int get_disk_info(mddev_t * mddev, void __user * arg)
5266{
5267 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 mdk_rdev_t *rdev;
5269
5270 if (copy_from_user(&info, arg, sizeof(info)))
5271 return -EFAULT;
5272
Andre Noll26ef3792008-07-11 22:02:21 +10005273 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 if (rdev) {
5275 info.major = MAJOR(rdev->bdev->bd_dev);
5276 info.minor = MINOR(rdev->bdev->bd_dev);
5277 info.raid_disk = rdev->raid_disk;
5278 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005279 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005281 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 info.state |= (1<<MD_DISK_ACTIVE);
5283 info.state |= (1<<MD_DISK_SYNC);
5284 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005285 if (test_bit(WriteMostly, &rdev->flags))
5286 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 } else {
5288 info.major = info.minor = 0;
5289 info.raid_disk = -1;
5290 info.state = (1<<MD_DISK_REMOVED);
5291 }
5292
5293 if (copy_to_user(arg, &info, sizeof(info)))
5294 return -EFAULT;
5295
5296 return 0;
5297}
5298
5299static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5300{
5301 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5302 mdk_rdev_t *rdev;
5303 dev_t dev = MKDEV(info->major,info->minor);
5304
5305 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5306 return -EOVERFLOW;
5307
5308 if (!mddev->raid_disks) {
5309 int err;
5310 /* expecting a device which has a superblock */
5311 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5312 if (IS_ERR(rdev)) {
5313 printk(KERN_WARNING
5314 "md: md_import_device returned %ld\n",
5315 PTR_ERR(rdev));
5316 return PTR_ERR(rdev);
5317 }
5318 if (!list_empty(&mddev->disks)) {
5319 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5320 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005321 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 .load_super(rdev, rdev0, mddev->minor_version);
5323 if (err < 0) {
5324 printk(KERN_WARNING
5325 "md: %s has different UUID to %s\n",
5326 bdevname(rdev->bdev,b),
5327 bdevname(rdev0->bdev,b2));
5328 export_rdev(rdev);
5329 return -EINVAL;
5330 }
5331 }
5332 err = bind_rdev_to_array(rdev, mddev);
5333 if (err)
5334 export_rdev(rdev);
5335 return err;
5336 }
5337
5338 /*
5339 * add_new_disk can be used once the array is assembled
5340 * to add "hot spares". They must already have a superblock
5341 * written
5342 */
5343 if (mddev->pers) {
5344 int err;
5345 if (!mddev->pers->hot_add_disk) {
5346 printk(KERN_WARNING
5347 "%s: personality does not support diskops!\n",
5348 mdname(mddev));
5349 return -EINVAL;
5350 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005351 if (mddev->persistent)
5352 rdev = md_import_device(dev, mddev->major_version,
5353 mddev->minor_version);
5354 else
5355 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 if (IS_ERR(rdev)) {
5357 printk(KERN_WARNING
5358 "md: md_import_device returned %ld\n",
5359 PTR_ERR(rdev));
5360 return PTR_ERR(rdev);
5361 }
NeilBrown1a855a02010-12-09 16:36:28 +11005362 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005363 if (!mddev->persistent) {
5364 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005365 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005366 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005367 set_bit(In_sync, &rdev->flags);
5368 } else
NeilBrown41158c72005-06-21 17:17:25 -07005369 rdev->raid_disk = -1;
5370 } else
5371 super_types[mddev->major_version].
5372 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005373 if ((info->state & (1<<MD_DISK_SYNC)) &&
5374 (!test_bit(In_sync, &rdev->flags) ||
5375 rdev->raid_disk != info->raid_disk)) {
5376 /* This was a hot-add request, but events doesn't
5377 * match, so reject it.
5378 */
5379 export_rdev(rdev);
5380 return -EINVAL;
5381 }
5382
NeilBrown1a855a02010-12-09 16:36:28 +11005383 if (test_bit(In_sync, &rdev->flags))
5384 rdev->saved_raid_disk = rdev->raid_disk;
5385 else
5386 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005387
NeilBrownb2d444d2005-11-08 21:39:31 -08005388 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005389 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5390 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005391 else
5392 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005393
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 rdev->raid_disk = -1;
5395 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005396 if (!err && !mddev->pers->hot_remove_disk) {
5397 /* If there is hot_add_disk but no hot_remove_disk
5398 * then added disks for geometry changes,
5399 * and should be added immediately.
5400 */
5401 super_types[mddev->major_version].
5402 validate_super(mddev, rdev);
5403 err = mddev->pers->hot_add_disk(mddev, rdev);
5404 if (err)
5405 unbind_rdev_from_array(rdev);
5406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 if (err)
5408 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005409 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005410 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005411
NeilBrown17571282006-12-10 02:20:52 -08005412 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005413 if (mddev->degraded)
5414 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005415 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005416 if (!err)
5417 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005418 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 return err;
5420 }
5421
5422 /* otherwise, add_new_disk is only allowed
5423 * for major_version==0 superblocks
5424 */
5425 if (mddev->major_version != 0) {
5426 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5427 mdname(mddev));
5428 return -EINVAL;
5429 }
5430
5431 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5432 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005433 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 if (IS_ERR(rdev)) {
5435 printk(KERN_WARNING
5436 "md: error, md_import_device() returned %ld\n",
5437 PTR_ERR(rdev));
5438 return PTR_ERR(rdev);
5439 }
5440 rdev->desc_nr = info->number;
5441 if (info->raid_disk < mddev->raid_disks)
5442 rdev->raid_disk = info->raid_disk;
5443 else
5444 rdev->raid_disk = -1;
5445
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005447 if (info->state & (1<<MD_DISK_SYNC))
5448 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005450 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5451 set_bit(WriteMostly, &rdev->flags);
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 if (!mddev->persistent) {
5454 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005455 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5456 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005457 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005458 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
NeilBrown2bf071b2006-01-06 00:20:55 -08005460 err = bind_rdev_to_array(rdev, mddev);
5461 if (err) {
5462 export_rdev(rdev);
5463 return err;
5464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 }
5466
5467 return 0;
5468}
5469
5470static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5471{
5472 char b[BDEVNAME_SIZE];
5473 mdk_rdev_t *rdev;
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 rdev = find_rdev(mddev, dev);
5476 if (!rdev)
5477 return -ENXIO;
5478
5479 if (rdev->raid_disk >= 0)
5480 goto busy;
5481
5482 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005483 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005484 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
5486 return 0;
5487busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005488 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 bdevname(rdev->bdev,b), mdname(mddev));
5490 return -EBUSY;
5491}
5492
5493static int hot_add_disk(mddev_t * mddev, dev_t dev)
5494{
5495 char b[BDEVNAME_SIZE];
5496 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 mdk_rdev_t *rdev;
5498
5499 if (!mddev->pers)
5500 return -ENODEV;
5501
5502 if (mddev->major_version != 0) {
5503 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5504 " version-0 superblocks.\n",
5505 mdname(mddev));
5506 return -EINVAL;
5507 }
5508 if (!mddev->pers->hot_add_disk) {
5509 printk(KERN_WARNING
5510 "%s: personality does not support diskops!\n",
5511 mdname(mddev));
5512 return -EINVAL;
5513 }
5514
NeilBrownd710e132008-10-13 11:55:12 +11005515 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 if (IS_ERR(rdev)) {
5517 printk(KERN_WARNING
5518 "md: error, md_import_device() returned %ld\n",
5519 PTR_ERR(rdev));
5520 return -EINVAL;
5521 }
5522
5523 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005524 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005526 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
NeilBrown8190e752009-06-18 08:48:58 +10005528 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
NeilBrownb2d444d2005-11-08 21:39:31 -08005530 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 printk(KERN_WARNING
5532 "md: can not hot-add faulty %s disk to %s!\n",
5533 bdevname(rdev->bdev,b), mdname(mddev));
5534 err = -EINVAL;
5535 goto abort_export;
5536 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005537 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005539 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005540 err = bind_rdev_to_array(rdev, mddev);
5541 if (err)
5542 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
5544 /*
5545 * The rest should better be atomic, we can have disk failures
5546 * noticed in interrupt contexts ...
5547 */
5548
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 rdev->raid_disk = -1;
5550
NeilBrown850b2b42006-10-03 01:15:46 -07005551 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
5553 /*
5554 * Kick recovery, maybe this spare has to be added to the
5555 * array immediately.
5556 */
5557 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5558 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005559 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 return 0;
5561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562abort_export:
5563 export_rdev(rdev);
5564 return err;
5565}
5566
NeilBrown32a76272005-06-21 17:17:14 -07005567static int set_bitmap_file(mddev_t *mddev, int fd)
5568{
5569 int err;
5570
NeilBrown36fa3062005-09-09 16:23:45 -07005571 if (mddev->pers) {
5572 if (!mddev->pers->quiesce)
5573 return -EBUSY;
5574 if (mddev->recovery || mddev->sync_thread)
5575 return -EBUSY;
5576 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005577 }
5578
NeilBrown36fa3062005-09-09 16:23:45 -07005579
5580 if (fd >= 0) {
5581 if (mddev->bitmap)
5582 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005583 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005584
NeilBrownc3d97142009-12-14 12:49:52 +11005585 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005586 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5587 mdname(mddev));
5588 return -EBADF;
5589 }
5590
NeilBrownc3d97142009-12-14 12:49:52 +11005591 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005592 if (err) {
5593 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5594 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005595 fput(mddev->bitmap_info.file);
5596 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005597 return err;
5598 }
NeilBrownc3d97142009-12-14 12:49:52 +11005599 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005600 } else if (mddev->bitmap == NULL)
5601 return -ENOENT; /* cannot remove what isn't there */
5602 err = 0;
5603 if (mddev->pers) {
5604 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005605 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005606 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005607 if (!err)
5608 err = bitmap_load(mddev);
5609 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005610 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005611 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005612 fd = -1; /* make sure to put the file */
5613 }
NeilBrown36fa3062005-09-09 16:23:45 -07005614 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005615 }
5616 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005617 if (mddev->bitmap_info.file) {
5618 restore_bitmap_write_access(mddev->bitmap_info.file);
5619 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005620 }
NeilBrownc3d97142009-12-14 12:49:52 +11005621 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005622 }
5623
NeilBrown32a76272005-06-21 17:17:14 -07005624 return err;
5625}
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627/*
5628 * set_array_info is used two different ways
5629 * The original usage is when creating a new array.
5630 * In this usage, raid_disks is > 0 and it together with
5631 * level, size, not_persistent,layout,chunksize determine the
5632 * shape of the array.
5633 * This will always create an array with a type-0.90.0 superblock.
5634 * The newer usage is when assembling an array.
5635 * In this case raid_disks will be 0, and the major_version field is
5636 * use to determine which style super-blocks are to be found on the devices.
5637 * The minor and patch _version numbers are also kept incase the
5638 * super_block handler wishes to interpret them.
5639 */
5640static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5641{
5642
5643 if (info->raid_disks == 0) {
5644 /* just setting version number for superblock loading */
5645 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005646 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 super_types[info->major_version].name == NULL) {
5648 /* maybe try to auto-load a module? */
5649 printk(KERN_INFO
5650 "md: superblock version %d not known\n",
5651 info->major_version);
5652 return -EINVAL;
5653 }
5654 mddev->major_version = info->major_version;
5655 mddev->minor_version = info->minor_version;
5656 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005657 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005658 /* ensure mddev_put doesn't delete this now that there
5659 * is some minimal configuration.
5660 */
5661 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 return 0;
5663 }
5664 mddev->major_version = MD_MAJOR_VERSION;
5665 mddev->minor_version = MD_MINOR_VERSION;
5666 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5667 mddev->ctime = get_seconds();
5668
5669 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005670 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005671 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 mddev->raid_disks = info->raid_disks;
5673 /* don't set md_minor, it is determined by which /dev/md* was
5674 * openned
5675 */
5676 if (info->state & (1<<MD_SB_CLEAN))
5677 mddev->recovery_cp = MaxSector;
5678 else
5679 mddev->recovery_cp = 0;
5680 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005681 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
5683 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005684 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
5686 mddev->max_disks = MD_SB_DISKS;
5687
NeilBrowne6910632008-02-06 01:39:51 -08005688 if (mddev->persistent)
5689 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005690 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
NeilBrownc3d97142009-12-14 12:49:52 +11005692 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5693 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005694
NeilBrownf6705572006-03-27 01:18:11 -08005695 mddev->reshape_position = MaxSector;
5696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 /*
5698 * Generate a 128 bit UUID
5699 */
5700 get_random_bytes(mddev->uuid, 16);
5701
NeilBrownf6705572006-03-27 01:18:11 -08005702 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005703 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005704 mddev->new_layout = mddev->layout;
5705 mddev->delta_disks = 0;
5706
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 return 0;
5708}
5709
Dan Williams1f403622009-03-31 14:59:03 +11005710void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5711{
Dan Williamsb522adc2009-03-31 15:00:31 +11005712 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5713
5714 if (mddev->external_size)
5715 return;
5716
Dan Williams1f403622009-03-31 14:59:03 +11005717 mddev->array_sectors = array_sectors;
5718}
5719EXPORT_SYMBOL(md_set_array_sectors);
5720
Andre Nolld71f9f82008-07-11 22:02:22 +10005721static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005722{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005723 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005724 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005725 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005726
5727 if (mddev->pers->resize == NULL)
5728 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005729 /* The "num_sectors" is the number of sectors of each device that
5730 * is used. This can only make sense for arrays with redundancy.
5731 * linear and raid0 always use whatever space is available. We can only
5732 * consider changing this number if no resync or reconstruction is
5733 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005734 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005735 * of each device. If num_sectors is zero, we find the largest size
5736 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005737 */
5738 if (mddev->sync_thread)
5739 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005740 if (mddev->bitmap)
5741 /* Sorry, cannot grow a bitmap yet, just remove it,
5742 * grow, and re-add.
5743 */
5744 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005745 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005746 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005747
Andre Nolld71f9f82008-07-11 22:02:22 +10005748 if (fit && (num_sectors == 0 || num_sectors > avail))
5749 num_sectors = avail;
5750 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005751 return -ENOSPC;
5752 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005753 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005754 if (!rv)
5755 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005756 return rv;
5757}
5758
NeilBrownda943b92006-01-06 00:20:54 -08005759static int update_raid_disks(mddev_t *mddev, int raid_disks)
5760{
5761 int rv;
5762 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005763 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005764 return -EINVAL;
5765 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005766 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005767 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005768 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005769 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005770 mddev->delta_disks = raid_disks - mddev->raid_disks;
5771
5772 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005773 if (rv < 0)
5774 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005775 return rv;
5776}
5777
5778
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779/*
5780 * update_array_info is used to change the configuration of an
5781 * on-line array.
5782 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5783 * fields in the info are checked against the array.
5784 * Any differences that cannot be handled will cause an error.
5785 * Normally, only one change can be managed at a time.
5786 */
5787static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5788{
5789 int rv = 0;
5790 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005791 int state = 0;
5792
5793 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005794 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005795 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 if (mddev->major_version != info->major_version ||
5798 mddev->minor_version != info->minor_version ||
5799/* mddev->patch_version != info->patch_version || */
5800 mddev->ctime != info->ctime ||
5801 mddev->level != info->level ||
5802/* mddev->layout != info->layout || */
5803 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005804 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005805 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5806 ((state^info->state) & 0xfffffe00)
5807 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 return -EINVAL;
5809 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005810 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5811 cnt++;
5812 if (mddev->raid_disks != info->raid_disks)
5813 cnt++;
5814 if (mddev->layout != info->layout)
5815 cnt++;
5816 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5817 cnt++;
5818 if (cnt == 0)
5819 return 0;
5820 if (cnt > 1)
5821 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
5823 if (mddev->layout != info->layout) {
5824 /* Change layout
5825 * we don't need to do anything at the md level, the
5826 * personality will take care of it all.
5827 */
NeilBrown50ac1682009-06-18 08:47:55 +10005828 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005830 else {
5831 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005832 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005833 if (rv)
5834 mddev->new_layout = mddev->layout;
5835 return rv;
5836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005838 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005839 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
NeilBrownda943b92006-01-06 00:20:54 -08005841 if (mddev->raid_disks != info->raid_disks)
5842 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrown36fa3062005-09-09 16:23:45 -07005844 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5845 if (mddev->pers->quiesce == NULL)
5846 return -EINVAL;
5847 if (mddev->recovery || mddev->sync_thread)
5848 return -EBUSY;
5849 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5850 /* add the bitmap */
5851 if (mddev->bitmap)
5852 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005853 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005854 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005855 mddev->bitmap_info.offset =
5856 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005857 mddev->pers->quiesce(mddev, 1);
5858 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005859 if (!rv)
5860 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005861 if (rv)
5862 bitmap_destroy(mddev);
5863 mddev->pers->quiesce(mddev, 0);
5864 } else {
5865 /* remove the bitmap */
5866 if (!mddev->bitmap)
5867 return -ENOENT;
5868 if (mddev->bitmap->file)
5869 return -EINVAL;
5870 mddev->pers->quiesce(mddev, 1);
5871 bitmap_destroy(mddev);
5872 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005873 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005874 }
5875 }
NeilBrown850b2b42006-10-03 01:15:46 -07005876 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 return rv;
5878}
5879
5880static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5881{
5882 mdk_rdev_t *rdev;
5883
5884 if (mddev->pers == NULL)
5885 return -ENODEV;
5886
5887 rdev = find_rdev(mddev, dev);
5888 if (!rdev)
5889 return -ENODEV;
5890
5891 md_error(mddev, rdev);
5892 return 0;
5893}
5894
Andre Noll2f9618c2008-04-25 18:57:58 +02005895/*
5896 * We have a problem here : there is no easy way to give a CHS
5897 * virtual geometry. We currently pretend that we have a 2 heads
5898 * 4 sectors (with a BIG number of cylinders...). This drives
5899 * dosfs just mad... ;-)
5900 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005901static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5902{
5903 mddev_t *mddev = bdev->bd_disk->private_data;
5904
5905 geo->heads = 2;
5906 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005907 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005908 return 0;
5909}
5910
Al Viroa39907f2008-03-02 10:31:15 -05005911static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 unsigned int cmd, unsigned long arg)
5913{
5914 int err = 0;
5915 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005917 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 if (!capable(CAP_SYS_ADMIN))
5920 return -EACCES;
5921
5922 /*
5923 * Commands dealing with the RAID driver but not any
5924 * particular array:
5925 */
5926 switch (cmd)
5927 {
5928 case RAID_VERSION:
5929 err = get_version(argp);
5930 goto done;
5931
5932 case PRINT_RAID_DEBUG:
5933 err = 0;
5934 md_print_devices();
5935 goto done;
5936
5937#ifndef MODULE
5938 case RAID_AUTORUN:
5939 err = 0;
5940 autostart_arrays(arg);
5941 goto done;
5942#endif
5943 default:;
5944 }
5945
5946 /*
5947 * Commands creating/starting a new array:
5948 */
5949
Al Viroa39907f2008-03-02 10:31:15 -05005950 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
5952 if (!mddev) {
5953 BUG();
5954 goto abort;
5955 }
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 err = mddev_lock(mddev);
5958 if (err) {
5959 printk(KERN_INFO
5960 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5961 err, cmd);
5962 goto abort;
5963 }
5964
5965 switch (cmd)
5966 {
5967 case SET_ARRAY_INFO:
5968 {
5969 mdu_array_info_t info;
5970 if (!arg)
5971 memset(&info, 0, sizeof(info));
5972 else if (copy_from_user(&info, argp, sizeof(info))) {
5973 err = -EFAULT;
5974 goto abort_unlock;
5975 }
5976 if (mddev->pers) {
5977 err = update_array_info(mddev, &info);
5978 if (err) {
5979 printk(KERN_WARNING "md: couldn't update"
5980 " array info. %d\n", err);
5981 goto abort_unlock;
5982 }
5983 goto done_unlock;
5984 }
5985 if (!list_empty(&mddev->disks)) {
5986 printk(KERN_WARNING
5987 "md: array %s already has disks!\n",
5988 mdname(mddev));
5989 err = -EBUSY;
5990 goto abort_unlock;
5991 }
5992 if (mddev->raid_disks) {
5993 printk(KERN_WARNING
5994 "md: array %s already initialised!\n",
5995 mdname(mddev));
5996 err = -EBUSY;
5997 goto abort_unlock;
5998 }
5999 err = set_array_info(mddev, &info);
6000 if (err) {
6001 printk(KERN_WARNING "md: couldn't set"
6002 " array info. %d\n", err);
6003 goto abort_unlock;
6004 }
6005 }
6006 goto done_unlock;
6007
6008 default:;
6009 }
6010
6011 /*
6012 * Commands querying/configuring an existing array:
6013 */
NeilBrown32a76272005-06-21 17:17:14 -07006014 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006015 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006016 if ((!mddev->raid_disks && !mddev->external)
6017 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6018 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6019 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 err = -ENODEV;
6021 goto abort_unlock;
6022 }
6023
6024 /*
6025 * Commands even a read-only array can execute:
6026 */
6027 switch (cmd)
6028 {
6029 case GET_ARRAY_INFO:
6030 err = get_array_info(mddev, argp);
6031 goto done_unlock;
6032
NeilBrown32a76272005-06-21 17:17:14 -07006033 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006034 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006035 goto done_unlock;
6036
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 case GET_DISK_INFO:
6038 err = get_disk_info(mddev, argp);
6039 goto done_unlock;
6040
6041 case RESTART_ARRAY_RW:
6042 err = restart_array(mddev);
6043 goto done_unlock;
6044
6045 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006046 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 goto done_unlock;
6048
6049 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006050 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 goto done_unlock;
6052
Dan Williamse2218352010-05-12 08:25:37 +10006053 case BLKROSET:
6054 if (get_user(ro, (int __user *)(arg))) {
6055 err = -EFAULT;
6056 goto done_unlock;
6057 }
6058 err = -EINVAL;
6059
6060 /* if the bdev is going readonly the value of mddev->ro
6061 * does not matter, no writes are coming
6062 */
6063 if (ro)
6064 goto done_unlock;
6065
6066 /* are we are already prepared for writes? */
6067 if (mddev->ro != 1)
6068 goto done_unlock;
6069
6070 /* transitioning to readauto need only happen for
6071 * arrays that call md_write_start
6072 */
6073 if (mddev->pers) {
6074 err = restart_array(mddev);
6075 if (err == 0) {
6076 mddev->ro = 2;
6077 set_disk_ro(mddev->gendisk, 0);
6078 }
6079 }
6080 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 }
6082
6083 /*
6084 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006085 * superblock, so we do not allow them on read-only arrays.
6086 * However non-MD ioctls (e.g. get-size) will still come through
6087 * here and hit the 'default' below, so only disallow
6088 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006090 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006091 if (mddev->ro == 2) {
6092 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006093 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006094 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6095 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006096 } else {
6097 err = -EROFS;
6098 goto abort_unlock;
6099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 }
6101
6102 switch (cmd)
6103 {
6104 case ADD_NEW_DISK:
6105 {
6106 mdu_disk_info_t info;
6107 if (copy_from_user(&info, argp, sizeof(info)))
6108 err = -EFAULT;
6109 else
6110 err = add_new_disk(mddev, &info);
6111 goto done_unlock;
6112 }
6113
6114 case HOT_REMOVE_DISK:
6115 err = hot_remove_disk(mddev, new_decode_dev(arg));
6116 goto done_unlock;
6117
6118 case HOT_ADD_DISK:
6119 err = hot_add_disk(mddev, new_decode_dev(arg));
6120 goto done_unlock;
6121
6122 case SET_DISK_FAULTY:
6123 err = set_disk_faulty(mddev, new_decode_dev(arg));
6124 goto done_unlock;
6125
6126 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006127 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 goto done_unlock;
6129
NeilBrown32a76272005-06-21 17:17:14 -07006130 case SET_BITMAP_FILE:
6131 err = set_bitmap_file(mddev, (int)arg);
6132 goto done_unlock;
6133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 err = -EINVAL;
6136 goto abort_unlock;
6137 }
6138
6139done_unlock:
6140abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006141 if (mddev->hold_active == UNTIL_IOCTL &&
6142 err != -EINVAL)
6143 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 mddev_unlock(mddev);
6145
6146 return err;
6147done:
6148 if (err)
6149 MD_BUG();
6150abort:
6151 return err;
6152}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006153#ifdef CONFIG_COMPAT
6154static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6155 unsigned int cmd, unsigned long arg)
6156{
6157 switch (cmd) {
6158 case HOT_REMOVE_DISK:
6159 case HOT_ADD_DISK:
6160 case SET_DISK_FAULTY:
6161 case SET_BITMAP_FILE:
6162 /* These take in integer arg, do not convert */
6163 break;
6164 default:
6165 arg = (unsigned long)compat_ptr(arg);
6166 break;
6167 }
6168
6169 return md_ioctl(bdev, mode, cmd, arg);
6170}
6171#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
Al Viroa39907f2008-03-02 10:31:15 -05006173static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174{
6175 /*
6176 * Succeed if we can lock the mddev, which confirms that
6177 * it isn't being stopped right now.
6178 */
NeilBrownd3374822009-01-09 08:31:10 +11006179 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 int err;
6181
NeilBrownd3374822009-01-09 08:31:10 +11006182 if (mddev->gendisk != bdev->bd_disk) {
6183 /* we are racing with mddev_put which is discarding this
6184 * bd_disk.
6185 */
6186 mddev_put(mddev);
6187 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006188 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006189 /* Then retry the open from the top */
6190 return -ERESTARTSYS;
6191 }
6192 BUG_ON(mddev != bdev->bd_disk->private_data);
6193
NeilBrownc8c00a62009-08-10 12:50:52 +10006194 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 goto out;
6196
6197 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006198 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006199 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006201 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 out:
6203 return err;
6204}
6205
Al Viroa39907f2008-03-02 10:31:15 -05006206static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207{
Al Viroa39907f2008-03-02 10:31:15 -05006208 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006210 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006211 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 mddev_put(mddev);
6213
6214 return 0;
6215}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006216
6217static int md_media_changed(struct gendisk *disk)
6218{
6219 mddev_t *mddev = disk->private_data;
6220
6221 return mddev->changed;
6222}
6223
6224static int md_revalidate(struct gendisk *disk)
6225{
6226 mddev_t *mddev = disk->private_data;
6227
6228 mddev->changed = 0;
6229 return 0;
6230}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006231static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232{
6233 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006234 .open = md_open,
6235 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006236 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006237#ifdef CONFIG_COMPAT
6238 .compat_ioctl = md_compat_ioctl,
6239#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006240 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006241 .media_changed = md_media_changed,
6242 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243};
6244
Adrian Bunk75c96f82005-05-05 16:16:09 -07006245static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246{
6247 mdk_thread_t *thread = arg;
6248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 /*
6250 * md_thread is a 'system-thread', it's priority should be very
6251 * high. We avoid resource deadlocks individually in each
6252 * raid personality. (RAID5 does preallocation) We also use RR and
6253 * the very same RT priority as kswapd, thus we will never get
6254 * into a priority inversion deadlock.
6255 *
6256 * we definitely have to have equal or higher priority than
6257 * bdflush, otherwise bdflush will deadlock if there are too
6258 * many dirty RAID5 blocks.
6259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
NeilBrown6985c432005-10-19 21:23:47 -07006261 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006262 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
NeilBrown93588e22005-11-15 00:09:12 -08006264 /* We need to wait INTERRUPTIBLE so that
6265 * we don't add to the load-average.
6266 * That means we need to be sure no signals are
6267 * pending
6268 */
6269 if (signal_pending(current))
6270 flush_signals(current);
6271
6272 wait_event_interruptible_timeout
6273 (thread->wqueue,
6274 test_bit(THREAD_WAKEUP, &thread->flags)
6275 || kthread_should_stop(),
6276 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277
NeilBrown6c987912011-01-14 09:13:53 +11006278 clear_bit(THREAD_WAKEUP, &thread->flags);
6279 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006280 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006282
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 return 0;
6284}
6285
6286void md_wakeup_thread(mdk_thread_t *thread)
6287{
6288 if (thread) {
6289 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6290 set_bit(THREAD_WAKEUP, &thread->flags);
6291 wake_up(&thread->wqueue);
6292 }
6293}
6294
6295mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6296 const char *name)
6297{
6298 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
NeilBrown9ffae0c2006-01-06 00:20:32 -08006300 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 if (!thread)
6302 return NULL;
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 init_waitqueue_head(&thread->wqueue);
6305
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 thread->run = run;
6307 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006308 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006309 thread->tsk = kthread_run(md_thread, thread,
6310 "%s_%s",
6311 mdname(thread->mddev),
6312 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006313 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 kfree(thread);
6315 return NULL;
6316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 return thread;
6318}
6319
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320void md_unregister_thread(mdk_thread_t *thread)
6321{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006322 if (!thread)
6323 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006324 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006325
6326 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 kfree(thread);
6328}
6329
6330void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6331{
6332 if (!mddev) {
6333 MD_BUG();
6334 return;
6335 }
6336
NeilBrownb2d444d2005-11-08 21:39:31 -08006337 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006339
6340 if (mddev->external)
6341 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006342/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6344 mdname(mddev),
6345 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6346 __builtin_return_address(0),__builtin_return_address(1),
6347 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006348*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006349 if (!mddev->pers)
6350 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 if (!mddev->pers->error_handler)
6352 return;
6353 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006354 if (mddev->degraded)
6355 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006356 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6358 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6359 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006360 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006361 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006362 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363}
6364
6365/* seq_file implementation /proc/mdstat */
6366
6367static void status_unused(struct seq_file *seq)
6368{
6369 int i = 0;
6370 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 seq_printf(seq, "unused devices: ");
6373
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006374 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 char b[BDEVNAME_SIZE];
6376 i++;
6377 seq_printf(seq, "%s ",
6378 bdevname(rdev->bdev,b));
6379 }
6380 if (!i)
6381 seq_printf(seq, "<none>");
6382
6383 seq_printf(seq, "\n");
6384}
6385
6386
6387static void status_resync(struct seq_file *seq, mddev_t * mddev)
6388{
NeilBrowndd71cf62009-05-07 12:49:35 +10006389 sector_t max_sectors, resync, res;
6390 unsigned long dt, db;
6391 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006392 int scale;
6393 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
NeilBrowndd71cf62009-05-07 12:49:35 +10006395 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
6397 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006398 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006400 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401
6402 /*
6403 * Should not happen.
6404 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006405 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 MD_BUG();
6407 return;
6408 }
NeilBrown4588b422006-03-27 01:18:04 -08006409 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006410 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006411 * u32, as those are the requirements for sector_div.
6412 * Thus 'scale' must be at least 10
6413 */
6414 scale = 10;
6415 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006416 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006417 scale++;
6418 }
6419 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006420 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006421
6422 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 {
NeilBrown4588b422006-03-27 01:18:04 -08006424 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 seq_printf(seq, "[");
6426 for (i = 0; i < x; i++)
6427 seq_printf(seq, "=");
6428 seq_printf(seq, ">");
6429 for (i = 0; i < y; i++)
6430 seq_printf(seq, ".");
6431 seq_printf(seq, "] ");
6432 }
NeilBrown4588b422006-03-27 01:18:04 -08006433 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006434 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6435 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006436 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6437 "check" :
6438 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6439 "resync" : "recovery"))),
6440 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006441 (unsigned long long) resync/2,
6442 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443
6444 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 * dt: time from mark until now
6446 * db: blocks written from mark until now
6447 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006448 *
6449 * rt is a sector_t, so could be 32bit or 64bit.
6450 * So we divide before multiply in case it is 32bit and close
6451 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006452 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006453 * near the end of resync when the number of remaining sectors
6454 * is close to 'db'.
6455 * We then divide rt by 32 after multiplying by db to compensate.
6456 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 */
6458 dt = ((jiffies - mddev->resync_mark) / HZ);
6459 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006460 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6461 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462
NeilBrowndd71cf62009-05-07 12:49:35 +10006463 rt = max_sectors - resync; /* number of remaining sectors */
6464 sector_div(rt, db/32+1);
6465 rt *= dt;
6466 rt >>= 5;
6467
6468 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6469 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470
NeilBrownff4e8d92006-07-10 04:44:16 -07006471 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472}
6473
6474static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6475{
6476 struct list_head *tmp;
6477 loff_t l = *pos;
6478 mddev_t *mddev;
6479
6480 if (l >= 0x10000)
6481 return NULL;
6482 if (!l--)
6483 /* header */
6484 return (void*)1;
6485
6486 spin_lock(&all_mddevs_lock);
6487 list_for_each(tmp,&all_mddevs)
6488 if (!l--) {
6489 mddev = list_entry(tmp, mddev_t, all_mddevs);
6490 mddev_get(mddev);
6491 spin_unlock(&all_mddevs_lock);
6492 return mddev;
6493 }
6494 spin_unlock(&all_mddevs_lock);
6495 if (!l--)
6496 return (void*)2;/* tail */
6497 return NULL;
6498}
6499
6500static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6501{
6502 struct list_head *tmp;
6503 mddev_t *next_mddev, *mddev = v;
6504
6505 ++*pos;
6506 if (v == (void*)2)
6507 return NULL;
6508
6509 spin_lock(&all_mddevs_lock);
6510 if (v == (void*)1)
6511 tmp = all_mddevs.next;
6512 else
6513 tmp = mddev->all_mddevs.next;
6514 if (tmp != &all_mddevs)
6515 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6516 else {
6517 next_mddev = (void*)2;
6518 *pos = 0x10000;
6519 }
6520 spin_unlock(&all_mddevs_lock);
6521
6522 if (v != (void*)1)
6523 mddev_put(mddev);
6524 return next_mddev;
6525
6526}
6527
6528static void md_seq_stop(struct seq_file *seq, void *v)
6529{
6530 mddev_t *mddev = v;
6531
6532 if (mddev && v != (void*)1 && v != (void*)2)
6533 mddev_put(mddev);
6534}
6535
NeilBrownd7603b72006-01-06 00:20:30 -08006536struct mdstat_info {
6537 int event;
6538};
6539
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540static int md_seq_show(struct seq_file *seq, void *v)
6541{
6542 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006543 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006545 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006546 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
6548 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006549 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 seq_printf(seq, "Personalities : ");
6551 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006552 list_for_each_entry(pers, &pers_list, list)
6553 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
6555 spin_unlock(&pers_lock);
6556 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006557 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 return 0;
6559 }
6560 if (v == (void*)2) {
6561 status_unused(seq);
6562 return 0;
6563 }
6564
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006565 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006567
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6569 seq_printf(seq, "%s : %sactive", mdname(mddev),
6570 mddev->pers ? "" : "in");
6571 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006572 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006574 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006575 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 seq_printf(seq, " %s", mddev->pers->name);
6577 }
6578
Andre Nolldd8ac332009-03-31 14:33:13 +11006579 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006580 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 char b[BDEVNAME_SIZE];
6582 seq_printf(seq, " %s[%d]",
6583 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006584 if (test_bit(WriteMostly, &rdev->flags))
6585 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006586 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 seq_printf(seq, "(F)");
6588 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006589 } else if (rdev->raid_disk < 0)
6590 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006591 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 }
6593
6594 if (!list_empty(&mddev->disks)) {
6595 if (mddev->pers)
6596 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006597 (unsigned long long)
6598 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 else
6600 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006601 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006603 if (mddev->persistent) {
6604 if (mddev->major_version != 0 ||
6605 mddev->minor_version != 90) {
6606 seq_printf(seq," super %d.%d",
6607 mddev->major_version,
6608 mddev->minor_version);
6609 }
NeilBrowne6910632008-02-06 01:39:51 -08006610 } else if (mddev->external)
6611 seq_printf(seq, " super external:%s",
6612 mddev->metadata_type);
6613 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006614 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
6616 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006617 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006619 if (mddev->pers->sync_request) {
6620 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006621 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006622 seq_printf(seq, "\n ");
6623 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6624 seq_printf(seq, "\tresync=DELAYED\n ");
6625 else if (mddev->recovery_cp < MaxSector)
6626 seq_printf(seq, "\tresync=PENDING\n ");
6627 }
NeilBrown32a76272005-06-21 17:17:14 -07006628 } else
6629 seq_printf(seq, "\n ");
6630
6631 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006632 unsigned long chunk_kb;
6633 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006634 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006635 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006636 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6637 "%lu%s chunk",
6638 bitmap->pages - bitmap->missing_pages,
6639 bitmap->pages,
6640 (bitmap->pages - bitmap->missing_pages)
6641 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006642 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006643 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006644 if (bitmap->file) {
6645 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006646 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006647 }
NeilBrown78d742d2005-06-21 17:17:15 -07006648
NeilBrown32a76272005-06-21 17:17:14 -07006649 seq_printf(seq, "\n");
6650 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 }
6652
6653 seq_printf(seq, "\n");
6654 }
6655 mddev_unlock(mddev);
6656
6657 return 0;
6658}
6659
Jan Engelhardt110518b2009-05-07 12:49:37 +10006660static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 .start = md_seq_start,
6662 .next = md_seq_next,
6663 .stop = md_seq_stop,
6664 .show = md_seq_show,
6665};
6666
6667static int md_seq_open(struct inode *inode, struct file *file)
6668{
6669 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006670 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6671 if (mi == NULL)
6672 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673
6674 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006675 if (error)
6676 kfree(mi);
6677 else {
6678 struct seq_file *p = file->private_data;
6679 p->private = mi;
6680 mi->event = atomic_read(&md_event_count);
6681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 return error;
6683}
6684
NeilBrownd7603b72006-01-06 00:20:30 -08006685static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6686{
6687 struct seq_file *m = filp->private_data;
6688 struct mdstat_info *mi = m->private;
6689 int mask;
6690
6691 poll_wait(filp, &md_event_waiters, wait);
6692
6693 /* always allow read */
6694 mask = POLLIN | POLLRDNORM;
6695
6696 if (mi->event != atomic_read(&md_event_count))
6697 mask |= POLLERR | POLLPRI;
6698 return mask;
6699}
6700
Arjan van de Venfa027c22007-02-12 00:55:33 -08006701static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006702 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 .open = md_seq_open,
6704 .read = seq_read,
6705 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006706 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006707 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708};
6709
NeilBrown2604b702006-01-06 00:20:36 -08006710int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006713 list_add_tail(&p->list, &pers_list);
6714 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 spin_unlock(&pers_lock);
6716 return 0;
6717}
6718
NeilBrown2604b702006-01-06 00:20:36 -08006719int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720{
NeilBrown2604b702006-01-06 00:20:36 -08006721 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006723 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 spin_unlock(&pers_lock);
6725 return 0;
6726}
6727
NeilBrowneea1bf32009-03-31 14:27:02 +11006728static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729{
6730 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006732 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
6734 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006735 rcu_read_lock();
6736 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006738 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6739 (int)part_stat_read(&disk->part0, sectors[1]) -
6740 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006741 /* sync IO will cause sync_io to increase before the disk_stats
6742 * as sync_io is counted when a request starts, and
6743 * disk_stats is counted when it completes.
6744 * So resync activity will cause curr_events to be smaller than
6745 * when there was no such activity.
6746 * non-sync IO will cause disk_stat to increase without
6747 * increasing sync_io so curr_events will (eventually)
6748 * be larger than it was before. Once it becomes
6749 * substantially larger, the test below will cause
6750 * the array to appear non-idle, and resync will slow
6751 * down.
6752 * If there is a lot of outstanding resync activity when
6753 * we set last_event to curr_events, then all that activity
6754 * completing might cause the array to appear non-idle
6755 * and resync will be slowed down even though there might
6756 * not have been non-resync activity. This will only
6757 * happen once though. 'last_events' will soon reflect
6758 * the state where there is little or no outstanding
6759 * resync requests, and further resync activity will
6760 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006763 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 rdev->last_events = curr_events;
6765 idle = 0;
6766 }
6767 }
NeilBrown4b809912008-07-21 17:05:25 +10006768 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 return idle;
6770}
6771
6772void md_done_sync(mddev_t *mddev, int blocks, int ok)
6773{
6774 /* another "blocks" (512byte) blocks have been synced */
6775 atomic_sub(blocks, &mddev->recovery_active);
6776 wake_up(&mddev->recovery_wait);
6777 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006778 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 md_wakeup_thread(mddev->thread);
6780 // stop recovery, signal do_sync ....
6781 }
6782}
6783
6784
NeilBrown06d91a52005-06-21 17:17:12 -07006785/* md_write_start(mddev, bi)
6786 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006787 * in superblock) before writing, schedule a superblock update
6788 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006789 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006790void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791{
Neil Brown0fd62b82008-06-28 08:31:36 +10006792 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006793 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006794 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006795
NeilBrownf91de922005-11-08 21:39:36 -08006796 BUG_ON(mddev->ro == 1);
6797 if (mddev->ro == 2) {
6798 /* need to switch to read/write */
6799 mddev->ro = 0;
6800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6801 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006802 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006803 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006804 }
NeilBrown06d91a52005-06-21 17:17:12 -07006805 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006806 if (mddev->safemode == 1)
6807 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006808 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006809 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006810 if (mddev->in_sync) {
6811 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006812 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006813 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006814 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006815 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006816 }
NeilBrowna9701a32005-11-08 21:39:34 -08006817 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006818 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006819 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006820 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006821 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006822 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823}
6824
6825void md_write_end(mddev_t *mddev)
6826{
6827 if (atomic_dec_and_test(&mddev->writes_pending)) {
6828 if (mddev->safemode == 2)
6829 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006830 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6832 }
6833}
6834
NeilBrown2a2275d2007-01-26 00:57:11 -08006835/* md_allow_write(mddev)
6836 * Calling this ensures that the array is marked 'active' so that writes
6837 * may proceed without blocking. It is important to call this before
6838 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6839 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006840 *
6841 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6842 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006843 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006844int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006845{
6846 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006847 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006848 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006849 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006850 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006851 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006852
6853 spin_lock_irq(&mddev->write_lock);
6854 if (mddev->in_sync) {
6855 mddev->in_sync = 0;
6856 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006857 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006858 if (mddev->safemode_delay &&
6859 mddev->safemode == 0)
6860 mddev->safemode = 1;
6861 spin_unlock_irq(&mddev->write_lock);
6862 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006863 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006864 } else
6865 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006866
NeilBrown070dc6d2010-08-30 17:33:34 +10006867 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006868 return -EAGAIN;
6869 else
6870 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006871}
6872EXPORT_SYMBOL_GPL(md_allow_write);
6873
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874#define SYNC_MARKS 10
6875#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006876void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877{
6878 mddev_t *mddev2;
6879 unsigned int currspeed = 0,
6880 window;
NeilBrown57afd892005-06-21 17:17:13 -07006881 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 unsigned long mark[SYNC_MARKS];
6883 sector_t mark_cnt[SYNC_MARKS];
6884 int last_mark,m;
6885 struct list_head *tmp;
6886 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006887 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006888 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006889 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
6891 /* just incase thread restarts... */
6892 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6893 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006894 if (mddev->ro) /* never try to sync a read-only array */
6895 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
NeilBrown61df9d92006-10-03 01:15:57 -07006897 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6898 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6899 desc = "data-check";
6900 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6901 desc = "requested-resync";
6902 else
6903 desc = "resync";
6904 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6905 desc = "reshape";
6906 else
6907 desc = "recovery";
6908
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 /* we overload curr_resync somewhat here.
6910 * 0 == not engaged in resync at all
6911 * 2 == checking that there is no conflict with another sync
6912 * 1 == like 2, but have yielded to allow conflicting resync to
6913 * commense
6914 * other == active in resync - this many blocks
6915 *
6916 * Before starting a resync we must have set curr_resync to
6917 * 2, and then checked that every "conflicting" array has curr_resync
6918 * less than ours. When we find one that is the same or higher
6919 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6920 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6921 * This will mean we have to start checking from the beginning again.
6922 *
6923 */
6924
6925 do {
6926 mddev->curr_resync = 2;
6927
6928 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006929 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006930 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006931
6932 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006934 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 if (mddev2 == mddev)
6936 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006937 if (!mddev->parallel_resync
6938 && mddev2->curr_resync
6939 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 DEFINE_WAIT(wq);
6941 if (mddev < mddev2 && mddev->curr_resync == 2) {
6942 /* arbitrarily yield */
6943 mddev->curr_resync = 1;
6944 wake_up(&resync_wait);
6945 }
6946 if (mddev > mddev2 && mddev->curr_resync == 1)
6947 /* no need to wait here, we can wait the next
6948 * time 'round when curr_resync == 2
6949 */
6950 continue;
NeilBrown97441972008-09-19 11:49:54 +10006951 /* We need to wait 'interruptible' so as not to
6952 * contribute to the load average, and not to
6953 * be caught by 'softlockup'
6954 */
6955 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006956 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006957 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006958 printk(KERN_INFO "md: delaying %s of %s"
6959 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006961 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006963 if (signal_pending(current))
6964 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 schedule();
6966 finish_wait(&resync_wait, &wq);
6967 goto try_again;
6968 }
6969 finish_wait(&resync_wait, &wq);
6970 }
6971 }
6972 } while (mddev->curr_resync < 2);
6973
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006974 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006975 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006977 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 */
6979 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006980 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006981 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006982 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6983 j = mddev->resync_min;
6984 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006985 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006986
NeilBrownccfcc3c2006-03-27 01:18:09 -08006987 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006988 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006989 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006991 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006992 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006993 rcu_read_lock();
6994 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006995 if (rdev->raid_disk >= 0 &&
6996 !test_bit(Faulty, &rdev->flags) &&
6997 !test_bit(In_sync, &rdev->flags) &&
6998 rdev->recovery_offset < j)
6999 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007000 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
NeilBrown61df9d92006-10-03 01:15:57 -07007003 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7004 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7005 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007006 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007007 "(but not more than %d KB/sec) for %s.\n",
7008 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009
NeilBrowneea1bf32009-03-31 14:27:02 +11007010 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007011
NeilBrown57afd892005-06-21 17:17:13 -07007012 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 for (m = 0; m < SYNC_MARKS; m++) {
7014 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007015 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 }
7017 last_mark = 0;
7018 mddev->resync_mark = mark[last_mark];
7019 mddev->resync_mark_cnt = mark_cnt[last_mark];
7020
7021 /*
7022 * Tune reconstruction:
7023 */
7024 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007025 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7026 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
7028 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 last_check = 0;
7030
7031 if (j>2) {
7032 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007033 "md: resuming %s of %s from checkpoint.\n",
7034 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 mddev->curr_resync = j;
7036 }
NeilBrown75d3da42011-01-14 09:14:34 +11007037 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
7039 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007040 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041
NeilBrown57afd892005-06-21 17:17:13 -07007042 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007043
NeilBrown7a91ee12009-05-26 12:57:21 +10007044 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7045 ((mddev->curr_resync > mddev->curr_resync_completed &&
7046 (mddev->curr_resync - mddev->curr_resync_completed)
7047 > (max_sectors >> 4)) ||
7048 (j - mddev->curr_resync_completed)*2
7049 >= mddev->resync_max - mddev->curr_resync_completed
7050 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007051 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007052 wait_event(mddev->recovery_wait,
7053 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007054 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007055 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007056 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007057 }
NeilBrownacb180b2009-04-14 16:28:34 +10007058
NeilBrowne62e58a2009-07-01 13:15:35 +10007059 while (j >= mddev->resync_max && !kthread_should_stop()) {
7060 /* As this condition is controlled by user-space,
7061 * we can block indefinitely, so use '_interruptible'
7062 * to avoid triggering warnings.
7063 */
7064 flush_signals(current); /* just in case */
7065 wait_event_interruptible(mddev->recovery_wait,
7066 mddev->resync_max > j
7067 || kthread_should_stop());
7068 }
NeilBrownacb180b2009-04-14 16:28:34 +10007069
7070 if (kthread_should_stop())
7071 goto interrupted;
7072
NeilBrown57afd892005-06-21 17:17:13 -07007073 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007074 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007075 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007076 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 goto out;
7078 }
NeilBrown57afd892005-06-21 17:17:13 -07007079
7080 if (!skipped) { /* actual IO requested */
7081 io_sectors += sectors;
7082 atomic_add(sectors, &mddev->recovery_active);
7083 }
7084
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 j += sectors;
7086 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007087 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007088 if (last_check == 0)
7089 /* this is the earliers that rebuilt will be
7090 * visible in /proc/mdstat
7091 */
7092 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007093
7094 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 continue;
7096
NeilBrown57afd892005-06-21 17:17:13 -07007097 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098
NeilBrowndfc70642008-05-23 13:04:39 -07007099 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 break;
7101
7102 repeat:
7103 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7104 /* step marks */
7105 int next = (last_mark+1) % SYNC_MARKS;
7106
7107 mddev->resync_mark = mark[next];
7108 mddev->resync_mark_cnt = mark_cnt[next];
7109 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007110 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 last_mark = next;
7112 }
7113
7114
NeilBrownc6207272008-02-06 01:39:52 -08007115 if (kthread_should_stop())
7116 goto interrupted;
7117
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118
7119 /*
7120 * this loop exits only if either when we are slower than
7121 * the 'hard' speed limit, or the system was IO-idle for
7122 * a jiffy.
7123 * the system might be non-idle CPU-wise, but we only care
7124 * about not overloading the IO subsystem. (things like an
7125 * e2fsck being done on the RAID array should execute fast)
7126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 cond_resched();
7128
NeilBrown57afd892005-06-21 17:17:13 -07007129 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7130 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
NeilBrown88202a02006-01-06 00:21:36 -08007132 if (currspeed > speed_min(mddev)) {
7133 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007134 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007135 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 goto repeat;
7137 }
7138 }
7139 }
NeilBrown61df9d92006-10-03 01:15:57 -07007140 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 /*
7142 * this also signals 'finished resyncing' to md_stop
7143 */
7144 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7146
7147 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007148 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
NeilBrowndfc70642008-05-23 13:04:39 -07007150 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007151 mddev->curr_resync > 2) {
7152 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7153 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7154 if (mddev->curr_resync >= mddev->recovery_cp) {
7155 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007156 "md: checkpointing %s of %s.\n",
7157 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007158 mddev->recovery_cp = mddev->curr_resync;
7159 }
7160 } else
7161 mddev->recovery_cp = MaxSector;
7162 } else {
7163 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7164 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007165 rcu_read_lock();
7166 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007167 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007168 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007169 !test_bit(Faulty, &rdev->flags) &&
7170 !test_bit(In_sync, &rdev->flags) &&
7171 rdev->recovery_offset < mddev->curr_resync)
7172 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007173 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 }
NeilBrown17571282006-12-10 02:20:52 -08007176 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007179 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7180 /* We completed so min/max setting can be forgotten if used. */
7181 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7182 mddev->resync_min = 0;
7183 mddev->resync_max = MaxSector;
7184 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7185 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 mddev->curr_resync = 0;
7187 wake_up(&resync_wait);
7188 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7189 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007190 return;
7191
7192 interrupted:
7193 /*
7194 * got a signal, exit.
7195 */
7196 printk(KERN_INFO
7197 "md: md_do_sync() got signal ... exiting\n");
7198 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7199 goto out;
7200
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201}
NeilBrown29269552006-03-27 01:18:10 -08007202EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007204static int remove_and_add_spares(mddev_t *mddev)
7205{
7206 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007207 int spares = 0;
7208
NeilBrown97e4f422009-03-31 14:33:13 +11007209 mddev->curr_resync_completed = 0;
7210
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007211 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007212 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007213 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007214 (test_bit(Faulty, &rdev->flags) ||
7215 ! test_bit(In_sync, &rdev->flags)) &&
7216 atomic_read(&rdev->nr_pending)==0) {
7217 if (mddev->pers->hot_remove_disk(
7218 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007219 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007220 rdev->raid_disk = -1;
7221 }
7222 }
7223
NeilBrown53890422011-07-27 11:00:36 +10007224 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007225 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007226 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007227 !test_bit(In_sync, &rdev->flags) &&
NeilBrown42742152011-06-28 16:59:42 +10007228 !test_bit(Faulty, &rdev->flags) &&
Dan Williamse5427132008-07-28 23:28:06 -07007229 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007230 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007231 if (rdev->raid_disk < 0
7232 && !test_bit(Faulty, &rdev->flags)) {
7233 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007234 if (mddev->pers->
7235 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007236 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007237 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007238 spares++;
7239 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007240 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007241 } else
7242 break;
7243 }
NeilBrowndfc70642008-05-23 13:04:39 -07007244 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007245 }
7246 return spares;
7247}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007248
7249static void reap_sync_thread(mddev_t *mddev)
7250{
7251 mdk_rdev_t *rdev;
7252
7253 /* resync has finished, collect result */
7254 md_unregister_thread(mddev->sync_thread);
7255 mddev->sync_thread = NULL;
7256 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7257 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7258 /* success...*/
7259 /* activate any spares */
7260 if (mddev->pers->spare_active(mddev))
7261 sysfs_notify(&mddev->kobj, NULL,
7262 "degraded");
7263 }
7264 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7265 mddev->pers->finish_reshape)
7266 mddev->pers->finish_reshape(mddev);
7267 md_update_sb(mddev, 1);
7268
7269 /* if array is no-longer degraded, then any saved_raid_disk
7270 * information must be scrapped
7271 */
7272 if (!mddev->degraded)
7273 list_for_each_entry(rdev, &mddev->disks, same_set)
7274 rdev->saved_raid_disk = -1;
7275
7276 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7277 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7278 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7279 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7280 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7281 /* flag recovery needed just to double check */
7282 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7283 sysfs_notify_dirent_safe(mddev->sysfs_action);
7284 md_new_event(mddev);
Jonathan Brassow768e5872011-07-27 11:00:37 +10007285 if (mddev->event_work.func)
7286 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007287}
7288
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289/*
7290 * This routine is regularly called by all per-raid-array threads to
7291 * deal with generic issues like resync and super-block update.
7292 * Raid personalities that don't have a thread (linear/raid0) do not
7293 * need this as they never do any recovery or update the superblock.
7294 *
7295 * It does not do any resync itself, but rather "forks" off other threads
7296 * to do that as needed.
7297 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7298 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007299 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 * and wakeups up this thread which will reap the thread and finish up.
7301 * This thread also removes any faulty devices (with nr_pending == 0).
7302 *
7303 * The overall approach is:
7304 * 1/ if the superblock needs updating, update it.
7305 * 2/ If a recovery thread is running, don't do anything else.
7306 * 3/ If recovery has finished, clean up, possibly marking spares active.
7307 * 4/ If there are any faulty devices, remove them.
7308 * 5/ If array is degraded, try to add spares devices
7309 * 6/ If array has spares or is not in-sync, start a resync thread.
7310 */
7311void md_check_recovery(mddev_t *mddev)
7312{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007313 if (mddev->suspended)
7314 return;
7315
NeilBrown5f404022005-06-21 17:17:16 -07007316 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007317 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318
NeilBrownfca4d842005-06-21 17:17:11 -07007319 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007320 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007321 printk(KERN_INFO "md: %s in immediate safe mode\n",
7322 mdname(mddev));
7323 mddev->safemode = 2;
7324 }
7325 flush_signals(current);
7326 }
7327
NeilBrownc89a8ee2008-08-05 15:54:13 +10007328 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007331 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007333 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007334 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007335 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7336 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 ))
7338 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007339
NeilBrowndf5b89b2006-03-27 01:18:20 -08007340 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007341 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007342
NeilBrownc89a8ee2008-08-05 15:54:13 +10007343 if (mddev->ro) {
7344 /* Only thing we do on a ro array is remove
7345 * failed devices.
7346 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007347 mdk_rdev_t *rdev;
7348 list_for_each_entry(rdev, &mddev->disks, same_set)
7349 if (rdev->raid_disk >= 0 &&
7350 !test_bit(Blocked, &rdev->flags) &&
7351 test_bit(Faulty, &rdev->flags) &&
7352 atomic_read(&rdev->nr_pending)==0) {
7353 if (mddev->pers->hot_remove_disk(
7354 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007355 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007356 rdev->raid_disk = -1;
7357 }
7358 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007359 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7360 goto unlock;
7361 }
7362
NeilBrown31a59e32008-04-30 00:52:30 -07007363 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007364 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007365 spin_lock_irq(&mddev->write_lock);
7366 if (mddev->safemode &&
7367 !atomic_read(&mddev->writes_pending) &&
7368 !mddev->in_sync &&
7369 mddev->recovery_cp == MaxSector) {
7370 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007371 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007372 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007373 }
7374 if (mddev->safemode == 1)
7375 mddev->safemode = 0;
7376 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007377 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007378 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007379 }
NeilBrownfca4d842005-06-21 17:17:11 -07007380
NeilBrown850b2b42006-10-03 01:15:46 -07007381 if (mddev->flags)
7382 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007383
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7385 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7386 /* resync/recovery still happening */
7387 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7388 goto unlock;
7389 }
7390 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007391 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 goto unlock;
7393 }
Neil Brown72a23c22008-06-28 08:31:41 +10007394 /* Set RUNNING before clearing NEEDED to avoid
7395 * any transients in the value of "sync_action".
7396 */
7397 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7398 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007399 /* Clear some bits that don't mean anything, but
7400 * might be left set
7401 */
NeilBrown24dd4692005-11-08 21:39:26 -08007402 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7403 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007405 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7406 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 /* no recovery is running.
7408 * remove any failed drives, then
7409 * add spares if possible.
7410 * Spare are also removed and re-added, to allow
7411 * the personality to fail the re-add.
7412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007414 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007415 if (mddev->pers->check_reshape == NULL ||
7416 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007417 /* Cannot proceed */
7418 goto unlock;
7419 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007420 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007421 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007422 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7423 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007424 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007425 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007426 } else if (mddev->recovery_cp < MaxSector) {
7427 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007428 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007429 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7430 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007432
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007434 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7435 /* We are adding a device or devices to an array
7436 * which has the bitmap stored on all devices.
7437 * So make sure all bitmap pages get written
7438 */
7439 bitmap_write_all(mddev->bitmap);
7440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 mddev->sync_thread = md_register_thread(md_do_sync,
7442 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007443 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 if (!mddev->sync_thread) {
7445 printk(KERN_ERR "%s: could not start resync"
7446 " thread...\n",
7447 mdname(mddev));
7448 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007449 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7450 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7451 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7452 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7453 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007454 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007456 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007457 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 }
7459 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007460 if (!mddev->sync_thread) {
7461 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7462 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7463 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007464 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007465 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 mddev_unlock(mddev);
7468 }
7469}
7470
Dan Williams6bfe0b42008-04-30 00:52:32 -07007471void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7472{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007473 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007474 wait_event_timeout(rdev->blocked_wait,
7475 !test_bit(Blocked, &rdev->flags),
7476 msecs_to_jiffies(5000));
7477 rdev_dec_pending(rdev, mddev);
7478}
7479EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7480
NeilBrown2230dfe2011-07-28 11:31:46 +10007481
7482/* Bad block management.
7483 * We can record which blocks on each device are 'bad' and so just
7484 * fail those blocks, or that stripe, rather than the whole device.
7485 * Entries in the bad-block table are 64bits wide. This comprises:
7486 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7487 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7488 * A 'shift' can be set so that larger blocks are tracked and
7489 * consequently larger devices can be covered.
7490 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7491 *
7492 * Locking of the bad-block table uses a seqlock so md_is_badblock
7493 * might need to retry if it is very unlucky.
7494 * We will sometimes want to check for bad blocks in a bi_end_io function,
7495 * so we use the write_seqlock_irq variant.
7496 *
7497 * When looking for a bad block we specify a range and want to
7498 * know if any block in the range is bad. So we binary-search
7499 * to the last range that starts at-or-before the given endpoint,
7500 * (or "before the sector after the target range")
7501 * then see if it ends after the given start.
7502 * We return
7503 * 0 if there are no known bad blocks in the range
7504 * 1 if there are known bad block which are all acknowledged
7505 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7506 * plus the start/length of the first bad section we overlap.
7507 */
7508int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7509 sector_t *first_bad, int *bad_sectors)
7510{
7511 int hi;
7512 int lo = 0;
7513 u64 *p = bb->page;
7514 int rv = 0;
7515 sector_t target = s + sectors;
7516 unsigned seq;
7517
7518 if (bb->shift > 0) {
7519 /* round the start down, and the end up */
7520 s >>= bb->shift;
7521 target += (1<<bb->shift) - 1;
7522 target >>= bb->shift;
7523 sectors = target - s;
7524 }
7525 /* 'target' is now the first block after the bad range */
7526
7527retry:
7528 seq = read_seqbegin(&bb->lock);
7529
7530 hi = bb->count;
7531
7532 /* Binary search between lo and hi for 'target'
7533 * i.e. for the last range that starts before 'target'
7534 */
7535 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7536 * are known not to be the last range before target.
7537 * VARIANT: hi-lo is the number of possible
7538 * ranges, and decreases until it reaches 1
7539 */
7540 while (hi - lo > 1) {
7541 int mid = (lo + hi) / 2;
7542 sector_t a = BB_OFFSET(p[mid]);
7543 if (a < target)
7544 /* This could still be the one, earlier ranges
7545 * could not. */
7546 lo = mid;
7547 else
7548 /* This and later ranges are definitely out. */
7549 hi = mid;
7550 }
7551 /* 'lo' might be the last that started before target, but 'hi' isn't */
7552 if (hi > lo) {
7553 /* need to check all range that end after 's' to see if
7554 * any are unacknowledged.
7555 */
7556 while (lo >= 0 &&
7557 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7558 if (BB_OFFSET(p[lo]) < target) {
7559 /* starts before the end, and finishes after
7560 * the start, so they must overlap
7561 */
7562 if (rv != -1 && BB_ACK(p[lo]))
7563 rv = 1;
7564 else
7565 rv = -1;
7566 *first_bad = BB_OFFSET(p[lo]);
7567 *bad_sectors = BB_LEN(p[lo]);
7568 }
7569 lo--;
7570 }
7571 }
7572
7573 if (read_seqretry(&bb->lock, seq))
7574 goto retry;
7575
7576 return rv;
7577}
7578EXPORT_SYMBOL_GPL(md_is_badblock);
7579
7580/*
7581 * Add a range of bad blocks to the table.
7582 * This might extend the table, or might contract it
7583 * if two adjacent ranges can be merged.
7584 * We binary-search to find the 'insertion' point, then
7585 * decide how best to handle it.
7586 */
7587static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7588 int acknowledged)
7589{
7590 u64 *p;
7591 int lo, hi;
7592 int rv = 1;
7593
7594 if (bb->shift < 0)
7595 /* badblocks are disabled */
7596 return 0;
7597
7598 if (bb->shift) {
7599 /* round the start down, and the end up */
7600 sector_t next = s + sectors;
7601 s >>= bb->shift;
7602 next += (1<<bb->shift) - 1;
7603 next >>= bb->shift;
7604 sectors = next - s;
7605 }
7606
7607 write_seqlock_irq(&bb->lock);
7608
7609 p = bb->page;
7610 lo = 0;
7611 hi = bb->count;
7612 /* Find the last range that starts at-or-before 's' */
7613 while (hi - lo > 1) {
7614 int mid = (lo + hi) / 2;
7615 sector_t a = BB_OFFSET(p[mid]);
7616 if (a <= s)
7617 lo = mid;
7618 else
7619 hi = mid;
7620 }
7621 if (hi > lo && BB_OFFSET(p[lo]) > s)
7622 hi = lo;
7623
7624 if (hi > lo) {
7625 /* we found a range that might merge with the start
7626 * of our new range
7627 */
7628 sector_t a = BB_OFFSET(p[lo]);
7629 sector_t e = a + BB_LEN(p[lo]);
7630 int ack = BB_ACK(p[lo]);
7631 if (e >= s) {
7632 /* Yes, we can merge with a previous range */
7633 if (s == a && s + sectors >= e)
7634 /* new range covers old */
7635 ack = acknowledged;
7636 else
7637 ack = ack && acknowledged;
7638
7639 if (e < s + sectors)
7640 e = s + sectors;
7641 if (e - a <= BB_MAX_LEN) {
7642 p[lo] = BB_MAKE(a, e-a, ack);
7643 s = e;
7644 } else {
7645 /* does not all fit in one range,
7646 * make p[lo] maximal
7647 */
7648 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7649 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7650 s = a + BB_MAX_LEN;
7651 }
7652 sectors = e - s;
7653 }
7654 }
7655 if (sectors && hi < bb->count) {
7656 /* 'hi' points to the first range that starts after 's'.
7657 * Maybe we can merge with the start of that range */
7658 sector_t a = BB_OFFSET(p[hi]);
7659 sector_t e = a + BB_LEN(p[hi]);
7660 int ack = BB_ACK(p[hi]);
7661 if (a <= s + sectors) {
7662 /* merging is possible */
7663 if (e <= s + sectors) {
7664 /* full overlap */
7665 e = s + sectors;
7666 ack = acknowledged;
7667 } else
7668 ack = ack && acknowledged;
7669
7670 a = s;
7671 if (e - a <= BB_MAX_LEN) {
7672 p[hi] = BB_MAKE(a, e-a, ack);
7673 s = e;
7674 } else {
7675 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7676 s = a + BB_MAX_LEN;
7677 }
7678 sectors = e - s;
7679 lo = hi;
7680 hi++;
7681 }
7682 }
7683 if (sectors == 0 && hi < bb->count) {
7684 /* we might be able to combine lo and hi */
7685 /* Note: 's' is at the end of 'lo' */
7686 sector_t a = BB_OFFSET(p[hi]);
7687 int lolen = BB_LEN(p[lo]);
7688 int hilen = BB_LEN(p[hi]);
7689 int newlen = lolen + hilen - (s - a);
7690 if (s >= a && newlen < BB_MAX_LEN) {
7691 /* yes, we can combine them */
7692 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7693 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7694 memmove(p + hi, p + hi + 1,
7695 (bb->count - hi - 1) * 8);
7696 bb->count--;
7697 }
7698 }
7699 while (sectors) {
7700 /* didn't merge (it all).
7701 * Need to add a range just before 'hi' */
7702 if (bb->count >= MD_MAX_BADBLOCKS) {
7703 /* No room for more */
7704 rv = 0;
7705 break;
7706 } else {
7707 int this_sectors = sectors;
7708 memmove(p + hi + 1, p + hi,
7709 (bb->count - hi) * 8);
7710 bb->count++;
7711
7712 if (this_sectors > BB_MAX_LEN)
7713 this_sectors = BB_MAX_LEN;
7714 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7715 sectors -= this_sectors;
7716 s += this_sectors;
7717 }
7718 }
7719
7720 bb->changed = 1;
7721 write_sequnlock_irq(&bb->lock);
7722
7723 return rv;
7724}
7725
7726int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors,
7727 int acknowledged)
7728{
7729 int rv = md_set_badblocks(&rdev->badblocks,
7730 s + rdev->data_offset, sectors, acknowledged);
7731 if (rv) {
7732 /* Make sure they get written out promptly */
7733 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7734 md_wakeup_thread(rdev->mddev->thread);
7735 }
7736 return rv;
7737}
7738EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7739
7740/*
7741 * Remove a range of bad blocks from the table.
7742 * This may involve extending the table if we spilt a region,
7743 * but it must not fail. So if the table becomes full, we just
7744 * drop the remove request.
7745 */
7746static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7747{
7748 u64 *p;
7749 int lo, hi;
7750 sector_t target = s + sectors;
7751 int rv = 0;
7752
7753 if (bb->shift > 0) {
7754 /* When clearing we round the start up and the end down.
7755 * This should not matter as the shift should align with
7756 * the block size and no rounding should ever be needed.
7757 * However it is better the think a block is bad when it
7758 * isn't than to think a block is not bad when it is.
7759 */
7760 s += (1<<bb->shift) - 1;
7761 s >>= bb->shift;
7762 target >>= bb->shift;
7763 sectors = target - s;
7764 }
7765
7766 write_seqlock_irq(&bb->lock);
7767
7768 p = bb->page;
7769 lo = 0;
7770 hi = bb->count;
7771 /* Find the last range that starts before 'target' */
7772 while (hi - lo > 1) {
7773 int mid = (lo + hi) / 2;
7774 sector_t a = BB_OFFSET(p[mid]);
7775 if (a < target)
7776 lo = mid;
7777 else
7778 hi = mid;
7779 }
7780 if (hi > lo) {
7781 /* p[lo] is the last range that could overlap the
7782 * current range. Earlier ranges could also overlap,
7783 * but only this one can overlap the end of the range.
7784 */
7785 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7786 /* Partial overlap, leave the tail of this range */
7787 int ack = BB_ACK(p[lo]);
7788 sector_t a = BB_OFFSET(p[lo]);
7789 sector_t end = a + BB_LEN(p[lo]);
7790
7791 if (a < s) {
7792 /* we need to split this range */
7793 if (bb->count >= MD_MAX_BADBLOCKS) {
7794 rv = 0;
7795 goto out;
7796 }
7797 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7798 bb->count++;
7799 p[lo] = BB_MAKE(a, s-a, ack);
7800 lo++;
7801 }
7802 p[lo] = BB_MAKE(target, end - target, ack);
7803 /* there is no longer an overlap */
7804 hi = lo;
7805 lo--;
7806 }
7807 while (lo >= 0 &&
7808 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7809 /* This range does overlap */
7810 if (BB_OFFSET(p[lo]) < s) {
7811 /* Keep the early parts of this range. */
7812 int ack = BB_ACK(p[lo]);
7813 sector_t start = BB_OFFSET(p[lo]);
7814 p[lo] = BB_MAKE(start, s - start, ack);
7815 /* now low doesn't overlap, so.. */
7816 break;
7817 }
7818 lo--;
7819 }
7820 /* 'lo' is strictly before, 'hi' is strictly after,
7821 * anything between needs to be discarded
7822 */
7823 if (hi - lo > 1) {
7824 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7825 bb->count -= (hi - lo - 1);
7826 }
7827 }
7828
7829 bb->changed = 1;
7830out:
7831 write_sequnlock_irq(&bb->lock);
7832 return rv;
7833}
7834
7835int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors)
7836{
7837 return md_clear_badblocks(&rdev->badblocks,
7838 s + rdev->data_offset,
7839 sectors);
7840}
7841EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7842
7843/*
7844 * Acknowledge all bad blocks in a list.
7845 * This only succeeds if ->changed is clear. It is used by
7846 * in-kernel metadata updates
7847 */
7848void md_ack_all_badblocks(struct badblocks *bb)
7849{
7850 if (bb->page == NULL || bb->changed)
7851 /* no point even trying */
7852 return;
7853 write_seqlock_irq(&bb->lock);
7854
7855 if (bb->changed == 0) {
7856 u64 *p = bb->page;
7857 int i;
7858 for (i = 0; i < bb->count ; i++) {
7859 if (!BB_ACK(p[i])) {
7860 sector_t start = BB_OFFSET(p[i]);
7861 int len = BB_LEN(p[i]);
7862 p[i] = BB_MAKE(start, len, 1);
7863 }
7864 }
7865 }
7866 write_sequnlock_irq(&bb->lock);
7867}
7868EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7869
NeilBrown16c791a2011-07-28 11:31:47 +10007870/* sysfs access to bad-blocks list.
7871 * We present two files.
7872 * 'bad-blocks' lists sector numbers and lengths of ranges that
7873 * are recorded as bad. The list is truncated to fit within
7874 * the one-page limit of sysfs.
7875 * Writing "sector length" to this file adds an acknowledged
7876 * bad block list.
7877 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7878 * been acknowledged. Writing to this file adds bad blocks
7879 * without acknowledging them. This is largely for testing.
7880 */
7881
7882static ssize_t
7883badblocks_show(struct badblocks *bb, char *page, int unack)
7884{
7885 size_t len;
7886 int i;
7887 u64 *p = bb->page;
7888 unsigned seq;
7889
7890 if (bb->shift < 0)
7891 return 0;
7892
7893retry:
7894 seq = read_seqbegin(&bb->lock);
7895
7896 len = 0;
7897 i = 0;
7898
7899 while (len < PAGE_SIZE && i < bb->count) {
7900 sector_t s = BB_OFFSET(p[i]);
7901 unsigned int length = BB_LEN(p[i]);
7902 int ack = BB_ACK(p[i]);
7903 i++;
7904
7905 if (unack && ack)
7906 continue;
7907
7908 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
7909 (unsigned long long)s << bb->shift,
7910 length << bb->shift);
7911 }
7912
7913 if (read_seqretry(&bb->lock, seq))
7914 goto retry;
7915
7916 return len;
7917}
7918
7919#define DO_DEBUG 1
7920
7921static ssize_t
7922badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
7923{
7924 unsigned long long sector;
7925 int length;
7926 char newline;
7927#ifdef DO_DEBUG
7928 /* Allow clearing via sysfs *only* for testing/debugging.
7929 * Normally only a successful write may clear a badblock
7930 */
7931 int clear = 0;
7932 if (page[0] == '-') {
7933 clear = 1;
7934 page++;
7935 }
7936#endif /* DO_DEBUG */
7937
7938 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
7939 case 3:
7940 if (newline != '\n')
7941 return -EINVAL;
7942 case 2:
7943 if (length <= 0)
7944 return -EINVAL;
7945 break;
7946 default:
7947 return -EINVAL;
7948 }
7949
7950#ifdef DO_DEBUG
7951 if (clear) {
7952 md_clear_badblocks(bb, sector, length);
7953 return len;
7954 }
7955#endif /* DO_DEBUG */
7956 if (md_set_badblocks(bb, sector, length, !unack))
7957 return len;
7958 else
7959 return -ENOSPC;
7960}
7961
Adrian Bunk75c96f82005-05-05 16:16:09 -07007962static int md_notify_reboot(struct notifier_block *this,
7963 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964{
7965 struct list_head *tmp;
7966 mddev_t *mddev;
7967
7968 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7969
7970 printk(KERN_INFO "md: stopping all md devices.\n");
7971
NeilBrown29ac4aa2008-02-06 01:39:58 -08007972 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007973 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007974 /* Force a switch to readonly even array
7975 * appears to still be in use. Hence
7976 * the '100'.
7977 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007978 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007979 mddev_unlock(mddev);
7980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007981 /*
7982 * certain more exotic SCSI devices are known to be
7983 * volatile wrt too early system reboots. While the
7984 * right place to handle this issue is the given
7985 * driver, we do want to have a safe RAID driver ...
7986 */
7987 mdelay(1000*1);
7988 }
7989 return NOTIFY_DONE;
7990}
7991
Adrian Bunk75c96f82005-05-05 16:16:09 -07007992static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993 .notifier_call = md_notify_reboot,
7994 .next = NULL,
7995 .priority = INT_MAX, /* before any real devices */
7996};
7997
7998static void md_geninit(void)
7999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008000 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8001
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008002 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008003}
8004
Adrian Bunk75c96f82005-05-05 16:16:09 -07008005static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008006{
Tejun Heoe804ac72010-10-15 15:36:08 +02008007 int ret = -ENOMEM;
8008
Tejun Heoada609e2011-01-25 14:35:54 +01008009 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008010 if (!md_wq)
8011 goto err_wq;
8012
8013 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8014 if (!md_misc_wq)
8015 goto err_misc_wq;
8016
8017 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8018 goto err_md;
8019
8020 if ((ret = register_blkdev(0, "mdp")) < 0)
8021 goto err_mdp;
8022 mdp_major = ret;
8023
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008024 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008025 md_probe, NULL, NULL);
8026 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008027 md_probe, NULL, NULL);
8028
Linus Torvalds1da177e2005-04-16 15:20:36 -07008029 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008030 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031
8032 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008033 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008034
Tejun Heoe804ac72010-10-15 15:36:08 +02008035err_mdp:
8036 unregister_blkdev(MD_MAJOR, "md");
8037err_md:
8038 destroy_workqueue(md_misc_wq);
8039err_misc_wq:
8040 destroy_workqueue(md_wq);
8041err_wq:
8042 return ret;
8043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044
8045#ifndef MODULE
8046
8047/*
8048 * Searches all registered partitions for autorun RAID arrays
8049 * at boot time.
8050 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008051
8052static LIST_HEAD(all_detected_devices);
8053struct detected_devices_node {
8054 struct list_head list;
8055 dev_t dev;
8056};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057
8058void md_autodetect_dev(dev_t dev)
8059{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008060 struct detected_devices_node *node_detected_dev;
8061
8062 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8063 if (node_detected_dev) {
8064 node_detected_dev->dev = dev;
8065 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8066 } else {
8067 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8068 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008070}
8071
8072
8073static void autostart_arrays(int part)
8074{
8075 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008076 struct detected_devices_node *node_detected_dev;
8077 dev_t dev;
8078 int i_scanned, i_passed;
8079
8080 i_scanned = 0;
8081 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008082
8083 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8084
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008085 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8086 i_scanned++;
8087 node_detected_dev = list_entry(all_detected_devices.next,
8088 struct detected_devices_node, list);
8089 list_del(&node_detected_dev->list);
8090 dev = node_detected_dev->dev;
8091 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008092 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093 if (IS_ERR(rdev))
8094 continue;
8095
NeilBrownb2d444d2005-11-08 21:39:31 -08008096 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097 MD_BUG();
8098 continue;
8099 }
NeilBrownd0fae182008-03-04 14:29:31 -08008100 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008101 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008102 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008104
8105 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8106 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008107
8108 autorun_devices(part);
8109}
8110
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008111#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112
8113static __exit void md_exit(void)
8114{
8115 mddev_t *mddev;
8116 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008117
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008118 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008119 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008121 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008122 unregister_blkdev(mdp_major, "mdp");
8123 unregister_reboot_notifier(&md_notifier);
8124 unregister_sysctl_table(raid_table_header);
8125 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008126 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008128 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008130 destroy_workqueue(md_misc_wq);
8131 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008132}
8133
Dan Williams685784a2007-07-09 11:56:42 -07008134subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008135module_exit(md_exit)
8136
NeilBrownf91de922005-11-08 21:39:36 -08008137static int get_ro(char *buffer, struct kernel_param *kp)
8138{
8139 return sprintf(buffer, "%d", start_readonly);
8140}
8141static int set_ro(const char *val, struct kernel_param *kp)
8142{
8143 char *e;
8144 int num = simple_strtoul(val, &e, 10);
8145 if (*val && (*e == '\0' || *e == '\n')) {
8146 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008147 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008148 }
8149 return -EINVAL;
8150}
8151
NeilBrown80ca3a42006-07-10 04:44:18 -07008152module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8153module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08008154
NeilBrownefeb53c2009-01-09 08:31:10 +11008155module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008156
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157EXPORT_SYMBOL(register_md_personality);
8158EXPORT_SYMBOL(unregister_md_personality);
8159EXPORT_SYMBOL(md_error);
8160EXPORT_SYMBOL(md_done_sync);
8161EXPORT_SYMBOL(md_write_start);
8162EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008163EXPORT_SYMBOL(md_register_thread);
8164EXPORT_SYMBOL(md_unregister_thread);
8165EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166EXPORT_SYMBOL(md_check_recovery);
8167MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008168MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008169MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008170MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);