blob: 1d7b9a23c7659ce1d2cf409a1377b18630bb4e60 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .procname = "speed_limit_min",
102 .data = &sysctl_speed_limit_min,
103 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700104 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800105 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 },
107 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .procname = "speed_limit_max",
109 .data = &sysctl_speed_limit_max,
110 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700111 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800114 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117static ctl_table raid_dir_table[] = {
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "raid",
120 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700121 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .child = raid_table,
123 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800124 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127static ctl_table raid_root_table[] = {
128 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .procname = "dev",
130 .maxlen = 0,
131 .mode = 0555,
132 .child = raid_dir_table,
133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700137static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
NeilBrownf91de922005-11-08 21:39:36 -0800139static int start_readonly;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
NeilBrownd7603b72006-01-06 00:20:30 -0800142 * We have a system wide 'event count' that is incremented
143 * on any 'interesting' event, and readers of /proc/mdstat
144 * can use 'poll' or 'select' to find out when the event
145 * count increases.
146 *
147 * Events are:
148 * start array, stop array, error, add device, remove device,
149 * start build, activate spare
150 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800151static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800152static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800153void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800154{
155 atomic_inc(&md_event_count);
156 wake_up(&md_event_waiters);
157}
NeilBrown29269552006-03-27 01:18:10 -0800158EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800159
NeilBrownc331eb02006-05-30 21:27:13 -0700160/* Alternate version that can be called from interrupts
161 * when calling sysfs_notify isn't needed.
162 */
Adrian Bunk05381952006-06-26 00:28:01 -0700163static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700164{
165 atomic_inc(&md_event_count);
166 wake_up(&md_event_waiters);
167}
168
NeilBrownd7603b72006-01-06 00:20:30 -0800169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Enables to iterate over all existing md arrays
171 * all_mddevs_lock protects this list.
172 */
173static LIST_HEAD(all_mddevs);
174static DEFINE_SPINLOCK(all_mddevs_lock);
175
176
177/*
178 * iterates through all used mddevs in the system.
179 * We take care to grab the all_mddevs_lock whenever navigating
180 * the list, and to always hold a refcount when unlocked.
181 * Any code which breaks out of this loop while own
182 * a reference to the current mddev and must mddev_put it.
183 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800184#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 \
186 for (({ spin_lock(&all_mddevs_lock); \
187 tmp = all_mddevs.next; \
188 mddev = NULL;}); \
189 ({ if (tmp != &all_mddevs) \
190 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
191 spin_unlock(&all_mddevs_lock); \
192 if (mddev) mddev_put(mddev); \
193 mddev = list_entry(tmp, mddev_t, all_mddevs); \
194 tmp != &all_mddevs;}); \
195 ({ spin_lock(&all_mddevs_lock); \
196 tmp = tmp->next;}) \
197 )
198
199
NeilBrown409c57f2009-03-31 14:39:39 +1100200/* Rather than calling directly into the personality make_request function,
201 * IO requests come here first so that we can check if the device is
202 * being suspended pending a reconfiguration.
203 * We hold a refcount over the call to ->make_request. By the time that
204 * call has finished, the bio has been linked into some internal structure
205 * and so is visible to ->quiesce(), so we don't need the refcount any more.
206 */
207static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
NeilBrown409c57f2009-03-31 14:39:39 +1100209 mddev_t *mddev = q->queuedata;
210 int rv;
211 if (mddev == NULL || mddev->pers == NULL) {
212 bio_io_error(bio);
213 return 0;
214 }
215 rcu_read_lock();
216 if (mddev->suspended) {
217 DEFINE_WAIT(__wait);
218 for (;;) {
219 prepare_to_wait(&mddev->sb_wait, &__wait,
220 TASK_UNINTERRUPTIBLE);
221 if (!mddev->suspended)
222 break;
223 rcu_read_unlock();
224 schedule();
225 rcu_read_lock();
226 }
227 finish_wait(&mddev->sb_wait, &__wait);
228 }
229 atomic_inc(&mddev->active_io);
230 rcu_read_unlock();
231 rv = mddev->pers->make_request(q, bio);
232 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
233 wake_up(&mddev->sb_wait);
234
235 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
NeilBrown409c57f2009-03-31 14:39:39 +1100238static void mddev_suspend(mddev_t *mddev)
239{
240 BUG_ON(mddev->suspended);
241 mddev->suspended = 1;
242 synchronize_rcu();
243 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
244 mddev->pers->quiesce(mddev, 1);
245 md_unregister_thread(mddev->thread);
246 mddev->thread = NULL;
247 /* we now know that no code is executing in the personality module,
248 * except possibly the tail end of a ->bi_end_io function, but that
249 * is certain to complete before the module has a chance to get
250 * unloaded
251 */
252}
253
254static void mddev_resume(mddev_t *mddev)
255{
256 mddev->suspended = 0;
257 wake_up(&mddev->sb_wait);
258 mddev->pers->quiesce(mddev, 0);
259}
260
NeilBrown3fa841d2009-09-23 18:10:29 +1000261int mddev_congested(mddev_t *mddev, int bits)
262{
263 return mddev->suspended;
264}
265EXPORT_SYMBOL(mddev_congested);
266
NeilBrown409c57f2009-03-31 14:39:39 +1100267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static inline mddev_t *mddev_get(mddev_t *mddev)
269{
270 atomic_inc(&mddev->active);
271 return mddev;
272}
273
Dan Williams5fd3a172009-03-04 00:57:25 -0700274static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static void mddev_put(mddev_t *mddev)
277{
278 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
279 return;
NeilBrownd3374822009-01-09 08:31:10 +1100280 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
281 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100283 if (mddev->gendisk) {
284 /* we did a probe so need to clean up.
285 * Call schedule_work inside the spinlock
286 * so that flush_scheduled_work() after
287 * mddev_find will succeed in waiting for the
288 * work to be done.
289 */
290 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
291 schedule_work(&mddev->del_work);
292 } else
293 kfree(mddev);
294 }
295 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static mddev_t * mddev_find(dev_t unit)
299{
300 mddev_t *mddev, *new = NULL;
301
302 retry:
303 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrownefeb53c2009-01-09 08:31:10 +1100305 if (unit) {
306 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
307 if (mddev->unit == unit) {
308 mddev_get(mddev);
309 spin_unlock(&all_mddevs_lock);
310 kfree(new);
311 return mddev;
312 }
313
314 if (new) {
315 list_add(&new->all_mddevs, &all_mddevs);
316 spin_unlock(&all_mddevs_lock);
317 new->hold_active = UNTIL_IOCTL;
318 return new;
319 }
320 } else if (new) {
321 /* find an unused unit number */
322 static int next_minor = 512;
323 int start = next_minor;
324 int is_free = 0;
325 int dev = 0;
326 while (!is_free) {
327 dev = MKDEV(MD_MAJOR, next_minor);
328 next_minor++;
329 if (next_minor > MINORMASK)
330 next_minor = 0;
331 if (next_minor == start) {
332 /* Oh dear, all in use. */
333 spin_unlock(&all_mddevs_lock);
334 kfree(new);
335 return NULL;
336 }
337
338 is_free = 1;
339 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
340 if (mddev->unit == dev) {
341 is_free = 0;
342 break;
343 }
344 }
345 new->unit = dev;
346 new->md_minor = MINOR(dev);
347 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 list_add(&new->all_mddevs, &all_mddevs);
349 spin_unlock(&all_mddevs_lock);
350 return new;
351 }
352 spin_unlock(&all_mddevs_lock);
353
NeilBrown9ffae0c2006-01-06 00:20:32 -0800354 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!new)
356 return NULL;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 new->unit = unit;
359 if (MAJOR(unit) == MD_MAJOR)
360 new->md_minor = MINOR(unit);
361 else
362 new->md_minor = MINOR(unit) >> MdpMinorShift;
363
NeilBrownc8c00a62009-08-10 12:50:52 +1000364 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800365 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 INIT_LIST_HEAD(&new->disks);
367 INIT_LIST_HEAD(&new->all_mddevs);
368 init_timer(&new->safemode_timer);
369 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000370 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100371 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700372 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700373 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700374 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700375 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000376 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800377 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700378 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 goto retry;
381}
382
383static inline int mddev_lock(mddev_t * mddev)
384{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800385 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Dan Williamsb522adc2009-03-31 15:00:31 +1100388static inline int mddev_is_locked(mddev_t *mddev)
389{
390 return mutex_is_locked(&mddev->reconfig_mutex);
391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static inline int mddev_trylock(mddev_t * mddev)
394{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800395 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398static inline void mddev_unlock(mddev_t * mddev)
399{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800400 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
NeilBrown005eca52005-08-22 13:11:08 -0700402 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
NeilBrown2989ddb2006-01-06 00:20:43 -0800405static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100407 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100409 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (rdev->desc_nr == nr)
411 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return NULL;
414}
415
416static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
417{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 mdk_rdev_t *rdev;
419
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100420 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (rdev->bdev->bd_dev == dev)
422 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return NULL;
425}
426
NeilBrownd9d166c2006-01-06 00:20:51 -0800427static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800428{
429 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800430 list_for_each_entry(pers, &pers_list, list) {
431 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800432 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800433 if (strcmp(pers->name, clevel)==0)
434 return pers;
435 }
NeilBrown2604b702006-01-06 00:20:36 -0800436 return NULL;
437}
438
Andre Nollb73df2d2008-07-11 22:02:23 +1000439/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700440static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Andre Nollb73df2d2008-07-11 22:02:23 +1000442 sector_t num_sectors = bdev->bd_inode->i_size / 512;
443 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static int alloc_disk_sb(mdk_rdev_t * rdev)
447{
448 if (rdev->sb_page)
449 MD_BUG();
450
451 rdev->sb_page = alloc_page(GFP_KERNEL);
452 if (!rdev->sb_page) {
453 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000454 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
457 return 0;
458}
459
460static void free_disk_sb(mdk_rdev_t * rdev)
461{
462 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800463 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 rdev->sb_loaded = 0;
465 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000466 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100467 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469}
470
471
NeilBrown6712ecf2007-09-27 12:47:43 +0200472static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700473{
474 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800475 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700476
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700477 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
478 printk("md: super_written gets error=%d, uptodate=%d\n",
479 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
480 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800481 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700482 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700483
NeilBrowna9701a32005-11-08 21:39:34 -0800484 if (atomic_dec_and_test(&mddev->pending_writes))
485 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700486 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700487}
488
NeilBrown6712ecf2007-09-27 12:47:43 +0200489static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800490{
491 struct bio *bio2 = bio->bi_private;
492 mdk_rdev_t *rdev = bio2->bi_private;
493 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800494
495 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
496 error == -EOPNOTSUPP) {
497 unsigned long flags;
498 /* barriers don't appear to be supported :-( */
499 set_bit(BarriersNotsupp, &rdev->flags);
500 mddev->barriers_work = 0;
501 spin_lock_irqsave(&mddev->write_lock, flags);
502 bio2->bi_next = mddev->biolist;
503 mddev->biolist = bio2;
504 spin_unlock_irqrestore(&mddev->write_lock, flags);
505 wake_up(&mddev->sb_wait);
506 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200507 } else {
508 bio_put(bio2);
509 bio->bi_private = rdev;
510 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800511 }
NeilBrowna9701a32005-11-08 21:39:34 -0800512}
513
NeilBrown7bfa19f2005-06-21 17:17:28 -0700514void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
515 sector_t sector, int size, struct page *page)
516{
517 /* write first size bytes of page to sector of rdev
518 * Increment mddev->pending_writes before returning
519 * and decrement it on completion, waking up sb_wait
520 * if zero is reached.
521 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800522 *
523 * As we might need to resubmit the request if BIO_RW_BARRIER
524 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700525 */
526 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100527 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700528
529 bio->bi_bdev = rdev->bdev;
530 bio->bi_sector = sector;
531 bio_add_page(bio, page, size, 0);
532 bio->bi_private = rdev;
533 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800534 bio->bi_rw = rw;
535
NeilBrown7bfa19f2005-06-21 17:17:28 -0700536 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800537 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
538 struct bio *rbio;
539 rw |= (1<<BIO_RW_BARRIER);
540 rbio = bio_clone(bio, GFP_NOIO);
541 rbio->bi_private = bio;
542 rbio->bi_end_io = super_written_barrier;
543 submit_bio(rw, rbio);
544 } else
545 submit_bio(rw, bio);
546}
547
548void md_super_wait(mddev_t *mddev)
549{
550 /* wait for all superblock writes that were scheduled to complete.
551 * if any had to be retried (due to BARRIER problems), retry them
552 */
553 DEFINE_WAIT(wq);
554 for(;;) {
555 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
556 if (atomic_read(&mddev->pending_writes)==0)
557 break;
558 while (mddev->biolist) {
559 struct bio *bio;
560 spin_lock_irq(&mddev->write_lock);
561 bio = mddev->biolist;
562 mddev->biolist = bio->bi_next ;
563 bio->bi_next = NULL;
564 spin_unlock_irq(&mddev->write_lock);
565 submit_bio(bio->bi_rw, bio);
566 }
567 schedule();
568 }
569 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700570}
571
NeilBrown6712ecf2007-09-27 12:47:43 +0200572static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
NeilBrowna654b9d82005-06-21 17:17:27 -0700577int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct page *page, int rw)
579{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700580 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct completion event;
582 int ret;
583
Jens Axboe93dbb392009-02-16 10:25:40 +0100584 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 bio->bi_bdev = bdev;
587 bio->bi_sector = sector;
588 bio_add_page(bio, page, size, 0);
589 init_completion(&event);
590 bio->bi_private = &event;
591 bio->bi_end_io = bi_complete;
592 submit_bio(rw, bio);
593 wait_for_completion(&event);
594
595 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
596 bio_put(bio);
597 return ret;
598}
NeilBrowna8745db2006-01-06 00:20:34 -0800599EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
NeilBrown0002b272005-09-09 16:23:53 -0700601static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 char b[BDEVNAME_SIZE];
604 if (!rdev->sb_page) {
605 MD_BUG();
606 return -EINVAL;
607 }
608 if (rdev->sb_loaded)
609 return 0;
610
611
Andre Noll0f420352008-07-11 22:02:23 +1000612 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto fail;
614 rdev->sb_loaded = 1;
615 return 0;
616
617fail:
618 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
619 bdevname(rdev->bdev,b));
620 return -EINVAL;
621}
622
623static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
624{
Andre Noll05710462008-07-11 22:02:20 +1000625 return sb1->set_uuid0 == sb2->set_uuid0 &&
626 sb1->set_uuid1 == sb2->set_uuid1 &&
627 sb1->set_uuid2 == sb2->set_uuid2 &&
628 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
632{
633 int ret;
634 mdp_super_t *tmp1, *tmp2;
635
636 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
637 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
638
639 if (!tmp1 || !tmp2) {
640 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100641 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto abort;
643 }
644
645 *tmp1 = *sb1;
646 *tmp2 = *sb2;
647
648 /*
649 * nr_disks is not constant
650 */
651 tmp1->nr_disks = 0;
652 tmp2->nr_disks = 0;
653
Andre Nollce0c8e02008-07-11 22:02:20 +1000654 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700656 kfree(tmp1);
657 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return ret;
659}
660
NeilBrown4d167f02007-05-09 02:35:37 -0700661
662static u32 md_csum_fold(u32 csum)
663{
664 csum = (csum & 0xffff) + (csum >> 16);
665 return (csum & 0xffff) + (csum >> 16);
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static unsigned int calc_sb_csum(mdp_super_t * sb)
669{
NeilBrown4d167f02007-05-09 02:35:37 -0700670 u64 newcsum = 0;
671 u32 *sb32 = (u32*)sb;
672 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned int disk_csum, csum;
674
675 disk_csum = sb->sb_csum;
676 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700677
678 for (i = 0; i < MD_SB_BYTES/4 ; i++)
679 newcsum += sb32[i];
680 csum = (newcsum & 0xffffffff) + (newcsum>>32);
681
682
683#ifdef CONFIG_ALPHA
684 /* This used to use csum_partial, which was wrong for several
685 * reasons including that different results are returned on
686 * different architectures. It isn't critical that we get exactly
687 * the same return value as before (we always csum_fold before
688 * testing, and that removes any differences). However as we
689 * know that csum_partial always returned a 16bit value on
690 * alphas, do a fold to maximise conformity to previous behaviour.
691 */
692 sb->sb_csum = md_csum_fold(disk_csum);
693#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return csum;
697}
698
699
700/*
701 * Handle superblock details.
702 * We want to be able to handle multiple superblock formats
703 * so we have a common interface to them all, and an array of
704 * different handlers.
705 * We rely on user-space to write the initial superblock, and support
706 * reading and updating of superblocks.
707 * Interface methods are:
708 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
709 * loads and validates a superblock on dev.
710 * if refdev != NULL, compare superblocks on both devices
711 * Return:
712 * 0 - dev has a superblock that is compatible with refdev
713 * 1 - dev has a superblock that is compatible and newer than refdev
714 * so dev should be used as the refdev in future
715 * -EINVAL superblock incompatible or invalid
716 * -othererror e.g. -EIO
717 *
718 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
719 * Verify that dev is acceptable into mddev.
720 * The first time, mddev->raid_disks will be 0, and data from
721 * dev should be merged in. Subsequent calls check that dev
722 * is new enough. Return 0 or -EINVAL
723 *
724 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
725 * Update the superblock for rdev with data in mddev
726 * This does not write to disc.
727 *
728 */
729
730struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000731 char *name;
732 struct module *owner;
733 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
734 int minor_version);
735 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
736 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
737 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000738 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739};
740
741/*
Andre Noll0894cc32009-06-18 08:49:23 +1000742 * Check that the given mddev has no bitmap.
743 *
744 * This function is called from the run method of all personalities that do not
745 * support bitmaps. It prints an error message and returns non-zero if mddev
746 * has a bitmap. Otherwise, it returns 0.
747 *
748 */
749int md_check_no_bitmap(mddev_t *mddev)
750{
751 if (!mddev->bitmap_file && !mddev->bitmap_offset)
752 return 0;
753 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
754 mdname(mddev), mddev->pers->name);
755 return 1;
756}
757EXPORT_SYMBOL(md_check_no_bitmap);
758
759/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * load_super for 0.90.0
761 */
762static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
763{
764 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
765 mdp_super_t *sb;
766 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
Andre Noll0f420352008-07-11 22:02:23 +1000769 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * it's at the end of the disk.
771 *
772 * It also happens to be a multiple of 4Kb.
773 */
Andre Noll0f420352008-07-11 22:02:23 +1000774 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
NeilBrown0002b272005-09-09 16:23:53 -0700776 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (ret) return ret;
778
779 ret = -EINVAL;
780
781 bdevname(rdev->bdev, b);
782 sb = (mdp_super_t*)page_address(rdev->sb_page);
783
784 if (sb->md_magic != MD_SB_MAGIC) {
785 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
786 b);
787 goto abort;
788 }
789
790 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800791 sb->minor_version < 90 ||
792 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
794 sb->major_version, sb->minor_version,
795 b);
796 goto abort;
797 }
798
799 if (sb->raid_disks <= 0)
800 goto abort;
801
NeilBrown4d167f02007-05-09 02:35:37 -0700802 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
804 b);
805 goto abort;
806 }
807
808 rdev->preferred_minor = sb->md_minor;
809 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700810 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (sb->level == LEVEL_MULTIPATH)
813 rdev->desc_nr = -1;
814 else
815 rdev->desc_nr = sb->this_disk.number;
816
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700817 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700819 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 __u64 ev1, ev2;
821 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
822 if (!uuid_equal(refsb, sb)) {
823 printk(KERN_WARNING "md: %s has different UUID to %s\n",
824 b, bdevname(refdev->bdev,b2));
825 goto abort;
826 }
827 if (!sb_equal(refsb, sb)) {
828 printk(KERN_WARNING "md: %s has same UUID"
829 " but different superblock to %s\n",
830 b, bdevname(refdev->bdev, b2));
831 goto abort;
832 }
833 ev1 = md_event(sb);
834 ev2 = md_event(refsb);
835 if (ev1 > ev2)
836 ret = 1;
837 else
838 ret = 0;
839 }
NeilBrown8190e752009-06-18 08:48:58 +1000840 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Andre Nolldd8ac332009-03-31 14:33:13 +1100842 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800843 /* "this cannot possibly happen" ... */
844 ret = -EINVAL;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 abort:
847 return ret;
848}
849
850/*
851 * validate_super for 0.90.0
852 */
853static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
854{
855 mdp_disk_t *desc;
856 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700857 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
NeilBrown41158c72005-06-21 17:17:25 -0700859 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800860 clear_bit(Faulty, &rdev->flags);
861 clear_bit(In_sync, &rdev->flags);
862 clear_bit(WriteMostly, &rdev->flags);
863 clear_bit(BarriersNotsupp, &rdev->flags);
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (mddev->raid_disks == 0) {
866 mddev->major_version = 0;
867 mddev->minor_version = sb->minor_version;
868 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800869 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000870 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 mddev->ctime = sb->ctime;
872 mddev->utime = sb->utime;
873 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800874 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 mddev->layout = sb->layout;
876 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100877 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700878 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700879 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700880 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
NeilBrownf6705572006-03-27 01:18:11 -0800882 if (mddev->minor_version >= 91) {
883 mddev->reshape_position = sb->reshape_position;
884 mddev->delta_disks = sb->delta_disks;
885 mddev->new_level = sb->new_level;
886 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000887 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800888 } else {
889 mddev->reshape_position = MaxSector;
890 mddev->delta_disks = 0;
891 mddev->new_level = mddev->level;
892 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000893 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800894 }
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (sb->state & (1<<MD_SB_CLEAN))
897 mddev->recovery_cp = MaxSector;
898 else {
899 if (sb->events_hi == sb->cp_events_hi &&
900 sb->events_lo == sb->cp_events_lo) {
901 mddev->recovery_cp = sb->recovery_cp;
902 } else
903 mddev->recovery_cp = 0;
904 }
905
906 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
907 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
908 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
909 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
910
911 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700912
913 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700914 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700915 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700916
NeilBrown41158c72005-06-21 17:17:25 -0700917 } else if (mddev->pers == NULL) {
918 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 ++ev1;
920 if (ev1 < mddev->events)
921 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700922 } else if (mddev->bitmap) {
923 /* if adding to array with a bitmap, then we can accept an
924 * older device ... but not too old.
925 */
NeilBrown41158c72005-06-21 17:17:25 -0700926 if (ev1 < mddev->bitmap->events_cleared)
927 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700928 } else {
929 if (ev1 < mddev->events)
930 /* just a hot-add of a new device, leave raid_disk at -1 */
931 return 0;
932 }
NeilBrown41158c72005-06-21 17:17:25 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 desc = sb->disks + rdev->desc_nr;
936
937 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800938 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700939 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
940 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800941 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 rdev->raid_disk = desc->raid_disk;
943 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700944 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
945 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700946 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800947 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949}
950
951/*
952 * sync_super for 0.90.0
953 */
954static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
955{
956 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 mdk_rdev_t *rdev2;
958 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* make rdev->sb match mddev data..
962 *
963 * 1/ zero out disks
964 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
965 * 3/ any empty disks < next_spare become removed
966 *
967 * disks[0] gets initialised to REMOVED because
968 * we cannot be sure from other fields if it has
969 * been initialised or not.
970 */
971 int i;
972 int active=0, working=0,failed=0,spare=0,nr_disks=0;
973
NeilBrown61181562005-09-09 16:24:02 -0700974 rdev->sb_size = MD_SB_BYTES;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 sb = (mdp_super_t*)page_address(rdev->sb_page);
977
978 memset(sb, 0, sizeof(*sb));
979
980 sb->md_magic = MD_SB_MAGIC;
981 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 sb->patch_version = mddev->patch_version;
983 sb->gvalid_words = 0; /* ignored */
984 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
985 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
986 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
987 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
988
989 sb->ctime = mddev->ctime;
990 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +1100991 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 sb->raid_disks = mddev->raid_disks;
993 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800994 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 sb->utime = mddev->utime;
996 sb->state = 0;
997 sb->events_hi = (mddev->events>>32);
998 sb->events_lo = (u32)mddev->events;
999
NeilBrownf6705572006-03-27 01:18:11 -08001000 if (mddev->reshape_position == MaxSector)
1001 sb->minor_version = 90;
1002 else {
1003 sb->minor_version = 91;
1004 sb->reshape_position = mddev->reshape_position;
1005 sb->new_level = mddev->new_level;
1006 sb->delta_disks = mddev->delta_disks;
1007 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001008 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001009 }
1010 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (mddev->in_sync)
1012 {
1013 sb->recovery_cp = mddev->recovery_cp;
1014 sb->cp_events_hi = (mddev->events>>32);
1015 sb->cp_events_lo = (u32)mddev->events;
1016 if (mddev->recovery_cp == MaxSector)
1017 sb->state = (1<< MD_SB_CLEAN);
1018 } else
1019 sb->recovery_cp = 0;
1020
1021 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001022 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
NeilBrowna654b9d82005-06-21 17:17:27 -07001024 if (mddev->bitmap && mddev->bitmap_file == NULL)
1025 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001028 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001030 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001031 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1032 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -08001033 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001035 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001036 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 d = &sb->disks[rdev2->desc_nr];
1038 nr_disks++;
1039 d->number = rdev2->desc_nr;
1040 d->major = MAJOR(rdev2->bdev->bd_dev);
1041 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001042 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1043 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 d->raid_disk = rdev2->raid_disk;
1045 else
1046 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001047 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -08001049 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 d->state = (1<<MD_DISK_ACTIVE);
1051 d->state |= (1<<MD_DISK_SYNC);
1052 active++;
1053 working++;
1054 } else {
1055 d->state = 0;
1056 spare++;
1057 working++;
1058 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001059 if (test_bit(WriteMostly, &rdev2->flags))
1060 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* now set the "removed" and "faulty" bits on any missing devices */
1063 for (i=0 ; i < mddev->raid_disks ; i++) {
1064 mdp_disk_t *d = &sb->disks[i];
1065 if (d->state == 0 && d->number == 0) {
1066 d->number = i;
1067 d->raid_disk = i;
1068 d->state = (1<<MD_DISK_REMOVED);
1069 d->state |= (1<<MD_DISK_FAULTY);
1070 failed++;
1071 }
1072 }
1073 sb->nr_disks = nr_disks;
1074 sb->active_disks = active;
1075 sb->working_disks = working;
1076 sb->failed_disks = failed;
1077 sb->spare_disks = spare;
1078
1079 sb->this_disk = sb->disks[rdev->desc_nr];
1080 sb->sb_csum = calc_sb_csum(sb);
1081}
1082
1083/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001084 * rdev_size_change for 0.90.0
1085 */
1086static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001087super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001088{
Andre Noll58c0fed2009-03-31 14:33:13 +11001089 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001090 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001091 if (rdev->mddev->bitmap_offset)
1092 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001093 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001094 if (!num_sectors || num_sectors > rdev->sb_start)
1095 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001096 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001097 rdev->sb_page);
1098 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001099 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001100}
1101
1102
1103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 * version 1 superblock
1105 */
1106
NeilBrown1c05b4b2006-10-21 10:24:08 -07001107static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001109 __le32 disk_csum;
1110 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 unsigned long long newcsum;
1112 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001113 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int i;
1115
1116 disk_csum = sb->sb_csum;
1117 sb->sb_csum = 0;
1118 newcsum = 0;
1119 for (i=0; size>=4; size -= 4 )
1120 newcsum += le32_to_cpu(*isuper++);
1121
1122 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001123 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1126 sb->sb_csum = disk_csum;
1127 return cpu_to_le32(csum);
1128}
1129
1130static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1131{
1132 struct mdp_superblock_1 *sb;
1133 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001134 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001136 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /*
Andre Noll0f420352008-07-11 22:02:23 +10001139 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * It is always aligned to a 4K boundary and
1141 * depeding on minor_version, it can be:
1142 * 0: At least 8K, but less than 12K, from end of device
1143 * 1: At start of device
1144 * 2: 4K from start of device.
1145 */
1146 switch(minor_version) {
1147 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001148 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1149 sb_start -= 8*2;
1150 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001153 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001156 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 default:
1159 return -EINVAL;
1160 }
Andre Noll0f420352008-07-11 22:02:23 +10001161 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
NeilBrown0002b272005-09-09 16:23:53 -07001163 /* superblock is rarely larger than 1K, but it can be larger,
1164 * and it is safe to read 4k, so we do that
1165 */
1166 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (ret) return ret;
1168
1169
1170 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1171
1172 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1173 sb->major_version != cpu_to_le32(1) ||
1174 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001175 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001176 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return -EINVAL;
1178
1179 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1180 printk("md: invalid superblock checksum on %s\n",
1181 bdevname(rdev->bdev,b));
1182 return -EINVAL;
1183 }
1184 if (le64_to_cpu(sb->data_size) < 10) {
1185 printk("md: data_size too small on %s\n",
1186 bdevname(rdev->bdev,b));
1187 return -EINVAL;
1188 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 rdev->preferred_minor = 0xffff;
1191 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001192 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
NeilBrown0002b272005-09-09 16:23:53 -07001194 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001195 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001196 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001197 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1198
1199 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001200 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001201 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001202
NeilBrown31b65a02006-07-10 04:44:14 -07001203 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1204 rdev->desc_nr = -1;
1205 else
1206 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1207
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001208 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001209 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001210 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 __u64 ev1, ev2;
1212 struct mdp_superblock_1 *refsb =
1213 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1214
1215 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1216 sb->level != refsb->level ||
1217 sb->layout != refsb->layout ||
1218 sb->chunksize != refsb->chunksize) {
1219 printk(KERN_WARNING "md: %s has strangely different"
1220 " superblock to %s\n",
1221 bdevname(rdev->bdev,b),
1222 bdevname(refdev->bdev,b2));
1223 return -EINVAL;
1224 }
1225 ev1 = le64_to_cpu(sb->events);
1226 ev2 = le64_to_cpu(refsb->events);
1227
1228 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001229 ret = 1;
1230 else
1231 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
NeilBrowna1801f82008-03-04 14:29:31 -08001233 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001234 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1235 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001237 rdev->sectors = rdev->sb_start;
1238 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001240 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001241 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001242 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1247{
1248 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001249 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
NeilBrown41158c72005-06-21 17:17:25 -07001251 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001252 clear_bit(Faulty, &rdev->flags);
1253 clear_bit(In_sync, &rdev->flags);
1254 clear_bit(WriteMostly, &rdev->flags);
1255 clear_bit(BarriersNotsupp, &rdev->flags);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (mddev->raid_disks == 0) {
1258 mddev->major_version = 1;
1259 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001260 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001261 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1263 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1264 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001265 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 mddev->layout = le32_to_cpu(sb->layout);
1267 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001268 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001269 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001270 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001271 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1274 memcpy(mddev->uuid, sb->set_uuid, 16);
1275
1276 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001277
NeilBrown71c08052005-09-09 16:23:51 -07001278 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001279 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001280 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001281
NeilBrownf6705572006-03-27 01:18:11 -08001282 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1283 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1284 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1285 mddev->new_level = le32_to_cpu(sb->new_level);
1286 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001287 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001288 } else {
1289 mddev->reshape_position = MaxSector;
1290 mddev->delta_disks = 0;
1291 mddev->new_level = mddev->level;
1292 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001293 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001294 }
1295
NeilBrown41158c72005-06-21 17:17:25 -07001296 } else if (mddev->pers == NULL) {
1297 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ++ev1;
1299 if (ev1 < mddev->events)
1300 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001301 } else if (mddev->bitmap) {
1302 /* If adding to array with a bitmap, then we can accept an
1303 * older device, but not too old.
1304 */
NeilBrown41158c72005-06-21 17:17:25 -07001305 if (ev1 < mddev->bitmap->events_cleared)
1306 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001307 } else {
1308 if (ev1 < mddev->events)
1309 /* just a hot-add of a new device, leave raid_disk at -1 */
1310 return 0;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (mddev->level != LEVEL_MULTIPATH) {
1313 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001314 if (rdev->desc_nr < 0 ||
1315 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1316 role = 0xffff;
1317 rdev->desc_nr = -1;
1318 } else
1319 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 switch(role) {
1321 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
1323 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001324 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 break;
1326 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001327 if ((le32_to_cpu(sb->feature_map) &
1328 MD_FEATURE_RECOVERY_OFFSET))
1329 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1330 else
1331 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 rdev->raid_disk = role;
1333 break;
1334 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001335 if (sb->devflags & WriteMostly1)
1336 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001337 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001338 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
1341}
1342
1343static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1344{
1345 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 mdk_rdev_t *rdev2;
1347 int max_dev, i;
1348 /* make rdev->sb match mddev and rdev data. */
1349
1350 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1351
1352 sb->feature_map = 0;
1353 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001354 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 memset(sb->pad1, 0, sizeof(sb->pad1));
1356 memset(sb->pad2, 0, sizeof(sb->pad2));
1357 memset(sb->pad3, 0, sizeof(sb->pad3));
1358
1359 sb->utime = cpu_to_le64((__u64)mddev->utime);
1360 sb->events = cpu_to_le64(mddev->events);
1361 if (mddev->in_sync)
1362 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1363 else
1364 sb->resync_offset = cpu_to_le64(0);
1365
NeilBrown1c05b4b2006-10-21 10:24:08 -07001366 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001367
NeilBrownf0ca3402006-02-02 14:28:04 -08001368 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001369 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001370 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001371 sb->level = cpu_to_le32(mddev->level);
1372 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001373
NeilBrowna654b9d82005-06-21 17:17:27 -07001374 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1375 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001376 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001377 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001378
1379 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001380 !test_bit(In_sync, &rdev->flags)) {
1381 if (mddev->curr_resync_completed > rdev->recovery_offset)
1382 rdev->recovery_offset = mddev->curr_resync_completed;
1383 if (rdev->recovery_offset > 0) {
1384 sb->feature_map |=
1385 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1386 sb->recovery_offset =
1387 cpu_to_le64(rdev->recovery_offset);
1388 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001389 }
1390
NeilBrownf6705572006-03-27 01:18:11 -08001391 if (mddev->reshape_position != MaxSector) {
1392 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1393 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1394 sb->new_layout = cpu_to_le32(mddev->new_layout);
1395 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1396 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001397 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001398 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001401 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (rdev2->desc_nr+1 > max_dev)
1403 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001404
NeilBrown70471da2009-08-03 10:59:57 +10001405 if (max_dev > le32_to_cpu(sb->max_dev)) {
1406 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001407 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001408 rdev->sb_size = max_dev * 2 + 256;
1409 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1410 if (rdev->sb_size & bmask)
1411 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 for (i=0; i<max_dev;i++)
1414 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1415
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001416 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001418 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001420 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001422 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1423 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 else
1425 sb->dev_roles[i] = cpu_to_le16(0xffff);
1426 }
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 sb->sb_csum = calc_sb_1_csum(sb);
1429}
1430
Chris Webb0cd17fe2008-06-28 08:31:46 +10001431static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001432super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001433{
1434 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001435 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001436 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001437 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001438 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001439 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001440 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1441 max_sectors -= rdev->data_offset;
1442 if (!num_sectors || num_sectors > max_sectors)
1443 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001444 } else if (rdev->mddev->bitmap_offset) {
1445 /* minor version 0 with bitmap we can't move */
1446 return 0;
1447 } else {
1448 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001449 sector_t sb_start;
1450 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1451 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001452 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001453 if (!num_sectors || num_sectors > max_sectors)
1454 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001455 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001456 }
1457 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001458 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001459 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001460 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001461 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001462 rdev->sb_page);
1463 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001464 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001465}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Adrian Bunk75c96f82005-05-05 16:16:09 -07001467static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 [0] = {
1469 .name = "0.90.0",
1470 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001471 .load_super = super_90_load,
1472 .validate_super = super_90_validate,
1473 .sync_super = super_90_sync,
1474 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 },
1476 [1] = {
1477 .name = "md-1",
1478 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001479 .load_super = super_1_load,
1480 .validate_super = super_1_validate,
1481 .sync_super = super_1_sync,
1482 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 },
1484};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1487{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001488 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
NeilBrown4b809912008-07-21 17:05:25 +10001490 rcu_read_lock();
1491 rdev_for_each_rcu(rdev, mddev1)
1492 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001493 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001494 rdev2->bdev->bd_contains) {
1495 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001496 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001497 }
1498 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return 0;
1500}
1501
1502static LIST_HEAD(pending_raid_disks);
1503
Andre Nollac5e7112009-08-03 10:59:47 +10001504/*
1505 * Try to register data integrity profile for an mddev
1506 *
1507 * This is called when an array is started and after a disk has been kicked
1508 * from the array. It only succeeds if all working and active component devices
1509 * are integrity capable with matching profiles.
1510 */
1511int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001512{
Andre Nollac5e7112009-08-03 10:59:47 +10001513 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001514
Andre Nollac5e7112009-08-03 10:59:47 +10001515 if (list_empty(&mddev->disks))
1516 return 0; /* nothing to do */
1517 if (blk_get_integrity(mddev->gendisk))
1518 return 0; /* already registered */
1519 list_for_each_entry(rdev, &mddev->disks, same_set) {
1520 /* skip spares and non-functional disks */
1521 if (test_bit(Faulty, &rdev->flags))
1522 continue;
1523 if (rdev->raid_disk < 0)
1524 continue;
1525 /*
1526 * If at least one rdev is not integrity capable, we can not
1527 * enable data integrity for the md device.
1528 */
1529 if (!bdev_get_integrity(rdev->bdev))
1530 return -EINVAL;
1531 if (!reference) {
1532 /* Use the first rdev as the reference */
1533 reference = rdev;
1534 continue;
1535 }
1536 /* does this rdev's profile match the reference profile? */
1537 if (blk_integrity_compare(reference->bdev->bd_disk,
1538 rdev->bdev->bd_disk) < 0)
1539 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001540 }
Andre Nollac5e7112009-08-03 10:59:47 +10001541 /*
1542 * All component devices are integrity capable and have matching
1543 * profiles, register the common profile for the md device.
1544 */
1545 if (blk_integrity_register(mddev->gendisk,
1546 bdev_get_integrity(reference->bdev)) != 0) {
1547 printk(KERN_ERR "md: failed to register integrity for %s\n",
1548 mdname(mddev));
1549 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001550 }
Andre Nollac5e7112009-08-03 10:59:47 +10001551 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1552 mdname(mddev));
1553 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001554}
Andre Nollac5e7112009-08-03 10:59:47 +10001555EXPORT_SYMBOL(md_integrity_register);
1556
1557/* Disable data integrity if non-capable/non-matching disk is being added */
1558void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1559{
1560 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1561 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1562
1563 if (!bi_mddev) /* nothing to do */
1564 return;
1565 if (rdev->raid_disk < 0) /* skip spares */
1566 return;
1567 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1568 rdev->bdev->bd_disk) >= 0)
1569 return;
1570 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1571 blk_integrity_unregister(mddev->gendisk);
1572}
1573EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1576{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001577 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001578 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001579 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001580 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (rdev->mddev) {
1583 MD_BUG();
1584 return -EINVAL;
1585 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001586
1587 /* prevent duplicates */
1588 if (find_rdev(mddev, rdev->bdev->bd_dev))
1589 return -EEXIST;
1590
Andre Nolldd8ac332009-03-31 14:33:13 +11001591 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1592 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1593 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001594 if (mddev->pers) {
1595 /* Cannot change size, so fail
1596 * If mddev->level <= 0, then we don't care
1597 * about aligning sizes (e.g. linear)
1598 */
1599 if (mddev->level > 0)
1600 return -ENOSPC;
1601 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001602 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 /* Verify rdev->desc_nr is unique.
1606 * If it is -1, assign a free number, else
1607 * check number is not in use
1608 */
1609 if (rdev->desc_nr < 0) {
1610 int choice = 0;
1611 if (mddev->pers) choice = mddev->raid_disks;
1612 while (find_rdev_nr(mddev, choice))
1613 choice++;
1614 rdev->desc_nr = choice;
1615 } else {
1616 if (find_rdev_nr(mddev, rdev->desc_nr))
1617 return -EBUSY;
1618 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001619 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1620 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1621 mdname(mddev), mddev->max_disks);
1622 return -EBUSY;
1623 }
NeilBrown19133a42005-11-08 21:39:35 -08001624 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001625 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001626 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001629 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001630
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001631 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001632 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001633
Tejun Heo0762b8b2008-08-25 19:56:12 +09001634 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001635 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1636 kobject_del(&rdev->kobj);
1637 goto fail;
1638 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001639 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1640
NeilBrown4b809912008-07-21 17:05:25 +10001641 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001642 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001643
1644 /* May as well allow recovery to be retried once */
1645 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001648
1649 fail:
1650 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1651 b, mdname(mddev));
1652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
NeilBrown177a99b2008-02-06 01:39:56 -08001655static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001656{
1657 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1658 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001659 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1663{
1664 char b[BDEVNAME_SIZE];
1665 if (!rdev->mddev) {
1666 MD_BUG();
1667 return;
1668 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001669 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001670 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1672 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001673 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001674 sysfs_put(rdev->sysfs_state);
1675 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001676 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001677 * writing to 'remove' to "dev/state". We also need
1678 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001679 */
NeilBrown4b809912008-07-21 17:05:25 +10001680 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001681 INIT_WORK(&rdev->del_work, md_delayed_delete);
1682 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001683 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/*
1687 * prevent the device from being mounted, repartitioned or
1688 * otherwise reused by a RAID array (or any other kernel
1689 * subsystem), by bd_claiming the device.
1690 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001691static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 int err = 0;
1694 struct block_device *bdev;
1695 char b[BDEVNAME_SIZE];
1696
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001697 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (IS_ERR(bdev)) {
1699 printk(KERN_ERR "md: could not open %s.\n",
1700 __bdevname(dev, b));
1701 return PTR_ERR(bdev);
1702 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001703 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (err) {
1705 printk(KERN_ERR "md: could not bd_claim %s.\n",
1706 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001707 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return err;
1709 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001710 if (!shared)
1711 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 rdev->bdev = bdev;
1713 return err;
1714}
1715
1716static void unlock_rdev(mdk_rdev_t *rdev)
1717{
1718 struct block_device *bdev = rdev->bdev;
1719 rdev->bdev = NULL;
1720 if (!bdev)
1721 MD_BUG();
1722 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001723 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
1726void md_autodetect_dev(dev_t dev);
1727
1728static void export_rdev(mdk_rdev_t * rdev)
1729{
1730 char b[BDEVNAME_SIZE];
1731 printk(KERN_INFO "md: export_rdev(%s)\n",
1732 bdevname(rdev->bdev,b));
1733 if (rdev->mddev)
1734 MD_BUG();
1735 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001737 if (test_bit(AutoDetected, &rdev->flags))
1738 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739#endif
1740 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001741 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
1744static void kick_rdev_from_array(mdk_rdev_t * rdev)
1745{
1746 unbind_rdev_from_array(rdev);
1747 export_rdev(rdev);
1748}
1749
1750static void export_array(mddev_t *mddev)
1751{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001752 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
NeilBrownd089c6a2008-02-06 01:39:59 -08001754 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (!rdev->mddev) {
1756 MD_BUG();
1757 continue;
1758 }
1759 kick_rdev_from_array(rdev);
1760 }
1761 if (!list_empty(&mddev->disks))
1762 MD_BUG();
1763 mddev->raid_disks = 0;
1764 mddev->major_version = 0;
1765}
1766
1767static void print_desc(mdp_disk_t *desc)
1768{
1769 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1770 desc->major,desc->minor,desc->raid_disk,desc->state);
1771}
1772
Cheng Renquancd2ac932009-01-09 08:31:08 +11001773static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 int i;
1776
1777 printk(KERN_INFO
1778 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1779 sb->major_version, sb->minor_version, sb->patch_version,
1780 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1781 sb->ctime);
1782 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1783 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1784 sb->md_minor, sb->layout, sb->chunk_size);
1785 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1786 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1787 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1788 sb->failed_disks, sb->spare_disks,
1789 sb->sb_csum, (unsigned long)sb->events_lo);
1790
1791 printk(KERN_INFO);
1792 for (i = 0; i < MD_SB_DISKS; i++) {
1793 mdp_disk_t *desc;
1794
1795 desc = sb->disks + i;
1796 if (desc->number || desc->major || desc->minor ||
1797 desc->raid_disk || (desc->state && (desc->state != 4))) {
1798 printk(" D %2d: ", i);
1799 print_desc(desc);
1800 }
1801 }
1802 printk(KERN_INFO "md: THIS: ");
1803 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Cheng Renquancd2ac932009-01-09 08:31:08 +11001806static void print_sb_1(struct mdp_superblock_1 *sb)
1807{
1808 __u8 *uuid;
1809
1810 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001811 printk(KERN_INFO
1812 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1813 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1814 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001815 le32_to_cpu(sb->major_version),
1816 le32_to_cpu(sb->feature_map),
1817 uuid[0], uuid[1], uuid[2], uuid[3],
1818 uuid[4], uuid[5], uuid[6], uuid[7],
1819 uuid[8], uuid[9], uuid[10], uuid[11],
1820 uuid[12], uuid[13], uuid[14], uuid[15],
1821 sb->set_name,
1822 (unsigned long long)le64_to_cpu(sb->ctime)
1823 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1824
1825 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001826 printk(KERN_INFO
1827 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001828 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001829 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1830 ":%02x%02x%02x%02x%02x%02x\n"
1831 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1832 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001833 le32_to_cpu(sb->level),
1834 (unsigned long long)le64_to_cpu(sb->size),
1835 le32_to_cpu(sb->raid_disks),
1836 le32_to_cpu(sb->layout),
1837 le32_to_cpu(sb->chunksize),
1838 (unsigned long long)le64_to_cpu(sb->data_offset),
1839 (unsigned long long)le64_to_cpu(sb->data_size),
1840 (unsigned long long)le64_to_cpu(sb->super_offset),
1841 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1842 le32_to_cpu(sb->dev_number),
1843 uuid[0], uuid[1], uuid[2], uuid[3],
1844 uuid[4], uuid[5], uuid[6], uuid[7],
1845 uuid[8], uuid[9], uuid[10], uuid[11],
1846 uuid[12], uuid[13], uuid[14], uuid[15],
1847 sb->devflags,
1848 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1849 (unsigned long long)le64_to_cpu(sb->events),
1850 (unsigned long long)le64_to_cpu(sb->resync_offset),
1851 le32_to_cpu(sb->sb_csum),
1852 le32_to_cpu(sb->max_dev)
1853 );
1854}
1855
1856static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001859 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1860 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001861 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1862 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001864 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1865 switch (major_version) {
1866 case 0:
1867 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1868 break;
1869 case 1:
1870 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1871 break;
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 } else
1874 printk(KERN_INFO "md: no rdev superblock!\n");
1875}
1876
Adrian Bunk5e563412006-06-26 00:27:42 -07001877static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001879 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 mdk_rdev_t *rdev;
1881 mddev_t *mddev;
1882 char b[BDEVNAME_SIZE];
1883
1884 printk("\n");
1885 printk("md: **********************************\n");
1886 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1887 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001888 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
NeilBrown32a76272005-06-21 17:17:14 -07001890 if (mddev->bitmap)
1891 bitmap_print_sb(mddev->bitmap);
1892 else
1893 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001894 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 printk("<%s>", bdevname(rdev->bdev,b));
1896 printk("\n");
1897
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001898 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001899 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901 printk("md: **********************************\n");
1902 printk("\n");
1903}
1904
1905
NeilBrown42543762006-06-26 00:27:57 -07001906static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
NeilBrown42543762006-06-26 00:27:57 -07001908 /* Update each superblock (in-memory image), but
1909 * if we are allowed to, skip spares which already
1910 * have the right event counter, or have one earlier
1911 * (which would mean they aren't being marked as dirty
1912 * with the rest of the array)
1913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001916 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001917 if (rdev->sb_events == mddev->events ||
1918 (nospares &&
1919 rdev->raid_disk < 0 &&
1920 (rdev->sb_events&1)==0 &&
1921 rdev->sb_events+1 == mddev->events)) {
1922 /* Don't update this superblock */
1923 rdev->sb_loaded = 2;
1924 } else {
1925 super_types[mddev->major_version].
1926 sync_super(mddev, rdev);
1927 rdev->sb_loaded = 1;
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930}
1931
NeilBrown850b2b42006-10-03 01:15:46 -07001932static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001935 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001936 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
NeilBrown1b57f132009-06-18 08:48:19 +10001938 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07001939 if (mddev->external)
1940 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001942 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001943
NeilBrown850b2b42006-10-03 01:15:46 -07001944 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1945 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1946 force_change = 1;
1947 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1948 /* just a clean<-> dirty transition, possibly leave spares alone,
1949 * though if events isn't the right even/odd, we will have to do
1950 * spares after all
1951 */
1952 nospares = 1;
1953 if (force_change)
1954 nospares = 0;
1955 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001956 /* If the array is degraded, then skipping spares is both
1957 * dangerous and fairly pointless.
1958 * Dangerous because a device that was removed from the array
1959 * might have a event_count that still looks up-to-date,
1960 * so it can be re-added without a resync.
1961 * Pointless because if there are any spares to skip,
1962 * then a recovery will happen and soon that array won't
1963 * be degraded any more and the spare can go back to sleep then.
1964 */
NeilBrown850b2b42006-10-03 01:15:46 -07001965 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001966
NeilBrown06d91a52005-06-21 17:17:12 -07001967 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07001968
1969 /* If this is just a dirty<->clean transition, and the array is clean
1970 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001971 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001972 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001973 && (mddev->events & 1)
1974 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001975 mddev->events--;
1976 else {
1977 /* otherwise we have to go forward and ... */
1978 mddev->events ++;
1979 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10001980 /* .. if the array isn't clean, an 'even' event must also go
1981 * to spares. */
1982 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07001983 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07001984 } else {
NeilBrown51d56682009-08-13 09:54:02 +10001985 /* otherwise an 'odd' event must go to spares */
1986 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07001987 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07001988 }
1989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 if (!mddev->events) {
1992 /*
1993 * oops, this 64-bit counter should never wrap.
1994 * Either we are in around ~1 trillion A.C., assuming
1995 * 1 reboot per second, or we have a bug:
1996 */
1997 MD_BUG();
1998 mddev->events --;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /*
2002 * do not write anything to disk if using
2003 * nonpersistent superblocks
2004 */
NeilBrown06d91a52005-06-21 17:17:12 -07002005 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002006 if (!mddev->external)
2007 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2008
NeilBrowna9701a32005-11-08 21:39:34 -08002009 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002010 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002012 }
NeilBrowne6910632008-02-06 01:39:51 -08002013 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002014 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 dprintk(KERN_INFO
2017 "md: updating %s RAID superblock on device (in sync %d)\n",
2018 mdname(mddev),mddev->in_sync);
2019
NeilBrown4ad13662007-07-17 04:06:13 -07002020 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002021 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 char b[BDEVNAME_SIZE];
2023 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002024 if (rdev->sb_loaded != 1)
2025 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002026 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 dprintk("(skipping faulty ");
2028
2029 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002030 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002031 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002032 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002033 rdev->sb_page);
2034 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2035 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002036 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002037 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 } else
2040 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002041 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /* only need to write one superblock... */
2043 break;
2044 }
NeilBrowna9701a32005-11-08 21:39:34 -08002045 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002046 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002047
NeilBrowna9701a32005-11-08 21:39:34 -08002048 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002049 if (mddev->in_sync != sync_req ||
2050 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002051 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002052 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002053 goto repeat;
2054 }
NeilBrown850b2b42006-10-03 01:15:46 -07002055 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002056 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002057 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002058 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2059 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Andre Noll7f6ce762008-03-23 18:34:54 +01002063/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002064 * We want to accept with case. For this we use cmd_match.
2065 */
2066static int cmd_match(const char *cmd, const char *str)
2067{
2068 /* See if cmd, written into a sysfs file, matches
2069 * str. They must either be the same, or cmd can
2070 * have a trailing newline
2071 */
2072 while (*cmd && *str && *cmd == *str) {
2073 cmd++;
2074 str++;
2075 }
2076 if (*cmd == '\n')
2077 cmd++;
2078 if (*str || *cmd)
2079 return 0;
2080 return 1;
2081}
2082
NeilBrown86e6ffd2005-11-08 21:39:24 -08002083struct rdev_sysfs_entry {
2084 struct attribute attr;
2085 ssize_t (*show)(mdk_rdev_t *, char *);
2086 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2087};
2088
2089static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002090state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002091{
2092 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002093 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002094
NeilBrownb2d444d2005-11-08 21:39:31 -08002095 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002096 len+= sprintf(page+len, "%sfaulty",sep);
2097 sep = ",";
2098 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002099 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002100 len += sprintf(page+len, "%sin_sync",sep);
2101 sep = ",";
2102 }
NeilBrownf6556752006-06-26 00:28:01 -07002103 if (test_bit(WriteMostly, &rdev->flags)) {
2104 len += sprintf(page+len, "%swrite_mostly",sep);
2105 sep = ",";
2106 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002107 if (test_bit(Blocked, &rdev->flags)) {
2108 len += sprintf(page+len, "%sblocked", sep);
2109 sep = ",";
2110 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002111 if (!test_bit(Faulty, &rdev->flags) &&
2112 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002113 len += sprintf(page+len, "%sspare", sep);
2114 sep = ",";
2115 }
2116 return len+sprintf(page+len, "\n");
2117}
2118
NeilBrown45dc2de2006-06-26 00:27:58 -07002119static ssize_t
2120state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2121{
2122 /* can write
2123 * faulty - simulates and error
2124 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002125 * writemostly - sets write_mostly
2126 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002127 * blocked - sets the Blocked flag
2128 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002129 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002130 */
2131 int err = -EINVAL;
2132 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2133 md_error(rdev->mddev, rdev);
2134 err = 0;
2135 } else if (cmd_match(buf, "remove")) {
2136 if (rdev->raid_disk >= 0)
2137 err = -EBUSY;
2138 else {
2139 mddev_t *mddev = rdev->mddev;
2140 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002141 if (mddev->pers)
2142 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002143 md_new_event(mddev);
2144 err = 0;
2145 }
NeilBrownf6556752006-06-26 00:28:01 -07002146 } else if (cmd_match(buf, "writemostly")) {
2147 set_bit(WriteMostly, &rdev->flags);
2148 err = 0;
2149 } else if (cmd_match(buf, "-writemostly")) {
2150 clear_bit(WriteMostly, &rdev->flags);
2151 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002152 } else if (cmd_match(buf, "blocked")) {
2153 set_bit(Blocked, &rdev->flags);
2154 err = 0;
2155 } else if (cmd_match(buf, "-blocked")) {
2156 clear_bit(Blocked, &rdev->flags);
2157 wake_up(&rdev->blocked_wait);
2158 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2159 md_wakeup_thread(rdev->mddev->thread);
2160
2161 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002162 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2163 set_bit(In_sync, &rdev->flags);
2164 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002165 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002166 if (!err && rdev->sysfs_state)
2167 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002168 return err ? err : len;
2169}
NeilBrown80ca3a42006-07-10 04:44:18 -07002170static struct rdev_sysfs_entry rdev_state =
2171__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002172
2173static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002174errors_show(mdk_rdev_t *rdev, char *page)
2175{
2176 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2177}
2178
2179static ssize_t
2180errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2181{
2182 char *e;
2183 unsigned long n = simple_strtoul(buf, &e, 10);
2184 if (*buf && (*e == 0 || *e == '\n')) {
2185 atomic_set(&rdev->corrected_errors, n);
2186 return len;
2187 }
2188 return -EINVAL;
2189}
2190static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002191__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002192
NeilBrown014236d2006-01-06 00:20:55 -08002193static ssize_t
2194slot_show(mdk_rdev_t *rdev, char *page)
2195{
2196 if (rdev->raid_disk < 0)
2197 return sprintf(page, "none\n");
2198 else
2199 return sprintf(page, "%d\n", rdev->raid_disk);
2200}
2201
2202static ssize_t
2203slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2204{
2205 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002206 int err;
2207 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002208 int slot = simple_strtoul(buf, &e, 10);
2209 if (strncmp(buf, "none", 4)==0)
2210 slot = -1;
2211 else if (e==buf || (*e && *e!= '\n'))
2212 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002213 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002214 /* Setting 'slot' on an active array requires also
2215 * updating the 'rd%d' link, and communicating
2216 * with the personality with ->hot_*_disk.
2217 * For now we only support removing
2218 * failed/spare devices. This normally happens automatically,
2219 * but not when the metadata is externally managed.
2220 */
NeilBrownc303da62008-02-06 01:39:51 -08002221 if (rdev->raid_disk == -1)
2222 return -EEXIST;
2223 /* personality does all needed checks */
2224 if (rdev->mddev->pers->hot_add_disk == NULL)
2225 return -EINVAL;
2226 err = rdev->mddev->pers->
2227 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2228 if (err)
2229 return err;
2230 sprintf(nm, "rd%d", rdev->raid_disk);
2231 sysfs_remove_link(&rdev->mddev->kobj, nm);
2232 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2233 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002234 } else if (rdev->mddev->pers) {
2235 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002236 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002237 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002238 */
2239
2240 if (rdev->raid_disk != -1)
2241 return -EBUSY;
2242
2243 if (rdev->mddev->pers->hot_add_disk == NULL)
2244 return -EINVAL;
2245
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002246 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002247 if (rdev2->raid_disk == slot)
2248 return -EEXIST;
2249
2250 rdev->raid_disk = slot;
2251 if (test_bit(In_sync, &rdev->flags))
2252 rdev->saved_raid_disk = slot;
2253 else
2254 rdev->saved_raid_disk = -1;
2255 err = rdev->mddev->pers->
2256 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002257 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002258 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002259 return err;
Neil Brown52664732008-06-28 08:31:44 +10002260 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002261 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002262 sprintf(nm, "rd%d", rdev->raid_disk);
2263 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2264 printk(KERN_WARNING
2265 "md: cannot register "
2266 "%s for %s\n",
2267 nm, mdname(rdev->mddev));
2268
2269 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002270 } else {
2271 if (slot >= rdev->mddev->raid_disks)
2272 return -ENOSPC;
2273 rdev->raid_disk = slot;
2274 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002275 clear_bit(Faulty, &rdev->flags);
2276 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002277 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002278 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002279 }
NeilBrown014236d2006-01-06 00:20:55 -08002280 return len;
2281}
2282
2283
2284static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002285__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002286
NeilBrown93c8cad2006-01-06 00:20:56 -08002287static ssize_t
2288offset_show(mdk_rdev_t *rdev, char *page)
2289{
Andrew Morton6961ece2006-01-06 00:20:59 -08002290 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002291}
2292
2293static ssize_t
2294offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2295{
2296 char *e;
2297 unsigned long long offset = simple_strtoull(buf, &e, 10);
2298 if (e==buf || (*e && *e != '\n'))
2299 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002300 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002301 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002302 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002303 /* Must set offset before size, so overlap checks
2304 * can be sane */
2305 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002306 rdev->data_offset = offset;
2307 return len;
2308}
2309
2310static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002311__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002312
NeilBrown83303b62006-01-06 00:21:06 -08002313static ssize_t
2314rdev_size_show(mdk_rdev_t *rdev, char *page)
2315{
Andre Nolldd8ac332009-03-31 14:33:13 +11002316 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002317}
2318
NeilBrownc5d79ad2008-02-06 01:39:54 -08002319static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2320{
2321 /* check if two start/length pairs overlap */
2322 if (s1+l1 <= s2)
2323 return 0;
2324 if (s2+l2 <= s1)
2325 return 0;
2326 return 1;
2327}
2328
Dan Williamsb522adc2009-03-31 15:00:31 +11002329static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2330{
2331 unsigned long long blocks;
2332 sector_t new;
2333
2334 if (strict_strtoull(buf, 10, &blocks) < 0)
2335 return -EINVAL;
2336
2337 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2338 return -EINVAL; /* sector conversion overflow */
2339
2340 new = blocks * 2;
2341 if (new != blocks * 2)
2342 return -EINVAL; /* unsigned long long to sector_t overflow */
2343
2344 *sectors = new;
2345 return 0;
2346}
2347
NeilBrown83303b62006-01-06 00:21:06 -08002348static ssize_t
2349rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2350{
NeilBrown27c529b2008-03-04 14:29:33 -08002351 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002352 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002353 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002354
Dan Williamsb522adc2009-03-31 15:00:31 +11002355 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002356 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002357 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002358 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002359 sectors = super_types[my_mddev->major_version].
2360 rdev_size_change(rdev, sectors);
2361 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002362 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002363 } else if (!sectors)
2364 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2365 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002366 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002367 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002368 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002369
Andre Nolldd8ac332009-03-31 14:33:13 +11002370 rdev->sectors = sectors;
2371 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002372 /* need to check that all other rdevs with the same ->bdev
2373 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002374 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002375 * we have to change it back, we will have the lock again.
2376 */
2377 mddev_t *mddev;
2378 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002379 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002380
NeilBrown27c529b2008-03-04 14:29:33 -08002381 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002382 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002383 mdk_rdev_t *rdev2;
2384
2385 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002386 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002387 if (test_bit(AllReserved, &rdev2->flags) ||
2388 (rdev->bdev == rdev2->bdev &&
2389 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002390 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002391 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002392 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002393 overlap = 1;
2394 break;
2395 }
2396 mddev_unlock(mddev);
2397 if (overlap) {
2398 mddev_put(mddev);
2399 break;
2400 }
2401 }
NeilBrown27c529b2008-03-04 14:29:33 -08002402 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002403 if (overlap) {
2404 /* Someone else could have slipped in a size
2405 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002406 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002407 * safe, and trust userspace not to race with
2408 * itself
2409 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002410 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002411 return -EBUSY;
2412 }
2413 }
NeilBrown83303b62006-01-06 00:21:06 -08002414 return len;
2415}
2416
2417static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002418__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002419
NeilBrown86e6ffd2005-11-08 21:39:24 -08002420static struct attribute *rdev_default_attrs[] = {
2421 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002422 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002423 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002424 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002425 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002426 NULL,
2427};
2428static ssize_t
2429rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2430{
2431 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2432 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002433 mddev_t *mddev = rdev->mddev;
2434 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002435
2436 if (!entry->show)
2437 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002438
2439 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2440 if (!rv) {
2441 if (rdev->mddev == NULL)
2442 rv = -EBUSY;
2443 else
2444 rv = entry->show(rdev, page);
2445 mddev_unlock(mddev);
2446 }
2447 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002448}
2449
2450static ssize_t
2451rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2452 const char *page, size_t length)
2453{
2454 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2455 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002456 ssize_t rv;
2457 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002458
2459 if (!entry->store)
2460 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002461 if (!capable(CAP_SYS_ADMIN))
2462 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002463 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002464 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002465 if (rdev->mddev == NULL)
2466 rv = -EBUSY;
2467 else
2468 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002469 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002470 }
2471 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002472}
2473
2474static void rdev_free(struct kobject *ko)
2475{
2476 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2477 kfree(rdev);
2478}
2479static struct sysfs_ops rdev_sysfs_ops = {
2480 .show = rdev_attr_show,
2481 .store = rdev_attr_store,
2482};
2483static struct kobj_type rdev_ktype = {
2484 .release = rdev_free,
2485 .sysfs_ops = &rdev_sysfs_ops,
2486 .default_attrs = rdev_default_attrs,
2487};
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489/*
2490 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2491 *
2492 * mark the device faulty if:
2493 *
2494 * - the device is nonexistent (zero size)
2495 * - the device has no valid superblock
2496 *
2497 * a faulty rdev _never_ has rdev->sb set.
2498 */
2499static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2500{
2501 char b[BDEVNAME_SIZE];
2502 int err;
2503 mdk_rdev_t *rdev;
2504 sector_t size;
2505
NeilBrown9ffae0c2006-01-06 00:20:32 -08002506 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if (!rdev) {
2508 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2509 return ERR_PTR(-ENOMEM);
2510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 if ((err = alloc_disk_sb(rdev)))
2513 goto abort_free;
2514
NeilBrownc5d79ad2008-02-06 01:39:54 -08002515 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (err)
2517 goto abort_free;
2518
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002519 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002522 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002523 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002524 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002526 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002528 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002529 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2532 if (!size) {
2533 printk(KERN_WARNING
2534 "md: %s has zero or unknown size, marking faulty!\n",
2535 bdevname(rdev->bdev,b));
2536 err = -EINVAL;
2537 goto abort_free;
2538 }
2539
2540 if (super_format >= 0) {
2541 err = super_types[super_format].
2542 load_super(rdev, NULL, super_minor);
2543 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002544 printk(KERN_WARNING
2545 "md: %s does not have a valid v%d.%d "
2546 "superblock, not importing!\n",
2547 bdevname(rdev->bdev,b),
2548 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 goto abort_free;
2550 }
2551 if (err < 0) {
2552 printk(KERN_WARNING
2553 "md: could not read %s's sb, not importing!\n",
2554 bdevname(rdev->bdev,b));
2555 goto abort_free;
2556 }
2557 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002560 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 return rdev;
2563
2564abort_free:
2565 if (rdev->sb_page) {
2566 if (rdev->bdev)
2567 unlock_rdev(rdev);
2568 free_disk_sb(rdev);
2569 }
2570 kfree(rdev);
2571 return ERR_PTR(err);
2572}
2573
2574/*
2575 * Check a full RAID array for plausibility
2576 */
2577
2578
NeilBrowna757e642005-04-16 15:26:42 -07002579static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002582 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 char b[BDEVNAME_SIZE];
2584
2585 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002586 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 switch (super_types[mddev->major_version].
2588 load_super(rdev, freshest, mddev->minor_version)) {
2589 case 1:
2590 freshest = rdev;
2591 break;
2592 case 0:
2593 break;
2594 default:
2595 printk( KERN_ERR \
2596 "md: fatal superblock inconsistency in %s"
2597 " -- removing from array\n",
2598 bdevname(rdev->bdev,b));
2599 kick_rdev_from_array(rdev);
2600 }
2601
2602
2603 super_types[mddev->major_version].
2604 validate_super(mddev, freshest);
2605
2606 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002607 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002608 if (rdev->desc_nr >= mddev->max_disks ||
2609 i > mddev->max_disks) {
2610 printk(KERN_WARNING
2611 "md: %s: %s: only %d devices permitted\n",
2612 mdname(mddev), bdevname(rdev->bdev, b),
2613 mddev->max_disks);
2614 kick_rdev_from_array(rdev);
2615 continue;
2616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (rdev != freshest)
2618 if (super_types[mddev->major_version].
2619 validate_super(mddev, rdev)) {
2620 printk(KERN_WARNING "md: kicking non-fresh %s"
2621 " from array!\n",
2622 bdevname(rdev->bdev,b));
2623 kick_rdev_from_array(rdev);
2624 continue;
2625 }
2626 if (mddev->level == LEVEL_MULTIPATH) {
2627 rdev->desc_nr = i++;
2628 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002629 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002630 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002631 rdev->raid_disk = -1;
2632 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
2634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
NeilBrown19052c02008-08-05 15:54:13 +10002637static void md_safemode_timeout(unsigned long data);
2638
NeilBrowneae17012005-11-08 21:39:23 -08002639static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002640safe_delay_show(mddev_t *mddev, char *page)
2641{
2642 int msec = (mddev->safemode_delay*1000)/HZ;
2643 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2644}
2645static ssize_t
2646safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2647{
2648 int scale=1;
2649 int dot=0;
2650 int i;
2651 unsigned long msec;
2652 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002653
NeilBrown16f17b32006-06-26 00:27:37 -07002654 /* remove a period, and count digits after it */
2655 if (len >= sizeof(buf))
2656 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002657 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002658 for (i=0; i<len; i++) {
2659 if (dot) {
2660 if (isdigit(buf[i])) {
2661 buf[i-1] = buf[i];
2662 scale *= 10;
2663 }
2664 buf[i] = 0;
2665 } else if (buf[i] == '.') {
2666 dot=1;
2667 buf[i] = 0;
2668 }
2669 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002670 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002671 return -EINVAL;
2672 msec = (msec * 1000) / scale;
2673 if (msec == 0)
2674 mddev->safemode_delay = 0;
2675 else {
NeilBrown19052c02008-08-05 15:54:13 +10002676 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002677 mddev->safemode_delay = (msec*HZ)/1000;
2678 if (mddev->safemode_delay == 0)
2679 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002680 if (mddev->safemode_delay < old_delay)
2681 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002682 }
2683 return len;
2684}
2685static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002686__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002687
2688static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002689level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002690{
NeilBrown2604b702006-01-06 00:20:36 -08002691 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002692 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002693 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002694 else if (mddev->clevel[0])
2695 return sprintf(page, "%s\n", mddev->clevel);
2696 else if (mddev->level != LEVEL_NONE)
2697 return sprintf(page, "%d\n", mddev->level);
2698 else
2699 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002700}
2701
NeilBrownd9d166c2006-01-06 00:20:51 -08002702static ssize_t
2703level_store(mddev_t *mddev, const char *buf, size_t len)
2704{
NeilBrown245f46c2009-03-31 14:39:39 +11002705 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002706 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002707 struct mdk_personality *pers;
2708 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002709 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002710
2711 if (mddev->pers == NULL) {
2712 if (len == 0)
2713 return 0;
2714 if (len >= sizeof(mddev->clevel))
2715 return -ENOSPC;
2716 strncpy(mddev->clevel, buf, len);
2717 if (mddev->clevel[len-1] == '\n')
2718 len--;
2719 mddev->clevel[len] = 0;
2720 mddev->level = LEVEL_NONE;
2721 return rv;
2722 }
2723
2724 /* request to change the personality. Need to ensure:
2725 * - array is not engaged in resync/recovery/reshape
2726 * - old personality can be suspended
2727 * - new personality will access other array.
2728 */
2729
2730 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002731 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002732
2733 if (!mddev->pers->quiesce) {
2734 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2735 mdname(mddev), mddev->pers->name);
2736 return -EINVAL;
2737 }
2738
2739 /* Now find the new personality */
2740 if (len == 0 || len >= sizeof(level))
2741 return -EINVAL;
2742 strncpy(level, buf, len);
2743 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002744 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002745 level[len] = 0;
2746
2747 request_module("md-%s", level);
2748 spin_lock(&pers_lock);
2749 pers = find_pers(LEVEL_NONE, level);
2750 if (!pers || !try_module_get(pers->owner)) {
2751 spin_unlock(&pers_lock);
2752 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2753 return -EINVAL;
2754 }
2755 spin_unlock(&pers_lock);
2756
2757 if (pers == mddev->pers) {
2758 /* Nothing to do! */
2759 module_put(pers->owner);
2760 return rv;
2761 }
2762 if (!pers->takeover) {
2763 module_put(pers->owner);
2764 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2765 mdname(mddev), level);
2766 return -EINVAL;
2767 }
2768
2769 /* ->takeover must set new_* and/or delta_disks
2770 * if it succeeds, and may set them when it fails.
2771 */
2772 priv = pers->takeover(mddev);
2773 if (IS_ERR(priv)) {
2774 mddev->new_level = mddev->level;
2775 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002776 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002777 mddev->raid_disks -= mddev->delta_disks;
2778 mddev->delta_disks = 0;
2779 module_put(pers->owner);
2780 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2781 mdname(mddev), level);
2782 return PTR_ERR(priv);
2783 }
2784
2785 /* Looks like we have a winner */
2786 mddev_suspend(mddev);
2787 mddev->pers->stop(mddev);
2788 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002789 /* Invalidate devices that are now superfluous */
2790 list_for_each_entry(rdev, &mddev->disks, same_set)
2791 if (rdev->raid_disk >= mddev->raid_disks) {
2792 rdev->raid_disk = -1;
2793 clear_bit(In_sync, &rdev->flags);
2794 }
NeilBrown245f46c2009-03-31 14:39:39 +11002795 mddev->pers = pers;
2796 mddev->private = priv;
2797 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2798 mddev->level = mddev->new_level;
2799 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002800 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002801 mddev->delta_disks = 0;
2802 pers->run(mddev);
2803 mddev_resume(mddev);
2804 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2805 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2806 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002807 return rv;
2808}
2809
2810static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002811__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002812
NeilBrownd4dbd022006-06-26 00:27:59 -07002813
2814static ssize_t
2815layout_show(mddev_t *mddev, char *page)
2816{
2817 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002818 if (mddev->reshape_position != MaxSector &&
2819 mddev->layout != mddev->new_layout)
2820 return sprintf(page, "%d (%d)\n",
2821 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002822 return sprintf(page, "%d\n", mddev->layout);
2823}
2824
2825static ssize_t
2826layout_store(mddev_t *mddev, const char *buf, size_t len)
2827{
2828 char *e;
2829 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002830
2831 if (!*buf || (*e && *e != '\n'))
2832 return -EINVAL;
2833
NeilBrownb3546032009-03-31 14:56:41 +11002834 if (mddev->pers) {
2835 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002836 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002837 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002838 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002839 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002840 if (err) {
2841 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002842 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002843 }
NeilBrownb3546032009-03-31 14:56:41 +11002844 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002845 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002846 if (mddev->reshape_position == MaxSector)
2847 mddev->layout = n;
2848 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002849 return len;
2850}
2851static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002852__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002853
2854
NeilBrowneae17012005-11-08 21:39:23 -08002855static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002856raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002857{
NeilBrownbb636542005-11-08 21:39:45 -08002858 if (mddev->raid_disks == 0)
2859 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002860 if (mddev->reshape_position != MaxSector &&
2861 mddev->delta_disks != 0)
2862 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2863 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002864 return sprintf(page, "%d\n", mddev->raid_disks);
2865}
2866
NeilBrownda943b992006-01-06 00:20:54 -08002867static int update_raid_disks(mddev_t *mddev, int raid_disks);
2868
2869static ssize_t
2870raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2871{
NeilBrownda943b992006-01-06 00:20:54 -08002872 char *e;
2873 int rv = 0;
2874 unsigned long n = simple_strtoul(buf, &e, 10);
2875
2876 if (!*buf || (*e && *e != '\n'))
2877 return -EINVAL;
2878
2879 if (mddev->pers)
2880 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002881 else if (mddev->reshape_position != MaxSector) {
2882 int olddisks = mddev->raid_disks - mddev->delta_disks;
2883 mddev->delta_disks = n - olddisks;
2884 mddev->raid_disks = n;
2885 } else
NeilBrownda943b992006-01-06 00:20:54 -08002886 mddev->raid_disks = n;
2887 return rv ? rv : len;
2888}
2889static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002890__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002891
NeilBrown24dd4692005-11-08 21:39:26 -08002892static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002893chunk_size_show(mddev_t *mddev, char *page)
2894{
NeilBrown08a02ec2007-05-09 02:35:38 -07002895 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10002896 mddev->chunk_sectors != mddev->new_chunk_sectors)
2897 return sprintf(page, "%d (%d)\n",
2898 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10002899 mddev->chunk_sectors << 9);
2900 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08002901}
2902
2903static ssize_t
2904chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2905{
NeilBrown3b343802006-01-06 00:20:47 -08002906 char *e;
2907 unsigned long n = simple_strtoul(buf, &e, 10);
2908
NeilBrown3b343802006-01-06 00:20:47 -08002909 if (!*buf || (*e && *e != '\n'))
2910 return -EINVAL;
2911
NeilBrownb3546032009-03-31 14:56:41 +11002912 if (mddev->pers) {
2913 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002914 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002915 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002916 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10002917 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002918 if (err) {
2919 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11002920 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002921 }
NeilBrownb3546032009-03-31 14:56:41 +11002922 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10002923 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002924 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10002925 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002926 }
NeilBrown3b343802006-01-06 00:20:47 -08002927 return len;
2928}
2929static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002930__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002931
NeilBrowna94213b2006-06-26 00:28:00 -07002932static ssize_t
2933resync_start_show(mddev_t *mddev, char *page)
2934{
NeilBrownd1a7c502009-03-31 15:24:32 +11002935 if (mddev->recovery_cp == MaxSector)
2936 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002937 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2938}
2939
2940static ssize_t
2941resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2942{
NeilBrowna94213b2006-06-26 00:28:00 -07002943 char *e;
2944 unsigned long long n = simple_strtoull(buf, &e, 10);
2945
2946 if (mddev->pers)
2947 return -EBUSY;
2948 if (!*buf || (*e && *e != '\n'))
2949 return -EINVAL;
2950
2951 mddev->recovery_cp = n;
2952 return len;
2953}
2954static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002955__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002956
NeilBrown9e653b62006-06-26 00:27:58 -07002957/*
2958 * The array state can be:
2959 *
2960 * clear
2961 * No devices, no size, no level
2962 * Equivalent to STOP_ARRAY ioctl
2963 * inactive
2964 * May have some settings, but array is not active
2965 * all IO results in error
2966 * When written, doesn't tear down array, but just stops it
2967 * suspended (not supported yet)
2968 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002969 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002970 * readonly
2971 * no resync can happen. no superblocks get written.
2972 * write requests fail
2973 * read-auto
2974 * like readonly, but behaves like 'clean' on a write request.
2975 *
2976 * clean - no pending writes, but otherwise active.
2977 * When written to inactive array, starts without resync
2978 * If a write request arrives then
2979 * if metadata is known, mark 'dirty' and switch to 'active'.
2980 * if not known, block and switch to write-pending
2981 * If written to an active array that has pending writes, then fails.
2982 * active
2983 * fully active: IO and resync can be happening.
2984 * When written to inactive array, starts with resync
2985 *
2986 * write-pending
2987 * clean, but writes are blocked waiting for 'active' to be written.
2988 *
2989 * active-idle
2990 * like active, but no writes have been seen for a while (100msec).
2991 *
2992 */
2993enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2994 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002995static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002996 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2997 "write-pending", "active-idle", NULL };
2998
2999static int match_word(const char *word, char **list)
3000{
3001 int n;
3002 for (n=0; list[n]; n++)
3003 if (cmd_match(word, list[n]))
3004 break;
3005 return n;
3006}
3007
3008static ssize_t
3009array_state_show(mddev_t *mddev, char *page)
3010{
3011 enum array_state st = inactive;
3012
3013 if (mddev->pers)
3014 switch(mddev->ro) {
3015 case 1:
3016 st = readonly;
3017 break;
3018 case 2:
3019 st = read_auto;
3020 break;
3021 case 0:
3022 if (mddev->in_sync)
3023 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003024 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3025 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003026 else if (mddev->safemode)
3027 st = active_idle;
3028 else
3029 st = active;
3030 }
3031 else {
3032 if (list_empty(&mddev->disks) &&
3033 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003034 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003035 st = clear;
3036 else
3037 st = inactive;
3038 }
3039 return sprintf(page, "%s\n", array_states[st]);
3040}
3041
Neil Browndf5b20c2008-07-11 22:02:22 +10003042static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003043static int do_md_run(mddev_t * mddev);
3044static int restart_array(mddev_t *mddev);
3045
3046static ssize_t
3047array_state_store(mddev_t *mddev, const char *buf, size_t len)
3048{
3049 int err = -EINVAL;
3050 enum array_state st = match_word(buf, array_states);
3051 switch(st) {
3052 case bad_word:
3053 break;
3054 case clear:
3055 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003056 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003057 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003058 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003059 break;
3060 case inactive:
3061 /* stopping an active array */
3062 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003063 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003064 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003065 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003066 } else
3067 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003068 break;
3069 case suspended:
3070 break; /* not supported yet */
3071 case readonly:
3072 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003073 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003074 else {
3075 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003076 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003077 err = do_md_run(mddev);
3078 }
3079 break;
3080 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003081 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003082 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003083 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003084 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003085 err = restart_array(mddev);
3086 if (err == 0) {
3087 mddev->ro = 2;
3088 set_disk_ro(mddev->gendisk, 0);
3089 }
NeilBrown9e653b62006-06-26 00:27:58 -07003090 } else {
3091 mddev->ro = 2;
3092 err = do_md_run(mddev);
3093 }
3094 break;
3095 case clean:
3096 if (mddev->pers) {
3097 restart_array(mddev);
3098 spin_lock_irq(&mddev->write_lock);
3099 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003100 if (mddev->in_sync == 0) {
3101 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003102 if (mddev->safemode == 1)
3103 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003104 if (mddev->persistent)
3105 set_bit(MD_CHANGE_CLEAN,
3106 &mddev->flags);
3107 }
3108 err = 0;
3109 } else
3110 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003111 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003112 } else
3113 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003114 break;
3115 case active:
3116 if (mddev->pers) {
3117 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003118 if (mddev->external)
3119 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003120 wake_up(&mddev->sb_wait);
3121 err = 0;
3122 } else {
3123 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003124 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003125 err = do_md_run(mddev);
3126 }
3127 break;
3128 case write_pending:
3129 case active_idle:
3130 /* these cannot be set */
3131 break;
3132 }
3133 if (err)
3134 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003135 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003136 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003137 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003138 }
NeilBrown9e653b62006-06-26 00:27:58 -07003139}
NeilBrown80ca3a42006-07-10 04:44:18 -07003140static struct md_sysfs_entry md_array_state =
3141__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003142
NeilBrown6d7ff732006-01-06 00:21:16 -08003143static ssize_t
3144null_show(mddev_t *mddev, char *page)
3145{
3146 return -EINVAL;
3147}
3148
3149static ssize_t
3150new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3151{
3152 /* buf must be %d:%d\n? giving major and minor numbers */
3153 /* The new device is added to the array.
3154 * If the array has a persistent superblock, we read the
3155 * superblock to initialise info and check validity.
3156 * Otherwise, only checking done is that in bind_rdev_to_array,
3157 * which mainly checks size.
3158 */
3159 char *e;
3160 int major = simple_strtoul(buf, &e, 10);
3161 int minor;
3162 dev_t dev;
3163 mdk_rdev_t *rdev;
3164 int err;
3165
3166 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3167 return -EINVAL;
3168 minor = simple_strtoul(e+1, &e, 10);
3169 if (*e && *e != '\n')
3170 return -EINVAL;
3171 dev = MKDEV(major, minor);
3172 if (major != MAJOR(dev) ||
3173 minor != MINOR(dev))
3174 return -EOVERFLOW;
3175
3176
3177 if (mddev->persistent) {
3178 rdev = md_import_device(dev, mddev->major_version,
3179 mddev->minor_version);
3180 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3181 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3182 mdk_rdev_t, same_set);
3183 err = super_types[mddev->major_version]
3184 .load_super(rdev, rdev0, mddev->minor_version);
3185 if (err < 0)
3186 goto out;
3187 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003188 } else if (mddev->external)
3189 rdev = md_import_device(dev, -2, -1);
3190 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003191 rdev = md_import_device(dev, -1, -1);
3192
3193 if (IS_ERR(rdev))
3194 return PTR_ERR(rdev);
3195 err = bind_rdev_to_array(rdev, mddev);
3196 out:
3197 if (err)
3198 export_rdev(rdev);
3199 return err ? err : len;
3200}
3201
3202static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003203__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003204
3205static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003206bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3207{
3208 char *end;
3209 unsigned long chunk, end_chunk;
3210
3211 if (!mddev->bitmap)
3212 goto out;
3213 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3214 while (*buf) {
3215 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3216 if (buf == end) break;
3217 if (*end == '-') { /* range */
3218 buf = end + 1;
3219 end_chunk = simple_strtoul(buf, &end, 0);
3220 if (buf == end) break;
3221 }
3222 if (*end && !isspace(*end)) break;
3223 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3224 buf = end;
3225 while (isspace(*buf)) buf++;
3226 }
3227 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3228out:
3229 return len;
3230}
3231
3232static struct md_sysfs_entry md_bitmap =
3233__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3234
3235static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003236size_show(mddev_t *mddev, char *page)
3237{
Andre Noll58c0fed2009-03-31 14:33:13 +11003238 return sprintf(page, "%llu\n",
3239 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003240}
3241
Andre Nolld71f9f82008-07-11 22:02:22 +10003242static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003243
3244static ssize_t
3245size_store(mddev_t *mddev, const char *buf, size_t len)
3246{
3247 /* If array is inactive, we can reduce the component size, but
3248 * not increase it (except from 0).
3249 * If array is active, we can try an on-line resize
3250 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003251 sector_t sectors;
3252 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003253
Andre Noll58c0fed2009-03-31 14:33:13 +11003254 if (err < 0)
3255 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003256 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003257 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003258 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003259 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003260 if (mddev->dev_sectors == 0 ||
3261 mddev->dev_sectors > sectors)
3262 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003263 else
3264 err = -ENOSPC;
3265 }
3266 return err ? err : len;
3267}
3268
3269static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003270__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003271
NeilBrown8bb93aa2006-01-06 00:20:50 -08003272
3273/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003274 * This is one of
3275 * 'none' for arrays with no metadata (good luck...)
3276 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003277 * or N.M for internally known formats
3278 */
3279static ssize_t
3280metadata_show(mddev_t *mddev, char *page)
3281{
3282 if (mddev->persistent)
3283 return sprintf(page, "%d.%d\n",
3284 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003285 else if (mddev->external)
3286 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003287 else
3288 return sprintf(page, "none\n");
3289}
3290
3291static ssize_t
3292metadata_store(mddev_t *mddev, const char *buf, size_t len)
3293{
3294 int major, minor;
3295 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003296 /* Changing the details of 'external' metadata is
3297 * always permitted. Otherwise there must be
3298 * no devices attached to the array.
3299 */
3300 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3301 ;
3302 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003303 return -EBUSY;
3304
3305 if (cmd_match(buf, "none")) {
3306 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003307 mddev->external = 0;
3308 mddev->major_version = 0;
3309 mddev->minor_version = 90;
3310 return len;
3311 }
3312 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003313 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003314 if (namelen >= sizeof(mddev->metadata_type))
3315 namelen = sizeof(mddev->metadata_type)-1;
3316 strncpy(mddev->metadata_type, buf+9, namelen);
3317 mddev->metadata_type[namelen] = 0;
3318 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3319 mddev->metadata_type[--namelen] = 0;
3320 mddev->persistent = 0;
3321 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003322 mddev->major_version = 0;
3323 mddev->minor_version = 90;
3324 return len;
3325 }
3326 major = simple_strtoul(buf, &e, 10);
3327 if (e==buf || *e != '.')
3328 return -EINVAL;
3329 buf = e+1;
3330 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003331 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003332 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003333 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003334 return -ENOENT;
3335 mddev->major_version = major;
3336 mddev->minor_version = minor;
3337 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003338 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003339 return len;
3340}
3341
3342static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003343__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003344
NeilBrowna35b0d62006-01-06 00:20:49 -08003345static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003346action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003347{
NeilBrown7eec3142005-11-08 21:39:44 -08003348 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003349 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3350 type = "frozen";
3351 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003352 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003353 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3354 type = "reshape";
3355 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003356 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3357 type = "resync";
3358 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3359 type = "check";
3360 else
3361 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003362 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003363 type = "recover";
3364 }
3365 return sprintf(page, "%s\n", type);
3366}
3367
3368static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003369action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003370{
NeilBrown7eec3142005-11-08 21:39:44 -08003371 if (!mddev->pers || !mddev->pers->sync_request)
3372 return -EINVAL;
3373
NeilBrownb6a9ce62009-05-26 09:41:17 +10003374 if (cmd_match(page, "frozen"))
3375 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3376 else
3377 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3378
3379 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003380 if (mddev->sync_thread) {
3381 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3382 md_unregister_thread(mddev->sync_thread);
3383 mddev->sync_thread = NULL;
3384 mddev->recovery = 0;
3385 }
NeilBrown03c902e2006-01-06 00:20:46 -08003386 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3387 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003388 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003389 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003390 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003391 else if (cmd_match(page, "recover")) {
3392 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3393 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3394 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003395 int err;
3396 if (mddev->pers->start_reshape == NULL)
3397 return -EINVAL;
3398 err = mddev->pers->start_reshape(mddev);
3399 if (err)
3400 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003401 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003402 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003403 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003404 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003405 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003406 return -EINVAL;
3407 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3408 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003409 }
NeilBrown03c902e2006-01-06 00:20:46 -08003410 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003411 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003412 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003413 return len;
3414}
3415
NeilBrown9d888832005-11-08 21:39:26 -08003416static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003417mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003418{
3419 return sprintf(page, "%llu\n",
3420 (unsigned long long) mddev->resync_mismatches);
3421}
3422
NeilBrown80ca3a42006-07-10 04:44:18 -07003423static struct md_sysfs_entry md_scan_mode =
3424__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003425
NeilBrown96de1e62005-11-08 21:39:39 -08003426
NeilBrown80ca3a42006-07-10 04:44:18 -07003427static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003428
NeilBrown88202a02006-01-06 00:21:36 -08003429static ssize_t
3430sync_min_show(mddev_t *mddev, char *page)
3431{
3432 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3433 mddev->sync_speed_min ? "local": "system");
3434}
3435
3436static ssize_t
3437sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3438{
3439 int min;
3440 char *e;
3441 if (strncmp(buf, "system", 6)==0) {
3442 mddev->sync_speed_min = 0;
3443 return len;
3444 }
3445 min = simple_strtoul(buf, &e, 10);
3446 if (buf == e || (*e && *e != '\n') || min <= 0)
3447 return -EINVAL;
3448 mddev->sync_speed_min = min;
3449 return len;
3450}
3451
3452static struct md_sysfs_entry md_sync_min =
3453__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3454
3455static ssize_t
3456sync_max_show(mddev_t *mddev, char *page)
3457{
3458 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3459 mddev->sync_speed_max ? "local": "system");
3460}
3461
3462static ssize_t
3463sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3464{
3465 int max;
3466 char *e;
3467 if (strncmp(buf, "system", 6)==0) {
3468 mddev->sync_speed_max = 0;
3469 return len;
3470 }
3471 max = simple_strtoul(buf, &e, 10);
3472 if (buf == e || (*e && *e != '\n') || max <= 0)
3473 return -EINVAL;
3474 mddev->sync_speed_max = max;
3475 return len;
3476}
3477
3478static struct md_sysfs_entry md_sync_max =
3479__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3480
Iustin Popd7f3d292007-10-16 23:30:54 -07003481static ssize_t
3482degraded_show(mddev_t *mddev, char *page)
3483{
3484 return sprintf(page, "%d\n", mddev->degraded);
3485}
3486static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003487
3488static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003489sync_force_parallel_show(mddev_t *mddev, char *page)
3490{
3491 return sprintf(page, "%d\n", mddev->parallel_resync);
3492}
3493
3494static ssize_t
3495sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3496{
3497 long n;
3498
3499 if (strict_strtol(buf, 10, &n))
3500 return -EINVAL;
3501
3502 if (n != 0 && n != 1)
3503 return -EINVAL;
3504
3505 mddev->parallel_resync = n;
3506
3507 if (mddev->sync_thread)
3508 wake_up(&resync_wait);
3509
3510 return len;
3511}
3512
3513/* force parallel resync, even with shared block devices */
3514static struct md_sysfs_entry md_sync_force_parallel =
3515__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3516 sync_force_parallel_show, sync_force_parallel_store);
3517
3518static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003519sync_speed_show(mddev_t *mddev, char *page)
3520{
3521 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003522 if (mddev->curr_resync == 0)
3523 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003524 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3525 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003526 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003527 db = resync - mddev->resync_mark_cnt;
3528 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003529}
3530
NeilBrown80ca3a42006-07-10 04:44:18 -07003531static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003532
3533static ssize_t
3534sync_completed_show(mddev_t *mddev, char *page)
3535{
Andre Noll58c0fed2009-03-31 14:33:13 +11003536 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003537
NeilBrownacb180b2009-04-14 16:28:34 +10003538 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3539 return sprintf(page, "none\n");
3540
NeilBrown88202a02006-01-06 00:21:36 -08003541 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003542 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003543 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003544 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003545
NeilBrownacb180b2009-04-14 16:28:34 +10003546 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003547 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003548}
3549
NeilBrown80ca3a42006-07-10 04:44:18 -07003550static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003551
NeilBrowne464eaf2006-03-27 01:18:14 -08003552static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003553min_sync_show(mddev_t *mddev, char *page)
3554{
3555 return sprintf(page, "%llu\n",
3556 (unsigned long long)mddev->resync_min);
3557}
3558static ssize_t
3559min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3560{
3561 unsigned long long min;
3562 if (strict_strtoull(buf, 10, &min))
3563 return -EINVAL;
3564 if (min > mddev->resync_max)
3565 return -EINVAL;
3566 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3567 return -EBUSY;
3568
3569 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003570 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003571 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003572 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003573 return -EINVAL;
3574 }
3575 mddev->resync_min = min;
3576
3577 return len;
3578}
3579
3580static struct md_sysfs_entry md_min_sync =
3581__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3582
3583static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003584max_sync_show(mddev_t *mddev, char *page)
3585{
3586 if (mddev->resync_max == MaxSector)
3587 return sprintf(page, "max\n");
3588 else
3589 return sprintf(page, "%llu\n",
3590 (unsigned long long)mddev->resync_max);
3591}
3592static ssize_t
3593max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3594{
3595 if (strncmp(buf, "max", 3) == 0)
3596 mddev->resync_max = MaxSector;
3597 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003598 unsigned long long max;
3599 if (strict_strtoull(buf, 10, &max))
3600 return -EINVAL;
3601 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003602 return -EINVAL;
3603 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003604 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003605 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3606 return -EBUSY;
3607
3608 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003609 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003610 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003611 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003612 return -EINVAL;
3613 }
3614 mddev->resync_max = max;
3615 }
3616 wake_up(&mddev->recovery_wait);
3617 return len;
3618}
3619
3620static struct md_sysfs_entry md_max_sync =
3621__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3622
3623static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003624suspend_lo_show(mddev_t *mddev, char *page)
3625{
3626 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3627}
3628
3629static ssize_t
3630suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3631{
3632 char *e;
3633 unsigned long long new = simple_strtoull(buf, &e, 10);
3634
NeilBrownb8d966e2009-07-01 11:14:04 +10003635 if (mddev->pers == NULL ||
3636 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003637 return -EINVAL;
3638 if (buf == e || (*e && *e != '\n'))
3639 return -EINVAL;
3640 if (new >= mddev->suspend_hi ||
3641 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3642 mddev->suspend_lo = new;
3643 mddev->pers->quiesce(mddev, 2);
3644 return len;
3645 } else
3646 return -EINVAL;
3647}
3648static struct md_sysfs_entry md_suspend_lo =
3649__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3650
3651
3652static ssize_t
3653suspend_hi_show(mddev_t *mddev, char *page)
3654{
3655 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3656}
3657
3658static ssize_t
3659suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3660{
3661 char *e;
3662 unsigned long long new = simple_strtoull(buf, &e, 10);
3663
NeilBrownb8d966e2009-07-01 11:14:04 +10003664 if (mddev->pers == NULL ||
3665 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003666 return -EINVAL;
3667 if (buf == e || (*e && *e != '\n'))
3668 return -EINVAL;
3669 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3670 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3671 mddev->suspend_hi = new;
3672 mddev->pers->quiesce(mddev, 1);
3673 mddev->pers->quiesce(mddev, 0);
3674 return len;
3675 } else
3676 return -EINVAL;
3677}
3678static struct md_sysfs_entry md_suspend_hi =
3679__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3680
NeilBrown08a02ec2007-05-09 02:35:38 -07003681static ssize_t
3682reshape_position_show(mddev_t *mddev, char *page)
3683{
3684 if (mddev->reshape_position != MaxSector)
3685 return sprintf(page, "%llu\n",
3686 (unsigned long long)mddev->reshape_position);
3687 strcpy(page, "none\n");
3688 return 5;
3689}
3690
3691static ssize_t
3692reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3693{
3694 char *e;
3695 unsigned long long new = simple_strtoull(buf, &e, 10);
3696 if (mddev->pers)
3697 return -EBUSY;
3698 if (buf == e || (*e && *e != '\n'))
3699 return -EINVAL;
3700 mddev->reshape_position = new;
3701 mddev->delta_disks = 0;
3702 mddev->new_level = mddev->level;
3703 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003704 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 return len;
3706}
3707
3708static struct md_sysfs_entry md_reshape_position =
3709__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3710 reshape_position_store);
3711
Dan Williamsb522adc2009-03-31 15:00:31 +11003712static ssize_t
3713array_size_show(mddev_t *mddev, char *page)
3714{
3715 if (mddev->external_size)
3716 return sprintf(page, "%llu\n",
3717 (unsigned long long)mddev->array_sectors/2);
3718 else
3719 return sprintf(page, "default\n");
3720}
3721
3722static ssize_t
3723array_size_store(mddev_t *mddev, const char *buf, size_t len)
3724{
3725 sector_t sectors;
3726
3727 if (strncmp(buf, "default", 7) == 0) {
3728 if (mddev->pers)
3729 sectors = mddev->pers->size(mddev, 0, 0);
3730 else
3731 sectors = mddev->array_sectors;
3732
3733 mddev->external_size = 0;
3734 } else {
3735 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3736 return -EINVAL;
3737 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003738 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003739
3740 mddev->external_size = 1;
3741 }
3742
3743 mddev->array_sectors = sectors;
3744 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003745 if (mddev->pers)
3746 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003747
3748 return len;
3749}
3750
3751static struct md_sysfs_entry md_array_size =
3752__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3753 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003754
NeilBrowneae17012005-11-08 21:39:23 -08003755static struct attribute *md_default_attrs[] = {
3756 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003757 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003758 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003759 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003760 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003761 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003762 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003763 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003764 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003765 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003766 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003767 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003768 NULL,
3769};
3770
3771static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003772 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003773 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003774 &md_sync_min.attr,
3775 &md_sync_max.attr,
3776 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003777 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003778 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003779 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003780 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003781 &md_suspend_lo.attr,
3782 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003783 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003784 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003785 NULL,
3786};
NeilBrown411036f2005-11-08 21:39:40 -08003787static struct attribute_group md_redundancy_group = {
3788 .name = NULL,
3789 .attrs = md_redundancy_attrs,
3790};
3791
NeilBrowneae17012005-11-08 21:39:23 -08003792
3793static ssize_t
3794md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3795{
3796 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3797 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003798 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003799
3800 if (!entry->show)
3801 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003802 rv = mddev_lock(mddev);
3803 if (!rv) {
3804 rv = entry->show(mddev, page);
3805 mddev_unlock(mddev);
3806 }
NeilBrown96de1e62005-11-08 21:39:39 -08003807 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003808}
3809
3810static ssize_t
3811md_attr_store(struct kobject *kobj, struct attribute *attr,
3812 const char *page, size_t length)
3813{
3814 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3815 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003816 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003817
3818 if (!entry->store)
3819 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003820 if (!capable(CAP_SYS_ADMIN))
3821 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003822 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003823 if (mddev->hold_active == UNTIL_IOCTL)
3824 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003825 if (!rv) {
3826 rv = entry->store(mddev, page, length);
3827 mddev_unlock(mddev);
3828 }
NeilBrown96de1e62005-11-08 21:39:39 -08003829 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003830}
3831
3832static void md_free(struct kobject *ko)
3833{
3834 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003835
3836 if (mddev->sysfs_state)
3837 sysfs_put(mddev->sysfs_state);
3838
3839 if (mddev->gendisk) {
3840 del_gendisk(mddev->gendisk);
3841 put_disk(mddev->gendisk);
3842 }
3843 if (mddev->queue)
3844 blk_cleanup_queue(mddev->queue);
3845
NeilBrowneae17012005-11-08 21:39:23 -08003846 kfree(mddev);
3847}
3848
3849static struct sysfs_ops md_sysfs_ops = {
3850 .show = md_attr_show,
3851 .store = md_attr_store,
3852};
3853static struct kobj_type md_ktype = {
3854 .release = md_free,
3855 .sysfs_ops = &md_sysfs_ops,
3856 .default_attrs = md_default_attrs,
3857};
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859int mdp_major = 0;
3860
Dan Williams5fd3a172009-03-04 00:57:25 -07003861static void mddev_delayed_delete(struct work_struct *ws)
3862{
3863 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3864
3865 if (mddev->private == &md_redundancy_group) {
3866 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3867 if (mddev->sysfs_action)
3868 sysfs_put(mddev->sysfs_action);
3869 mddev->sysfs_action = NULL;
3870 mddev->private = NULL;
3871 }
3872 kobject_del(&mddev->kobj);
3873 kobject_put(&mddev->kobj);
3874}
3875
NeilBrownefeb53c2009-01-09 08:31:10 +11003876static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003878 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 mddev_t *mddev = mddev_find(dev);
3880 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003881 int partitioned;
3882 int shift;
3883 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003884 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003887 return -ENODEV;
3888
3889 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3890 shift = partitioned ? MdpMinorShift : 0;
3891 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
NeilBrownd3374822009-01-09 08:31:10 +11003893 /* wait for any previous instance if this device
3894 * to be completed removed (mddev_delayed_delete).
3895 */
3896 flush_scheduled_work();
3897
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003898 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10003899 error = -EEXIST;
3900 if (mddev->gendisk)
3901 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003902
3903 if (name) {
3904 /* Need to ensure that 'name' is not a duplicate.
3905 */
3906 mddev_t *mddev2;
3907 spin_lock(&all_mddevs_lock);
3908
3909 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3910 if (mddev2->gendisk &&
3911 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3912 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10003913 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003914 }
3915 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 }
NeilBrown8b765392009-01-09 08:31:08 +11003917
NeilBrown0909dc42009-07-01 12:27:21 +10003918 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003919 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10003920 if (!mddev->queue)
3921 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11003922 mddev->queue->queuedata = mddev;
3923
NeilBrown8b765392009-01-09 08:31:08 +11003924 /* Can be unlocked because the queue is new: no concurrency */
3925 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3926
NeilBrown409c57f2009-03-31 14:39:39 +11003927 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 disk = alloc_disk(1 << shift);
3930 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11003931 blk_cleanup_queue(mddev->queue);
3932 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10003933 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003935 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003937 if (name)
3938 strcpy(disk->disk_name, name);
3939 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003941 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 disk->fops = &md_fops;
3944 disk->private_data = mddev;
3945 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003946 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003947 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003948 * remove it now.
3949 */
3950 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 add_disk(disk);
3952 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003953 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3954 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10003955 if (error) {
3956 /* This isn't possible, but as kobject_init_and_add is marked
3957 * __must_check, we must do something with the result
3958 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08003959 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3960 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10003961 error = 0;
3962 }
3963 abort:
3964 mutex_unlock(&disks_mutex);
3965 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003966 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003967 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3968 }
NeilBrownd3374822009-01-09 08:31:10 +11003969 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10003970 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11003971}
3972
3973static struct kobject *md_probe(dev_t dev, int *part, void *data)
3974{
3975 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return NULL;
3977}
3978
NeilBrownefeb53c2009-01-09 08:31:10 +11003979static int add_named_array(const char *val, struct kernel_param *kp)
3980{
3981 /* val must be "md_*" where * is not all digits.
3982 * We allocate an array with a large free minor number, and
3983 * set the name to val. val must not already be an active name.
3984 */
3985 int len = strlen(val);
3986 char buf[DISK_NAME_LEN];
3987
3988 while (len && val[len-1] == '\n')
3989 len--;
3990 if (len >= DISK_NAME_LEN)
3991 return -E2BIG;
3992 strlcpy(buf, val, len+1);
3993 if (strncmp(buf, "md_", 3) != 0)
3994 return -EINVAL;
3995 return md_alloc(0, buf);
3996}
3997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998static void md_safemode_timeout(unsigned long data)
3999{
4000 mddev_t *mddev = (mddev_t *) data;
4001
Neil Brown0fd62b82008-06-28 08:31:36 +10004002 if (!atomic_read(&mddev->writes_pending)) {
4003 mddev->safemode = 1;
4004 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004005 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 md_wakeup_thread(mddev->thread);
4008}
4009
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004010static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
4012static int do_md_run(mddev_t * mddev)
4013{
NeilBrown2604b702006-01-06 00:20:36 -08004014 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 mdk_rdev_t *rdev;
4016 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004017 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
NeilBrowna757e642005-04-16 15:26:42 -07004019 if (list_empty(&mddev->disks))
4020 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023 if (mddev->pers)
4024 return -EBUSY;
4025
4026 /*
4027 * Analyze all RAID superblock(s)
4028 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004029 if (!mddev->raid_disks) {
4030 if (!mddev->persistent)
4031 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004032 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
NeilBrownd9d166c2006-01-06 00:20:51 -08004035 if (mddev->level != LEVEL_NONE)
4036 request_module("md-level-%d", mddev->level);
4037 else if (mddev->clevel[0])
4038 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040 /*
4041 * Drop all container device buffers, from now on
4042 * the only valid external interface is through the md
4043 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004045 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004046 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 continue;
4048 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004049 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004050
4051 /* perform some consistency tests on the device.
4052 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004053 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004054 */
Andre Noll0f420352008-07-11 22:02:23 +10004055 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004056 if (mddev->dev_sectors &&
4057 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004058 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004059 printk("md: %s: data overlaps metadata\n",
4060 mdname(mddev));
4061 return -EINVAL;
4062 }
4063 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004064 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004065 > rdev->data_offset) {
4066 printk("md: %s: metadata overlaps data\n",
4067 mdname(mddev));
4068 return -EINVAL;
4069 }
4070 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004071 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
4073
4074 md_probe(mddev->unit, NULL, NULL);
4075 disk = mddev->gendisk;
4076 if (!disk)
4077 return -ENOMEM;
4078
4079 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004080 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004081 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004083 if (mddev->level != LEVEL_NONE)
4084 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4085 mddev->level);
4086 else
4087 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4088 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 return -EINVAL;
4090 }
NeilBrown2604b702006-01-06 00:20:36 -08004091 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004093 if (mddev->level != pers->level) {
4094 mddev->level = pers->level;
4095 mddev->new_level = pers->level;
4096 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004097 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
NeilBrownf6705572006-03-27 01:18:11 -08004099 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004100 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004101 /* This personality cannot handle reshaping... */
4102 mddev->pers = NULL;
4103 module_put(pers->owner);
4104 return -EINVAL;
4105 }
4106
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004107 if (pers->sync_request) {
4108 /* Warn if this is a potentially silly
4109 * configuration.
4110 */
4111 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4112 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004113 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004114
4115 list_for_each_entry(rdev, &mddev->disks, same_set)
4116 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004117 if (rdev < rdev2 &&
4118 rdev->bdev->bd_contains ==
4119 rdev2->bdev->bd_contains) {
4120 printk(KERN_WARNING
4121 "%s: WARNING: %s appears to be"
4122 " on the same physical disk as"
4123 " %s.\n",
4124 mdname(mddev),
4125 bdevname(rdev->bdev,b),
4126 bdevname(rdev2->bdev,b2));
4127 warned = 1;
4128 }
4129 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004130
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004131 if (warned)
4132 printk(KERN_WARNING
4133 "True protection against single-disk"
4134 " failure might be compromised.\n");
4135 }
4136
NeilBrown657390d2005-08-26 18:34:16 -07004137 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004138 /* may be over-ridden by personality */
4139 mddev->resync_max_sectors = mddev->dev_sectors;
4140
NeilBrowna9701a32005-11-08 21:39:34 -08004141 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004142 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
NeilBrownf91de922005-11-08 21:39:36 -08004144 if (start_readonly)
4145 mddev->ro = 2; /* read-only, but switch on first write */
4146
NeilBrownb15c2e52006-01-06 00:20:16 -08004147 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004148 if (err)
4149 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004150 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4151 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4152 " but 'external_size' not in effect?\n", __func__);
4153 printk(KERN_ERR
4154 "md: invalid array_size %llu > default size %llu\n",
4155 (unsigned long long)mddev->array_sectors / 2,
4156 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4157 err = -EINVAL;
4158 mddev->pers->stop(mddev);
4159 }
4160 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004161 err = bitmap_create(mddev);
4162 if (err) {
4163 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4164 mdname(mddev), err);
4165 mddev->pers->stop(mddev);
4166 }
4167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 module_put(mddev->pers->owner);
4170 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004171 bitmap_destroy(mddev);
4172 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004174 if (mddev->pers->sync_request) {
4175 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4176 printk(KERN_WARNING
4177 "md: cannot register extra attributes for %s\n",
4178 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004179 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004180 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004181 mddev->ro = 0;
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 atomic_set(&mddev->writes_pending,0);
4184 mddev->safemode = 0;
4185 mddev->safemode_timer.function = md_safemode_timeout;
4186 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004187 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004189
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004190 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004191 if (rdev->raid_disk >= 0) {
4192 char nm[20];
4193 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004194 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4195 printk("md: cannot register %s for %s\n",
4196 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4200
NeilBrown850b2b42006-10-03 01:15:46 -07004201 if (mddev->flags)
4202 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Andre Nollf233ea52008-07-21 17:05:22 +10004204 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004206 /* If there is a partially-recovered drive we need to
4207 * start recovery here. If we leave it to md_check_recovery,
4208 * it will remove the drives and not do the right thing
4209 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004210 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004211 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004212 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004213 if (rdev->raid_disk >= 0 &&
4214 !test_bit(In_sync, &rdev->flags) &&
4215 !test_bit(Faulty, &rdev->flags))
4216 /* complete an interrupted recovery */
4217 spares++;
4218 if (spares && mddev->pers->sync_request) {
4219 mddev->recovery = 0;
4220 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4221 mddev->sync_thread = md_register_thread(md_do_sync,
4222 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004223 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004224 if (!mddev->sync_thread) {
4225 printk(KERN_ERR "%s: could not start resync"
4226 " thread...\n",
4227 mdname(mddev));
4228 /* leave the spares where they are, it shouldn't hurt */
4229 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004230 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004231 }
4232 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004233 md_wakeup_thread(mddev->thread);
4234 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004235
NeilBrown449aad32009-08-03 10:59:58 +10004236 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004237 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004238 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004239 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004240 if (mddev->sysfs_action)
4241 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004242 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004243 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 return 0;
4245}
4246
4247static int restart_array(mddev_t *mddev)
4248{
4249 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Andre Noll80fab1d2008-07-11 22:02:21 +10004251 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004253 return -ENXIO;
4254 if (!mddev->pers)
4255 return -EINVAL;
4256 if (!mddev->ro)
4257 return -EBUSY;
4258 mddev->safemode = 0;
4259 mddev->ro = 0;
4260 set_disk_ro(disk, 0);
4261 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4262 mdname(mddev));
4263 /* Kick recovery or resync if necessary */
4264 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4265 md_wakeup_thread(mddev->thread);
4266 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004267 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
NeilBrownacc55e22006-06-26 00:27:47 -07004271/* similar to deny_write_access, but accounts for our holding a reference
4272 * to the file ourselves */
4273static int deny_bitmap_write_access(struct file * file)
4274{
4275 struct inode *inode = file->f_mapping->host;
4276
4277 spin_lock(&inode->i_lock);
4278 if (atomic_read(&inode->i_writecount) > 1) {
4279 spin_unlock(&inode->i_lock);
4280 return -ETXTBSY;
4281 }
4282 atomic_set(&inode->i_writecount, -1);
4283 spin_unlock(&inode->i_lock);
4284
4285 return 0;
4286}
4287
4288static void restore_bitmap_write_access(struct file *file)
4289{
4290 struct inode *inode = file->f_mapping->host;
4291
4292 spin_lock(&inode->i_lock);
4293 atomic_set(&inode->i_writecount, 1);
4294 spin_unlock(&inode->i_lock);
4295}
4296
NeilBrown9e653b62006-06-26 00:27:58 -07004297/* mode:
4298 * 0 - completely stop and dis-assemble array
4299 * 1 - switch to readonly
4300 * 2 - stop but do not disassemble array
4301 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004302static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
4304 int err = 0;
4305 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004306 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
NeilBrownc8c00a62009-08-10 12:50:52 +10004308 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004309 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004310 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004311 err = -EBUSY;
4312 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
4314 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004315 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4317 md_unregister_thread(mddev->sync_thread);
4318 mddev->sync_thread = NULL;
4319 }
4320
4321 del_timer_sync(&mddev->safemode_timer);
4322
NeilBrown9e653b62006-06-26 00:27:58 -07004323 switch(mode) {
4324 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004326 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 goto out;
4328 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004329 break;
4330 case 0: /* disassemble */
4331 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004332 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004333 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (mddev->ro)
4335 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004338 mddev->queue->merge_bvec_fn = NULL;
4339 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004340 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004342 if (mddev->pers->sync_request)
4343 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004345 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004346 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004347
NeilBrownc4647292009-05-07 12:51:06 +10004348 list_for_each_entry(rdev, &mddev->disks, same_set)
4349 if (rdev->raid_disk >= 0) {
4350 char nm[20];
4351 sprintf(nm, "rd%d", rdev->raid_disk);
4352 sysfs_remove_link(&mddev->kobj, nm);
4353 }
4354
NeilBrown0d4ca602006-12-10 02:20:44 -08004355 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004356 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004357
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 if (mddev->ro)
4359 mddev->ro = 0;
4360 }
NeilBrown850b2b42006-10-03 01:15:46 -07004361 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 /* mark array as shutdown cleanly */
4363 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004364 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 }
NeilBrown9e653b62006-06-26 00:27:58 -07004366 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004368 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004369 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004371out:
4372 mutex_unlock(&mddev->open_mutex);
4373 if (err)
4374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 /*
4376 * Free resources if final stop
4377 */
NeilBrown9e653b62006-06-26 00:27:58 -07004378 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004379
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4381
NeilBrown978f9462006-02-02 14:28:05 -08004382 bitmap_destroy(mddev);
4383 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004384 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004385 fput(mddev->bitmap_file);
4386 mddev->bitmap_file = NULL;
4387 }
4388 mddev->bitmap_offset = 0;
4389
NeilBrown177a99b2008-02-06 01:39:56 -08004390 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004391 flush_scheduled_work();
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 export_array(mddev);
4394
Andre Nollf233ea52008-07-21 17:05:22 +10004395 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004396 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004397 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004398 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004399 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004400 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004401 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004402 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004403 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004404 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004405 mddev->level = LEVEL_NONE;
4406 mddev->clevel[0] = 0;
4407 mddev->flags = 0;
4408 mddev->ro = 0;
4409 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004410 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004411 mddev->ctime = mddev->utime = 0;
4412 mddev->layout = 0;
4413 mddev->max_disks = 0;
4414 mddev->events = 0;
4415 mddev->delta_disks = 0;
4416 mddev->new_level = LEVEL_NONE;
4417 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004418 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004419 mddev->curr_resync = 0;
4420 mddev->resync_mismatches = 0;
4421 mddev->suspend_lo = mddev->suspend_hi = 0;
4422 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4423 mddev->recovery = 0;
4424 mddev->in_sync = 0;
4425 mddev->changed = 0;
4426 mddev->degraded = 0;
4427 mddev->barriers_work = 0;
4428 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004429 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004430 if (mddev->hold_active == UNTIL_STOP)
4431 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004432
NeilBrowna8a55c32006-06-26 00:27:39 -07004433 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4435 mdname(mddev));
4436 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004437 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004438 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004439 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return err;
4441}
4442
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004443#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444static void autorun_array(mddev_t *mddev)
4445{
4446 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 int err;
4448
NeilBrowna757e642005-04-16 15:26:42 -07004449 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
4452 printk(KERN_INFO "md: running: ");
4453
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004454 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 char b[BDEVNAME_SIZE];
4456 printk("<%s>", bdevname(rdev->bdev,b));
4457 }
4458 printk("\n");
4459
NeilBrownd710e132008-10-13 11:55:12 +11004460 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 if (err) {
4462 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004463 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 }
4465}
4466
4467/*
4468 * lets try to run arrays based on all disks that have arrived
4469 * until now. (those are in pending_raid_disks)
4470 *
4471 * the method: pick the first pending disk, collect all disks with
4472 * the same UUID, remove all from the pending list and put them into
4473 * the 'same_array' list. Then order this list based on superblock
4474 * update time (freshest comes first), kick out 'old' disks and
4475 * compare superblocks. If everything's fine then run it.
4476 *
4477 * If "unit" is allocated, then bump its reference count
4478 */
4479static void autorun_devices(int part)
4480{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004481 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 mddev_t *mddev;
4483 char b[BDEVNAME_SIZE];
4484
4485 printk(KERN_INFO "md: autorun ...\n");
4486 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004487 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004489 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 rdev0 = list_entry(pending_raid_disks.next,
4491 mdk_rdev_t, same_set);
4492
4493 printk(KERN_INFO "md: considering %s ...\n",
4494 bdevname(rdev0->bdev,b));
4495 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004496 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 if (super_90_load(rdev, rdev0, 0) >= 0) {
4498 printk(KERN_INFO "md: adding %s ...\n",
4499 bdevname(rdev->bdev,b));
4500 list_move(&rdev->same_set, &candidates);
4501 }
4502 /*
4503 * now we have a set of devices, with all of them having
4504 * mostly sane superblocks. It's time to allocate the
4505 * mddev.
4506 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004507 if (part) {
4508 dev = MKDEV(mdp_major,
4509 rdev0->preferred_minor << MdpMinorShift);
4510 unit = MINOR(dev) >> MdpMinorShift;
4511 } else {
4512 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4513 unit = MINOR(dev);
4514 }
4515 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4517 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4518 break;
4519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 md_probe(dev, NULL, NULL);
4522 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004523 if (!mddev || !mddev->gendisk) {
4524 if (mddev)
4525 mddev_put(mddev);
4526 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 "md: cannot allocate memory for md drive.\n");
4528 break;
4529 }
4530 if (mddev_lock(mddev))
4531 printk(KERN_WARNING "md: %s locked, cannot run\n",
4532 mdname(mddev));
4533 else if (mddev->raid_disks || mddev->major_version
4534 || !list_empty(&mddev->disks)) {
4535 printk(KERN_WARNING
4536 "md: %s already running, cannot run %s\n",
4537 mdname(mddev), bdevname(rdev0->bdev,b));
4538 mddev_unlock(mddev);
4539 } else {
4540 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004541 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004542 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 list_del_init(&rdev->same_set);
4544 if (bind_rdev_to_array(rdev, mddev))
4545 export_rdev(rdev);
4546 }
4547 autorun_array(mddev);
4548 mddev_unlock(mddev);
4549 }
4550 /* on success, candidates will be empty, on error
4551 * it won't...
4552 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004553 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004554 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 mddev_put(mddev);
4558 }
4559 printk(KERN_INFO "md: ... autorun DONE.\n");
4560}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004561#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563static int get_version(void __user * arg)
4564{
4565 mdu_version_t ver;
4566
4567 ver.major = MD_MAJOR_VERSION;
4568 ver.minor = MD_MINOR_VERSION;
4569 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4570
4571 if (copy_to_user(arg, &ver, sizeof(ver)))
4572 return -EFAULT;
4573
4574 return 0;
4575}
4576
4577static int get_array_info(mddev_t * mddev, void __user * arg)
4578{
4579 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004580 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
NeilBrowna9f326e2009-09-23 18:06:41 +10004583 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004584 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004586 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 failed++;
4588 else {
4589 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004590 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004591 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 else
4593 spare++;
4594 }
4595 }
4596
4597 info.major_version = mddev->major_version;
4598 info.minor_version = mddev->minor_version;
4599 info.patch_version = MD_PATCHLEVEL_VERSION;
4600 info.ctime = mddev->ctime;
4601 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004602 info.size = mddev->dev_sectors / 2;
4603 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004604 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 info.nr_disks = nr;
4606 info.raid_disks = mddev->raid_disks;
4607 info.md_minor = mddev->md_minor;
4608 info.not_persistent= !mddev->persistent;
4609
4610 info.utime = mddev->utime;
4611 info.state = 0;
4612 if (mddev->in_sync)
4613 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004614 if (mddev->bitmap && mddev->bitmap_offset)
4615 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004616 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 info.working_disks = working;
4618 info.failed_disks = failed;
4619 info.spare_disks = spare;
4620
4621 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004622 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624 if (copy_to_user(arg, &info, sizeof(info)))
4625 return -EFAULT;
4626
4627 return 0;
4628}
4629
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004630static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004631{
4632 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4633 char *ptr, *buf = NULL;
4634 int err = -ENOMEM;
4635
Dan Williamsb5470dc2008-06-27 21:44:04 -07004636 if (md_allow_write(mddev))
4637 file = kmalloc(sizeof(*file), GFP_NOIO);
4638 else
4639 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004640
NeilBrown32a76272005-06-21 17:17:14 -07004641 if (!file)
4642 goto out;
4643
4644 /* bitmap disabled, zero the first byte and copy out */
4645 if (!mddev->bitmap || !mddev->bitmap->file) {
4646 file->pathname[0] = '\0';
4647 goto copy_out;
4648 }
4649
4650 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4651 if (!buf)
4652 goto out;
4653
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004654 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4655 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004656 goto out;
4657
4658 strcpy(file->pathname, ptr);
4659
4660copy_out:
4661 err = 0;
4662 if (copy_to_user(arg, file, sizeof(*file)))
4663 err = -EFAULT;
4664out:
4665 kfree(buf);
4666 kfree(file);
4667 return err;
4668}
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670static int get_disk_info(mddev_t * mddev, void __user * arg)
4671{
4672 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 mdk_rdev_t *rdev;
4674
4675 if (copy_from_user(&info, arg, sizeof(info)))
4676 return -EFAULT;
4677
Andre Noll26ef3792008-07-11 22:02:21 +10004678 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 if (rdev) {
4680 info.major = MAJOR(rdev->bdev->bd_dev);
4681 info.minor = MINOR(rdev->bdev->bd_dev);
4682 info.raid_disk = rdev->raid_disk;
4683 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004684 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004686 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 info.state |= (1<<MD_DISK_ACTIVE);
4688 info.state |= (1<<MD_DISK_SYNC);
4689 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004690 if (test_bit(WriteMostly, &rdev->flags))
4691 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 } else {
4693 info.major = info.minor = 0;
4694 info.raid_disk = -1;
4695 info.state = (1<<MD_DISK_REMOVED);
4696 }
4697
4698 if (copy_to_user(arg, &info, sizeof(info)))
4699 return -EFAULT;
4700
4701 return 0;
4702}
4703
4704static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4705{
4706 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4707 mdk_rdev_t *rdev;
4708 dev_t dev = MKDEV(info->major,info->minor);
4709
4710 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4711 return -EOVERFLOW;
4712
4713 if (!mddev->raid_disks) {
4714 int err;
4715 /* expecting a device which has a superblock */
4716 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4717 if (IS_ERR(rdev)) {
4718 printk(KERN_WARNING
4719 "md: md_import_device returned %ld\n",
4720 PTR_ERR(rdev));
4721 return PTR_ERR(rdev);
4722 }
4723 if (!list_empty(&mddev->disks)) {
4724 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4725 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004726 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 .load_super(rdev, rdev0, mddev->minor_version);
4728 if (err < 0) {
4729 printk(KERN_WARNING
4730 "md: %s has different UUID to %s\n",
4731 bdevname(rdev->bdev,b),
4732 bdevname(rdev0->bdev,b2));
4733 export_rdev(rdev);
4734 return -EINVAL;
4735 }
4736 }
4737 err = bind_rdev_to_array(rdev, mddev);
4738 if (err)
4739 export_rdev(rdev);
4740 return err;
4741 }
4742
4743 /*
4744 * add_new_disk can be used once the array is assembled
4745 * to add "hot spares". They must already have a superblock
4746 * written
4747 */
4748 if (mddev->pers) {
4749 int err;
4750 if (!mddev->pers->hot_add_disk) {
4751 printk(KERN_WARNING
4752 "%s: personality does not support diskops!\n",
4753 mdname(mddev));
4754 return -EINVAL;
4755 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004756 if (mddev->persistent)
4757 rdev = md_import_device(dev, mddev->major_version,
4758 mddev->minor_version);
4759 else
4760 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 if (IS_ERR(rdev)) {
4762 printk(KERN_WARNING
4763 "md: md_import_device returned %ld\n",
4764 PTR_ERR(rdev));
4765 return PTR_ERR(rdev);
4766 }
NeilBrown41158c72005-06-21 17:17:25 -07004767 /* set save_raid_disk if appropriate */
4768 if (!mddev->persistent) {
4769 if (info->state & (1<<MD_DISK_SYNC) &&
4770 info->raid_disk < mddev->raid_disks)
4771 rdev->raid_disk = info->raid_disk;
4772 else
4773 rdev->raid_disk = -1;
4774 } else
4775 super_types[mddev->major_version].
4776 validate_super(mddev, rdev);
4777 rdev->saved_raid_disk = rdev->raid_disk;
4778
NeilBrownb2d444d2005-11-08 21:39:31 -08004779 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004780 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4781 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004782 else
4783 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 rdev->raid_disk = -1;
4786 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004787 if (!err && !mddev->pers->hot_remove_disk) {
4788 /* If there is hot_add_disk but no hot_remove_disk
4789 * then added disks for geometry changes,
4790 * and should be added immediately.
4791 */
4792 super_types[mddev->major_version].
4793 validate_super(mddev, rdev);
4794 err = mddev->pers->hot_add_disk(mddev, rdev);
4795 if (err)
4796 unbind_rdev_from_array(rdev);
4797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 if (err)
4799 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004800 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004801 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004802
NeilBrown17571282006-12-10 02:20:52 -08004803 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004804 if (mddev->degraded)
4805 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004806 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004807 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 return err;
4809 }
4810
4811 /* otherwise, add_new_disk is only allowed
4812 * for major_version==0 superblocks
4813 */
4814 if (mddev->major_version != 0) {
4815 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4816 mdname(mddev));
4817 return -EINVAL;
4818 }
4819
4820 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4821 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004822 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (IS_ERR(rdev)) {
4824 printk(KERN_WARNING
4825 "md: error, md_import_device() returned %ld\n",
4826 PTR_ERR(rdev));
4827 return PTR_ERR(rdev);
4828 }
4829 rdev->desc_nr = info->number;
4830 if (info->raid_disk < mddev->raid_disks)
4831 rdev->raid_disk = info->raid_disk;
4832 else
4833 rdev->raid_disk = -1;
4834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004836 if (info->state & (1<<MD_DISK_SYNC))
4837 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004839 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4840 set_bit(WriteMostly, &rdev->flags);
4841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 if (!mddev->persistent) {
4843 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004844 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 } else
Andre Noll0f420352008-07-11 22:02:23 +10004846 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004847 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
NeilBrown2bf071b2006-01-06 00:20:55 -08004849 err = bind_rdev_to_array(rdev, mddev);
4850 if (err) {
4851 export_rdev(rdev);
4852 return err;
4853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
4855
4856 return 0;
4857}
4858
4859static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4860{
4861 char b[BDEVNAME_SIZE];
4862 mdk_rdev_t *rdev;
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 rdev = find_rdev(mddev, dev);
4865 if (!rdev)
4866 return -ENXIO;
4867
4868 if (rdev->raid_disk >= 0)
4869 goto busy;
4870
4871 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004872 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004873 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875 return 0;
4876busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004877 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 bdevname(rdev->bdev,b), mdname(mddev));
4879 return -EBUSY;
4880}
4881
4882static int hot_add_disk(mddev_t * mddev, dev_t dev)
4883{
4884 char b[BDEVNAME_SIZE];
4885 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 mdk_rdev_t *rdev;
4887
4888 if (!mddev->pers)
4889 return -ENODEV;
4890
4891 if (mddev->major_version != 0) {
4892 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4893 " version-0 superblocks.\n",
4894 mdname(mddev));
4895 return -EINVAL;
4896 }
4897 if (!mddev->pers->hot_add_disk) {
4898 printk(KERN_WARNING
4899 "%s: personality does not support diskops!\n",
4900 mdname(mddev));
4901 return -EINVAL;
4902 }
4903
NeilBrownd710e132008-10-13 11:55:12 +11004904 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 if (IS_ERR(rdev)) {
4906 printk(KERN_WARNING
4907 "md: error, md_import_device() returned %ld\n",
4908 PTR_ERR(rdev));
4909 return -EINVAL;
4910 }
4911
4912 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004913 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 else
Andre Noll0f420352008-07-11 22:02:23 +10004915 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
NeilBrown8190e752009-06-18 08:48:58 +10004917 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
NeilBrownb2d444d2005-11-08 21:39:31 -08004919 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 printk(KERN_WARNING
4921 "md: can not hot-add faulty %s disk to %s!\n",
4922 bdevname(rdev->bdev,b), mdname(mddev));
4923 err = -EINVAL;
4924 goto abort_export;
4925 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004926 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004928 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004929 err = bind_rdev_to_array(rdev, mddev);
4930 if (err)
4931 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 /*
4934 * The rest should better be atomic, we can have disk failures
4935 * noticed in interrupt contexts ...
4936 */
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 rdev->raid_disk = -1;
4939
NeilBrown850b2b42006-10-03 01:15:46 -07004940 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
4942 /*
4943 * Kick recovery, maybe this spare has to be added to the
4944 * array immediately.
4945 */
4946 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4947 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004948 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return 0;
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951abort_export:
4952 export_rdev(rdev);
4953 return err;
4954}
4955
NeilBrown32a76272005-06-21 17:17:14 -07004956static int set_bitmap_file(mddev_t *mddev, int fd)
4957{
4958 int err;
4959
NeilBrown36fa3062005-09-09 16:23:45 -07004960 if (mddev->pers) {
4961 if (!mddev->pers->quiesce)
4962 return -EBUSY;
4963 if (mddev->recovery || mddev->sync_thread)
4964 return -EBUSY;
4965 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004966 }
4967
NeilBrown36fa3062005-09-09 16:23:45 -07004968
4969 if (fd >= 0) {
4970 if (mddev->bitmap)
4971 return -EEXIST; /* cannot add when bitmap is present */
4972 mddev->bitmap_file = fget(fd);
4973
4974 if (mddev->bitmap_file == NULL) {
4975 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4976 mdname(mddev));
4977 return -EBADF;
4978 }
4979
4980 err = deny_bitmap_write_access(mddev->bitmap_file);
4981 if (err) {
4982 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4983 mdname(mddev));
4984 fput(mddev->bitmap_file);
4985 mddev->bitmap_file = NULL;
4986 return err;
4987 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004988 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004989 } else if (mddev->bitmap == NULL)
4990 return -ENOENT; /* cannot remove what isn't there */
4991 err = 0;
4992 if (mddev->pers) {
4993 mddev->pers->quiesce(mddev, 1);
4994 if (fd >= 0)
4995 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004996 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004997 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004998 fd = -1; /* make sure to put the file */
4999 }
NeilBrown36fa3062005-09-09 16:23:45 -07005000 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005001 }
5002 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07005003 if (mddev->bitmap_file) {
5004 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07005005 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07005006 }
NeilBrown36fa3062005-09-09 16:23:45 -07005007 mddev->bitmap_file = NULL;
5008 }
5009
NeilBrown32a76272005-06-21 17:17:14 -07005010 return err;
5011}
5012
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013/*
5014 * set_array_info is used two different ways
5015 * The original usage is when creating a new array.
5016 * In this usage, raid_disks is > 0 and it together with
5017 * level, size, not_persistent,layout,chunksize determine the
5018 * shape of the array.
5019 * This will always create an array with a type-0.90.0 superblock.
5020 * The newer usage is when assembling an array.
5021 * In this case raid_disks will be 0, and the major_version field is
5022 * use to determine which style super-blocks are to be found on the devices.
5023 * The minor and patch _version numbers are also kept incase the
5024 * super_block handler wishes to interpret them.
5025 */
5026static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5027{
5028
5029 if (info->raid_disks == 0) {
5030 /* just setting version number for superblock loading */
5031 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005032 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 super_types[info->major_version].name == NULL) {
5034 /* maybe try to auto-load a module? */
5035 printk(KERN_INFO
5036 "md: superblock version %d not known\n",
5037 info->major_version);
5038 return -EINVAL;
5039 }
5040 mddev->major_version = info->major_version;
5041 mddev->minor_version = info->minor_version;
5042 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005043 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 return 0;
5045 }
5046 mddev->major_version = MD_MAJOR_VERSION;
5047 mddev->minor_version = MD_MINOR_VERSION;
5048 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5049 mddev->ctime = get_seconds();
5050
5051 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005052 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005053 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 mddev->raid_disks = info->raid_disks;
5055 /* don't set md_minor, it is determined by which /dev/md* was
5056 * openned
5057 */
5058 if (info->state & (1<<MD_SB_CLEAN))
5059 mddev->recovery_cp = MaxSector;
5060 else
5061 mddev->recovery_cp = 0;
5062 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005063 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005066 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 mddev->max_disks = MD_SB_DISKS;
5069
NeilBrowne6910632008-02-06 01:39:51 -08005070 if (mddev->persistent)
5071 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005072 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
NeilBrownb2a27032005-11-28 13:44:12 -08005074 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5075 mddev->bitmap_offset = 0;
5076
NeilBrownf6705572006-03-27 01:18:11 -08005077 mddev->reshape_position = MaxSector;
5078
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 /*
5080 * Generate a 128 bit UUID
5081 */
5082 get_random_bytes(mddev->uuid, 16);
5083
NeilBrownf6705572006-03-27 01:18:11 -08005084 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005085 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005086 mddev->new_layout = mddev->layout;
5087 mddev->delta_disks = 0;
5088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 return 0;
5090}
5091
Dan Williams1f403622009-03-31 14:59:03 +11005092void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5093{
Dan Williamsb522adc2009-03-31 15:00:31 +11005094 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5095
5096 if (mddev->external_size)
5097 return;
5098
Dan Williams1f403622009-03-31 14:59:03 +11005099 mddev->array_sectors = array_sectors;
5100}
5101EXPORT_SYMBOL(md_set_array_sectors);
5102
Andre Nolld71f9f82008-07-11 22:02:22 +10005103static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005104{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005105 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005106 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005107 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005108
5109 if (mddev->pers->resize == NULL)
5110 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005111 /* The "num_sectors" is the number of sectors of each device that
5112 * is used. This can only make sense for arrays with redundancy.
5113 * linear and raid0 always use whatever space is available. We can only
5114 * consider changing this number if no resync or reconstruction is
5115 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005116 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005117 * of each device. If num_sectors is zero, we find the largest size
5118 * that fits.
5119
NeilBrowna35b0d62006-01-06 00:20:49 -08005120 */
5121 if (mddev->sync_thread)
5122 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005123 if (mddev->bitmap)
5124 /* Sorry, cannot grow a bitmap yet, just remove it,
5125 * grow, and re-add.
5126 */
5127 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005128 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005129 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005130
Andre Nolld71f9f82008-07-11 22:02:22 +10005131 if (fit && (num_sectors == 0 || num_sectors > avail))
5132 num_sectors = avail;
5133 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005134 return -ENOSPC;
5135 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005136 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005137 if (!rv)
5138 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005139 return rv;
5140}
5141
NeilBrownda943b992006-01-06 00:20:54 -08005142static int update_raid_disks(mddev_t *mddev, int raid_disks)
5143{
5144 int rv;
5145 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005146 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005147 return -EINVAL;
5148 if (raid_disks <= 0 ||
5149 raid_disks >= mddev->max_disks)
5150 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005151 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005152 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005153 mddev->delta_disks = raid_disks - mddev->raid_disks;
5154
5155 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005156 return rv;
5157}
5158
5159
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160/*
5161 * update_array_info is used to change the configuration of an
5162 * on-line array.
5163 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5164 * fields in the info are checked against the array.
5165 * Any differences that cannot be handled will cause an error.
5166 * Normally, only one change can be managed at a time.
5167 */
5168static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5169{
5170 int rv = 0;
5171 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005172 int state = 0;
5173
5174 /* calculate expected state,ignoring low bits */
5175 if (mddev->bitmap && mddev->bitmap_offset)
5176 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
5178 if (mddev->major_version != info->major_version ||
5179 mddev->minor_version != info->minor_version ||
5180/* mddev->patch_version != info->patch_version || */
5181 mddev->ctime != info->ctime ||
5182 mddev->level != info->level ||
5183/* mddev->layout != info->layout || */
5184 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005185 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005186 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5187 ((state^info->state) & 0xfffffe00)
5188 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 return -EINVAL;
5190 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005191 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5192 cnt++;
5193 if (mddev->raid_disks != info->raid_disks)
5194 cnt++;
5195 if (mddev->layout != info->layout)
5196 cnt++;
5197 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5198 cnt++;
5199 if (cnt == 0)
5200 return 0;
5201 if (cnt > 1)
5202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
5204 if (mddev->layout != info->layout) {
5205 /* Change layout
5206 * we don't need to do anything at the md level, the
5207 * personality will take care of it all.
5208 */
NeilBrown50ac1682009-06-18 08:47:55 +10005209 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005211 else {
5212 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005213 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005214 if (rv)
5215 mddev->new_layout = mddev->layout;
5216 return rv;
5217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005219 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005220 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
NeilBrownda943b992006-01-06 00:20:54 -08005222 if (mddev->raid_disks != info->raid_disks)
5223 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
NeilBrown36fa3062005-09-09 16:23:45 -07005225 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5226 if (mddev->pers->quiesce == NULL)
5227 return -EINVAL;
5228 if (mddev->recovery || mddev->sync_thread)
5229 return -EBUSY;
5230 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5231 /* add the bitmap */
5232 if (mddev->bitmap)
5233 return -EEXIST;
5234 if (mddev->default_bitmap_offset == 0)
5235 return -EINVAL;
5236 mddev->bitmap_offset = mddev->default_bitmap_offset;
5237 mddev->pers->quiesce(mddev, 1);
5238 rv = bitmap_create(mddev);
5239 if (rv)
5240 bitmap_destroy(mddev);
5241 mddev->pers->quiesce(mddev, 0);
5242 } else {
5243 /* remove the bitmap */
5244 if (!mddev->bitmap)
5245 return -ENOENT;
5246 if (mddev->bitmap->file)
5247 return -EINVAL;
5248 mddev->pers->quiesce(mddev, 1);
5249 bitmap_destroy(mddev);
5250 mddev->pers->quiesce(mddev, 0);
5251 mddev->bitmap_offset = 0;
5252 }
5253 }
NeilBrown850b2b42006-10-03 01:15:46 -07005254 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 return rv;
5256}
5257
5258static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5259{
5260 mdk_rdev_t *rdev;
5261
5262 if (mddev->pers == NULL)
5263 return -ENODEV;
5264
5265 rdev = find_rdev(mddev, dev);
5266 if (!rdev)
5267 return -ENODEV;
5268
5269 md_error(mddev, rdev);
5270 return 0;
5271}
5272
Andre Noll2f9618c2008-04-25 18:57:58 +02005273/*
5274 * We have a problem here : there is no easy way to give a CHS
5275 * virtual geometry. We currently pretend that we have a 2 heads
5276 * 4 sectors (with a BIG number of cylinders...). This drives
5277 * dosfs just mad... ;-)
5278 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005279static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5280{
5281 mddev_t *mddev = bdev->bd_disk->private_data;
5282
5283 geo->heads = 2;
5284 geo->sectors = 4;
5285 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5286 return 0;
5287}
5288
Al Viroa39907f2008-03-02 10:31:15 -05005289static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 unsigned int cmd, unsigned long arg)
5291{
5292 int err = 0;
5293 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 mddev_t *mddev = NULL;
5295
5296 if (!capable(CAP_SYS_ADMIN))
5297 return -EACCES;
5298
5299 /*
5300 * Commands dealing with the RAID driver but not any
5301 * particular array:
5302 */
5303 switch (cmd)
5304 {
5305 case RAID_VERSION:
5306 err = get_version(argp);
5307 goto done;
5308
5309 case PRINT_RAID_DEBUG:
5310 err = 0;
5311 md_print_devices();
5312 goto done;
5313
5314#ifndef MODULE
5315 case RAID_AUTORUN:
5316 err = 0;
5317 autostart_arrays(arg);
5318 goto done;
5319#endif
5320 default:;
5321 }
5322
5323 /*
5324 * Commands creating/starting a new array:
5325 */
5326
Al Viroa39907f2008-03-02 10:31:15 -05005327 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
5329 if (!mddev) {
5330 BUG();
5331 goto abort;
5332 }
5333
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 err = mddev_lock(mddev);
5335 if (err) {
5336 printk(KERN_INFO
5337 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5338 err, cmd);
5339 goto abort;
5340 }
5341
5342 switch (cmd)
5343 {
5344 case SET_ARRAY_INFO:
5345 {
5346 mdu_array_info_t info;
5347 if (!arg)
5348 memset(&info, 0, sizeof(info));
5349 else if (copy_from_user(&info, argp, sizeof(info))) {
5350 err = -EFAULT;
5351 goto abort_unlock;
5352 }
5353 if (mddev->pers) {
5354 err = update_array_info(mddev, &info);
5355 if (err) {
5356 printk(KERN_WARNING "md: couldn't update"
5357 " array info. %d\n", err);
5358 goto abort_unlock;
5359 }
5360 goto done_unlock;
5361 }
5362 if (!list_empty(&mddev->disks)) {
5363 printk(KERN_WARNING
5364 "md: array %s already has disks!\n",
5365 mdname(mddev));
5366 err = -EBUSY;
5367 goto abort_unlock;
5368 }
5369 if (mddev->raid_disks) {
5370 printk(KERN_WARNING
5371 "md: array %s already initialised!\n",
5372 mdname(mddev));
5373 err = -EBUSY;
5374 goto abort_unlock;
5375 }
5376 err = set_array_info(mddev, &info);
5377 if (err) {
5378 printk(KERN_WARNING "md: couldn't set"
5379 " array info. %d\n", err);
5380 goto abort_unlock;
5381 }
5382 }
5383 goto done_unlock;
5384
5385 default:;
5386 }
5387
5388 /*
5389 * Commands querying/configuring an existing array:
5390 */
NeilBrown32a76272005-06-21 17:17:14 -07005391 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005392 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005393 if ((!mddev->raid_disks && !mddev->external)
5394 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5395 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5396 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 err = -ENODEV;
5398 goto abort_unlock;
5399 }
5400
5401 /*
5402 * Commands even a read-only array can execute:
5403 */
5404 switch (cmd)
5405 {
5406 case GET_ARRAY_INFO:
5407 err = get_array_info(mddev, argp);
5408 goto done_unlock;
5409
NeilBrown32a76272005-06-21 17:17:14 -07005410 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005411 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005412 goto done_unlock;
5413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 case GET_DISK_INFO:
5415 err = get_disk_info(mddev, argp);
5416 goto done_unlock;
5417
5418 case RESTART_ARRAY_RW:
5419 err = restart_array(mddev);
5420 goto done_unlock;
5421
5422 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005423 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 goto done_unlock;
5425
5426 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005427 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 goto done_unlock;
5429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 }
5431
5432 /*
5433 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005434 * superblock, so we do not allow them on read-only arrays.
5435 * However non-MD ioctls (e.g. get-size) will still come through
5436 * here and hit the 'default' below, so only disallow
5437 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005439 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005440 if (mddev->ro == 2) {
5441 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005442 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005443 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5444 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005445 } else {
5446 err = -EROFS;
5447 goto abort_unlock;
5448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
5450
5451 switch (cmd)
5452 {
5453 case ADD_NEW_DISK:
5454 {
5455 mdu_disk_info_t info;
5456 if (copy_from_user(&info, argp, sizeof(info)))
5457 err = -EFAULT;
5458 else
5459 err = add_new_disk(mddev, &info);
5460 goto done_unlock;
5461 }
5462
5463 case HOT_REMOVE_DISK:
5464 err = hot_remove_disk(mddev, new_decode_dev(arg));
5465 goto done_unlock;
5466
5467 case HOT_ADD_DISK:
5468 err = hot_add_disk(mddev, new_decode_dev(arg));
5469 goto done_unlock;
5470
5471 case SET_DISK_FAULTY:
5472 err = set_disk_faulty(mddev, new_decode_dev(arg));
5473 goto done_unlock;
5474
5475 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005476 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 goto done_unlock;
5478
NeilBrown32a76272005-06-21 17:17:14 -07005479 case SET_BITMAP_FILE:
5480 err = set_bitmap_file(mddev, (int)arg);
5481 goto done_unlock;
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 err = -EINVAL;
5485 goto abort_unlock;
5486 }
5487
5488done_unlock:
5489abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005490 if (mddev->hold_active == UNTIL_IOCTL &&
5491 err != -EINVAL)
5492 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 mddev_unlock(mddev);
5494
5495 return err;
5496done:
5497 if (err)
5498 MD_BUG();
5499abort:
5500 return err;
5501}
5502
Al Viroa39907f2008-03-02 10:31:15 -05005503static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504{
5505 /*
5506 * Succeed if we can lock the mddev, which confirms that
5507 * it isn't being stopped right now.
5508 */
NeilBrownd3374822009-01-09 08:31:10 +11005509 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 int err;
5511
NeilBrownd3374822009-01-09 08:31:10 +11005512 if (mddev->gendisk != bdev->bd_disk) {
5513 /* we are racing with mddev_put which is discarding this
5514 * bd_disk.
5515 */
5516 mddev_put(mddev);
5517 /* Wait until bdev->bd_disk is definitely gone */
5518 flush_scheduled_work();
5519 /* Then retry the open from the top */
5520 return -ERESTARTSYS;
5521 }
5522 BUG_ON(mddev != bdev->bd_disk->private_data);
5523
NeilBrownc8c00a62009-08-10 12:50:52 +10005524 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 goto out;
5526
5527 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005528 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005529 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Al Viroa39907f2008-03-02 10:31:15 -05005531 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 out:
5533 return err;
5534}
5535
Al Viroa39907f2008-03-02 10:31:15 -05005536static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537{
Al Viroa39907f2008-03-02 10:31:15 -05005538 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005540 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005541 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 mddev_put(mddev);
5543
5544 return 0;
5545}
5546
Linus Torvalds44ce6292007-05-09 18:51:36 -07005547static int md_media_changed(struct gendisk *disk)
5548{
5549 mddev_t *mddev = disk->private_data;
5550
5551 return mddev->changed;
5552}
5553
5554static int md_revalidate(struct gendisk *disk)
5555{
5556 mddev_t *mddev = disk->private_data;
5557
5558 mddev->changed = 0;
5559 return 0;
5560}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005561static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562{
5563 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005564 .open = md_open,
5565 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005566 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005567 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005568 .media_changed = md_media_changed,
5569 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570};
5571
Adrian Bunk75c96f82005-05-05 16:16:09 -07005572static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573{
5574 mdk_thread_t *thread = arg;
5575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 /*
5577 * md_thread is a 'system-thread', it's priority should be very
5578 * high. We avoid resource deadlocks individually in each
5579 * raid personality. (RAID5 does preallocation) We also use RR and
5580 * the very same RT priority as kswapd, thus we will never get
5581 * into a priority inversion deadlock.
5582 *
5583 * we definitely have to have equal or higher priority than
5584 * bdflush, otherwise bdflush will deadlock if there are too
5585 * many dirty RAID5 blocks.
5586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
NeilBrown6985c432005-10-19 21:23:47 -07005588 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005589 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
NeilBrown93588e22005-11-15 00:09:12 -08005591 /* We need to wait INTERRUPTIBLE so that
5592 * we don't add to the load-average.
5593 * That means we need to be sure no signals are
5594 * pending
5595 */
5596 if (signal_pending(current))
5597 flush_signals(current);
5598
5599 wait_event_interruptible_timeout
5600 (thread->wqueue,
5601 test_bit(THREAD_WAKEUP, &thread->flags)
5602 || kthread_should_stop(),
5603 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 clear_bit(THREAD_WAKEUP, &thread->flags);
5606
NeilBrown787453c2005-11-08 21:39:43 -08005607 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 return 0;
5611}
5612
5613void md_wakeup_thread(mdk_thread_t *thread)
5614{
5615 if (thread) {
5616 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5617 set_bit(THREAD_WAKEUP, &thread->flags);
5618 wake_up(&thread->wqueue);
5619 }
5620}
5621
5622mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5623 const char *name)
5624{
5625 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
NeilBrown9ffae0c2006-01-06 00:20:32 -08005627 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 if (!thread)
5629 return NULL;
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 init_waitqueue_head(&thread->wqueue);
5632
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 thread->run = run;
5634 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005635 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005636 thread->tsk = kthread_run(md_thread, thread,
5637 "%s_%s",
5638 mdname(thread->mddev),
5639 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005640 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 kfree(thread);
5642 return NULL;
5643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 return thread;
5645}
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647void md_unregister_thread(mdk_thread_t *thread)
5648{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005649 if (!thread)
5650 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005651 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005652
5653 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 kfree(thread);
5655}
5656
5657void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5658{
5659 if (!mddev) {
5660 MD_BUG();
5661 return;
5662 }
5663
NeilBrownb2d444d2005-11-08 21:39:31 -08005664 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005666
5667 if (mddev->external)
5668 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005669/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5671 mdname(mddev),
5672 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5673 __builtin_return_address(0),__builtin_return_address(1),
5674 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005675*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005676 if (!mddev->pers)
5677 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 if (!mddev->pers->error_handler)
5679 return;
5680 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005681 if (mddev->degraded)
5682 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005683 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5685 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5686 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005687 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688}
5689
5690/* seq_file implementation /proc/mdstat */
5691
5692static void status_unused(struct seq_file *seq)
5693{
5694 int i = 0;
5695 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 seq_printf(seq, "unused devices: ");
5698
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005699 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 char b[BDEVNAME_SIZE];
5701 i++;
5702 seq_printf(seq, "%s ",
5703 bdevname(rdev->bdev,b));
5704 }
5705 if (!i)
5706 seq_printf(seq, "<none>");
5707
5708 seq_printf(seq, "\n");
5709}
5710
5711
5712static void status_resync(struct seq_file *seq, mddev_t * mddev)
5713{
NeilBrowndd71cf62009-05-07 12:49:35 +10005714 sector_t max_sectors, resync, res;
5715 unsigned long dt, db;
5716 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005717 int scale;
5718 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
NeilBrowndd71cf62009-05-07 12:49:35 +10005720 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
5722 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005723 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005725 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
5727 /*
5728 * Should not happen.
5729 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005730 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 MD_BUG();
5732 return;
5733 }
NeilBrown4588b422006-03-27 01:18:04 -08005734 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005735 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005736 * u32, as those are the requirements for sector_div.
5737 * Thus 'scale' must be at least 10
5738 */
5739 scale = 10;
5740 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005741 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005742 scale++;
5743 }
5744 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005745 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005746
5747 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 {
NeilBrown4588b422006-03-27 01:18:04 -08005749 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 seq_printf(seq, "[");
5751 for (i = 0; i < x; i++)
5752 seq_printf(seq, "=");
5753 seq_printf(seq, ">");
5754 for (i = 0; i < y; i++)
5755 seq_printf(seq, ".");
5756 seq_printf(seq, "] ");
5757 }
NeilBrown4588b422006-03-27 01:18:04 -08005758 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005759 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5760 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005761 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5762 "check" :
5763 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5764 "resync" : "recovery"))),
5765 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005766 (unsigned long long) resync/2,
5767 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 * dt: time from mark until now
5771 * db: blocks written from mark until now
5772 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005773 *
5774 * rt is a sector_t, so could be 32bit or 64bit.
5775 * So we divide before multiply in case it is 32bit and close
5776 * to the limit.
5777 * We scale the divisor (db) by 32 to avoid loosing precision
5778 * near the end of resync when the number of remaining sectors
5779 * is close to 'db'.
5780 * We then divide rt by 32 after multiplying by db to compensate.
5781 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 */
5783 dt = ((jiffies - mddev->resync_mark) / HZ);
5784 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005785 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5786 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
NeilBrowndd71cf62009-05-07 12:49:35 +10005788 rt = max_sectors - resync; /* number of remaining sectors */
5789 sector_div(rt, db/32+1);
5790 rt *= dt;
5791 rt >>= 5;
5792
5793 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5794 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
NeilBrownff4e8d92006-07-10 04:44:16 -07005796 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797}
5798
5799static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5800{
5801 struct list_head *tmp;
5802 loff_t l = *pos;
5803 mddev_t *mddev;
5804
5805 if (l >= 0x10000)
5806 return NULL;
5807 if (!l--)
5808 /* header */
5809 return (void*)1;
5810
5811 spin_lock(&all_mddevs_lock);
5812 list_for_each(tmp,&all_mddevs)
5813 if (!l--) {
5814 mddev = list_entry(tmp, mddev_t, all_mddevs);
5815 mddev_get(mddev);
5816 spin_unlock(&all_mddevs_lock);
5817 return mddev;
5818 }
5819 spin_unlock(&all_mddevs_lock);
5820 if (!l--)
5821 return (void*)2;/* tail */
5822 return NULL;
5823}
5824
5825static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5826{
5827 struct list_head *tmp;
5828 mddev_t *next_mddev, *mddev = v;
5829
5830 ++*pos;
5831 if (v == (void*)2)
5832 return NULL;
5833
5834 spin_lock(&all_mddevs_lock);
5835 if (v == (void*)1)
5836 tmp = all_mddevs.next;
5837 else
5838 tmp = mddev->all_mddevs.next;
5839 if (tmp != &all_mddevs)
5840 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5841 else {
5842 next_mddev = (void*)2;
5843 *pos = 0x10000;
5844 }
5845 spin_unlock(&all_mddevs_lock);
5846
5847 if (v != (void*)1)
5848 mddev_put(mddev);
5849 return next_mddev;
5850
5851}
5852
5853static void md_seq_stop(struct seq_file *seq, void *v)
5854{
5855 mddev_t *mddev = v;
5856
5857 if (mddev && v != (void*)1 && v != (void*)2)
5858 mddev_put(mddev);
5859}
5860
NeilBrownd7603b72006-01-06 00:20:30 -08005861struct mdstat_info {
5862 int event;
5863};
5864
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865static int md_seq_show(struct seq_file *seq, void *v)
5866{
5867 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005868 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005870 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005871 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005874 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 seq_printf(seq, "Personalities : ");
5876 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005877 list_for_each_entry(pers, &pers_list, list)
5878 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
5880 spin_unlock(&pers_lock);
5881 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005882 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 return 0;
5884 }
5885 if (v == (void*)2) {
5886 status_unused(seq);
5887 return 0;
5888 }
5889
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005890 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005892
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5894 seq_printf(seq, "%s : %sactive", mdname(mddev),
5895 mddev->pers ? "" : "in");
5896 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005897 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005899 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005900 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 seq_printf(seq, " %s", mddev->pers->name);
5902 }
5903
Andre Nolldd8ac332009-03-31 14:33:13 +11005904 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005905 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 char b[BDEVNAME_SIZE];
5907 seq_printf(seq, " %s[%d]",
5908 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005909 if (test_bit(WriteMostly, &rdev->flags))
5910 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005911 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 seq_printf(seq, "(F)");
5913 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005914 } else if (rdev->raid_disk < 0)
5915 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005916 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 }
5918
5919 if (!list_empty(&mddev->disks)) {
5920 if (mddev->pers)
5921 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005922 (unsigned long long)
5923 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 else
5925 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005926 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005928 if (mddev->persistent) {
5929 if (mddev->major_version != 0 ||
5930 mddev->minor_version != 90) {
5931 seq_printf(seq," super %d.%d",
5932 mddev->major_version,
5933 mddev->minor_version);
5934 }
NeilBrowne6910632008-02-06 01:39:51 -08005935 } else if (mddev->external)
5936 seq_printf(seq, " super external:%s",
5937 mddev->metadata_type);
5938 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005939 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005942 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005944 if (mddev->pers->sync_request) {
5945 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005946 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005947 seq_printf(seq, "\n ");
5948 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5949 seq_printf(seq, "\tresync=DELAYED\n ");
5950 else if (mddev->recovery_cp < MaxSector)
5951 seq_printf(seq, "\tresync=PENDING\n ");
5952 }
NeilBrown32a76272005-06-21 17:17:14 -07005953 } else
5954 seq_printf(seq, "\n ");
5955
5956 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005957 unsigned long chunk_kb;
5958 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005959 spin_lock_irqsave(&bitmap->lock, flags);
5960 chunk_kb = bitmap->chunksize >> 10;
5961 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5962 "%lu%s chunk",
5963 bitmap->pages - bitmap->missing_pages,
5964 bitmap->pages,
5965 (bitmap->pages - bitmap->missing_pages)
5966 << (PAGE_SHIFT - 10),
5967 chunk_kb ? chunk_kb : bitmap->chunksize,
5968 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005969 if (bitmap->file) {
5970 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005971 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005972 }
NeilBrown78d742d2005-06-21 17:17:15 -07005973
NeilBrown32a76272005-06-21 17:17:14 -07005974 seq_printf(seq, "\n");
5975 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 }
5977
5978 seq_printf(seq, "\n");
5979 }
5980 mddev_unlock(mddev);
5981
5982 return 0;
5983}
5984
Jan Engelhardt110518b2009-05-07 12:49:37 +10005985static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 .start = md_seq_start,
5987 .next = md_seq_next,
5988 .stop = md_seq_stop,
5989 .show = md_seq_show,
5990};
5991
5992static int md_seq_open(struct inode *inode, struct file *file)
5993{
5994 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005995 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5996 if (mi == NULL)
5997 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
5999 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006000 if (error)
6001 kfree(mi);
6002 else {
6003 struct seq_file *p = file->private_data;
6004 p->private = mi;
6005 mi->event = atomic_read(&md_event_count);
6006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 return error;
6008}
6009
NeilBrownd7603b72006-01-06 00:20:30 -08006010static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6011{
6012 struct seq_file *m = filp->private_data;
6013 struct mdstat_info *mi = m->private;
6014 int mask;
6015
6016 poll_wait(filp, &md_event_waiters, wait);
6017
6018 /* always allow read */
6019 mask = POLLIN | POLLRDNORM;
6020
6021 if (mi->event != atomic_read(&md_event_count))
6022 mask |= POLLERR | POLLPRI;
6023 return mask;
6024}
6025
Arjan van de Venfa027c22007-02-12 00:55:33 -08006026static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006027 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 .open = md_seq_open,
6029 .read = seq_read,
6030 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006031 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006032 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033};
6034
NeilBrown2604b702006-01-06 00:20:36 -08006035int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006038 list_add_tail(&p->list, &pers_list);
6039 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 spin_unlock(&pers_lock);
6041 return 0;
6042}
6043
NeilBrown2604b702006-01-06 00:20:36 -08006044int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045{
NeilBrown2604b702006-01-06 00:20:36 -08006046 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006048 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 spin_unlock(&pers_lock);
6050 return 0;
6051}
6052
NeilBrowneea1bf32009-03-31 14:27:02 +11006053static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054{
6055 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006057 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
6059 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006060 rcu_read_lock();
6061 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006063 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6064 (int)part_stat_read(&disk->part0, sectors[1]) -
6065 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006066 /* sync IO will cause sync_io to increase before the disk_stats
6067 * as sync_io is counted when a request starts, and
6068 * disk_stats is counted when it completes.
6069 * So resync activity will cause curr_events to be smaller than
6070 * when there was no such activity.
6071 * non-sync IO will cause disk_stat to increase without
6072 * increasing sync_io so curr_events will (eventually)
6073 * be larger than it was before. Once it becomes
6074 * substantially larger, the test below will cause
6075 * the array to appear non-idle, and resync will slow
6076 * down.
6077 * If there is a lot of outstanding resync activity when
6078 * we set last_event to curr_events, then all that activity
6079 * completing might cause the array to appear non-idle
6080 * and resync will be slowed down even though there might
6081 * not have been non-resync activity. This will only
6082 * happen once though. 'last_events' will soon reflect
6083 * the state where there is little or no outstanding
6084 * resync requests, and further resync activity will
6085 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006086 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006088 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 rdev->last_events = curr_events;
6090 idle = 0;
6091 }
6092 }
NeilBrown4b809912008-07-21 17:05:25 +10006093 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 return idle;
6095}
6096
6097void md_done_sync(mddev_t *mddev, int blocks, int ok)
6098{
6099 /* another "blocks" (512byte) blocks have been synced */
6100 atomic_sub(blocks, &mddev->recovery_active);
6101 wake_up(&mddev->recovery_wait);
6102 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006103 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 md_wakeup_thread(mddev->thread);
6105 // stop recovery, signal do_sync ....
6106 }
6107}
6108
6109
NeilBrown06d91a52005-06-21 17:17:12 -07006110/* md_write_start(mddev, bi)
6111 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006112 * in superblock) before writing, schedule a superblock update
6113 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006114 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006115void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116{
Neil Brown0fd62b82008-06-28 08:31:36 +10006117 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006118 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006119 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006120
NeilBrownf91de922005-11-08 21:39:36 -08006121 BUG_ON(mddev->ro == 1);
6122 if (mddev->ro == 2) {
6123 /* need to switch to read/write */
6124 mddev->ro = 0;
6125 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6126 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006127 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006128 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006129 }
NeilBrown06d91a52005-06-21 17:17:12 -07006130 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006131 if (mddev->safemode == 1)
6132 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006133 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006134 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006135 if (mddev->in_sync) {
6136 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006137 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006138 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006139 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006140 }
NeilBrowna9701a32005-11-08 21:39:34 -08006141 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006142 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006143 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006144 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006145 wait_event(mddev->sb_wait,
6146 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6147 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148}
6149
6150void md_write_end(mddev_t *mddev)
6151{
6152 if (atomic_dec_and_test(&mddev->writes_pending)) {
6153 if (mddev->safemode == 2)
6154 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006155 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6157 }
6158}
6159
NeilBrown2a2275d2007-01-26 00:57:11 -08006160/* md_allow_write(mddev)
6161 * Calling this ensures that the array is marked 'active' so that writes
6162 * may proceed without blocking. It is important to call this before
6163 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6164 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006165 *
6166 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6167 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006168 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006169int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006170{
6171 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006172 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006173 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006174 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006175 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006176 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006177
6178 spin_lock_irq(&mddev->write_lock);
6179 if (mddev->in_sync) {
6180 mddev->in_sync = 0;
6181 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6182 if (mddev->safemode_delay &&
6183 mddev->safemode == 0)
6184 mddev->safemode = 1;
6185 spin_unlock_irq(&mddev->write_lock);
6186 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006187 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006188 } else
6189 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006190
6191 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6192 return -EAGAIN;
6193 else
6194 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006195}
6196EXPORT_SYMBOL_GPL(md_allow_write);
6197
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198#define SYNC_MARKS 10
6199#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006200void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201{
6202 mddev_t *mddev2;
6203 unsigned int currspeed = 0,
6204 window;
NeilBrown57afd892005-06-21 17:17:13 -07006205 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 unsigned long mark[SYNC_MARKS];
6207 sector_t mark_cnt[SYNC_MARKS];
6208 int last_mark,m;
6209 struct list_head *tmp;
6210 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006211 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006212 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006213 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
6215 /* just incase thread restarts... */
6216 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6217 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006218 if (mddev->ro) /* never try to sync a read-only array */
6219 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
NeilBrown61df9d92006-10-03 01:15:57 -07006221 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6222 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6223 desc = "data-check";
6224 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6225 desc = "requested-resync";
6226 else
6227 desc = "resync";
6228 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6229 desc = "reshape";
6230 else
6231 desc = "recovery";
6232
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 /* we overload curr_resync somewhat here.
6234 * 0 == not engaged in resync at all
6235 * 2 == checking that there is no conflict with another sync
6236 * 1 == like 2, but have yielded to allow conflicting resync to
6237 * commense
6238 * other == active in resync - this many blocks
6239 *
6240 * Before starting a resync we must have set curr_resync to
6241 * 2, and then checked that every "conflicting" array has curr_resync
6242 * less than ours. When we find one that is the same or higher
6243 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6244 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6245 * This will mean we have to start checking from the beginning again.
6246 *
6247 */
6248
6249 do {
6250 mddev->curr_resync = 2;
6251
6252 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006253 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006254 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 goto skip;
6256 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006257 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 if (mddev2 == mddev)
6259 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006260 if (!mddev->parallel_resync
6261 && mddev2->curr_resync
6262 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 DEFINE_WAIT(wq);
6264 if (mddev < mddev2 && mddev->curr_resync == 2) {
6265 /* arbitrarily yield */
6266 mddev->curr_resync = 1;
6267 wake_up(&resync_wait);
6268 }
6269 if (mddev > mddev2 && mddev->curr_resync == 1)
6270 /* no need to wait here, we can wait the next
6271 * time 'round when curr_resync == 2
6272 */
6273 continue;
NeilBrown97441972008-09-19 11:49:54 +10006274 /* We need to wait 'interruptible' so as not to
6275 * contribute to the load average, and not to
6276 * be caught by 'softlockup'
6277 */
6278 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006279 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006280 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006281 printk(KERN_INFO "md: delaying %s of %s"
6282 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006284 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006286 if (signal_pending(current))
6287 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 schedule();
6289 finish_wait(&resync_wait, &wq);
6290 goto try_again;
6291 }
6292 finish_wait(&resync_wait, &wq);
6293 }
6294 }
6295 } while (mddev->curr_resync < 2);
6296
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006297 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006298 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006300 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 */
6302 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006303 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006304 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006305 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6306 j = mddev->resync_min;
6307 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006308 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006309
NeilBrownccfcc3c2006-03-27 01:18:09 -08006310 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006311 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006312 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006314 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006315 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006316 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006317 if (rdev->raid_disk >= 0 &&
6318 !test_bit(Faulty, &rdev->flags) &&
6319 !test_bit(In_sync, &rdev->flags) &&
6320 rdev->recovery_offset < j)
6321 j = rdev->recovery_offset;
6322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323
NeilBrown61df9d92006-10-03 01:15:57 -07006324 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6325 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6326 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006327 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006328 "(but not more than %d KB/sec) for %s.\n",
6329 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
NeilBrowneea1bf32009-03-31 14:27:02 +11006331 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006332
NeilBrown57afd892005-06-21 17:17:13 -07006333 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 for (m = 0; m < SYNC_MARKS; m++) {
6335 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006336 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 }
6338 last_mark = 0;
6339 mddev->resync_mark = mark[last_mark];
6340 mddev->resync_mark_cnt = mark_cnt[last_mark];
6341
6342 /*
6343 * Tune reconstruction:
6344 */
6345 window = 32*(PAGE_SIZE/512);
6346 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6347 window/2,(unsigned long long) max_sectors/2);
6348
6349 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 last_check = 0;
6351
6352 if (j>2) {
6353 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006354 "md: resuming %s of %s from checkpoint.\n",
6355 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 mddev->curr_resync = j;
6357 }
6358
6359 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006360 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
NeilBrown57afd892005-06-21 17:17:13 -07006362 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006363
NeilBrown7a91ee12009-05-26 12:57:21 +10006364 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6365 ((mddev->curr_resync > mddev->curr_resync_completed &&
6366 (mddev->curr_resync - mddev->curr_resync_completed)
6367 > (max_sectors >> 4)) ||
6368 (j - mddev->curr_resync_completed)*2
6369 >= mddev->resync_max - mddev->curr_resync_completed
6370 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006371 /* time to update curr_resync_completed */
6372 blk_unplug(mddev->queue);
6373 wait_event(mddev->recovery_wait,
6374 atomic_read(&mddev->recovery_active) == 0);
6375 mddev->curr_resync_completed =
6376 mddev->curr_resync;
6377 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006378 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006379 }
NeilBrownacb180b2009-04-14 16:28:34 +10006380
NeilBrowne62e58a2009-07-01 13:15:35 +10006381 while (j >= mddev->resync_max && !kthread_should_stop()) {
6382 /* As this condition is controlled by user-space,
6383 * we can block indefinitely, so use '_interruptible'
6384 * to avoid triggering warnings.
6385 */
6386 flush_signals(current); /* just in case */
6387 wait_event_interruptible(mddev->recovery_wait,
6388 mddev->resync_max > j
6389 || kthread_should_stop());
6390 }
NeilBrownacb180b2009-04-14 16:28:34 +10006391
6392 if (kthread_should_stop())
6393 goto interrupted;
6394
NeilBrown57afd892005-06-21 17:17:13 -07006395 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006396 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006397 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006398 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 goto out;
6400 }
NeilBrown57afd892005-06-21 17:17:13 -07006401
6402 if (!skipped) { /* actual IO requested */
6403 io_sectors += sectors;
6404 atomic_add(sectors, &mddev->recovery_active);
6405 }
6406
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 j += sectors;
6408 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006409 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006410 if (last_check == 0)
6411 /* this is the earliers that rebuilt will be
6412 * visible in /proc/mdstat
6413 */
6414 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006415
6416 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 continue;
6418
NeilBrown57afd892005-06-21 17:17:13 -07006419 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
NeilBrowndfc70642008-05-23 13:04:39 -07006421 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 break;
6423
6424 repeat:
6425 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6426 /* step marks */
6427 int next = (last_mark+1) % SYNC_MARKS;
6428
6429 mddev->resync_mark = mark[next];
6430 mddev->resync_mark_cnt = mark_cnt[next];
6431 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006432 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 last_mark = next;
6434 }
6435
6436
NeilBrownc6207272008-02-06 01:39:52 -08006437 if (kthread_should_stop())
6438 goto interrupted;
6439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
6441 /*
6442 * this loop exits only if either when we are slower than
6443 * the 'hard' speed limit, or the system was IO-idle for
6444 * a jiffy.
6445 * the system might be non-idle CPU-wise, but we only care
6446 * about not overloading the IO subsystem. (things like an
6447 * e2fsck being done on the RAID array should execute fast)
6448 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006449 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 cond_resched();
6451
NeilBrown57afd892005-06-21 17:17:13 -07006452 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6453 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
NeilBrown88202a02006-01-06 00:21:36 -08006455 if (currspeed > speed_min(mddev)) {
6456 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006457 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006458 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 goto repeat;
6460 }
6461 }
6462 }
NeilBrown61df9d92006-10-03 01:15:57 -07006463 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 /*
6465 * this also signals 'finished resyncing' to md_stop
6466 */
6467 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006468 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
6470 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6471
6472 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006473 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
NeilBrowndfc70642008-05-23 13:04:39 -07006475 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006476 mddev->curr_resync > 2) {
6477 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6478 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6479 if (mddev->curr_resync >= mddev->recovery_cp) {
6480 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006481 "md: checkpointing %s of %s.\n",
6482 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006483 mddev->recovery_cp = mddev->curr_resync;
6484 }
6485 } else
6486 mddev->recovery_cp = MaxSector;
6487 } else {
6488 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6489 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006490 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006491 if (rdev->raid_disk >= 0 &&
6492 !test_bit(Faulty, &rdev->flags) &&
6493 !test_bit(In_sync, &rdev->flags) &&
6494 rdev->recovery_offset < mddev->curr_resync)
6495 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 }
NeilBrown17571282006-12-10 02:20:52 -08006498 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 skip:
6501 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006502 mddev->curr_resync_completed = 0;
NeilBrown24395a852009-11-06 14:59:27 +11006503 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6504 /* We completed so max setting can be forgotten. */
6505 mddev->resync_max = MaxSector;
NeilBrownc6207272008-02-06 01:39:52 -08006506 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 wake_up(&resync_wait);
6508 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6509 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006510 return;
6511
6512 interrupted:
6513 /*
6514 * got a signal, exit.
6515 */
6516 printk(KERN_INFO
6517 "md: md_do_sync() got signal ... exiting\n");
6518 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6519 goto out;
6520
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521}
NeilBrown29269552006-03-27 01:18:10 -08006522EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
6524
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006525static int remove_and_add_spares(mddev_t *mddev)
6526{
6527 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006528 int spares = 0;
6529
NeilBrown97e4f422009-03-31 14:33:13 +11006530 mddev->curr_resync_completed = 0;
6531
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006532 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006533 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006534 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006535 (test_bit(Faulty, &rdev->flags) ||
6536 ! test_bit(In_sync, &rdev->flags)) &&
6537 atomic_read(&rdev->nr_pending)==0) {
6538 if (mddev->pers->hot_remove_disk(
6539 mddev, rdev->raid_disk)==0) {
6540 char nm[20];
6541 sprintf(nm,"rd%d", rdev->raid_disk);
6542 sysfs_remove_link(&mddev->kobj, nm);
6543 rdev->raid_disk = -1;
6544 }
6545 }
6546
NeilBrown4044ba52009-01-09 08:31:11 +11006547 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006548 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006549 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006550 !test_bit(In_sync, &rdev->flags) &&
6551 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006552 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006553 if (rdev->raid_disk < 0
6554 && !test_bit(Faulty, &rdev->flags)) {
6555 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006556 if (mddev->pers->
6557 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006558 char nm[20];
6559 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006560 if (sysfs_create_link(&mddev->kobj,
6561 &rdev->kobj, nm))
6562 printk(KERN_WARNING
6563 "md: cannot register "
6564 "%s for %s\n",
6565 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006566 spares++;
6567 md_new_event(mddev);
6568 } else
6569 break;
6570 }
NeilBrowndfc70642008-05-23 13:04:39 -07006571 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006572 }
6573 return spares;
6574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575/*
6576 * This routine is regularly called by all per-raid-array threads to
6577 * deal with generic issues like resync and super-block update.
6578 * Raid personalities that don't have a thread (linear/raid0) do not
6579 * need this as they never do any recovery or update the superblock.
6580 *
6581 * It does not do any resync itself, but rather "forks" off other threads
6582 * to do that as needed.
6583 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6584 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006585 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 * and wakeups up this thread which will reap the thread and finish up.
6587 * This thread also removes any faulty devices (with nr_pending == 0).
6588 *
6589 * The overall approach is:
6590 * 1/ if the superblock needs updating, update it.
6591 * 2/ If a recovery thread is running, don't do anything else.
6592 * 3/ If recovery has finished, clean up, possibly marking spares active.
6593 * 4/ If there are any faulty devices, remove them.
6594 * 5/ If array is degraded, try to add spares devices
6595 * 6/ If array has spares or is not in-sync, start a resync thread.
6596 */
6597void md_check_recovery(mddev_t *mddev)
6598{
6599 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
6601
NeilBrown5f404022005-06-21 17:17:16 -07006602 if (mddev->bitmap)
6603 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604
6605 if (mddev->ro)
6606 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006607
6608 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006609 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006610 printk(KERN_INFO "md: %s in immediate safe mode\n",
6611 mdname(mddev));
6612 mddev->safemode = 2;
6613 }
6614 flush_signals(current);
6615 }
6616
NeilBrownc89a8ee2008-08-05 15:54:13 +10006617 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6618 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006620 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006622 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006623 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006624 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6625 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 ))
6627 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006628
NeilBrowndf5b89b2006-03-27 01:18:20 -08006629 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006630 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006631
NeilBrownc89a8ee2008-08-05 15:54:13 +10006632 if (mddev->ro) {
6633 /* Only thing we do on a ro array is remove
6634 * failed devices.
6635 */
6636 remove_and_add_spares(mddev);
6637 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6638 goto unlock;
6639 }
6640
NeilBrown31a59e32008-04-30 00:52:30 -07006641 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006642 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006643 spin_lock_irq(&mddev->write_lock);
6644 if (mddev->safemode &&
6645 !atomic_read(&mddev->writes_pending) &&
6646 !mddev->in_sync &&
6647 mddev->recovery_cp == MaxSector) {
6648 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006649 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006650 if (mddev->persistent)
6651 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6652 }
6653 if (mddev->safemode == 1)
6654 mddev->safemode = 0;
6655 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006656 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006657 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006658 }
NeilBrownfca4d842005-06-21 17:17:11 -07006659
NeilBrown850b2b42006-10-03 01:15:46 -07006660 if (mddev->flags)
6661 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006662
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006663 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006664 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006665 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006666
NeilBrown06d91a52005-06-21 17:17:12 -07006667
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6669 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6670 /* resync/recovery still happening */
6671 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6672 goto unlock;
6673 }
6674 if (mddev->sync_thread) {
6675 /* resync has finished, collect result */
6676 md_unregister_thread(mddev->sync_thread);
6677 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006678 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6679 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 /* success...*/
6681 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006682 if (mddev->pers->spare_active(mddev))
6683 sysfs_notify(&mddev->kobj, NULL,
6684 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 }
NeilBrowncea9c222009-03-31 15:15:05 +11006686 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6687 mddev->pers->finish_reshape)
6688 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006689 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006690
6691 /* if array is no-longer degraded, then any saved_raid_disk
6692 * information must be scrapped
6693 */
6694 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006695 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006696 rdev->saved_raid_disk = -1;
6697
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 mddev->recovery = 0;
6699 /* flag recovery needed just to double check */
6700 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006701 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006702 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 goto unlock;
6704 }
Neil Brown72a23c22008-06-28 08:31:41 +10006705 /* Set RUNNING before clearing NEEDED to avoid
6706 * any transients in the value of "sync_action".
6707 */
6708 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6709 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006710 /* Clear some bits that don't mean anything, but
6711 * might be left set
6712 */
NeilBrown24dd4692005-11-08 21:39:26 -08006713 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6714 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006716 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6717 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 /* no recovery is running.
6719 * remove any failed drives, then
6720 * add spares if possible.
6721 * Spare are also removed and re-added, to allow
6722 * the personality to fail the re-add.
6723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006725 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006726 if (mddev->pers->check_reshape == NULL ||
6727 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006728 /* Cannot proceed */
6729 goto unlock;
6730 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006731 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006732 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006733 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6734 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006735 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006736 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006737 } else if (mddev->recovery_cp < MaxSector) {
6738 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006739 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006740 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6741 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006743
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006745 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6746 /* We are adding a device or devices to an array
6747 * which has the bitmap stored on all devices.
6748 * So make sure all bitmap pages get written
6749 */
6750 bitmap_write_all(mddev->bitmap);
6751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 mddev->sync_thread = md_register_thread(md_do_sync,
6753 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006754 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 if (!mddev->sync_thread) {
6756 printk(KERN_ERR "%s: could not start resync"
6757 " thread...\n",
6758 mdname(mddev));
6759 /* leave the spares where they are, it shouldn't hurt */
6760 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006761 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006763 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006764 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 }
6766 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006767 if (!mddev->sync_thread) {
6768 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6769 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6770 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006771 if (mddev->sysfs_action)
6772 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 mddev_unlock(mddev);
6775 }
6776}
6777
Dan Williams6bfe0b42008-04-30 00:52:32 -07006778void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6779{
NeilBrown3c0ee632008-10-21 13:25:28 +11006780 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006781 wait_event_timeout(rdev->blocked_wait,
6782 !test_bit(Blocked, &rdev->flags),
6783 msecs_to_jiffies(5000));
6784 rdev_dec_pending(rdev, mddev);
6785}
6786EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6787
Adrian Bunk75c96f82005-05-05 16:16:09 -07006788static int md_notify_reboot(struct notifier_block *this,
6789 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
6791 struct list_head *tmp;
6792 mddev_t *mddev;
6793
6794 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6795
6796 printk(KERN_INFO "md: stopping all md devices.\n");
6797
NeilBrown29ac4aa2008-02-06 01:39:58 -08006798 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006799 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006800 /* Force a switch to readonly even array
6801 * appears to still be in use. Hence
6802 * the '100'.
6803 */
NeilBrownd710e132008-10-13 11:55:12 +11006804 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006805 mddev_unlock(mddev);
6806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 /*
6808 * certain more exotic SCSI devices are known to be
6809 * volatile wrt too early system reboots. While the
6810 * right place to handle this issue is the given
6811 * driver, we do want to have a safe RAID driver ...
6812 */
6813 mdelay(1000*1);
6814 }
6815 return NOTIFY_DONE;
6816}
6817
Adrian Bunk75c96f82005-05-05 16:16:09 -07006818static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 .notifier_call = md_notify_reboot,
6820 .next = NULL,
6821 .priority = INT_MAX, /* before any real devices */
6822};
6823
6824static void md_geninit(void)
6825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6827
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006828 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829}
6830
Adrian Bunk75c96f82005-05-05 16:16:09 -07006831static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006833 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 return -1;
6835 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006836 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 return -1;
6838 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006839 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006840 md_probe, NULL, NULL);
6841 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 md_probe, NULL, NULL);
6843
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006845 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
6847 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849}
6850
6851
6852#ifndef MODULE
6853
6854/*
6855 * Searches all registered partitions for autorun RAID arrays
6856 * at boot time.
6857 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006858
6859static LIST_HEAD(all_detected_devices);
6860struct detected_devices_node {
6861 struct list_head list;
6862 dev_t dev;
6863};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
6865void md_autodetect_dev(dev_t dev)
6866{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006867 struct detected_devices_node *node_detected_dev;
6868
6869 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6870 if (node_detected_dev) {
6871 node_detected_dev->dev = dev;
6872 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6873 } else {
6874 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6875 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877}
6878
6879
6880static void autostart_arrays(int part)
6881{
6882 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006883 struct detected_devices_node *node_detected_dev;
6884 dev_t dev;
6885 int i_scanned, i_passed;
6886
6887 i_scanned = 0;
6888 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889
6890 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6891
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006892 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6893 i_scanned++;
6894 node_detected_dev = list_entry(all_detected_devices.next,
6895 struct detected_devices_node, list);
6896 list_del(&node_detected_dev->list);
6897 dev = node_detected_dev->dev;
6898 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006899 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 if (IS_ERR(rdev))
6901 continue;
6902
NeilBrownb2d444d2005-11-08 21:39:31 -08006903 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 MD_BUG();
6905 continue;
6906 }
NeilBrownd0fae182008-03-04 14:29:31 -08006907 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006909 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006911
6912 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6913 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915 autorun_devices(part);
6916}
6917
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006918#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
6920static __exit void md_exit(void)
6921{
6922 mddev_t *mddev;
6923 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006924
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006925 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006926 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006928 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 unregister_blkdev(mdp_major, "mdp");
6930 unregister_reboot_notifier(&md_notifier);
6931 unregister_sysctl_table(raid_table_header);
6932 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006933 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006935 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 }
6937}
6938
Dan Williams685784a2007-07-09 11:56:42 -07006939subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940module_exit(md_exit)
6941
NeilBrownf91de922005-11-08 21:39:36 -08006942static int get_ro(char *buffer, struct kernel_param *kp)
6943{
6944 return sprintf(buffer, "%d", start_readonly);
6945}
6946static int set_ro(const char *val, struct kernel_param *kp)
6947{
6948 char *e;
6949 int num = simple_strtoul(val, &e, 10);
6950 if (*val && (*e == '\0' || *e == '\n')) {
6951 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006952 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006953 }
6954 return -EINVAL;
6955}
6956
NeilBrown80ca3a42006-07-10 04:44:18 -07006957module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6958module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006959
NeilBrownefeb53c2009-01-09 08:31:10 +11006960module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006961
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962EXPORT_SYMBOL(register_md_personality);
6963EXPORT_SYMBOL(unregister_md_personality);
6964EXPORT_SYMBOL(md_error);
6965EXPORT_SYMBOL(md_done_sync);
6966EXPORT_SYMBOL(md_write_start);
6967EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968EXPORT_SYMBOL(md_register_thread);
6969EXPORT_SYMBOL(md_unregister_thread);
6970EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971EXPORT_SYMBOL(md_check_recovery);
6972MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006973MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006974MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);