blob: d18805fea11197e86e1dbd118a4b77abca420949 [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 {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700105 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .proc_handler = &proc_dointvec,
107 },
108 {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .proc_handler = &proc_dointvec,
115 },
116 { .ctl_name = 0 }
117};
118
119static ctl_table raid_dir_table[] = {
120 {
121 .ctl_name = DEV_RAID,
122 .procname = "raid",
123 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .child = raid_table,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_root_table[] = {
131 {
132 .ctl_name = CTL_DEV,
133 .procname = "dev",
134 .maxlen = 0,
135 .mode = 0555,
136 .child = raid_dir_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static struct block_device_operations md_fops;
142
NeilBrownf91de922005-11-08 21:39:36 -0800143static int start_readonly;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
NeilBrownd7603b72006-01-06 00:20:30 -0800146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
149 * count increases.
150 *
151 * Events are:
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
154 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800155static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800156static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800157void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800158{
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
161}
NeilBrown29269552006-03-27 01:18:10 -0800162EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800163
NeilBrownc331eb02006-05-30 21:27:13 -0700164/* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
166 */
Adrian Bunk05381952006-06-26 00:28:01 -0700167static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700168{
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171}
172
NeilBrownd7603b72006-01-06 00:20:30 -0800173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
176 */
177static LIST_HEAD(all_mddevs);
178static DEFINE_SPINLOCK(all_mddevs_lock);
179
180
181/*
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
187 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800188#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
192 mddev = NULL;}); \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
200 tmp = tmp->next;}) \
201 )
202
203
NeilBrown409c57f2009-03-31 14:39:39 +1100204/* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
210 */
211static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
NeilBrown409c57f2009-03-31 14:39:39 +1100213 mddev_t *mddev = q->queuedata;
214 int rv;
215 if (mddev == NULL || mddev->pers == NULL) {
216 bio_io_error(bio);
217 return 0;
218 }
219 rcu_read_lock();
220 if (mddev->suspended) {
221 DEFINE_WAIT(__wait);
222 for (;;) {
223 prepare_to_wait(&mddev->sb_wait, &__wait,
224 TASK_UNINTERRUPTIBLE);
225 if (!mddev->suspended)
226 break;
227 rcu_read_unlock();
228 schedule();
229 rcu_read_lock();
230 }
231 finish_wait(&mddev->sb_wait, &__wait);
232 }
233 atomic_inc(&mddev->active_io);
234 rcu_read_unlock();
235 rv = mddev->pers->make_request(q, bio);
236 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
237 wake_up(&mddev->sb_wait);
238
239 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
NeilBrown409c57f2009-03-31 14:39:39 +1100242static void mddev_suspend(mddev_t *mddev)
243{
244 BUG_ON(mddev->suspended);
245 mddev->suspended = 1;
246 synchronize_rcu();
247 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
248 mddev->pers->quiesce(mddev, 1);
249 md_unregister_thread(mddev->thread);
250 mddev->thread = NULL;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
254 * unloaded
255 */
256}
257
258static void mddev_resume(mddev_t *mddev)
259{
260 mddev->suspended = 0;
261 wake_up(&mddev->sb_wait);
262 mddev->pers->quiesce(mddev, 0);
263}
264
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static inline mddev_t *mddev_get(mddev_t *mddev)
267{
268 atomic_inc(&mddev->active);
269 return mddev;
270}
271
Dan Williams5fd3a172009-03-04 00:57:25 -0700272static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static void mddev_put(mddev_t *mddev)
275{
276 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
277 return;
NeilBrownd3374822009-01-09 08:31:10 +1100278 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
279 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100281 if (mddev->gendisk) {
282 /* we did a probe so need to clean up.
283 * Call schedule_work inside the spinlock
284 * so that flush_scheduled_work() after
285 * mddev_find will succeed in waiting for the
286 * work to be done.
287 */
288 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
289 schedule_work(&mddev->del_work);
290 } else
291 kfree(mddev);
292 }
293 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static mddev_t * mddev_find(dev_t unit)
297{
298 mddev_t *mddev, *new = NULL;
299
300 retry:
301 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
NeilBrownefeb53c2009-01-09 08:31:10 +1100303 if (unit) {
304 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
305 if (mddev->unit == unit) {
306 mddev_get(mddev);
307 spin_unlock(&all_mddevs_lock);
308 kfree(new);
309 return mddev;
310 }
311
312 if (new) {
313 list_add(&new->all_mddevs, &all_mddevs);
314 spin_unlock(&all_mddevs_lock);
315 new->hold_active = UNTIL_IOCTL;
316 return new;
317 }
318 } else if (new) {
319 /* find an unused unit number */
320 static int next_minor = 512;
321 int start = next_minor;
322 int is_free = 0;
323 int dev = 0;
324 while (!is_free) {
325 dev = MKDEV(MD_MAJOR, next_minor);
326 next_minor++;
327 if (next_minor > MINORMASK)
328 next_minor = 0;
329 if (next_minor == start) {
330 /* Oh dear, all in use. */
331 spin_unlock(&all_mddevs_lock);
332 kfree(new);
333 return NULL;
334 }
335
336 is_free = 1;
337 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
338 if (mddev->unit == dev) {
339 is_free = 0;
340 break;
341 }
342 }
343 new->unit = dev;
344 new->md_minor = MINOR(dev);
345 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 list_add(&new->all_mddevs, &all_mddevs);
347 spin_unlock(&all_mddevs_lock);
348 return new;
349 }
350 spin_unlock(&all_mddevs_lock);
351
NeilBrown9ffae0c2006-01-06 00:20:32 -0800352 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!new)
354 return NULL;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 new->unit = unit;
357 if (MAJOR(unit) == MD_MAJOR)
358 new->md_minor = MINOR(unit);
359 else
360 new->md_minor = MINOR(unit) >> MdpMinorShift;
361
NeilBrownc8c00a62009-08-10 12:50:52 +1000362 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800363 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 INIT_LIST_HEAD(&new->disks);
365 INIT_LIST_HEAD(&new->all_mddevs);
366 init_timer(&new->safemode_timer);
367 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000368 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100369 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700370 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700371 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700372 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700373 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000374 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800375 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700376 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto retry;
379}
380
381static inline int mddev_lock(mddev_t * mddev)
382{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800383 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Dan Williamsb522adc2009-03-31 15:00:31 +1100386static inline int mddev_is_locked(mddev_t *mddev)
387{
388 return mutex_is_locked(&mddev->reconfig_mutex);
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391static inline int mddev_trylock(mddev_t * mddev)
392{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800393 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396static inline void mddev_unlock(mddev_t * mddev)
397{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800398 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
NeilBrown005eca52005-08-22 13:11:08 -0700400 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
NeilBrown2989ddb2006-01-06 00:20:43 -0800403static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100405 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100407 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (rdev->desc_nr == nr)
409 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return NULL;
412}
413
414static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
415{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 mdk_rdev_t *rdev;
417
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100418 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (rdev->bdev->bd_dev == dev)
420 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return NULL;
423}
424
NeilBrownd9d166c2006-01-06 00:20:51 -0800425static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800426{
427 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800428 list_for_each_entry(pers, &pers_list, list) {
429 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800430 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800431 if (strcmp(pers->name, clevel)==0)
432 return pers;
433 }
NeilBrown2604b702006-01-06 00:20:36 -0800434 return NULL;
435}
436
Andre Nollb73df2d2008-07-11 22:02:23 +1000437/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700438static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Andre Nollb73df2d2008-07-11 22:02:23 +1000440 sector_t num_sectors = bdev->bd_inode->i_size / 512;
441 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static int alloc_disk_sb(mdk_rdev_t * rdev)
445{
446 if (rdev->sb_page)
447 MD_BUG();
448
449 rdev->sb_page = alloc_page(GFP_KERNEL);
450 if (!rdev->sb_page) {
451 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000452 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454
455 return 0;
456}
457
458static void free_disk_sb(mdk_rdev_t * rdev)
459{
460 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800461 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 rdev->sb_loaded = 0;
463 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000464 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100465 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467}
468
469
NeilBrown6712ecf2007-09-27 12:47:43 +0200470static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700471{
472 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800473 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700474
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700475 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
476 printk("md: super_written gets error=%d, uptodate=%d\n",
477 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
478 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800479 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700480 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700481
NeilBrowna9701a32005-11-08 21:39:34 -0800482 if (atomic_dec_and_test(&mddev->pending_writes))
483 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700484 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700485}
486
NeilBrown6712ecf2007-09-27 12:47:43 +0200487static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800488{
489 struct bio *bio2 = bio->bi_private;
490 mdk_rdev_t *rdev = bio2->bi_private;
491 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800492
493 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
494 error == -EOPNOTSUPP) {
495 unsigned long flags;
496 /* barriers don't appear to be supported :-( */
497 set_bit(BarriersNotsupp, &rdev->flags);
498 mddev->barriers_work = 0;
499 spin_lock_irqsave(&mddev->write_lock, flags);
500 bio2->bi_next = mddev->biolist;
501 mddev->biolist = bio2;
502 spin_unlock_irqrestore(&mddev->write_lock, flags);
503 wake_up(&mddev->sb_wait);
504 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200505 } else {
506 bio_put(bio2);
507 bio->bi_private = rdev;
508 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800509 }
NeilBrowna9701a32005-11-08 21:39:34 -0800510}
511
NeilBrown7bfa19f2005-06-21 17:17:28 -0700512void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
513 sector_t sector, int size, struct page *page)
514{
515 /* write first size bytes of page to sector of rdev
516 * Increment mddev->pending_writes before returning
517 * and decrement it on completion, waking up sb_wait
518 * if zero is reached.
519 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800520 *
521 * As we might need to resubmit the request if BIO_RW_BARRIER
522 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700523 */
524 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100525 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700526
527 bio->bi_bdev = rdev->bdev;
528 bio->bi_sector = sector;
529 bio_add_page(bio, page, size, 0);
530 bio->bi_private = rdev;
531 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800532 bio->bi_rw = rw;
533
NeilBrown7bfa19f2005-06-21 17:17:28 -0700534 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800535 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
536 struct bio *rbio;
537 rw |= (1<<BIO_RW_BARRIER);
538 rbio = bio_clone(bio, GFP_NOIO);
539 rbio->bi_private = bio;
540 rbio->bi_end_io = super_written_barrier;
541 submit_bio(rw, rbio);
542 } else
543 submit_bio(rw, bio);
544}
545
546void md_super_wait(mddev_t *mddev)
547{
548 /* wait for all superblock writes that were scheduled to complete.
549 * if any had to be retried (due to BARRIER problems), retry them
550 */
551 DEFINE_WAIT(wq);
552 for(;;) {
553 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
554 if (atomic_read(&mddev->pending_writes)==0)
555 break;
556 while (mddev->biolist) {
557 struct bio *bio;
558 spin_lock_irq(&mddev->write_lock);
559 bio = mddev->biolist;
560 mddev->biolist = bio->bi_next ;
561 bio->bi_next = NULL;
562 spin_unlock_irq(&mddev->write_lock);
563 submit_bio(bio->bi_rw, bio);
564 }
565 schedule();
566 }
567 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700568}
569
NeilBrown6712ecf2007-09-27 12:47:43 +0200570static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
NeilBrowna654b9d82005-06-21 17:17:27 -0700575int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct page *page, int rw)
577{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700578 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct completion event;
580 int ret;
581
Jens Axboe93dbb392009-02-16 10:25:40 +0100582 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 bio->bi_bdev = bdev;
585 bio->bi_sector = sector;
586 bio_add_page(bio, page, size, 0);
587 init_completion(&event);
588 bio->bi_private = &event;
589 bio->bi_end_io = bi_complete;
590 submit_bio(rw, bio);
591 wait_for_completion(&event);
592
593 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
594 bio_put(bio);
595 return ret;
596}
NeilBrowna8745db2006-01-06 00:20:34 -0800597EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
NeilBrown0002b272005-09-09 16:23:53 -0700599static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 char b[BDEVNAME_SIZE];
602 if (!rdev->sb_page) {
603 MD_BUG();
604 return -EINVAL;
605 }
606 if (rdev->sb_loaded)
607 return 0;
608
609
Andre Noll0f420352008-07-11 22:02:23 +1000610 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto fail;
612 rdev->sb_loaded = 1;
613 return 0;
614
615fail:
616 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
617 bdevname(rdev->bdev,b));
618 return -EINVAL;
619}
620
621static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
622{
Andre Noll05710462008-07-11 22:02:20 +1000623 return sb1->set_uuid0 == sb2->set_uuid0 &&
624 sb1->set_uuid1 == sb2->set_uuid1 &&
625 sb1->set_uuid2 == sb2->set_uuid2 &&
626 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
630{
631 int ret;
632 mdp_super_t *tmp1, *tmp2;
633
634 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
635 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
636
637 if (!tmp1 || !tmp2) {
638 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100639 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto abort;
641 }
642
643 *tmp1 = *sb1;
644 *tmp2 = *sb2;
645
646 /*
647 * nr_disks is not constant
648 */
649 tmp1->nr_disks = 0;
650 tmp2->nr_disks = 0;
651
Andre Nollce0c8e02008-07-11 22:02:20 +1000652 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700654 kfree(tmp1);
655 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return ret;
657}
658
NeilBrown4d167f02007-05-09 02:35:37 -0700659
660static u32 md_csum_fold(u32 csum)
661{
662 csum = (csum & 0xffff) + (csum >> 16);
663 return (csum & 0xffff) + (csum >> 16);
664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static unsigned int calc_sb_csum(mdp_super_t * sb)
667{
NeilBrown4d167f02007-05-09 02:35:37 -0700668 u64 newcsum = 0;
669 u32 *sb32 = (u32*)sb;
670 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 unsigned int disk_csum, csum;
672
673 disk_csum = sb->sb_csum;
674 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700675
676 for (i = 0; i < MD_SB_BYTES/4 ; i++)
677 newcsum += sb32[i];
678 csum = (newcsum & 0xffffffff) + (newcsum>>32);
679
680
681#ifdef CONFIG_ALPHA
682 /* This used to use csum_partial, which was wrong for several
683 * reasons including that different results are returned on
684 * different architectures. It isn't critical that we get exactly
685 * the same return value as before (we always csum_fold before
686 * testing, and that removes any differences). However as we
687 * know that csum_partial always returned a 16bit value on
688 * alphas, do a fold to maximise conformity to previous behaviour.
689 */
690 sb->sb_csum = md_csum_fold(disk_csum);
691#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700693#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return csum;
695}
696
697
698/*
699 * Handle superblock details.
700 * We want to be able to handle multiple superblock formats
701 * so we have a common interface to them all, and an array of
702 * different handlers.
703 * We rely on user-space to write the initial superblock, and support
704 * reading and updating of superblocks.
705 * Interface methods are:
706 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
707 * loads and validates a superblock on dev.
708 * if refdev != NULL, compare superblocks on both devices
709 * Return:
710 * 0 - dev has a superblock that is compatible with refdev
711 * 1 - dev has a superblock that is compatible and newer than refdev
712 * so dev should be used as the refdev in future
713 * -EINVAL superblock incompatible or invalid
714 * -othererror e.g. -EIO
715 *
716 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
717 * Verify that dev is acceptable into mddev.
718 * The first time, mddev->raid_disks will be 0, and data from
719 * dev should be merged in. Subsequent calls check that dev
720 * is new enough. Return 0 or -EINVAL
721 *
722 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
723 * Update the superblock for rdev with data in mddev
724 * This does not write to disc.
725 *
726 */
727
728struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000729 char *name;
730 struct module *owner;
731 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
732 int minor_version);
733 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
734 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
735 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000736 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737};
738
739/*
Andre Noll0894cc32009-06-18 08:49:23 +1000740 * Check that the given mddev has no bitmap.
741 *
742 * This function is called from the run method of all personalities that do not
743 * support bitmaps. It prints an error message and returns non-zero if mddev
744 * has a bitmap. Otherwise, it returns 0.
745 *
746 */
747int md_check_no_bitmap(mddev_t *mddev)
748{
749 if (!mddev->bitmap_file && !mddev->bitmap_offset)
750 return 0;
751 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
752 mdname(mddev), mddev->pers->name);
753 return 1;
754}
755EXPORT_SYMBOL(md_check_no_bitmap);
756
757/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * load_super for 0.90.0
759 */
760static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
761{
762 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
763 mdp_super_t *sb;
764 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /*
Andre Noll0f420352008-07-11 22:02:23 +1000767 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * it's at the end of the disk.
769 *
770 * It also happens to be a multiple of 4Kb.
771 */
Andre Noll0f420352008-07-11 22:02:23 +1000772 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
NeilBrown0002b272005-09-09 16:23:53 -0700774 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (ret) return ret;
776
777 ret = -EINVAL;
778
779 bdevname(rdev->bdev, b);
780 sb = (mdp_super_t*)page_address(rdev->sb_page);
781
782 if (sb->md_magic != MD_SB_MAGIC) {
783 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
784 b);
785 goto abort;
786 }
787
788 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800789 sb->minor_version < 90 ||
790 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
792 sb->major_version, sb->minor_version,
793 b);
794 goto abort;
795 }
796
797 if (sb->raid_disks <= 0)
798 goto abort;
799
NeilBrown4d167f02007-05-09 02:35:37 -0700800 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
802 b);
803 goto abort;
804 }
805
806 rdev->preferred_minor = sb->md_minor;
807 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700808 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (sb->level == LEVEL_MULTIPATH)
811 rdev->desc_nr = -1;
812 else
813 rdev->desc_nr = sb->this_disk.number;
814
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700815 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700817 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 __u64 ev1, ev2;
819 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
820 if (!uuid_equal(refsb, sb)) {
821 printk(KERN_WARNING "md: %s has different UUID to %s\n",
822 b, bdevname(refdev->bdev,b2));
823 goto abort;
824 }
825 if (!sb_equal(refsb, sb)) {
826 printk(KERN_WARNING "md: %s has same UUID"
827 " but different superblock to %s\n",
828 b, bdevname(refdev->bdev, b2));
829 goto abort;
830 }
831 ev1 = md_event(sb);
832 ev2 = md_event(refsb);
833 if (ev1 > ev2)
834 ret = 1;
835 else
836 ret = 0;
837 }
NeilBrown8190e752009-06-18 08:48:58 +1000838 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Andre Nolldd8ac332009-03-31 14:33:13 +1100840 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800841 /* "this cannot possibly happen" ... */
842 ret = -EINVAL;
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 abort:
845 return ret;
846}
847
848/*
849 * validate_super for 0.90.0
850 */
851static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
852{
853 mdp_disk_t *desc;
854 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700855 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
NeilBrown41158c72005-06-21 17:17:25 -0700857 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800858 clear_bit(Faulty, &rdev->flags);
859 clear_bit(In_sync, &rdev->flags);
860 clear_bit(WriteMostly, &rdev->flags);
861 clear_bit(BarriersNotsupp, &rdev->flags);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (mddev->raid_disks == 0) {
864 mddev->major_version = 0;
865 mddev->minor_version = sb->minor_version;
866 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800867 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000868 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 mddev->ctime = sb->ctime;
870 mddev->utime = sb->utime;
871 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800872 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 mddev->layout = sb->layout;
874 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100875 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700876 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700877 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700878 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
NeilBrownf6705572006-03-27 01:18:11 -0800880 if (mddev->minor_version >= 91) {
881 mddev->reshape_position = sb->reshape_position;
882 mddev->delta_disks = sb->delta_disks;
883 mddev->new_level = sb->new_level;
884 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000885 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800886 } else {
887 mddev->reshape_position = MaxSector;
888 mddev->delta_disks = 0;
889 mddev->new_level = mddev->level;
890 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000891 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800892 }
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (sb->state & (1<<MD_SB_CLEAN))
895 mddev->recovery_cp = MaxSector;
896 else {
897 if (sb->events_hi == sb->cp_events_hi &&
898 sb->events_lo == sb->cp_events_lo) {
899 mddev->recovery_cp = sb->recovery_cp;
900 } else
901 mddev->recovery_cp = 0;
902 }
903
904 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
905 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
906 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
907 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
908
909 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700910
911 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700912 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700913 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700914
NeilBrown41158c72005-06-21 17:17:25 -0700915 } else if (mddev->pers == NULL) {
916 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 ++ev1;
918 if (ev1 < mddev->events)
919 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700920 } else if (mddev->bitmap) {
921 /* if adding to array with a bitmap, then we can accept an
922 * older device ... but not too old.
923 */
NeilBrown41158c72005-06-21 17:17:25 -0700924 if (ev1 < mddev->bitmap->events_cleared)
925 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700926 } else {
927 if (ev1 < mddev->events)
928 /* just a hot-add of a new device, leave raid_disk at -1 */
929 return 0;
930 }
NeilBrown41158c72005-06-21 17:17:25 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 desc = sb->disks + rdev->desc_nr;
934
935 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800936 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700937 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
938 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800939 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 rdev->raid_disk = desc->raid_disk;
941 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700942 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
943 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700944 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800945 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947}
948
949/*
950 * sync_super for 0.90.0
951 */
952static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
953{
954 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 mdk_rdev_t *rdev2;
956 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* make rdev->sb match mddev data..
960 *
961 * 1/ zero out disks
962 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
963 * 3/ any empty disks < next_spare become removed
964 *
965 * disks[0] gets initialised to REMOVED because
966 * we cannot be sure from other fields if it has
967 * been initialised or not.
968 */
969 int i;
970 int active=0, working=0,failed=0,spare=0,nr_disks=0;
971
NeilBrown61181562005-09-09 16:24:02 -0700972 rdev->sb_size = MD_SB_BYTES;
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 sb = (mdp_super_t*)page_address(rdev->sb_page);
975
976 memset(sb, 0, sizeof(*sb));
977
978 sb->md_magic = MD_SB_MAGIC;
979 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sb->patch_version = mddev->patch_version;
981 sb->gvalid_words = 0; /* ignored */
982 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
983 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
984 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
985 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
986
987 sb->ctime = mddev->ctime;
988 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +1100989 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 sb->raid_disks = mddev->raid_disks;
991 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800992 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sb->utime = mddev->utime;
994 sb->state = 0;
995 sb->events_hi = (mddev->events>>32);
996 sb->events_lo = (u32)mddev->events;
997
NeilBrownf6705572006-03-27 01:18:11 -0800998 if (mddev->reshape_position == MaxSector)
999 sb->minor_version = 90;
1000 else {
1001 sb->minor_version = 91;
1002 sb->reshape_position = mddev->reshape_position;
1003 sb->new_level = mddev->new_level;
1004 sb->delta_disks = mddev->delta_disks;
1005 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001006 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001007 }
1008 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (mddev->in_sync)
1010 {
1011 sb->recovery_cp = mddev->recovery_cp;
1012 sb->cp_events_hi = (mddev->events>>32);
1013 sb->cp_events_lo = (u32)mddev->events;
1014 if (mddev->recovery_cp == MaxSector)
1015 sb->state = (1<< MD_SB_CLEAN);
1016 } else
1017 sb->recovery_cp = 0;
1018
1019 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001020 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
NeilBrowna654b9d82005-06-21 17:17:27 -07001022 if (mddev->bitmap && mddev->bitmap_file == NULL)
1023 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001026 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001028 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001029 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1030 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -08001031 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001033 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001034 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 d = &sb->disks[rdev2->desc_nr];
1036 nr_disks++;
1037 d->number = rdev2->desc_nr;
1038 d->major = MAJOR(rdev2->bdev->bd_dev);
1039 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001040 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1041 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 d->raid_disk = rdev2->raid_disk;
1043 else
1044 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001045 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -08001047 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 d->state = (1<<MD_DISK_ACTIVE);
1049 d->state |= (1<<MD_DISK_SYNC);
1050 active++;
1051 working++;
1052 } else {
1053 d->state = 0;
1054 spare++;
1055 working++;
1056 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001057 if (test_bit(WriteMostly, &rdev2->flags))
1058 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* now set the "removed" and "faulty" bits on any missing devices */
1061 for (i=0 ; i < mddev->raid_disks ; i++) {
1062 mdp_disk_t *d = &sb->disks[i];
1063 if (d->state == 0 && d->number == 0) {
1064 d->number = i;
1065 d->raid_disk = i;
1066 d->state = (1<<MD_DISK_REMOVED);
1067 d->state |= (1<<MD_DISK_FAULTY);
1068 failed++;
1069 }
1070 }
1071 sb->nr_disks = nr_disks;
1072 sb->active_disks = active;
1073 sb->working_disks = working;
1074 sb->failed_disks = failed;
1075 sb->spare_disks = spare;
1076
1077 sb->this_disk = sb->disks[rdev->desc_nr];
1078 sb->sb_csum = calc_sb_csum(sb);
1079}
1080
1081/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001082 * rdev_size_change for 0.90.0
1083 */
1084static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001085super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001086{
Andre Noll58c0fed2009-03-31 14:33:13 +11001087 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001088 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001089 if (rdev->mddev->bitmap_offset)
1090 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001091 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001092 if (!num_sectors || num_sectors > rdev->sb_start)
1093 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001094 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001095 rdev->sb_page);
1096 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001097 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001098}
1099
1100
1101/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 * version 1 superblock
1103 */
1104
NeilBrown1c05b4b2006-10-21 10:24:08 -07001105static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001107 __le32 disk_csum;
1108 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 unsigned long long newcsum;
1110 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001111 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int i;
1113
1114 disk_csum = sb->sb_csum;
1115 sb->sb_csum = 0;
1116 newcsum = 0;
1117 for (i=0; size>=4; size -= 4 )
1118 newcsum += le32_to_cpu(*isuper++);
1119
1120 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001121 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1124 sb->sb_csum = disk_csum;
1125 return cpu_to_le32(csum);
1126}
1127
1128static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1129{
1130 struct mdp_superblock_1 *sb;
1131 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001132 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001134 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 /*
Andre Noll0f420352008-07-11 22:02:23 +10001137 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * It is always aligned to a 4K boundary and
1139 * depeding on minor_version, it can be:
1140 * 0: At least 8K, but less than 12K, from end of device
1141 * 1: At start of device
1142 * 2: 4K from start of device.
1143 */
1144 switch(minor_version) {
1145 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001146 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1147 sb_start -= 8*2;
1148 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
1150 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001151 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001154 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 default:
1157 return -EINVAL;
1158 }
Andre Noll0f420352008-07-11 22:02:23 +10001159 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
NeilBrown0002b272005-09-09 16:23:53 -07001161 /* superblock is rarely larger than 1K, but it can be larger,
1162 * and it is safe to read 4k, so we do that
1163 */
1164 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (ret) return ret;
1166
1167
1168 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1169
1170 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1171 sb->major_version != cpu_to_le32(1) ||
1172 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001173 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001174 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return -EINVAL;
1176
1177 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1178 printk("md: invalid superblock checksum on %s\n",
1179 bdevname(rdev->bdev,b));
1180 return -EINVAL;
1181 }
1182 if (le64_to_cpu(sb->data_size) < 10) {
1183 printk("md: data_size too small on %s\n",
1184 bdevname(rdev->bdev,b));
1185 return -EINVAL;
1186 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 rdev->preferred_minor = 0xffff;
1189 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001190 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
NeilBrown0002b272005-09-09 16:23:53 -07001192 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001193 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001194 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001195 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1196
1197 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001198 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001199 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001200
NeilBrown31b65a02006-07-10 04:44:14 -07001201 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1202 rdev->desc_nr = -1;
1203 else
1204 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1205
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001206 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001207 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001208 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 __u64 ev1, ev2;
1210 struct mdp_superblock_1 *refsb =
1211 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1212
1213 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1214 sb->level != refsb->level ||
1215 sb->layout != refsb->layout ||
1216 sb->chunksize != refsb->chunksize) {
1217 printk(KERN_WARNING "md: %s has strangely different"
1218 " superblock to %s\n",
1219 bdevname(rdev->bdev,b),
1220 bdevname(refdev->bdev,b2));
1221 return -EINVAL;
1222 }
1223 ev1 = le64_to_cpu(sb->events);
1224 ev2 = le64_to_cpu(refsb->events);
1225
1226 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001227 ret = 1;
1228 else
1229 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
NeilBrowna1801f82008-03-04 14:29:31 -08001231 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001232 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1233 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001235 rdev->sectors = rdev->sb_start;
1236 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001238 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001239 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001240 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1245{
1246 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001247 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
NeilBrown41158c72005-06-21 17:17:25 -07001249 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001250 clear_bit(Faulty, &rdev->flags);
1251 clear_bit(In_sync, &rdev->flags);
1252 clear_bit(WriteMostly, &rdev->flags);
1253 clear_bit(BarriersNotsupp, &rdev->flags);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (mddev->raid_disks == 0) {
1256 mddev->major_version = 1;
1257 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001258 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001259 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1261 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1262 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001263 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 mddev->layout = le32_to_cpu(sb->layout);
1265 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001266 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001267 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001268 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001269 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1272 memcpy(mddev->uuid, sb->set_uuid, 16);
1273
1274 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001275
NeilBrown71c08052005-09-09 16:23:51 -07001276 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001277 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001278 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001279
NeilBrownf6705572006-03-27 01:18:11 -08001280 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1281 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1282 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1283 mddev->new_level = le32_to_cpu(sb->new_level);
1284 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001285 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001286 } else {
1287 mddev->reshape_position = MaxSector;
1288 mddev->delta_disks = 0;
1289 mddev->new_level = mddev->level;
1290 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001291 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001292 }
1293
NeilBrown41158c72005-06-21 17:17:25 -07001294 } else if (mddev->pers == NULL) {
1295 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ++ev1;
1297 if (ev1 < mddev->events)
1298 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001299 } else if (mddev->bitmap) {
1300 /* If adding to array with a bitmap, then we can accept an
1301 * older device, but not too old.
1302 */
NeilBrown41158c72005-06-21 17:17:25 -07001303 if (ev1 < mddev->bitmap->events_cleared)
1304 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001305 } else {
1306 if (ev1 < mddev->events)
1307 /* just a hot-add of a new device, leave raid_disk at -1 */
1308 return 0;
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (mddev->level != LEVEL_MULTIPATH) {
1311 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001312 if (rdev->desc_nr < 0 ||
1313 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1314 role = 0xffff;
1315 rdev->desc_nr = -1;
1316 } else
1317 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 switch(role) {
1319 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001322 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 break;
1324 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001325 if ((le32_to_cpu(sb->feature_map) &
1326 MD_FEATURE_RECOVERY_OFFSET))
1327 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1328 else
1329 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 rdev->raid_disk = role;
1331 break;
1332 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001333 if (sb->devflags & WriteMostly1)
1334 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001335 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001336 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 0;
1339}
1340
1341static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1342{
1343 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 mdk_rdev_t *rdev2;
1345 int max_dev, i;
1346 /* make rdev->sb match mddev and rdev data. */
1347
1348 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1349
1350 sb->feature_map = 0;
1351 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001352 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 memset(sb->pad1, 0, sizeof(sb->pad1));
1354 memset(sb->pad2, 0, sizeof(sb->pad2));
1355 memset(sb->pad3, 0, sizeof(sb->pad3));
1356
1357 sb->utime = cpu_to_le64((__u64)mddev->utime);
1358 sb->events = cpu_to_le64(mddev->events);
1359 if (mddev->in_sync)
1360 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1361 else
1362 sb->resync_offset = cpu_to_le64(0);
1363
NeilBrown1c05b4b2006-10-21 10:24:08 -07001364 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001365
NeilBrownf0ca3402006-02-02 14:28:04 -08001366 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001367 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001368 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001369 sb->level = cpu_to_le32(mddev->level);
1370 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001371
NeilBrowna654b9d82005-06-21 17:17:27 -07001372 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1373 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001374 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001375 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001376
1377 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001378 !test_bit(In_sync, &rdev->flags)) {
1379 if (mddev->curr_resync_completed > rdev->recovery_offset)
1380 rdev->recovery_offset = mddev->curr_resync_completed;
1381 if (rdev->recovery_offset > 0) {
1382 sb->feature_map |=
1383 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1384 sb->recovery_offset =
1385 cpu_to_le64(rdev->recovery_offset);
1386 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001387 }
1388
NeilBrownf6705572006-03-27 01:18:11 -08001389 if (mddev->reshape_position != MaxSector) {
1390 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1391 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1392 sb->new_layout = cpu_to_le32(mddev->new_layout);
1393 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1394 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001395 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001396 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001399 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (rdev2->desc_nr+1 > max_dev)
1401 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001402
NeilBrown70471da2009-08-03 10:59:57 +10001403 if (max_dev > le32_to_cpu(sb->max_dev)) {
1404 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001405 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001406 rdev->sb_size = max_dev * 2 + 256;
1407 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1408 if (rdev->sb_size & bmask)
1409 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 for (i=0; i<max_dev;i++)
1412 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1413
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001414 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001416 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001418 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001420 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1421 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 else
1423 sb->dev_roles[i] = cpu_to_le16(0xffff);
1424 }
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 sb->sb_csum = calc_sb_1_csum(sb);
1427}
1428
Chris Webb0cd17fe2008-06-28 08:31:46 +10001429static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001430super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001431{
1432 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001433 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001434 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001435 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001436 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001437 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001438 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1439 max_sectors -= rdev->data_offset;
1440 if (!num_sectors || num_sectors > max_sectors)
1441 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001442 } else if (rdev->mddev->bitmap_offset) {
1443 /* minor version 0 with bitmap we can't move */
1444 return 0;
1445 } else {
1446 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001447 sector_t sb_start;
1448 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1449 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001450 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001451 if (!num_sectors || num_sectors > max_sectors)
1452 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001453 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001454 }
1455 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001456 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001457 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001458 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001459 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001460 rdev->sb_page);
1461 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001462 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Adrian Bunk75c96f82005-05-05 16:16:09 -07001465static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 [0] = {
1467 .name = "0.90.0",
1468 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001469 .load_super = super_90_load,
1470 .validate_super = super_90_validate,
1471 .sync_super = super_90_sync,
1472 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 },
1474 [1] = {
1475 .name = "md-1",
1476 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001477 .load_super = super_1_load,
1478 .validate_super = super_1_validate,
1479 .sync_super = super_1_sync,
1480 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 },
1482};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1485{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001486 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
NeilBrown4b809912008-07-21 17:05:25 +10001488 rcu_read_lock();
1489 rdev_for_each_rcu(rdev, mddev1)
1490 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001491 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001492 rdev2->bdev->bd_contains) {
1493 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001494 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001495 }
1496 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 0;
1498}
1499
1500static LIST_HEAD(pending_raid_disks);
1501
Andre Nollac5e7112009-08-03 10:59:47 +10001502/*
1503 * Try to register data integrity profile for an mddev
1504 *
1505 * This is called when an array is started and after a disk has been kicked
1506 * from the array. It only succeeds if all working and active component devices
1507 * are integrity capable with matching profiles.
1508 */
1509int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001510{
Andre Nollac5e7112009-08-03 10:59:47 +10001511 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001512
Andre Nollac5e7112009-08-03 10:59:47 +10001513 if (list_empty(&mddev->disks))
1514 return 0; /* nothing to do */
1515 if (blk_get_integrity(mddev->gendisk))
1516 return 0; /* already registered */
1517 list_for_each_entry(rdev, &mddev->disks, same_set) {
1518 /* skip spares and non-functional disks */
1519 if (test_bit(Faulty, &rdev->flags))
1520 continue;
1521 if (rdev->raid_disk < 0)
1522 continue;
1523 /*
1524 * If at least one rdev is not integrity capable, we can not
1525 * enable data integrity for the md device.
1526 */
1527 if (!bdev_get_integrity(rdev->bdev))
1528 return -EINVAL;
1529 if (!reference) {
1530 /* Use the first rdev as the reference */
1531 reference = rdev;
1532 continue;
1533 }
1534 /* does this rdev's profile match the reference profile? */
1535 if (blk_integrity_compare(reference->bdev->bd_disk,
1536 rdev->bdev->bd_disk) < 0)
1537 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001538 }
Andre Nollac5e7112009-08-03 10:59:47 +10001539 /*
1540 * All component devices are integrity capable and have matching
1541 * profiles, register the common profile for the md device.
1542 */
1543 if (blk_integrity_register(mddev->gendisk,
1544 bdev_get_integrity(reference->bdev)) != 0) {
1545 printk(KERN_ERR "md: failed to register integrity for %s\n",
1546 mdname(mddev));
1547 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001548 }
Andre Nollac5e7112009-08-03 10:59:47 +10001549 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1550 mdname(mddev));
1551 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001552}
Andre Nollac5e7112009-08-03 10:59:47 +10001553EXPORT_SYMBOL(md_integrity_register);
1554
1555/* Disable data integrity if non-capable/non-matching disk is being added */
1556void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1557{
1558 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1559 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1560
1561 if (!bi_mddev) /* nothing to do */
1562 return;
1563 if (rdev->raid_disk < 0) /* skip spares */
1564 return;
1565 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1566 rdev->bdev->bd_disk) >= 0)
1567 return;
1568 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1569 blk_integrity_unregister(mddev->gendisk);
1570}
1571EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1574{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001575 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001576 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001577 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001578 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (rdev->mddev) {
1581 MD_BUG();
1582 return -EINVAL;
1583 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001584
1585 /* prevent duplicates */
1586 if (find_rdev(mddev, rdev->bdev->bd_dev))
1587 return -EEXIST;
1588
Andre Nolldd8ac332009-03-31 14:33:13 +11001589 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1590 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1591 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001592 if (mddev->pers) {
1593 /* Cannot change size, so fail
1594 * If mddev->level <= 0, then we don't care
1595 * about aligning sizes (e.g. linear)
1596 */
1597 if (mddev->level > 0)
1598 return -ENOSPC;
1599 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001600 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* Verify rdev->desc_nr is unique.
1604 * If it is -1, assign a free number, else
1605 * check number is not in use
1606 */
1607 if (rdev->desc_nr < 0) {
1608 int choice = 0;
1609 if (mddev->pers) choice = mddev->raid_disks;
1610 while (find_rdev_nr(mddev, choice))
1611 choice++;
1612 rdev->desc_nr = choice;
1613 } else {
1614 if (find_rdev_nr(mddev, rdev->desc_nr))
1615 return -EBUSY;
1616 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001617 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1618 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1619 mdname(mddev), mddev->max_disks);
1620 return -EBUSY;
1621 }
NeilBrown19133a42005-11-08 21:39:35 -08001622 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001623 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001624 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001627 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001628
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001629 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001630 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001631
Tejun Heo0762b8b2008-08-25 19:56:12 +09001632 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001633 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1634 kobject_del(&rdev->kobj);
1635 goto fail;
1636 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001637 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1638
NeilBrown4b809912008-07-21 17:05:25 +10001639 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001640 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001641
1642 /* May as well allow recovery to be retried once */
1643 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001646
1647 fail:
1648 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1649 b, mdname(mddev));
1650 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
NeilBrown177a99b2008-02-06 01:39:56 -08001653static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001654{
1655 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1656 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001657 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1661{
1662 char b[BDEVNAME_SIZE];
1663 if (!rdev->mddev) {
1664 MD_BUG();
1665 return;
1666 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001667 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001668 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1670 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001671 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001672 sysfs_put(rdev->sysfs_state);
1673 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001674 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001675 * writing to 'remove' to "dev/state". We also need
1676 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001677 */
NeilBrown4b809912008-07-21 17:05:25 +10001678 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001679 INIT_WORK(&rdev->del_work, md_delayed_delete);
1680 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001681 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684/*
1685 * prevent the device from being mounted, repartitioned or
1686 * otherwise reused by a RAID array (or any other kernel
1687 * subsystem), by bd_claiming the device.
1688 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001689static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 int err = 0;
1692 struct block_device *bdev;
1693 char b[BDEVNAME_SIZE];
1694
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001695 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (IS_ERR(bdev)) {
1697 printk(KERN_ERR "md: could not open %s.\n",
1698 __bdevname(dev, b));
1699 return PTR_ERR(bdev);
1700 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001701 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (err) {
1703 printk(KERN_ERR "md: could not bd_claim %s.\n",
1704 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001705 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return err;
1707 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001708 if (!shared)
1709 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 rdev->bdev = bdev;
1711 return err;
1712}
1713
1714static void unlock_rdev(mdk_rdev_t *rdev)
1715{
1716 struct block_device *bdev = rdev->bdev;
1717 rdev->bdev = NULL;
1718 if (!bdev)
1719 MD_BUG();
1720 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001721 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724void md_autodetect_dev(dev_t dev);
1725
1726static void export_rdev(mdk_rdev_t * rdev)
1727{
1728 char b[BDEVNAME_SIZE];
1729 printk(KERN_INFO "md: export_rdev(%s)\n",
1730 bdevname(rdev->bdev,b));
1731 if (rdev->mddev)
1732 MD_BUG();
1733 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001735 if (test_bit(AutoDetected, &rdev->flags))
1736 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737#endif
1738 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001739 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
1742static void kick_rdev_from_array(mdk_rdev_t * rdev)
1743{
1744 unbind_rdev_from_array(rdev);
1745 export_rdev(rdev);
1746}
1747
1748static void export_array(mddev_t *mddev)
1749{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001750 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
NeilBrownd089c6a2008-02-06 01:39:59 -08001752 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (!rdev->mddev) {
1754 MD_BUG();
1755 continue;
1756 }
1757 kick_rdev_from_array(rdev);
1758 }
1759 if (!list_empty(&mddev->disks))
1760 MD_BUG();
1761 mddev->raid_disks = 0;
1762 mddev->major_version = 0;
1763}
1764
1765static void print_desc(mdp_disk_t *desc)
1766{
1767 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1768 desc->major,desc->minor,desc->raid_disk,desc->state);
1769}
1770
Cheng Renquancd2ac932009-01-09 08:31:08 +11001771static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 int i;
1774
1775 printk(KERN_INFO
1776 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1777 sb->major_version, sb->minor_version, sb->patch_version,
1778 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1779 sb->ctime);
1780 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1781 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1782 sb->md_minor, sb->layout, sb->chunk_size);
1783 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1784 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1785 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1786 sb->failed_disks, sb->spare_disks,
1787 sb->sb_csum, (unsigned long)sb->events_lo);
1788
1789 printk(KERN_INFO);
1790 for (i = 0; i < MD_SB_DISKS; i++) {
1791 mdp_disk_t *desc;
1792
1793 desc = sb->disks + i;
1794 if (desc->number || desc->major || desc->minor ||
1795 desc->raid_disk || (desc->state && (desc->state != 4))) {
1796 printk(" D %2d: ", i);
1797 print_desc(desc);
1798 }
1799 }
1800 printk(KERN_INFO "md: THIS: ");
1801 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Cheng Renquancd2ac932009-01-09 08:31:08 +11001804static void print_sb_1(struct mdp_superblock_1 *sb)
1805{
1806 __u8 *uuid;
1807
1808 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001809 printk(KERN_INFO
1810 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1811 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1812 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001813 le32_to_cpu(sb->major_version),
1814 le32_to_cpu(sb->feature_map),
1815 uuid[0], uuid[1], uuid[2], uuid[3],
1816 uuid[4], uuid[5], uuid[6], uuid[7],
1817 uuid[8], uuid[9], uuid[10], uuid[11],
1818 uuid[12], uuid[13], uuid[14], uuid[15],
1819 sb->set_name,
1820 (unsigned long long)le64_to_cpu(sb->ctime)
1821 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1822
1823 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001824 printk(KERN_INFO
1825 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001826 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001827 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1828 ":%02x%02x%02x%02x%02x%02x\n"
1829 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1830 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001831 le32_to_cpu(sb->level),
1832 (unsigned long long)le64_to_cpu(sb->size),
1833 le32_to_cpu(sb->raid_disks),
1834 le32_to_cpu(sb->layout),
1835 le32_to_cpu(sb->chunksize),
1836 (unsigned long long)le64_to_cpu(sb->data_offset),
1837 (unsigned long long)le64_to_cpu(sb->data_size),
1838 (unsigned long long)le64_to_cpu(sb->super_offset),
1839 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1840 le32_to_cpu(sb->dev_number),
1841 uuid[0], uuid[1], uuid[2], uuid[3],
1842 uuid[4], uuid[5], uuid[6], uuid[7],
1843 uuid[8], uuid[9], uuid[10], uuid[11],
1844 uuid[12], uuid[13], uuid[14], uuid[15],
1845 sb->devflags,
1846 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1847 (unsigned long long)le64_to_cpu(sb->events),
1848 (unsigned long long)le64_to_cpu(sb->resync_offset),
1849 le32_to_cpu(sb->sb_csum),
1850 le32_to_cpu(sb->max_dev)
1851 );
1852}
1853
1854static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001857 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1858 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001859 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1860 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001862 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1863 switch (major_version) {
1864 case 0:
1865 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1866 break;
1867 case 1:
1868 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1869 break;
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 } else
1872 printk(KERN_INFO "md: no rdev superblock!\n");
1873}
1874
Adrian Bunk5e563412006-06-26 00:27:42 -07001875static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001877 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 mdk_rdev_t *rdev;
1879 mddev_t *mddev;
1880 char b[BDEVNAME_SIZE];
1881
1882 printk("\n");
1883 printk("md: **********************************\n");
1884 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1885 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001886 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
NeilBrown32a76272005-06-21 17:17:14 -07001888 if (mddev->bitmap)
1889 bitmap_print_sb(mddev->bitmap);
1890 else
1891 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001892 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 printk("<%s>", bdevname(rdev->bdev,b));
1894 printk("\n");
1895
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001896 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001897 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899 printk("md: **********************************\n");
1900 printk("\n");
1901}
1902
1903
NeilBrown42543762006-06-26 00:27:57 -07001904static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
NeilBrown42543762006-06-26 00:27:57 -07001906 /* Update each superblock (in-memory image), but
1907 * if we are allowed to, skip spares which already
1908 * have the right event counter, or have one earlier
1909 * (which would mean they aren't being marked as dirty
1910 * with the rest of the array)
1911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001914 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001915 if (rdev->sb_events == mddev->events ||
1916 (nospares &&
1917 rdev->raid_disk < 0 &&
1918 (rdev->sb_events&1)==0 &&
1919 rdev->sb_events+1 == mddev->events)) {
1920 /* Don't update this superblock */
1921 rdev->sb_loaded = 2;
1922 } else {
1923 super_types[mddev->major_version].
1924 sync_super(mddev, rdev);
1925 rdev->sb_loaded = 1;
1926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928}
1929
NeilBrown850b2b42006-10-03 01:15:46 -07001930static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001933 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001934 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
NeilBrown1b57f132009-06-18 08:48:19 +10001936 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07001937 if (mddev->external)
1938 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001940 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001941
NeilBrown850b2b42006-10-03 01:15:46 -07001942 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1943 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1944 force_change = 1;
1945 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1946 /* just a clean<-> dirty transition, possibly leave spares alone,
1947 * though if events isn't the right even/odd, we will have to do
1948 * spares after all
1949 */
1950 nospares = 1;
1951 if (force_change)
1952 nospares = 0;
1953 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001954 /* If the array is degraded, then skipping spares is both
1955 * dangerous and fairly pointless.
1956 * Dangerous because a device that was removed from the array
1957 * might have a event_count that still looks up-to-date,
1958 * so it can be re-added without a resync.
1959 * Pointless because if there are any spares to skip,
1960 * then a recovery will happen and soon that array won't
1961 * be degraded any more and the spare can go back to sleep then.
1962 */
NeilBrown850b2b42006-10-03 01:15:46 -07001963 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001964
NeilBrown06d91a52005-06-21 17:17:12 -07001965 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07001966
1967 /* If this is just a dirty<->clean transition, and the array is clean
1968 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001969 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001970 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001971 && (mddev->events & 1)
1972 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001973 mddev->events--;
1974 else {
1975 /* otherwise we have to go forward and ... */
1976 mddev->events ++;
1977 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10001978 /* .. if the array isn't clean, an 'even' event must also go
1979 * to spares. */
1980 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07001981 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07001982 } else {
NeilBrown51d56682009-08-13 09:54:02 +10001983 /* otherwise an 'odd' event must go to spares */
1984 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07001985 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07001986 }
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 if (!mddev->events) {
1990 /*
1991 * oops, this 64-bit counter should never wrap.
1992 * Either we are in around ~1 trillion A.C., assuming
1993 * 1 reboot per second, or we have a bug:
1994 */
1995 MD_BUG();
1996 mddev->events --;
1997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 /*
2000 * do not write anything to disk if using
2001 * nonpersistent superblocks
2002 */
NeilBrown06d91a52005-06-21 17:17:12 -07002003 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002004 if (!mddev->external)
2005 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2006
NeilBrowna9701a32005-11-08 21:39:34 -08002007 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002008 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002010 }
NeilBrowne6910632008-02-06 01:39:51 -08002011 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002012 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 dprintk(KERN_INFO
2015 "md: updating %s RAID superblock on device (in sync %d)\n",
2016 mdname(mddev),mddev->in_sync);
2017
NeilBrown4ad13662007-07-17 04:06:13 -07002018 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002019 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 char b[BDEVNAME_SIZE];
2021 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002022 if (rdev->sb_loaded != 1)
2023 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002024 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 dprintk("(skipping faulty ");
2026
2027 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002028 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002029 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002030 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002031 rdev->sb_page);
2032 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2033 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002034 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002035 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 } else
2038 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002039 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* only need to write one superblock... */
2041 break;
2042 }
NeilBrowna9701a32005-11-08 21:39:34 -08002043 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002044 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002045
NeilBrowna9701a32005-11-08 21:39:34 -08002046 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002047 if (mddev->in_sync != sync_req ||
2048 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002049 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002050 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002051 goto repeat;
2052 }
NeilBrown850b2b42006-10-03 01:15:46 -07002053 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002054 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002055 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002056 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2057 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Andre Noll7f6ce762008-03-23 18:34:54 +01002061/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002062 * We want to accept with case. For this we use cmd_match.
2063 */
2064static int cmd_match(const char *cmd, const char *str)
2065{
2066 /* See if cmd, written into a sysfs file, matches
2067 * str. They must either be the same, or cmd can
2068 * have a trailing newline
2069 */
2070 while (*cmd && *str && *cmd == *str) {
2071 cmd++;
2072 str++;
2073 }
2074 if (*cmd == '\n')
2075 cmd++;
2076 if (*str || *cmd)
2077 return 0;
2078 return 1;
2079}
2080
NeilBrown86e6ffd2005-11-08 21:39:24 -08002081struct rdev_sysfs_entry {
2082 struct attribute attr;
2083 ssize_t (*show)(mdk_rdev_t *, char *);
2084 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2085};
2086
2087static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002088state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002089{
2090 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002091 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002092
NeilBrownb2d444d2005-11-08 21:39:31 -08002093 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002094 len+= sprintf(page+len, "%sfaulty",sep);
2095 sep = ",";
2096 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002097 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002098 len += sprintf(page+len, "%sin_sync",sep);
2099 sep = ",";
2100 }
NeilBrownf6556752006-06-26 00:28:01 -07002101 if (test_bit(WriteMostly, &rdev->flags)) {
2102 len += sprintf(page+len, "%swrite_mostly",sep);
2103 sep = ",";
2104 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002105 if (test_bit(Blocked, &rdev->flags)) {
2106 len += sprintf(page+len, "%sblocked", sep);
2107 sep = ",";
2108 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002109 if (!test_bit(Faulty, &rdev->flags) &&
2110 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002111 len += sprintf(page+len, "%sspare", sep);
2112 sep = ",";
2113 }
2114 return len+sprintf(page+len, "\n");
2115}
2116
NeilBrown45dc2de2006-06-26 00:27:58 -07002117static ssize_t
2118state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2119{
2120 /* can write
2121 * faulty - simulates and error
2122 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002123 * writemostly - sets write_mostly
2124 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002125 * blocked - sets the Blocked flag
2126 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002127 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002128 */
2129 int err = -EINVAL;
2130 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2131 md_error(rdev->mddev, rdev);
2132 err = 0;
2133 } else if (cmd_match(buf, "remove")) {
2134 if (rdev->raid_disk >= 0)
2135 err = -EBUSY;
2136 else {
2137 mddev_t *mddev = rdev->mddev;
2138 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002139 if (mddev->pers)
2140 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002141 md_new_event(mddev);
2142 err = 0;
2143 }
NeilBrownf6556752006-06-26 00:28:01 -07002144 } else if (cmd_match(buf, "writemostly")) {
2145 set_bit(WriteMostly, &rdev->flags);
2146 err = 0;
2147 } else if (cmd_match(buf, "-writemostly")) {
2148 clear_bit(WriteMostly, &rdev->flags);
2149 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002150 } else if (cmd_match(buf, "blocked")) {
2151 set_bit(Blocked, &rdev->flags);
2152 err = 0;
2153 } else if (cmd_match(buf, "-blocked")) {
2154 clear_bit(Blocked, &rdev->flags);
2155 wake_up(&rdev->blocked_wait);
2156 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2157 md_wakeup_thread(rdev->mddev->thread);
2158
2159 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002160 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2161 set_bit(In_sync, &rdev->flags);
2162 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002163 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002164 if (!err && rdev->sysfs_state)
2165 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002166 return err ? err : len;
2167}
NeilBrown80ca3a42006-07-10 04:44:18 -07002168static struct rdev_sysfs_entry rdev_state =
2169__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002170
2171static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002172errors_show(mdk_rdev_t *rdev, char *page)
2173{
2174 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2175}
2176
2177static ssize_t
2178errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2179{
2180 char *e;
2181 unsigned long n = simple_strtoul(buf, &e, 10);
2182 if (*buf && (*e == 0 || *e == '\n')) {
2183 atomic_set(&rdev->corrected_errors, n);
2184 return len;
2185 }
2186 return -EINVAL;
2187}
2188static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002189__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002190
NeilBrown014236d2006-01-06 00:20:55 -08002191static ssize_t
2192slot_show(mdk_rdev_t *rdev, char *page)
2193{
2194 if (rdev->raid_disk < 0)
2195 return sprintf(page, "none\n");
2196 else
2197 return sprintf(page, "%d\n", rdev->raid_disk);
2198}
2199
2200static ssize_t
2201slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2202{
2203 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002204 int err;
2205 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002206 int slot = simple_strtoul(buf, &e, 10);
2207 if (strncmp(buf, "none", 4)==0)
2208 slot = -1;
2209 else if (e==buf || (*e && *e!= '\n'))
2210 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002211 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002212 /* Setting 'slot' on an active array requires also
2213 * updating the 'rd%d' link, and communicating
2214 * with the personality with ->hot_*_disk.
2215 * For now we only support removing
2216 * failed/spare devices. This normally happens automatically,
2217 * but not when the metadata is externally managed.
2218 */
NeilBrownc303da62008-02-06 01:39:51 -08002219 if (rdev->raid_disk == -1)
2220 return -EEXIST;
2221 /* personality does all needed checks */
2222 if (rdev->mddev->pers->hot_add_disk == NULL)
2223 return -EINVAL;
2224 err = rdev->mddev->pers->
2225 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2226 if (err)
2227 return err;
2228 sprintf(nm, "rd%d", rdev->raid_disk);
2229 sysfs_remove_link(&rdev->mddev->kobj, nm);
2230 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2231 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002232 } else if (rdev->mddev->pers) {
2233 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002234 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002235 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002236 */
2237
2238 if (rdev->raid_disk != -1)
2239 return -EBUSY;
2240
2241 if (rdev->mddev->pers->hot_add_disk == NULL)
2242 return -EINVAL;
2243
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002244 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002245 if (rdev2->raid_disk == slot)
2246 return -EEXIST;
2247
2248 rdev->raid_disk = slot;
2249 if (test_bit(In_sync, &rdev->flags))
2250 rdev->saved_raid_disk = slot;
2251 else
2252 rdev->saved_raid_disk = -1;
2253 err = rdev->mddev->pers->
2254 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002255 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002256 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002257 return err;
Neil Brown52664732008-06-28 08:31:44 +10002258 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002259 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002260 sprintf(nm, "rd%d", rdev->raid_disk);
2261 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2262 printk(KERN_WARNING
2263 "md: cannot register "
2264 "%s for %s\n",
2265 nm, mdname(rdev->mddev));
2266
2267 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002268 } else {
2269 if (slot >= rdev->mddev->raid_disks)
2270 return -ENOSPC;
2271 rdev->raid_disk = slot;
2272 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002273 clear_bit(Faulty, &rdev->flags);
2274 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002275 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002276 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002277 }
NeilBrown014236d2006-01-06 00:20:55 -08002278 return len;
2279}
2280
2281
2282static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002283__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002284
NeilBrown93c8cad2006-01-06 00:20:56 -08002285static ssize_t
2286offset_show(mdk_rdev_t *rdev, char *page)
2287{
Andrew Morton6961ece2006-01-06 00:20:59 -08002288 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002289}
2290
2291static ssize_t
2292offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2293{
2294 char *e;
2295 unsigned long long offset = simple_strtoull(buf, &e, 10);
2296 if (e==buf || (*e && *e != '\n'))
2297 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002298 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002299 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002300 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002301 /* Must set offset before size, so overlap checks
2302 * can be sane */
2303 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002304 rdev->data_offset = offset;
2305 return len;
2306}
2307
2308static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002309__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002310
NeilBrown83303b62006-01-06 00:21:06 -08002311static ssize_t
2312rdev_size_show(mdk_rdev_t *rdev, char *page)
2313{
Andre Nolldd8ac332009-03-31 14:33:13 +11002314 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002315}
2316
NeilBrownc5d79ad2008-02-06 01:39:54 -08002317static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2318{
2319 /* check if two start/length pairs overlap */
2320 if (s1+l1 <= s2)
2321 return 0;
2322 if (s2+l2 <= s1)
2323 return 0;
2324 return 1;
2325}
2326
Dan Williamsb522adc2009-03-31 15:00:31 +11002327static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2328{
2329 unsigned long long blocks;
2330 sector_t new;
2331
2332 if (strict_strtoull(buf, 10, &blocks) < 0)
2333 return -EINVAL;
2334
2335 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2336 return -EINVAL; /* sector conversion overflow */
2337
2338 new = blocks * 2;
2339 if (new != blocks * 2)
2340 return -EINVAL; /* unsigned long long to sector_t overflow */
2341
2342 *sectors = new;
2343 return 0;
2344}
2345
NeilBrown83303b62006-01-06 00:21:06 -08002346static ssize_t
2347rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2348{
NeilBrown27c529b2008-03-04 14:29:33 -08002349 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002350 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002351 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002352
Dan Williamsb522adc2009-03-31 15:00:31 +11002353 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002354 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002355 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002356 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002357 sectors = super_types[my_mddev->major_version].
2358 rdev_size_change(rdev, sectors);
2359 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002360 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002361 } else if (!sectors)
2362 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2363 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002364 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002365 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002366 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002367
Andre Nolldd8ac332009-03-31 14:33:13 +11002368 rdev->sectors = sectors;
2369 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002370 /* need to check that all other rdevs with the same ->bdev
2371 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002372 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002373 * we have to change it back, we will have the lock again.
2374 */
2375 mddev_t *mddev;
2376 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002377 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002378
NeilBrown27c529b2008-03-04 14:29:33 -08002379 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002380 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002381 mdk_rdev_t *rdev2;
2382
2383 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002384 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002385 if (test_bit(AllReserved, &rdev2->flags) ||
2386 (rdev->bdev == rdev2->bdev &&
2387 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002388 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002389 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002390 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002391 overlap = 1;
2392 break;
2393 }
2394 mddev_unlock(mddev);
2395 if (overlap) {
2396 mddev_put(mddev);
2397 break;
2398 }
2399 }
NeilBrown27c529b2008-03-04 14:29:33 -08002400 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002401 if (overlap) {
2402 /* Someone else could have slipped in a size
2403 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002404 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002405 * safe, and trust userspace not to race with
2406 * itself
2407 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002408 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002409 return -EBUSY;
2410 }
2411 }
NeilBrown83303b62006-01-06 00:21:06 -08002412 return len;
2413}
2414
2415static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002416__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002417
NeilBrown86e6ffd2005-11-08 21:39:24 -08002418static struct attribute *rdev_default_attrs[] = {
2419 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002420 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002421 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002422 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002423 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002424 NULL,
2425};
2426static ssize_t
2427rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2428{
2429 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2430 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002431 mddev_t *mddev = rdev->mddev;
2432 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002433
2434 if (!entry->show)
2435 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002436
2437 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2438 if (!rv) {
2439 if (rdev->mddev == NULL)
2440 rv = -EBUSY;
2441 else
2442 rv = entry->show(rdev, page);
2443 mddev_unlock(mddev);
2444 }
2445 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002446}
2447
2448static ssize_t
2449rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2450 const char *page, size_t length)
2451{
2452 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2453 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002454 ssize_t rv;
2455 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002456
2457 if (!entry->store)
2458 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002459 if (!capable(CAP_SYS_ADMIN))
2460 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002461 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002462 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002463 if (rdev->mddev == NULL)
2464 rv = -EBUSY;
2465 else
2466 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002467 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002468 }
2469 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002470}
2471
2472static void rdev_free(struct kobject *ko)
2473{
2474 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2475 kfree(rdev);
2476}
2477static struct sysfs_ops rdev_sysfs_ops = {
2478 .show = rdev_attr_show,
2479 .store = rdev_attr_store,
2480};
2481static struct kobj_type rdev_ktype = {
2482 .release = rdev_free,
2483 .sysfs_ops = &rdev_sysfs_ops,
2484 .default_attrs = rdev_default_attrs,
2485};
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487/*
2488 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2489 *
2490 * mark the device faulty if:
2491 *
2492 * - the device is nonexistent (zero size)
2493 * - the device has no valid superblock
2494 *
2495 * a faulty rdev _never_ has rdev->sb set.
2496 */
2497static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2498{
2499 char b[BDEVNAME_SIZE];
2500 int err;
2501 mdk_rdev_t *rdev;
2502 sector_t size;
2503
NeilBrown9ffae0c2006-01-06 00:20:32 -08002504 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (!rdev) {
2506 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2507 return ERR_PTR(-ENOMEM);
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 if ((err = alloc_disk_sb(rdev)))
2511 goto abort_free;
2512
NeilBrownc5d79ad2008-02-06 01:39:54 -08002513 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (err)
2515 goto abort_free;
2516
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002517 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002520 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002521 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002522 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002524 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002526 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002527 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2530 if (!size) {
2531 printk(KERN_WARNING
2532 "md: %s has zero or unknown size, marking faulty!\n",
2533 bdevname(rdev->bdev,b));
2534 err = -EINVAL;
2535 goto abort_free;
2536 }
2537
2538 if (super_format >= 0) {
2539 err = super_types[super_format].
2540 load_super(rdev, NULL, super_minor);
2541 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002542 printk(KERN_WARNING
2543 "md: %s does not have a valid v%d.%d "
2544 "superblock, not importing!\n",
2545 bdevname(rdev->bdev,b),
2546 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 goto abort_free;
2548 }
2549 if (err < 0) {
2550 printk(KERN_WARNING
2551 "md: could not read %s's sb, not importing!\n",
2552 bdevname(rdev->bdev,b));
2553 goto abort_free;
2554 }
2555 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002558 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 return rdev;
2561
2562abort_free:
2563 if (rdev->sb_page) {
2564 if (rdev->bdev)
2565 unlock_rdev(rdev);
2566 free_disk_sb(rdev);
2567 }
2568 kfree(rdev);
2569 return ERR_PTR(err);
2570}
2571
2572/*
2573 * Check a full RAID array for plausibility
2574 */
2575
2576
NeilBrowna757e642005-04-16 15:26:42 -07002577static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002580 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 char b[BDEVNAME_SIZE];
2582
2583 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002584 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 switch (super_types[mddev->major_version].
2586 load_super(rdev, freshest, mddev->minor_version)) {
2587 case 1:
2588 freshest = rdev;
2589 break;
2590 case 0:
2591 break;
2592 default:
2593 printk( KERN_ERR \
2594 "md: fatal superblock inconsistency in %s"
2595 " -- removing from array\n",
2596 bdevname(rdev->bdev,b));
2597 kick_rdev_from_array(rdev);
2598 }
2599
2600
2601 super_types[mddev->major_version].
2602 validate_super(mddev, freshest);
2603
2604 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002605 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002606 if (rdev->desc_nr >= mddev->max_disks ||
2607 i > mddev->max_disks) {
2608 printk(KERN_WARNING
2609 "md: %s: %s: only %d devices permitted\n",
2610 mdname(mddev), bdevname(rdev->bdev, b),
2611 mddev->max_disks);
2612 kick_rdev_from_array(rdev);
2613 continue;
2614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (rdev != freshest)
2616 if (super_types[mddev->major_version].
2617 validate_super(mddev, rdev)) {
2618 printk(KERN_WARNING "md: kicking non-fresh %s"
2619 " from array!\n",
2620 bdevname(rdev->bdev,b));
2621 kick_rdev_from_array(rdev);
2622 continue;
2623 }
2624 if (mddev->level == LEVEL_MULTIPATH) {
2625 rdev->desc_nr = i++;
2626 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002627 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002628 } else if (rdev->raid_disk >= mddev->raid_disks) {
2629 rdev->raid_disk = -1;
2630 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
NeilBrown19052c02008-08-05 15:54:13 +10002635static void md_safemode_timeout(unsigned long data);
2636
NeilBrowneae17012005-11-08 21:39:23 -08002637static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002638safe_delay_show(mddev_t *mddev, char *page)
2639{
2640 int msec = (mddev->safemode_delay*1000)/HZ;
2641 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2642}
2643static ssize_t
2644safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2645{
2646 int scale=1;
2647 int dot=0;
2648 int i;
2649 unsigned long msec;
2650 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002651
NeilBrown16f17b32006-06-26 00:27:37 -07002652 /* remove a period, and count digits after it */
2653 if (len >= sizeof(buf))
2654 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002655 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002656 for (i=0; i<len; i++) {
2657 if (dot) {
2658 if (isdigit(buf[i])) {
2659 buf[i-1] = buf[i];
2660 scale *= 10;
2661 }
2662 buf[i] = 0;
2663 } else if (buf[i] == '.') {
2664 dot=1;
2665 buf[i] = 0;
2666 }
2667 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002668 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002669 return -EINVAL;
2670 msec = (msec * 1000) / scale;
2671 if (msec == 0)
2672 mddev->safemode_delay = 0;
2673 else {
NeilBrown19052c02008-08-05 15:54:13 +10002674 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002675 mddev->safemode_delay = (msec*HZ)/1000;
2676 if (mddev->safemode_delay == 0)
2677 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002678 if (mddev->safemode_delay < old_delay)
2679 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002680 }
2681 return len;
2682}
2683static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002684__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002685
2686static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002687level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002688{
NeilBrown2604b702006-01-06 00:20:36 -08002689 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002690 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002691 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002692 else if (mddev->clevel[0])
2693 return sprintf(page, "%s\n", mddev->clevel);
2694 else if (mddev->level != LEVEL_NONE)
2695 return sprintf(page, "%d\n", mddev->level);
2696 else
2697 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002698}
2699
NeilBrownd9d166c2006-01-06 00:20:51 -08002700static ssize_t
2701level_store(mddev_t *mddev, const char *buf, size_t len)
2702{
NeilBrown245f46c2009-03-31 14:39:39 +11002703 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002704 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002705 struct mdk_personality *pers;
2706 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002707 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002708
2709 if (mddev->pers == NULL) {
2710 if (len == 0)
2711 return 0;
2712 if (len >= sizeof(mddev->clevel))
2713 return -ENOSPC;
2714 strncpy(mddev->clevel, buf, len);
2715 if (mddev->clevel[len-1] == '\n')
2716 len--;
2717 mddev->clevel[len] = 0;
2718 mddev->level = LEVEL_NONE;
2719 return rv;
2720 }
2721
2722 /* request to change the personality. Need to ensure:
2723 * - array is not engaged in resync/recovery/reshape
2724 * - old personality can be suspended
2725 * - new personality will access other array.
2726 */
2727
2728 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002729 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002730
2731 if (!mddev->pers->quiesce) {
2732 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2733 mdname(mddev), mddev->pers->name);
2734 return -EINVAL;
2735 }
2736
2737 /* Now find the new personality */
2738 if (len == 0 || len >= sizeof(level))
2739 return -EINVAL;
2740 strncpy(level, buf, len);
2741 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002742 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002743 level[len] = 0;
2744
2745 request_module("md-%s", level);
2746 spin_lock(&pers_lock);
2747 pers = find_pers(LEVEL_NONE, level);
2748 if (!pers || !try_module_get(pers->owner)) {
2749 spin_unlock(&pers_lock);
2750 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2751 return -EINVAL;
2752 }
2753 spin_unlock(&pers_lock);
2754
2755 if (pers == mddev->pers) {
2756 /* Nothing to do! */
2757 module_put(pers->owner);
2758 return rv;
2759 }
2760 if (!pers->takeover) {
2761 module_put(pers->owner);
2762 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2763 mdname(mddev), level);
2764 return -EINVAL;
2765 }
2766
2767 /* ->takeover must set new_* and/or delta_disks
2768 * if it succeeds, and may set them when it fails.
2769 */
2770 priv = pers->takeover(mddev);
2771 if (IS_ERR(priv)) {
2772 mddev->new_level = mddev->level;
2773 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002774 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002775 mddev->raid_disks -= mddev->delta_disks;
2776 mddev->delta_disks = 0;
2777 module_put(pers->owner);
2778 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2779 mdname(mddev), level);
2780 return PTR_ERR(priv);
2781 }
2782
2783 /* Looks like we have a winner */
2784 mddev_suspend(mddev);
2785 mddev->pers->stop(mddev);
2786 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002787 /* Invalidate devices that are now superfluous */
2788 list_for_each_entry(rdev, &mddev->disks, same_set)
2789 if (rdev->raid_disk >= mddev->raid_disks) {
2790 rdev->raid_disk = -1;
2791 clear_bit(In_sync, &rdev->flags);
2792 }
NeilBrown245f46c2009-03-31 14:39:39 +11002793 mddev->pers = pers;
2794 mddev->private = priv;
2795 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2796 mddev->level = mddev->new_level;
2797 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002798 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002799 mddev->delta_disks = 0;
2800 pers->run(mddev);
2801 mddev_resume(mddev);
2802 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2803 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2804 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002805 return rv;
2806}
2807
2808static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002809__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002810
NeilBrownd4dbd022006-06-26 00:27:59 -07002811
2812static ssize_t
2813layout_show(mddev_t *mddev, char *page)
2814{
2815 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002816 if (mddev->reshape_position != MaxSector &&
2817 mddev->layout != mddev->new_layout)
2818 return sprintf(page, "%d (%d)\n",
2819 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002820 return sprintf(page, "%d\n", mddev->layout);
2821}
2822
2823static ssize_t
2824layout_store(mddev_t *mddev, const char *buf, size_t len)
2825{
2826 char *e;
2827 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002828
2829 if (!*buf || (*e && *e != '\n'))
2830 return -EINVAL;
2831
NeilBrownb3546032009-03-31 14:56:41 +11002832 if (mddev->pers) {
2833 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002834 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002835 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002836 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002837 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002838 if (err) {
2839 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002840 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002841 }
NeilBrownb3546032009-03-31 14:56:41 +11002842 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002843 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002844 if (mddev->reshape_position == MaxSector)
2845 mddev->layout = n;
2846 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002847 return len;
2848}
2849static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002850__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002851
2852
NeilBrowneae17012005-11-08 21:39:23 -08002853static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002854raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002855{
NeilBrownbb636542005-11-08 21:39:45 -08002856 if (mddev->raid_disks == 0)
2857 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002858 if (mddev->reshape_position != MaxSector &&
2859 mddev->delta_disks != 0)
2860 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2861 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002862 return sprintf(page, "%d\n", mddev->raid_disks);
2863}
2864
NeilBrownda943b992006-01-06 00:20:54 -08002865static int update_raid_disks(mddev_t *mddev, int raid_disks);
2866
2867static ssize_t
2868raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2869{
NeilBrownda943b992006-01-06 00:20:54 -08002870 char *e;
2871 int rv = 0;
2872 unsigned long n = simple_strtoul(buf, &e, 10);
2873
2874 if (!*buf || (*e && *e != '\n'))
2875 return -EINVAL;
2876
2877 if (mddev->pers)
2878 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002879 else if (mddev->reshape_position != MaxSector) {
2880 int olddisks = mddev->raid_disks - mddev->delta_disks;
2881 mddev->delta_disks = n - olddisks;
2882 mddev->raid_disks = n;
2883 } else
NeilBrownda943b992006-01-06 00:20:54 -08002884 mddev->raid_disks = n;
2885 return rv ? rv : len;
2886}
2887static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002888__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002889
NeilBrown24dd4692005-11-08 21:39:26 -08002890static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002891chunk_size_show(mddev_t *mddev, char *page)
2892{
NeilBrown08a02ec2007-05-09 02:35:38 -07002893 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10002894 mddev->chunk_sectors != mddev->new_chunk_sectors)
2895 return sprintf(page, "%d (%d)\n",
2896 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10002897 mddev->chunk_sectors << 9);
2898 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08002899}
2900
2901static ssize_t
2902chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2903{
NeilBrown3b343802006-01-06 00:20:47 -08002904 char *e;
2905 unsigned long n = simple_strtoul(buf, &e, 10);
2906
NeilBrown3b343802006-01-06 00:20:47 -08002907 if (!*buf || (*e && *e != '\n'))
2908 return -EINVAL;
2909
NeilBrownb3546032009-03-31 14:56:41 +11002910 if (mddev->pers) {
2911 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002912 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002913 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002914 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10002915 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002916 if (err) {
2917 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11002918 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002919 }
NeilBrownb3546032009-03-31 14:56:41 +11002920 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10002921 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002922 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10002923 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002924 }
NeilBrown3b343802006-01-06 00:20:47 -08002925 return len;
2926}
2927static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002928__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002929
NeilBrowna94213b2006-06-26 00:28:00 -07002930static ssize_t
2931resync_start_show(mddev_t *mddev, char *page)
2932{
NeilBrownd1a7c502009-03-31 15:24:32 +11002933 if (mddev->recovery_cp == MaxSector)
2934 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002935 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2936}
2937
2938static ssize_t
2939resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2940{
NeilBrowna94213b2006-06-26 00:28:00 -07002941 char *e;
2942 unsigned long long n = simple_strtoull(buf, &e, 10);
2943
2944 if (mddev->pers)
2945 return -EBUSY;
2946 if (!*buf || (*e && *e != '\n'))
2947 return -EINVAL;
2948
2949 mddev->recovery_cp = n;
2950 return len;
2951}
2952static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002953__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002954
NeilBrown9e653b62006-06-26 00:27:58 -07002955/*
2956 * The array state can be:
2957 *
2958 * clear
2959 * No devices, no size, no level
2960 * Equivalent to STOP_ARRAY ioctl
2961 * inactive
2962 * May have some settings, but array is not active
2963 * all IO results in error
2964 * When written, doesn't tear down array, but just stops it
2965 * suspended (not supported yet)
2966 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002967 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002968 * readonly
2969 * no resync can happen. no superblocks get written.
2970 * write requests fail
2971 * read-auto
2972 * like readonly, but behaves like 'clean' on a write request.
2973 *
2974 * clean - no pending writes, but otherwise active.
2975 * When written to inactive array, starts without resync
2976 * If a write request arrives then
2977 * if metadata is known, mark 'dirty' and switch to 'active'.
2978 * if not known, block and switch to write-pending
2979 * If written to an active array that has pending writes, then fails.
2980 * active
2981 * fully active: IO and resync can be happening.
2982 * When written to inactive array, starts with resync
2983 *
2984 * write-pending
2985 * clean, but writes are blocked waiting for 'active' to be written.
2986 *
2987 * active-idle
2988 * like active, but no writes have been seen for a while (100msec).
2989 *
2990 */
2991enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2992 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002993static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002994 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2995 "write-pending", "active-idle", NULL };
2996
2997static int match_word(const char *word, char **list)
2998{
2999 int n;
3000 for (n=0; list[n]; n++)
3001 if (cmd_match(word, list[n]))
3002 break;
3003 return n;
3004}
3005
3006static ssize_t
3007array_state_show(mddev_t *mddev, char *page)
3008{
3009 enum array_state st = inactive;
3010
3011 if (mddev->pers)
3012 switch(mddev->ro) {
3013 case 1:
3014 st = readonly;
3015 break;
3016 case 2:
3017 st = read_auto;
3018 break;
3019 case 0:
3020 if (mddev->in_sync)
3021 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003022 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3023 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003024 else if (mddev->safemode)
3025 st = active_idle;
3026 else
3027 st = active;
3028 }
3029 else {
3030 if (list_empty(&mddev->disks) &&
3031 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003032 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003033 st = clear;
3034 else
3035 st = inactive;
3036 }
3037 return sprintf(page, "%s\n", array_states[st]);
3038}
3039
Neil Browndf5b20c2008-07-11 22:02:22 +10003040static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003041static int do_md_run(mddev_t * mddev);
3042static int restart_array(mddev_t *mddev);
3043
3044static ssize_t
3045array_state_store(mddev_t *mddev, const char *buf, size_t len)
3046{
3047 int err = -EINVAL;
3048 enum array_state st = match_word(buf, array_states);
3049 switch(st) {
3050 case bad_word:
3051 break;
3052 case clear:
3053 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003054 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003055 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003056 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003057 break;
3058 case inactive:
3059 /* stopping an active array */
3060 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003061 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003062 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003063 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003064 } else
3065 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003066 break;
3067 case suspended:
3068 break; /* not supported yet */
3069 case readonly:
3070 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003071 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003072 else {
3073 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003074 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003075 err = do_md_run(mddev);
3076 }
3077 break;
3078 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003079 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003080 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003081 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003082 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003083 err = restart_array(mddev);
3084 if (err == 0) {
3085 mddev->ro = 2;
3086 set_disk_ro(mddev->gendisk, 0);
3087 }
NeilBrown9e653b62006-06-26 00:27:58 -07003088 } else {
3089 mddev->ro = 2;
3090 err = do_md_run(mddev);
3091 }
3092 break;
3093 case clean:
3094 if (mddev->pers) {
3095 restart_array(mddev);
3096 spin_lock_irq(&mddev->write_lock);
3097 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003098 if (mddev->in_sync == 0) {
3099 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003100 if (mddev->safemode == 1)
3101 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003102 if (mddev->persistent)
3103 set_bit(MD_CHANGE_CLEAN,
3104 &mddev->flags);
3105 }
3106 err = 0;
3107 } else
3108 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003109 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003110 } else
3111 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003112 break;
3113 case active:
3114 if (mddev->pers) {
3115 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003116 if (mddev->external)
3117 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003118 wake_up(&mddev->sb_wait);
3119 err = 0;
3120 } else {
3121 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003122 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003123 err = do_md_run(mddev);
3124 }
3125 break;
3126 case write_pending:
3127 case active_idle:
3128 /* these cannot be set */
3129 break;
3130 }
3131 if (err)
3132 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003133 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003134 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003135 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003136 }
NeilBrown9e653b62006-06-26 00:27:58 -07003137}
NeilBrown80ca3a42006-07-10 04:44:18 -07003138static struct md_sysfs_entry md_array_state =
3139__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003140
NeilBrown6d7ff7382006-01-06 00:21:16 -08003141static ssize_t
3142null_show(mddev_t *mddev, char *page)
3143{
3144 return -EINVAL;
3145}
3146
3147static ssize_t
3148new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3149{
3150 /* buf must be %d:%d\n? giving major and minor numbers */
3151 /* The new device is added to the array.
3152 * If the array has a persistent superblock, we read the
3153 * superblock to initialise info and check validity.
3154 * Otherwise, only checking done is that in bind_rdev_to_array,
3155 * which mainly checks size.
3156 */
3157 char *e;
3158 int major = simple_strtoul(buf, &e, 10);
3159 int minor;
3160 dev_t dev;
3161 mdk_rdev_t *rdev;
3162 int err;
3163
3164 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3165 return -EINVAL;
3166 minor = simple_strtoul(e+1, &e, 10);
3167 if (*e && *e != '\n')
3168 return -EINVAL;
3169 dev = MKDEV(major, minor);
3170 if (major != MAJOR(dev) ||
3171 minor != MINOR(dev))
3172 return -EOVERFLOW;
3173
3174
3175 if (mddev->persistent) {
3176 rdev = md_import_device(dev, mddev->major_version,
3177 mddev->minor_version);
3178 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3179 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3180 mdk_rdev_t, same_set);
3181 err = super_types[mddev->major_version]
3182 .load_super(rdev, rdev0, mddev->minor_version);
3183 if (err < 0)
3184 goto out;
3185 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003186 } else if (mddev->external)
3187 rdev = md_import_device(dev, -2, -1);
3188 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003189 rdev = md_import_device(dev, -1, -1);
3190
3191 if (IS_ERR(rdev))
3192 return PTR_ERR(rdev);
3193 err = bind_rdev_to_array(rdev, mddev);
3194 out:
3195 if (err)
3196 export_rdev(rdev);
3197 return err ? err : len;
3198}
3199
3200static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003201__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003202
3203static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003204bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3205{
3206 char *end;
3207 unsigned long chunk, end_chunk;
3208
3209 if (!mddev->bitmap)
3210 goto out;
3211 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3212 while (*buf) {
3213 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3214 if (buf == end) break;
3215 if (*end == '-') { /* range */
3216 buf = end + 1;
3217 end_chunk = simple_strtoul(buf, &end, 0);
3218 if (buf == end) break;
3219 }
3220 if (*end && !isspace(*end)) break;
3221 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3222 buf = end;
3223 while (isspace(*buf)) buf++;
3224 }
3225 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3226out:
3227 return len;
3228}
3229
3230static struct md_sysfs_entry md_bitmap =
3231__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3232
3233static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003234size_show(mddev_t *mddev, char *page)
3235{
Andre Noll58c0fed2009-03-31 14:33:13 +11003236 return sprintf(page, "%llu\n",
3237 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003238}
3239
Andre Nolld71f9f82008-07-11 22:02:22 +10003240static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003241
3242static ssize_t
3243size_store(mddev_t *mddev, const char *buf, size_t len)
3244{
3245 /* If array is inactive, we can reduce the component size, but
3246 * not increase it (except from 0).
3247 * If array is active, we can try an on-line resize
3248 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003249 sector_t sectors;
3250 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003251
Andre Noll58c0fed2009-03-31 14:33:13 +11003252 if (err < 0)
3253 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003254 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003255 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003256 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003257 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003258 if (mddev->dev_sectors == 0 ||
3259 mddev->dev_sectors > sectors)
3260 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003261 else
3262 err = -ENOSPC;
3263 }
3264 return err ? err : len;
3265}
3266
3267static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003268__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003269
NeilBrown8bb93aa2006-01-06 00:20:50 -08003270
3271/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003272 * This is one of
3273 * 'none' for arrays with no metadata (good luck...)
3274 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003275 * or N.M for internally known formats
3276 */
3277static ssize_t
3278metadata_show(mddev_t *mddev, char *page)
3279{
3280 if (mddev->persistent)
3281 return sprintf(page, "%d.%d\n",
3282 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003283 else if (mddev->external)
3284 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003285 else
3286 return sprintf(page, "none\n");
3287}
3288
3289static ssize_t
3290metadata_store(mddev_t *mddev, const char *buf, size_t len)
3291{
3292 int major, minor;
3293 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003294 /* Changing the details of 'external' metadata is
3295 * always permitted. Otherwise there must be
3296 * no devices attached to the array.
3297 */
3298 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3299 ;
3300 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003301 return -EBUSY;
3302
3303 if (cmd_match(buf, "none")) {
3304 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003305 mddev->external = 0;
3306 mddev->major_version = 0;
3307 mddev->minor_version = 90;
3308 return len;
3309 }
3310 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003311 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003312 if (namelen >= sizeof(mddev->metadata_type))
3313 namelen = sizeof(mddev->metadata_type)-1;
3314 strncpy(mddev->metadata_type, buf+9, namelen);
3315 mddev->metadata_type[namelen] = 0;
3316 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3317 mddev->metadata_type[--namelen] = 0;
3318 mddev->persistent = 0;
3319 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003320 mddev->major_version = 0;
3321 mddev->minor_version = 90;
3322 return len;
3323 }
3324 major = simple_strtoul(buf, &e, 10);
3325 if (e==buf || *e != '.')
3326 return -EINVAL;
3327 buf = e+1;
3328 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003329 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003330 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003331 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003332 return -ENOENT;
3333 mddev->major_version = major;
3334 mddev->minor_version = minor;
3335 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003336 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003337 return len;
3338}
3339
3340static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003341__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003342
NeilBrowna35b0d62006-01-06 00:20:49 -08003343static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003344action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003345{
NeilBrown7eec3142005-11-08 21:39:44 -08003346 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003347 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3348 type = "frozen";
3349 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003350 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003351 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3352 type = "reshape";
3353 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003354 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3355 type = "resync";
3356 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3357 type = "check";
3358 else
3359 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003360 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003361 type = "recover";
3362 }
3363 return sprintf(page, "%s\n", type);
3364}
3365
3366static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003367action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003368{
NeilBrown7eec3142005-11-08 21:39:44 -08003369 if (!mddev->pers || !mddev->pers->sync_request)
3370 return -EINVAL;
3371
NeilBrownb6a9ce62009-05-26 09:41:17 +10003372 if (cmd_match(page, "frozen"))
3373 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3374 else
3375 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3376
3377 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003378 if (mddev->sync_thread) {
3379 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3380 md_unregister_thread(mddev->sync_thread);
3381 mddev->sync_thread = NULL;
3382 mddev->recovery = 0;
3383 }
NeilBrown03c902e2006-01-06 00:20:46 -08003384 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3385 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003386 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003387 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003388 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003389 else if (cmd_match(page, "recover")) {
3390 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3391 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3392 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003393 int err;
3394 if (mddev->pers->start_reshape == NULL)
3395 return -EINVAL;
3396 err = mddev->pers->start_reshape(mddev);
3397 if (err)
3398 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003399 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003400 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003401 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003402 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003403 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003404 return -EINVAL;
3405 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3406 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003407 }
NeilBrown03c902e2006-01-06 00:20:46 -08003408 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003409 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003410 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003411 return len;
3412}
3413
NeilBrown9d888832005-11-08 21:39:26 -08003414static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003415mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003416{
3417 return sprintf(page, "%llu\n",
3418 (unsigned long long) mddev->resync_mismatches);
3419}
3420
NeilBrown80ca3a42006-07-10 04:44:18 -07003421static struct md_sysfs_entry md_scan_mode =
3422__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003423
NeilBrown96de1e62005-11-08 21:39:39 -08003424
NeilBrown80ca3a42006-07-10 04:44:18 -07003425static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003426
NeilBrown88202a02006-01-06 00:21:36 -08003427static ssize_t
3428sync_min_show(mddev_t *mddev, char *page)
3429{
3430 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3431 mddev->sync_speed_min ? "local": "system");
3432}
3433
3434static ssize_t
3435sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3436{
3437 int min;
3438 char *e;
3439 if (strncmp(buf, "system", 6)==0) {
3440 mddev->sync_speed_min = 0;
3441 return len;
3442 }
3443 min = simple_strtoul(buf, &e, 10);
3444 if (buf == e || (*e && *e != '\n') || min <= 0)
3445 return -EINVAL;
3446 mddev->sync_speed_min = min;
3447 return len;
3448}
3449
3450static struct md_sysfs_entry md_sync_min =
3451__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3452
3453static ssize_t
3454sync_max_show(mddev_t *mddev, char *page)
3455{
3456 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3457 mddev->sync_speed_max ? "local": "system");
3458}
3459
3460static ssize_t
3461sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3462{
3463 int max;
3464 char *e;
3465 if (strncmp(buf, "system", 6)==0) {
3466 mddev->sync_speed_max = 0;
3467 return len;
3468 }
3469 max = simple_strtoul(buf, &e, 10);
3470 if (buf == e || (*e && *e != '\n') || max <= 0)
3471 return -EINVAL;
3472 mddev->sync_speed_max = max;
3473 return len;
3474}
3475
3476static struct md_sysfs_entry md_sync_max =
3477__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3478
Iustin Popd7f3d292007-10-16 23:30:54 -07003479static ssize_t
3480degraded_show(mddev_t *mddev, char *page)
3481{
3482 return sprintf(page, "%d\n", mddev->degraded);
3483}
3484static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003485
3486static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003487sync_force_parallel_show(mddev_t *mddev, char *page)
3488{
3489 return sprintf(page, "%d\n", mddev->parallel_resync);
3490}
3491
3492static ssize_t
3493sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3494{
3495 long n;
3496
3497 if (strict_strtol(buf, 10, &n))
3498 return -EINVAL;
3499
3500 if (n != 0 && n != 1)
3501 return -EINVAL;
3502
3503 mddev->parallel_resync = n;
3504
3505 if (mddev->sync_thread)
3506 wake_up(&resync_wait);
3507
3508 return len;
3509}
3510
3511/* force parallel resync, even with shared block devices */
3512static struct md_sysfs_entry md_sync_force_parallel =
3513__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3514 sync_force_parallel_show, sync_force_parallel_store);
3515
3516static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003517sync_speed_show(mddev_t *mddev, char *page)
3518{
3519 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003520 if (mddev->curr_resync == 0)
3521 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003522 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3523 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003524 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003525 db = resync - mddev->resync_mark_cnt;
3526 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003527}
3528
NeilBrown80ca3a42006-07-10 04:44:18 -07003529static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003530
3531static ssize_t
3532sync_completed_show(mddev_t *mddev, char *page)
3533{
Andre Noll58c0fed2009-03-31 14:33:13 +11003534 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003535
NeilBrownacb180b2009-04-14 16:28:34 +10003536 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3537 return sprintf(page, "none\n");
3538
NeilBrown88202a02006-01-06 00:21:36 -08003539 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003540 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003541 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003542 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003543
NeilBrownacb180b2009-04-14 16:28:34 +10003544 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003545 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003546}
3547
NeilBrown80ca3a42006-07-10 04:44:18 -07003548static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003549
NeilBrowne464eaf2006-03-27 01:18:14 -08003550static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003551min_sync_show(mddev_t *mddev, char *page)
3552{
3553 return sprintf(page, "%llu\n",
3554 (unsigned long long)mddev->resync_min);
3555}
3556static ssize_t
3557min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3558{
3559 unsigned long long min;
3560 if (strict_strtoull(buf, 10, &min))
3561 return -EINVAL;
3562 if (min > mddev->resync_max)
3563 return -EINVAL;
3564 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3565 return -EBUSY;
3566
3567 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003568 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003569 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003570 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003571 return -EINVAL;
3572 }
3573 mddev->resync_min = min;
3574
3575 return len;
3576}
3577
3578static struct md_sysfs_entry md_min_sync =
3579__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3580
3581static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003582max_sync_show(mddev_t *mddev, char *page)
3583{
3584 if (mddev->resync_max == MaxSector)
3585 return sprintf(page, "max\n");
3586 else
3587 return sprintf(page, "%llu\n",
3588 (unsigned long long)mddev->resync_max);
3589}
3590static ssize_t
3591max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3592{
3593 if (strncmp(buf, "max", 3) == 0)
3594 mddev->resync_max = MaxSector;
3595 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003596 unsigned long long max;
3597 if (strict_strtoull(buf, 10, &max))
3598 return -EINVAL;
3599 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003600 return -EINVAL;
3601 if (max < mddev->resync_max &&
3602 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3603 return -EBUSY;
3604
3605 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003606 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003607 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003608 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003609 return -EINVAL;
3610 }
3611 mddev->resync_max = max;
3612 }
3613 wake_up(&mddev->recovery_wait);
3614 return len;
3615}
3616
3617static struct md_sysfs_entry md_max_sync =
3618__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3619
3620static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003621suspend_lo_show(mddev_t *mddev, char *page)
3622{
3623 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3624}
3625
3626static ssize_t
3627suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3628{
3629 char *e;
3630 unsigned long long new = simple_strtoull(buf, &e, 10);
3631
NeilBrownb8d966e2009-07-01 11:14:04 +10003632 if (mddev->pers == NULL ||
3633 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003634 return -EINVAL;
3635 if (buf == e || (*e && *e != '\n'))
3636 return -EINVAL;
3637 if (new >= mddev->suspend_hi ||
3638 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3639 mddev->suspend_lo = new;
3640 mddev->pers->quiesce(mddev, 2);
3641 return len;
3642 } else
3643 return -EINVAL;
3644}
3645static struct md_sysfs_entry md_suspend_lo =
3646__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3647
3648
3649static ssize_t
3650suspend_hi_show(mddev_t *mddev, char *page)
3651{
3652 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3653}
3654
3655static ssize_t
3656suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3657{
3658 char *e;
3659 unsigned long long new = simple_strtoull(buf, &e, 10);
3660
NeilBrownb8d966e2009-07-01 11:14:04 +10003661 if (mddev->pers == NULL ||
3662 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003663 return -EINVAL;
3664 if (buf == e || (*e && *e != '\n'))
3665 return -EINVAL;
3666 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3667 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3668 mddev->suspend_hi = new;
3669 mddev->pers->quiesce(mddev, 1);
3670 mddev->pers->quiesce(mddev, 0);
3671 return len;
3672 } else
3673 return -EINVAL;
3674}
3675static struct md_sysfs_entry md_suspend_hi =
3676__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3677
NeilBrown08a02ec2007-05-09 02:35:38 -07003678static ssize_t
3679reshape_position_show(mddev_t *mddev, char *page)
3680{
3681 if (mddev->reshape_position != MaxSector)
3682 return sprintf(page, "%llu\n",
3683 (unsigned long long)mddev->reshape_position);
3684 strcpy(page, "none\n");
3685 return 5;
3686}
3687
3688static ssize_t
3689reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3690{
3691 char *e;
3692 unsigned long long new = simple_strtoull(buf, &e, 10);
3693 if (mddev->pers)
3694 return -EBUSY;
3695 if (buf == e || (*e && *e != '\n'))
3696 return -EINVAL;
3697 mddev->reshape_position = new;
3698 mddev->delta_disks = 0;
3699 mddev->new_level = mddev->level;
3700 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003701 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003702 return len;
3703}
3704
3705static struct md_sysfs_entry md_reshape_position =
3706__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3707 reshape_position_store);
3708
Dan Williamsb522adc2009-03-31 15:00:31 +11003709static ssize_t
3710array_size_show(mddev_t *mddev, char *page)
3711{
3712 if (mddev->external_size)
3713 return sprintf(page, "%llu\n",
3714 (unsigned long long)mddev->array_sectors/2);
3715 else
3716 return sprintf(page, "default\n");
3717}
3718
3719static ssize_t
3720array_size_store(mddev_t *mddev, const char *buf, size_t len)
3721{
3722 sector_t sectors;
3723
3724 if (strncmp(buf, "default", 7) == 0) {
3725 if (mddev->pers)
3726 sectors = mddev->pers->size(mddev, 0, 0);
3727 else
3728 sectors = mddev->array_sectors;
3729
3730 mddev->external_size = 0;
3731 } else {
3732 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3733 return -EINVAL;
3734 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003735 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003736
3737 mddev->external_size = 1;
3738 }
3739
3740 mddev->array_sectors = sectors;
3741 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003742 if (mddev->pers)
3743 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003744
3745 return len;
3746}
3747
3748static struct md_sysfs_entry md_array_size =
3749__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3750 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003751
NeilBrowneae17012005-11-08 21:39:23 -08003752static struct attribute *md_default_attrs[] = {
3753 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003754 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003755 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003756 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003757 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003758 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003759 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003760 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003761 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003762 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003763 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003764 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003765 NULL,
3766};
3767
3768static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003769 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003770 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003771 &md_sync_min.attr,
3772 &md_sync_max.attr,
3773 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003774 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003775 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003776 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003777 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003778 &md_suspend_lo.attr,
3779 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003780 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003781 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003782 NULL,
3783};
NeilBrown411036f2005-11-08 21:39:40 -08003784static struct attribute_group md_redundancy_group = {
3785 .name = NULL,
3786 .attrs = md_redundancy_attrs,
3787};
3788
NeilBrowneae17012005-11-08 21:39:23 -08003789
3790static ssize_t
3791md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3792{
3793 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3794 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003795 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003796
3797 if (!entry->show)
3798 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003799 rv = mddev_lock(mddev);
3800 if (!rv) {
3801 rv = entry->show(mddev, page);
3802 mddev_unlock(mddev);
3803 }
NeilBrown96de1e62005-11-08 21:39:39 -08003804 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003805}
3806
3807static ssize_t
3808md_attr_store(struct kobject *kobj, struct attribute *attr,
3809 const char *page, size_t length)
3810{
3811 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3812 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003813 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003814
3815 if (!entry->store)
3816 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003817 if (!capable(CAP_SYS_ADMIN))
3818 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003819 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003820 if (mddev->hold_active == UNTIL_IOCTL)
3821 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003822 if (!rv) {
3823 rv = entry->store(mddev, page, length);
3824 mddev_unlock(mddev);
3825 }
NeilBrown96de1e62005-11-08 21:39:39 -08003826 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003827}
3828
3829static void md_free(struct kobject *ko)
3830{
3831 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003832
3833 if (mddev->sysfs_state)
3834 sysfs_put(mddev->sysfs_state);
3835
3836 if (mddev->gendisk) {
3837 del_gendisk(mddev->gendisk);
3838 put_disk(mddev->gendisk);
3839 }
3840 if (mddev->queue)
3841 blk_cleanup_queue(mddev->queue);
3842
NeilBrowneae17012005-11-08 21:39:23 -08003843 kfree(mddev);
3844}
3845
3846static struct sysfs_ops md_sysfs_ops = {
3847 .show = md_attr_show,
3848 .store = md_attr_store,
3849};
3850static struct kobj_type md_ktype = {
3851 .release = md_free,
3852 .sysfs_ops = &md_sysfs_ops,
3853 .default_attrs = md_default_attrs,
3854};
3855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856int mdp_major = 0;
3857
Dan Williams5fd3a172009-03-04 00:57:25 -07003858static void mddev_delayed_delete(struct work_struct *ws)
3859{
3860 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3861
3862 if (mddev->private == &md_redundancy_group) {
3863 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3864 if (mddev->sysfs_action)
3865 sysfs_put(mddev->sysfs_action);
3866 mddev->sysfs_action = NULL;
3867 mddev->private = NULL;
3868 }
3869 kobject_del(&mddev->kobj);
3870 kobject_put(&mddev->kobj);
3871}
3872
NeilBrownefeb53c2009-01-09 08:31:10 +11003873static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003875 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 mddev_t *mddev = mddev_find(dev);
3877 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003878 int partitioned;
3879 int shift;
3880 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003881 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
3883 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003884 return -ENODEV;
3885
3886 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3887 shift = partitioned ? MdpMinorShift : 0;
3888 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
NeilBrownd3374822009-01-09 08:31:10 +11003890 /* wait for any previous instance if this device
3891 * to be completed removed (mddev_delayed_delete).
3892 */
3893 flush_scheduled_work();
3894
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003895 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10003896 error = -EEXIST;
3897 if (mddev->gendisk)
3898 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003899
3900 if (name) {
3901 /* Need to ensure that 'name' is not a duplicate.
3902 */
3903 mddev_t *mddev2;
3904 spin_lock(&all_mddevs_lock);
3905
3906 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3907 if (mddev2->gendisk &&
3908 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3909 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10003910 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003911 }
3912 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
NeilBrown8b765392009-01-09 08:31:08 +11003914
NeilBrown0909dc42009-07-01 12:27:21 +10003915 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003916 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10003917 if (!mddev->queue)
3918 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11003919 mddev->queue->queuedata = mddev;
3920
NeilBrown8b765392009-01-09 08:31:08 +11003921 /* Can be unlocked because the queue is new: no concurrency */
3922 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3923
NeilBrown409c57f2009-03-31 14:39:39 +11003924 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 disk = alloc_disk(1 << shift);
3927 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11003928 blk_cleanup_queue(mddev->queue);
3929 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10003930 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003932 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003934 if (name)
3935 strcpy(disk->disk_name, name);
3936 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003938 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 disk->fops = &md_fops;
3941 disk->private_data = mddev;
3942 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003943 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003944 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003945 * remove it now.
3946 */
3947 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 add_disk(disk);
3949 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003950 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3951 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10003952 if (error) {
3953 /* This isn't possible, but as kobject_init_and_add is marked
3954 * __must_check, we must do something with the result
3955 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08003956 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3957 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10003958 error = 0;
3959 }
3960 abort:
3961 mutex_unlock(&disks_mutex);
3962 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003963 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003964 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3965 }
NeilBrownd3374822009-01-09 08:31:10 +11003966 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10003967 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11003968}
3969
3970static struct kobject *md_probe(dev_t dev, int *part, void *data)
3971{
3972 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return NULL;
3974}
3975
NeilBrownefeb53c2009-01-09 08:31:10 +11003976static int add_named_array(const char *val, struct kernel_param *kp)
3977{
3978 /* val must be "md_*" where * is not all digits.
3979 * We allocate an array with a large free minor number, and
3980 * set the name to val. val must not already be an active name.
3981 */
3982 int len = strlen(val);
3983 char buf[DISK_NAME_LEN];
3984
3985 while (len && val[len-1] == '\n')
3986 len--;
3987 if (len >= DISK_NAME_LEN)
3988 return -E2BIG;
3989 strlcpy(buf, val, len+1);
3990 if (strncmp(buf, "md_", 3) != 0)
3991 return -EINVAL;
3992 return md_alloc(0, buf);
3993}
3994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995static void md_safemode_timeout(unsigned long data)
3996{
3997 mddev_t *mddev = (mddev_t *) data;
3998
Neil Brown0fd62b82008-06-28 08:31:36 +10003999 if (!atomic_read(&mddev->writes_pending)) {
4000 mddev->safemode = 1;
4001 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004002 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 md_wakeup_thread(mddev->thread);
4005}
4006
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004007static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
4009static int do_md_run(mddev_t * mddev)
4010{
NeilBrown2604b702006-01-06 00:20:36 -08004011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 mdk_rdev_t *rdev;
4013 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004014 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
NeilBrowna757e642005-04-16 15:26:42 -07004016 if (list_empty(&mddev->disks))
4017 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020 if (mddev->pers)
4021 return -EBUSY;
4022
4023 /*
4024 * Analyze all RAID superblock(s)
4025 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004026 if (!mddev->raid_disks) {
4027 if (!mddev->persistent)
4028 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004029 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
NeilBrownd9d166c2006-01-06 00:20:51 -08004032 if (mddev->level != LEVEL_NONE)
4033 request_module("md-level-%d", mddev->level);
4034 else if (mddev->clevel[0])
4035 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
4037 /*
4038 * Drop all container device buffers, from now on
4039 * the only valid external interface is through the md
4040 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004042 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004043 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 continue;
4045 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004046 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004047
4048 /* perform some consistency tests on the device.
4049 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004050 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004051 */
Andre Noll0f420352008-07-11 22:02:23 +10004052 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004053 if (mddev->dev_sectors &&
4054 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004055 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004056 printk("md: %s: data overlaps metadata\n",
4057 mdname(mddev));
4058 return -EINVAL;
4059 }
4060 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004061 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004062 > rdev->data_offset) {
4063 printk("md: %s: metadata overlaps data\n",
4064 mdname(mddev));
4065 return -EINVAL;
4066 }
4067 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004068 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
4070
4071 md_probe(mddev->unit, NULL, NULL);
4072 disk = mddev->gendisk;
4073 if (!disk)
4074 return -ENOMEM;
4075
4076 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004077 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004078 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004080 if (mddev->level != LEVEL_NONE)
4081 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4082 mddev->level);
4083 else
4084 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4085 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return -EINVAL;
4087 }
NeilBrown2604b702006-01-06 00:20:36 -08004088 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004090 if (mddev->level != pers->level) {
4091 mddev->level = pers->level;
4092 mddev->new_level = pers->level;
4093 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004094 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
NeilBrownf6705572006-03-27 01:18:11 -08004096 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004097 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004098 /* This personality cannot handle reshaping... */
4099 mddev->pers = NULL;
4100 module_put(pers->owner);
4101 return -EINVAL;
4102 }
4103
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004104 if (pers->sync_request) {
4105 /* Warn if this is a potentially silly
4106 * configuration.
4107 */
4108 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4109 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004110 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004111
4112 list_for_each_entry(rdev, &mddev->disks, same_set)
4113 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004114 if (rdev < rdev2 &&
4115 rdev->bdev->bd_contains ==
4116 rdev2->bdev->bd_contains) {
4117 printk(KERN_WARNING
4118 "%s: WARNING: %s appears to be"
4119 " on the same physical disk as"
4120 " %s.\n",
4121 mdname(mddev),
4122 bdevname(rdev->bdev,b),
4123 bdevname(rdev2->bdev,b2));
4124 warned = 1;
4125 }
4126 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004127
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004128 if (warned)
4129 printk(KERN_WARNING
4130 "True protection against single-disk"
4131 " failure might be compromised.\n");
4132 }
4133
NeilBrown657390d2005-08-26 18:34:16 -07004134 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004135 /* may be over-ridden by personality */
4136 mddev->resync_max_sectors = mddev->dev_sectors;
4137
NeilBrowna9701a32005-11-08 21:39:34 -08004138 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004139 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
NeilBrownf91de922005-11-08 21:39:36 -08004141 if (start_readonly)
4142 mddev->ro = 2; /* read-only, but switch on first write */
4143
NeilBrownb15c2e52006-01-06 00:20:16 -08004144 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004145 if (err)
4146 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004147 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4148 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4149 " but 'external_size' not in effect?\n", __func__);
4150 printk(KERN_ERR
4151 "md: invalid array_size %llu > default size %llu\n",
4152 (unsigned long long)mddev->array_sectors / 2,
4153 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4154 err = -EINVAL;
4155 mddev->pers->stop(mddev);
4156 }
4157 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004158 err = bitmap_create(mddev);
4159 if (err) {
4160 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4161 mdname(mddev), err);
4162 mddev->pers->stop(mddev);
4163 }
4164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 module_put(mddev->pers->owner);
4167 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004168 bitmap_destroy(mddev);
4169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004171 if (mddev->pers->sync_request) {
4172 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4173 printk(KERN_WARNING
4174 "md: cannot register extra attributes for %s\n",
4175 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004176 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004177 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004178 mddev->ro = 0;
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 atomic_set(&mddev->writes_pending,0);
4181 mddev->safemode = 0;
4182 mddev->safemode_timer.function = md_safemode_timeout;
4183 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004184 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004186
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004187 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004188 if (rdev->raid_disk >= 0) {
4189 char nm[20];
4190 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004191 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4192 printk("md: cannot register %s for %s\n",
4193 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4197
NeilBrown850b2b42006-10-03 01:15:46 -07004198 if (mddev->flags)
4199 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Andre Nollf233ea52008-07-21 17:05:22 +10004201 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004203 /* If there is a partially-recovered drive we need to
4204 * start recovery here. If we leave it to md_check_recovery,
4205 * it will remove the drives and not do the right thing
4206 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004207 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004208 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004209 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004210 if (rdev->raid_disk >= 0 &&
4211 !test_bit(In_sync, &rdev->flags) &&
4212 !test_bit(Faulty, &rdev->flags))
4213 /* complete an interrupted recovery */
4214 spares++;
4215 if (spares && mddev->pers->sync_request) {
4216 mddev->recovery = 0;
4217 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4218 mddev->sync_thread = md_register_thread(md_do_sync,
4219 mddev,
4220 "%s_resync");
4221 if (!mddev->sync_thread) {
4222 printk(KERN_ERR "%s: could not start resync"
4223 " thread...\n",
4224 mdname(mddev));
4225 /* leave the spares where they are, it shouldn't hurt */
4226 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004227 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004228 }
4229 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004230 md_wakeup_thread(mddev->thread);
4231 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004232
NeilBrown449aad32009-08-03 10:59:58 +10004233 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004234 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004235 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004236 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004237 if (mddev->sysfs_action)
4238 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004239 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004240 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return 0;
4242}
4243
4244static int restart_array(mddev_t *mddev)
4245{
4246 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Andre Noll80fab1d2008-07-11 22:02:21 +10004248 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004250 return -ENXIO;
4251 if (!mddev->pers)
4252 return -EINVAL;
4253 if (!mddev->ro)
4254 return -EBUSY;
4255 mddev->safemode = 0;
4256 mddev->ro = 0;
4257 set_disk_ro(disk, 0);
4258 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4259 mdname(mddev));
4260 /* Kick recovery or resync if necessary */
4261 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4262 md_wakeup_thread(mddev->thread);
4263 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004264 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267
NeilBrownacc55e22006-06-26 00:27:47 -07004268/* similar to deny_write_access, but accounts for our holding a reference
4269 * to the file ourselves */
4270static int deny_bitmap_write_access(struct file * file)
4271{
4272 struct inode *inode = file->f_mapping->host;
4273
4274 spin_lock(&inode->i_lock);
4275 if (atomic_read(&inode->i_writecount) > 1) {
4276 spin_unlock(&inode->i_lock);
4277 return -ETXTBSY;
4278 }
4279 atomic_set(&inode->i_writecount, -1);
4280 spin_unlock(&inode->i_lock);
4281
4282 return 0;
4283}
4284
4285static void restore_bitmap_write_access(struct file *file)
4286{
4287 struct inode *inode = file->f_mapping->host;
4288
4289 spin_lock(&inode->i_lock);
4290 atomic_set(&inode->i_writecount, 1);
4291 spin_unlock(&inode->i_lock);
4292}
4293
NeilBrown9e653b62006-06-26 00:27:58 -07004294/* mode:
4295 * 0 - completely stop and dis-assemble array
4296 * 1 - switch to readonly
4297 * 2 - stop but do not disassemble array
4298 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004299static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 int err = 0;
4302 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004303 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
NeilBrownc8c00a62009-08-10 12:50:52 +10004305 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004306 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004307 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004308 err = -EBUSY;
4309 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004312 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4314 md_unregister_thread(mddev->sync_thread);
4315 mddev->sync_thread = NULL;
4316 }
4317
4318 del_timer_sync(&mddev->safemode_timer);
4319
NeilBrown9e653b62006-06-26 00:27:58 -07004320 switch(mode) {
4321 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004323 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 goto out;
4325 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004326 break;
4327 case 0: /* disassemble */
4328 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004329 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004330 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (mddev->ro)
4332 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004335 mddev->queue->merge_bvec_fn = NULL;
4336 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004337 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004339 if (mddev->pers->sync_request)
4340 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004342 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004343 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004344
NeilBrownc4647292009-05-07 12:51:06 +10004345 list_for_each_entry(rdev, &mddev->disks, same_set)
4346 if (rdev->raid_disk >= 0) {
4347 char nm[20];
4348 sprintf(nm, "rd%d", rdev->raid_disk);
4349 sysfs_remove_link(&mddev->kobj, nm);
4350 }
4351
NeilBrown0d4ca602006-12-10 02:20:44 -08004352 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004353 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (mddev->ro)
4356 mddev->ro = 0;
4357 }
NeilBrown850b2b42006-10-03 01:15:46 -07004358 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 /* mark array as shutdown cleanly */
4360 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004361 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
NeilBrown9e653b62006-06-26 00:27:58 -07004363 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004365 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004367out:
4368 mutex_unlock(&mddev->open_mutex);
4369 if (err)
4370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 /*
4372 * Free resources if final stop
4373 */
NeilBrown9e653b62006-06-26 00:27:58 -07004374 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4377
NeilBrown978f9462006-02-02 14:28:05 -08004378 bitmap_destroy(mddev);
4379 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004380 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004381 fput(mddev->bitmap_file);
4382 mddev->bitmap_file = NULL;
4383 }
4384 mddev->bitmap_offset = 0;
4385
NeilBrown177a99b2008-02-06 01:39:56 -08004386 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004387 flush_scheduled_work();
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 export_array(mddev);
4390
Andre Nollf233ea52008-07-21 17:05:22 +10004391 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004392 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004393 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004394 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004395 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004396 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004397 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004398 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004399 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004400 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004401 mddev->level = LEVEL_NONE;
4402 mddev->clevel[0] = 0;
4403 mddev->flags = 0;
4404 mddev->ro = 0;
4405 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004406 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004407 mddev->ctime = mddev->utime = 0;
4408 mddev->layout = 0;
4409 mddev->max_disks = 0;
4410 mddev->events = 0;
4411 mddev->delta_disks = 0;
4412 mddev->new_level = LEVEL_NONE;
4413 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004414 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004415 mddev->curr_resync = 0;
4416 mddev->resync_mismatches = 0;
4417 mddev->suspend_lo = mddev->suspend_hi = 0;
4418 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4419 mddev->recovery = 0;
4420 mddev->in_sync = 0;
4421 mddev->changed = 0;
4422 mddev->degraded = 0;
4423 mddev->barriers_work = 0;
4424 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004425 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004426 if (mddev->hold_active == UNTIL_STOP)
4427 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004428
NeilBrowna8a55c32006-06-26 00:27:39 -07004429 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4431 mdname(mddev));
4432 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004433 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004434 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004435 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return err;
4437}
4438
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004439#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440static void autorun_array(mddev_t *mddev)
4441{
4442 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 int err;
4444
NeilBrowna757e642005-04-16 15:26:42 -07004445 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 printk(KERN_INFO "md: running: ");
4449
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004450 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 char b[BDEVNAME_SIZE];
4452 printk("<%s>", bdevname(rdev->bdev,b));
4453 }
4454 printk("\n");
4455
NeilBrownd710e132008-10-13 11:55:12 +11004456 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 if (err) {
4458 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004459 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 }
4461}
4462
4463/*
4464 * lets try to run arrays based on all disks that have arrived
4465 * until now. (those are in pending_raid_disks)
4466 *
4467 * the method: pick the first pending disk, collect all disks with
4468 * the same UUID, remove all from the pending list and put them into
4469 * the 'same_array' list. Then order this list based on superblock
4470 * update time (freshest comes first), kick out 'old' disks and
4471 * compare superblocks. If everything's fine then run it.
4472 *
4473 * If "unit" is allocated, then bump its reference count
4474 */
4475static void autorun_devices(int part)
4476{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004477 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 mddev_t *mddev;
4479 char b[BDEVNAME_SIZE];
4480
4481 printk(KERN_INFO "md: autorun ...\n");
4482 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004483 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004485 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 rdev0 = list_entry(pending_raid_disks.next,
4487 mdk_rdev_t, same_set);
4488
4489 printk(KERN_INFO "md: considering %s ...\n",
4490 bdevname(rdev0->bdev,b));
4491 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004492 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 if (super_90_load(rdev, rdev0, 0) >= 0) {
4494 printk(KERN_INFO "md: adding %s ...\n",
4495 bdevname(rdev->bdev,b));
4496 list_move(&rdev->same_set, &candidates);
4497 }
4498 /*
4499 * now we have a set of devices, with all of them having
4500 * mostly sane superblocks. It's time to allocate the
4501 * mddev.
4502 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004503 if (part) {
4504 dev = MKDEV(mdp_major,
4505 rdev0->preferred_minor << MdpMinorShift);
4506 unit = MINOR(dev) >> MdpMinorShift;
4507 } else {
4508 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4509 unit = MINOR(dev);
4510 }
4511 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4513 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4514 break;
4515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
4517 md_probe(dev, NULL, NULL);
4518 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004519 if (!mddev || !mddev->gendisk) {
4520 if (mddev)
4521 mddev_put(mddev);
4522 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 "md: cannot allocate memory for md drive.\n");
4524 break;
4525 }
4526 if (mddev_lock(mddev))
4527 printk(KERN_WARNING "md: %s locked, cannot run\n",
4528 mdname(mddev));
4529 else if (mddev->raid_disks || mddev->major_version
4530 || !list_empty(&mddev->disks)) {
4531 printk(KERN_WARNING
4532 "md: %s already running, cannot run %s\n",
4533 mdname(mddev), bdevname(rdev0->bdev,b));
4534 mddev_unlock(mddev);
4535 } else {
4536 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004537 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004538 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 list_del_init(&rdev->same_set);
4540 if (bind_rdev_to_array(rdev, mddev))
4541 export_rdev(rdev);
4542 }
4543 autorun_array(mddev);
4544 mddev_unlock(mddev);
4545 }
4546 /* on success, candidates will be empty, on error
4547 * it won't...
4548 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004549 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004550 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 mddev_put(mddev);
4554 }
4555 printk(KERN_INFO "md: ... autorun DONE.\n");
4556}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004557#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559static int get_version(void __user * arg)
4560{
4561 mdu_version_t ver;
4562
4563 ver.major = MD_MAJOR_VERSION;
4564 ver.minor = MD_MINOR_VERSION;
4565 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4566
4567 if (copy_to_user(arg, &ver, sizeof(ver)))
4568 return -EFAULT;
4569
4570 return 0;
4571}
4572
4573static int get_array_info(mddev_t * mddev, void __user * arg)
4574{
4575 mdu_array_info_t info;
4576 int nr,working,active,failed,spare;
4577 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004580 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004582 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 failed++;
4584 else {
4585 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004586 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 active++;
4588 else
4589 spare++;
4590 }
4591 }
4592
4593 info.major_version = mddev->major_version;
4594 info.minor_version = mddev->minor_version;
4595 info.patch_version = MD_PATCHLEVEL_VERSION;
4596 info.ctime = mddev->ctime;
4597 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004598 info.size = mddev->dev_sectors / 2;
4599 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004600 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 info.nr_disks = nr;
4602 info.raid_disks = mddev->raid_disks;
4603 info.md_minor = mddev->md_minor;
4604 info.not_persistent= !mddev->persistent;
4605
4606 info.utime = mddev->utime;
4607 info.state = 0;
4608 if (mddev->in_sync)
4609 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004610 if (mddev->bitmap && mddev->bitmap_offset)
4611 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 info.active_disks = active;
4613 info.working_disks = working;
4614 info.failed_disks = failed;
4615 info.spare_disks = spare;
4616
4617 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004618 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 if (copy_to_user(arg, &info, sizeof(info)))
4621 return -EFAULT;
4622
4623 return 0;
4624}
4625
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004626static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004627{
4628 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4629 char *ptr, *buf = NULL;
4630 int err = -ENOMEM;
4631
Dan Williamsb5470dc2008-06-27 21:44:04 -07004632 if (md_allow_write(mddev))
4633 file = kmalloc(sizeof(*file), GFP_NOIO);
4634 else
4635 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004636
NeilBrown32a76272005-06-21 17:17:14 -07004637 if (!file)
4638 goto out;
4639
4640 /* bitmap disabled, zero the first byte and copy out */
4641 if (!mddev->bitmap || !mddev->bitmap->file) {
4642 file->pathname[0] = '\0';
4643 goto copy_out;
4644 }
4645
4646 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4647 if (!buf)
4648 goto out;
4649
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004650 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4651 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004652 goto out;
4653
4654 strcpy(file->pathname, ptr);
4655
4656copy_out:
4657 err = 0;
4658 if (copy_to_user(arg, file, sizeof(*file)))
4659 err = -EFAULT;
4660out:
4661 kfree(buf);
4662 kfree(file);
4663 return err;
4664}
4665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666static int get_disk_info(mddev_t * mddev, void __user * arg)
4667{
4668 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 mdk_rdev_t *rdev;
4670
4671 if (copy_from_user(&info, arg, sizeof(info)))
4672 return -EFAULT;
4673
Andre Noll26ef3792008-07-11 22:02:21 +10004674 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 if (rdev) {
4676 info.major = MAJOR(rdev->bdev->bd_dev);
4677 info.minor = MINOR(rdev->bdev->bd_dev);
4678 info.raid_disk = rdev->raid_disk;
4679 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004680 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004682 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 info.state |= (1<<MD_DISK_ACTIVE);
4684 info.state |= (1<<MD_DISK_SYNC);
4685 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004686 if (test_bit(WriteMostly, &rdev->flags))
4687 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 } else {
4689 info.major = info.minor = 0;
4690 info.raid_disk = -1;
4691 info.state = (1<<MD_DISK_REMOVED);
4692 }
4693
4694 if (copy_to_user(arg, &info, sizeof(info)))
4695 return -EFAULT;
4696
4697 return 0;
4698}
4699
4700static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4701{
4702 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4703 mdk_rdev_t *rdev;
4704 dev_t dev = MKDEV(info->major,info->minor);
4705
4706 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4707 return -EOVERFLOW;
4708
4709 if (!mddev->raid_disks) {
4710 int err;
4711 /* expecting a device which has a superblock */
4712 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4713 if (IS_ERR(rdev)) {
4714 printk(KERN_WARNING
4715 "md: md_import_device returned %ld\n",
4716 PTR_ERR(rdev));
4717 return PTR_ERR(rdev);
4718 }
4719 if (!list_empty(&mddev->disks)) {
4720 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4721 mdk_rdev_t, same_set);
4722 int err = super_types[mddev->major_version]
4723 .load_super(rdev, rdev0, mddev->minor_version);
4724 if (err < 0) {
4725 printk(KERN_WARNING
4726 "md: %s has different UUID to %s\n",
4727 bdevname(rdev->bdev,b),
4728 bdevname(rdev0->bdev,b2));
4729 export_rdev(rdev);
4730 return -EINVAL;
4731 }
4732 }
4733 err = bind_rdev_to_array(rdev, mddev);
4734 if (err)
4735 export_rdev(rdev);
4736 return err;
4737 }
4738
4739 /*
4740 * add_new_disk can be used once the array is assembled
4741 * to add "hot spares". They must already have a superblock
4742 * written
4743 */
4744 if (mddev->pers) {
4745 int err;
4746 if (!mddev->pers->hot_add_disk) {
4747 printk(KERN_WARNING
4748 "%s: personality does not support diskops!\n",
4749 mdname(mddev));
4750 return -EINVAL;
4751 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004752 if (mddev->persistent)
4753 rdev = md_import_device(dev, mddev->major_version,
4754 mddev->minor_version);
4755 else
4756 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 if (IS_ERR(rdev)) {
4758 printk(KERN_WARNING
4759 "md: md_import_device returned %ld\n",
4760 PTR_ERR(rdev));
4761 return PTR_ERR(rdev);
4762 }
NeilBrown41158c72005-06-21 17:17:25 -07004763 /* set save_raid_disk if appropriate */
4764 if (!mddev->persistent) {
4765 if (info->state & (1<<MD_DISK_SYNC) &&
4766 info->raid_disk < mddev->raid_disks)
4767 rdev->raid_disk = info->raid_disk;
4768 else
4769 rdev->raid_disk = -1;
4770 } else
4771 super_types[mddev->major_version].
4772 validate_super(mddev, rdev);
4773 rdev->saved_raid_disk = rdev->raid_disk;
4774
NeilBrownb2d444d2005-11-08 21:39:31 -08004775 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004776 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4777 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004778 else
4779 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 rdev->raid_disk = -1;
4782 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004783 if (!err && !mddev->pers->hot_remove_disk) {
4784 /* If there is hot_add_disk but no hot_remove_disk
4785 * then added disks for geometry changes,
4786 * and should be added immediately.
4787 */
4788 super_types[mddev->major_version].
4789 validate_super(mddev, rdev);
4790 err = mddev->pers->hot_add_disk(mddev, rdev);
4791 if (err)
4792 unbind_rdev_from_array(rdev);
4793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 if (err)
4795 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004796 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004797 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004798
NeilBrown17571282006-12-10 02:20:52 -08004799 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004800 if (mddev->degraded)
4801 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004802 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004803 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return err;
4805 }
4806
4807 /* otherwise, add_new_disk is only allowed
4808 * for major_version==0 superblocks
4809 */
4810 if (mddev->major_version != 0) {
4811 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4812 mdname(mddev));
4813 return -EINVAL;
4814 }
4815
4816 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4817 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004818 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 if (IS_ERR(rdev)) {
4820 printk(KERN_WARNING
4821 "md: error, md_import_device() returned %ld\n",
4822 PTR_ERR(rdev));
4823 return PTR_ERR(rdev);
4824 }
4825 rdev->desc_nr = info->number;
4826 if (info->raid_disk < mddev->raid_disks)
4827 rdev->raid_disk = info->raid_disk;
4828 else
4829 rdev->raid_disk = -1;
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004832 if (info->state & (1<<MD_DISK_SYNC))
4833 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004835 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4836 set_bit(WriteMostly, &rdev->flags);
4837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 if (!mddev->persistent) {
4839 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004840 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 } else
Andre Noll0f420352008-07-11 22:02:23 +10004842 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004843 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
NeilBrown2bf071b2006-01-06 00:20:55 -08004845 err = bind_rdev_to_array(rdev, mddev);
4846 if (err) {
4847 export_rdev(rdev);
4848 return err;
4849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
4851
4852 return 0;
4853}
4854
4855static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4856{
4857 char b[BDEVNAME_SIZE];
4858 mdk_rdev_t *rdev;
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 rdev = find_rdev(mddev, dev);
4861 if (!rdev)
4862 return -ENXIO;
4863
4864 if (rdev->raid_disk >= 0)
4865 goto busy;
4866
4867 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004868 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004869 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
4871 return 0;
4872busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004873 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 bdevname(rdev->bdev,b), mdname(mddev));
4875 return -EBUSY;
4876}
4877
4878static int hot_add_disk(mddev_t * mddev, dev_t dev)
4879{
4880 char b[BDEVNAME_SIZE];
4881 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 mdk_rdev_t *rdev;
4883
4884 if (!mddev->pers)
4885 return -ENODEV;
4886
4887 if (mddev->major_version != 0) {
4888 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4889 " version-0 superblocks.\n",
4890 mdname(mddev));
4891 return -EINVAL;
4892 }
4893 if (!mddev->pers->hot_add_disk) {
4894 printk(KERN_WARNING
4895 "%s: personality does not support diskops!\n",
4896 mdname(mddev));
4897 return -EINVAL;
4898 }
4899
NeilBrownd710e132008-10-13 11:55:12 +11004900 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 if (IS_ERR(rdev)) {
4902 printk(KERN_WARNING
4903 "md: error, md_import_device() returned %ld\n",
4904 PTR_ERR(rdev));
4905 return -EINVAL;
4906 }
4907
4908 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004909 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 else
Andre Noll0f420352008-07-11 22:02:23 +10004911 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
NeilBrown8190e752009-06-18 08:48:58 +10004913 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
NeilBrownb2d444d2005-11-08 21:39:31 -08004915 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 printk(KERN_WARNING
4917 "md: can not hot-add faulty %s disk to %s!\n",
4918 bdevname(rdev->bdev,b), mdname(mddev));
4919 err = -EINVAL;
4920 goto abort_export;
4921 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004922 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004924 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004925 err = bind_rdev_to_array(rdev, mddev);
4926 if (err)
4927 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
4929 /*
4930 * The rest should better be atomic, we can have disk failures
4931 * noticed in interrupt contexts ...
4932 */
4933
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 rdev->raid_disk = -1;
4935
NeilBrown850b2b42006-10-03 01:15:46 -07004936 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 /*
4939 * Kick recovery, maybe this spare has to be added to the
4940 * array immediately.
4941 */
4942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4943 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004944 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return 0;
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947abort_export:
4948 export_rdev(rdev);
4949 return err;
4950}
4951
NeilBrown32a76272005-06-21 17:17:14 -07004952static int set_bitmap_file(mddev_t *mddev, int fd)
4953{
4954 int err;
4955
NeilBrown36fa3062005-09-09 16:23:45 -07004956 if (mddev->pers) {
4957 if (!mddev->pers->quiesce)
4958 return -EBUSY;
4959 if (mddev->recovery || mddev->sync_thread)
4960 return -EBUSY;
4961 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004962 }
4963
NeilBrown36fa3062005-09-09 16:23:45 -07004964
4965 if (fd >= 0) {
4966 if (mddev->bitmap)
4967 return -EEXIST; /* cannot add when bitmap is present */
4968 mddev->bitmap_file = fget(fd);
4969
4970 if (mddev->bitmap_file == NULL) {
4971 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4972 mdname(mddev));
4973 return -EBADF;
4974 }
4975
4976 err = deny_bitmap_write_access(mddev->bitmap_file);
4977 if (err) {
4978 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4979 mdname(mddev));
4980 fput(mddev->bitmap_file);
4981 mddev->bitmap_file = NULL;
4982 return err;
4983 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004984 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004985 } else if (mddev->bitmap == NULL)
4986 return -ENOENT; /* cannot remove what isn't there */
4987 err = 0;
4988 if (mddev->pers) {
4989 mddev->pers->quiesce(mddev, 1);
4990 if (fd >= 0)
4991 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004992 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004993 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004994 fd = -1; /* make sure to put the file */
4995 }
NeilBrown36fa3062005-09-09 16:23:45 -07004996 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004997 }
4998 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004999 if (mddev->bitmap_file) {
5000 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07005001 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07005002 }
NeilBrown36fa3062005-09-09 16:23:45 -07005003 mddev->bitmap_file = NULL;
5004 }
5005
NeilBrown32a76272005-06-21 17:17:14 -07005006 return err;
5007}
5008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009/*
5010 * set_array_info is used two different ways
5011 * The original usage is when creating a new array.
5012 * In this usage, raid_disks is > 0 and it together with
5013 * level, size, not_persistent,layout,chunksize determine the
5014 * shape of the array.
5015 * This will always create an array with a type-0.90.0 superblock.
5016 * The newer usage is when assembling an array.
5017 * In this case raid_disks will be 0, and the major_version field is
5018 * use to determine which style super-blocks are to be found on the devices.
5019 * The minor and patch _version numbers are also kept incase the
5020 * super_block handler wishes to interpret them.
5021 */
5022static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5023{
5024
5025 if (info->raid_disks == 0) {
5026 /* just setting version number for superblock loading */
5027 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005028 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 super_types[info->major_version].name == NULL) {
5030 /* maybe try to auto-load a module? */
5031 printk(KERN_INFO
5032 "md: superblock version %d not known\n",
5033 info->major_version);
5034 return -EINVAL;
5035 }
5036 mddev->major_version = info->major_version;
5037 mddev->minor_version = info->minor_version;
5038 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005039 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 return 0;
5041 }
5042 mddev->major_version = MD_MAJOR_VERSION;
5043 mddev->minor_version = MD_MINOR_VERSION;
5044 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5045 mddev->ctime = get_seconds();
5046
5047 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005048 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005049 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 mddev->raid_disks = info->raid_disks;
5051 /* don't set md_minor, it is determined by which /dev/md* was
5052 * openned
5053 */
5054 if (info->state & (1<<MD_SB_CLEAN))
5055 mddev->recovery_cp = MaxSector;
5056 else
5057 mddev->recovery_cp = 0;
5058 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005059 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005062 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063
5064 mddev->max_disks = MD_SB_DISKS;
5065
NeilBrowne6910632008-02-06 01:39:51 -08005066 if (mddev->persistent)
5067 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005068 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
NeilBrownb2a27032005-11-28 13:44:12 -08005070 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5071 mddev->bitmap_offset = 0;
5072
NeilBrownf6705572006-03-27 01:18:11 -08005073 mddev->reshape_position = MaxSector;
5074
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 /*
5076 * Generate a 128 bit UUID
5077 */
5078 get_random_bytes(mddev->uuid, 16);
5079
NeilBrownf6705572006-03-27 01:18:11 -08005080 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005081 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005082 mddev->new_layout = mddev->layout;
5083 mddev->delta_disks = 0;
5084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 return 0;
5086}
5087
Dan Williams1f403622009-03-31 14:59:03 +11005088void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5089{
Dan Williamsb522adc2009-03-31 15:00:31 +11005090 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5091
5092 if (mddev->external_size)
5093 return;
5094
Dan Williams1f403622009-03-31 14:59:03 +11005095 mddev->array_sectors = array_sectors;
5096}
5097EXPORT_SYMBOL(md_set_array_sectors);
5098
Andre Nolld71f9f82008-07-11 22:02:22 +10005099static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005100{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005101 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005102 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005103 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005104
5105 if (mddev->pers->resize == NULL)
5106 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005107 /* The "num_sectors" is the number of sectors of each device that
5108 * is used. This can only make sense for arrays with redundancy.
5109 * linear and raid0 always use whatever space is available. We can only
5110 * consider changing this number if no resync or reconstruction is
5111 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005112 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005113 * of each device. If num_sectors is zero, we find the largest size
5114 * that fits.
5115
NeilBrowna35b0d62006-01-06 00:20:49 -08005116 */
5117 if (mddev->sync_thread)
5118 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005119 if (mddev->bitmap)
5120 /* Sorry, cannot grow a bitmap yet, just remove it,
5121 * grow, and re-add.
5122 */
5123 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005124 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005125 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005126
Andre Nolld71f9f82008-07-11 22:02:22 +10005127 if (fit && (num_sectors == 0 || num_sectors > avail))
5128 num_sectors = avail;
5129 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005130 return -ENOSPC;
5131 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005132 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005133 if (!rv)
5134 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005135 return rv;
5136}
5137
NeilBrownda943b992006-01-06 00:20:54 -08005138static int update_raid_disks(mddev_t *mddev, int raid_disks)
5139{
5140 int rv;
5141 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005142 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005143 return -EINVAL;
5144 if (raid_disks <= 0 ||
5145 raid_disks >= mddev->max_disks)
5146 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005147 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005148 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005149 mddev->delta_disks = raid_disks - mddev->raid_disks;
5150
5151 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005152 return rv;
5153}
5154
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156/*
5157 * update_array_info is used to change the configuration of an
5158 * on-line array.
5159 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5160 * fields in the info are checked against the array.
5161 * Any differences that cannot be handled will cause an error.
5162 * Normally, only one change can be managed at a time.
5163 */
5164static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5165{
5166 int rv = 0;
5167 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005168 int state = 0;
5169
5170 /* calculate expected state,ignoring low bits */
5171 if (mddev->bitmap && mddev->bitmap_offset)
5172 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
5174 if (mddev->major_version != info->major_version ||
5175 mddev->minor_version != info->minor_version ||
5176/* mddev->patch_version != info->patch_version || */
5177 mddev->ctime != info->ctime ||
5178 mddev->level != info->level ||
5179/* mddev->layout != info->layout || */
5180 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005181 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005182 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5183 ((state^info->state) & 0xfffffe00)
5184 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 return -EINVAL;
5186 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005187 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5188 cnt++;
5189 if (mddev->raid_disks != info->raid_disks)
5190 cnt++;
5191 if (mddev->layout != info->layout)
5192 cnt++;
5193 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5194 cnt++;
5195 if (cnt == 0)
5196 return 0;
5197 if (cnt > 1)
5198 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
5200 if (mddev->layout != info->layout) {
5201 /* Change layout
5202 * we don't need to do anything at the md level, the
5203 * personality will take care of it all.
5204 */
NeilBrown50ac1682009-06-18 08:47:55 +10005205 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005207 else {
5208 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005209 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005210 if (rv)
5211 mddev->new_layout = mddev->layout;
5212 return rv;
5213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005215 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005216 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
NeilBrownda943b992006-01-06 00:20:54 -08005218 if (mddev->raid_disks != info->raid_disks)
5219 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
NeilBrown36fa3062005-09-09 16:23:45 -07005221 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5222 if (mddev->pers->quiesce == NULL)
5223 return -EINVAL;
5224 if (mddev->recovery || mddev->sync_thread)
5225 return -EBUSY;
5226 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5227 /* add the bitmap */
5228 if (mddev->bitmap)
5229 return -EEXIST;
5230 if (mddev->default_bitmap_offset == 0)
5231 return -EINVAL;
5232 mddev->bitmap_offset = mddev->default_bitmap_offset;
5233 mddev->pers->quiesce(mddev, 1);
5234 rv = bitmap_create(mddev);
5235 if (rv)
5236 bitmap_destroy(mddev);
5237 mddev->pers->quiesce(mddev, 0);
5238 } else {
5239 /* remove the bitmap */
5240 if (!mddev->bitmap)
5241 return -ENOENT;
5242 if (mddev->bitmap->file)
5243 return -EINVAL;
5244 mddev->pers->quiesce(mddev, 1);
5245 bitmap_destroy(mddev);
5246 mddev->pers->quiesce(mddev, 0);
5247 mddev->bitmap_offset = 0;
5248 }
5249 }
NeilBrown850b2b42006-10-03 01:15:46 -07005250 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 return rv;
5252}
5253
5254static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5255{
5256 mdk_rdev_t *rdev;
5257
5258 if (mddev->pers == NULL)
5259 return -ENODEV;
5260
5261 rdev = find_rdev(mddev, dev);
5262 if (!rdev)
5263 return -ENODEV;
5264
5265 md_error(mddev, rdev);
5266 return 0;
5267}
5268
Andre Noll2f9618c2008-04-25 18:57:58 +02005269/*
5270 * We have a problem here : there is no easy way to give a CHS
5271 * virtual geometry. We currently pretend that we have a 2 heads
5272 * 4 sectors (with a BIG number of cylinders...). This drives
5273 * dosfs just mad... ;-)
5274 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005275static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5276{
5277 mddev_t *mddev = bdev->bd_disk->private_data;
5278
5279 geo->heads = 2;
5280 geo->sectors = 4;
5281 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5282 return 0;
5283}
5284
Al Viroa39907f2008-03-02 10:31:15 -05005285static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 unsigned int cmd, unsigned long arg)
5287{
5288 int err = 0;
5289 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 mddev_t *mddev = NULL;
5291
5292 if (!capable(CAP_SYS_ADMIN))
5293 return -EACCES;
5294
5295 /*
5296 * Commands dealing with the RAID driver but not any
5297 * particular array:
5298 */
5299 switch (cmd)
5300 {
5301 case RAID_VERSION:
5302 err = get_version(argp);
5303 goto done;
5304
5305 case PRINT_RAID_DEBUG:
5306 err = 0;
5307 md_print_devices();
5308 goto done;
5309
5310#ifndef MODULE
5311 case RAID_AUTORUN:
5312 err = 0;
5313 autostart_arrays(arg);
5314 goto done;
5315#endif
5316 default:;
5317 }
5318
5319 /*
5320 * Commands creating/starting a new array:
5321 */
5322
Al Viroa39907f2008-03-02 10:31:15 -05005323 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
5325 if (!mddev) {
5326 BUG();
5327 goto abort;
5328 }
5329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 err = mddev_lock(mddev);
5331 if (err) {
5332 printk(KERN_INFO
5333 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5334 err, cmd);
5335 goto abort;
5336 }
5337
5338 switch (cmd)
5339 {
5340 case SET_ARRAY_INFO:
5341 {
5342 mdu_array_info_t info;
5343 if (!arg)
5344 memset(&info, 0, sizeof(info));
5345 else if (copy_from_user(&info, argp, sizeof(info))) {
5346 err = -EFAULT;
5347 goto abort_unlock;
5348 }
5349 if (mddev->pers) {
5350 err = update_array_info(mddev, &info);
5351 if (err) {
5352 printk(KERN_WARNING "md: couldn't update"
5353 " array info. %d\n", err);
5354 goto abort_unlock;
5355 }
5356 goto done_unlock;
5357 }
5358 if (!list_empty(&mddev->disks)) {
5359 printk(KERN_WARNING
5360 "md: array %s already has disks!\n",
5361 mdname(mddev));
5362 err = -EBUSY;
5363 goto abort_unlock;
5364 }
5365 if (mddev->raid_disks) {
5366 printk(KERN_WARNING
5367 "md: array %s already initialised!\n",
5368 mdname(mddev));
5369 err = -EBUSY;
5370 goto abort_unlock;
5371 }
5372 err = set_array_info(mddev, &info);
5373 if (err) {
5374 printk(KERN_WARNING "md: couldn't set"
5375 " array info. %d\n", err);
5376 goto abort_unlock;
5377 }
5378 }
5379 goto done_unlock;
5380
5381 default:;
5382 }
5383
5384 /*
5385 * Commands querying/configuring an existing array:
5386 */
NeilBrown32a76272005-06-21 17:17:14 -07005387 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005388 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005389 if ((!mddev->raid_disks && !mddev->external)
5390 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5391 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5392 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 err = -ENODEV;
5394 goto abort_unlock;
5395 }
5396
5397 /*
5398 * Commands even a read-only array can execute:
5399 */
5400 switch (cmd)
5401 {
5402 case GET_ARRAY_INFO:
5403 err = get_array_info(mddev, argp);
5404 goto done_unlock;
5405
NeilBrown32a76272005-06-21 17:17:14 -07005406 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005407 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005408 goto done_unlock;
5409
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 case GET_DISK_INFO:
5411 err = get_disk_info(mddev, argp);
5412 goto done_unlock;
5413
5414 case RESTART_ARRAY_RW:
5415 err = restart_array(mddev);
5416 goto done_unlock;
5417
5418 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005419 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 goto done_unlock;
5421
5422 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005423 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 goto done_unlock;
5425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 }
5427
5428 /*
5429 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005430 * superblock, so we do not allow them on read-only arrays.
5431 * However non-MD ioctls (e.g. get-size) will still come through
5432 * here and hit the 'default' below, so only disallow
5433 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005435 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005436 if (mddev->ro == 2) {
5437 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005438 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005439 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5440 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005441 } else {
5442 err = -EROFS;
5443 goto abort_unlock;
5444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 }
5446
5447 switch (cmd)
5448 {
5449 case ADD_NEW_DISK:
5450 {
5451 mdu_disk_info_t info;
5452 if (copy_from_user(&info, argp, sizeof(info)))
5453 err = -EFAULT;
5454 else
5455 err = add_new_disk(mddev, &info);
5456 goto done_unlock;
5457 }
5458
5459 case HOT_REMOVE_DISK:
5460 err = hot_remove_disk(mddev, new_decode_dev(arg));
5461 goto done_unlock;
5462
5463 case HOT_ADD_DISK:
5464 err = hot_add_disk(mddev, new_decode_dev(arg));
5465 goto done_unlock;
5466
5467 case SET_DISK_FAULTY:
5468 err = set_disk_faulty(mddev, new_decode_dev(arg));
5469 goto done_unlock;
5470
5471 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005472 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 goto done_unlock;
5474
NeilBrown32a76272005-06-21 17:17:14 -07005475 case SET_BITMAP_FILE:
5476 err = set_bitmap_file(mddev, (int)arg);
5477 goto done_unlock;
5478
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 err = -EINVAL;
5481 goto abort_unlock;
5482 }
5483
5484done_unlock:
5485abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005486 if (mddev->hold_active == UNTIL_IOCTL &&
5487 err != -EINVAL)
5488 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 mddev_unlock(mddev);
5490
5491 return err;
5492done:
5493 if (err)
5494 MD_BUG();
5495abort:
5496 return err;
5497}
5498
Al Viroa39907f2008-03-02 10:31:15 -05005499static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500{
5501 /*
5502 * Succeed if we can lock the mddev, which confirms that
5503 * it isn't being stopped right now.
5504 */
NeilBrownd3374822009-01-09 08:31:10 +11005505 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 int err;
5507
NeilBrownd3374822009-01-09 08:31:10 +11005508 if (mddev->gendisk != bdev->bd_disk) {
5509 /* we are racing with mddev_put which is discarding this
5510 * bd_disk.
5511 */
5512 mddev_put(mddev);
5513 /* Wait until bdev->bd_disk is definitely gone */
5514 flush_scheduled_work();
5515 /* Then retry the open from the top */
5516 return -ERESTARTSYS;
5517 }
5518 BUG_ON(mddev != bdev->bd_disk->private_data);
5519
NeilBrownc8c00a62009-08-10 12:50:52 +10005520 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 goto out;
5522
5523 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005524 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005525 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
Al Viroa39907f2008-03-02 10:31:15 -05005527 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 out:
5529 return err;
5530}
5531
Al Viroa39907f2008-03-02 10:31:15 -05005532static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
Al Viroa39907f2008-03-02 10:31:15 -05005534 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005536 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005537 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 mddev_put(mddev);
5539
5540 return 0;
5541}
5542
Linus Torvalds44ce62942007-05-09 18:51:36 -07005543static int md_media_changed(struct gendisk *disk)
5544{
5545 mddev_t *mddev = disk->private_data;
5546
5547 return mddev->changed;
5548}
5549
5550static int md_revalidate(struct gendisk *disk)
5551{
5552 mddev_t *mddev = disk->private_data;
5553
5554 mddev->changed = 0;
5555 return 0;
5556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557static struct block_device_operations md_fops =
5558{
5559 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005560 .open = md_open,
5561 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005562 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005563 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005564 .media_changed = md_media_changed,
5565 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566};
5567
Adrian Bunk75c96f82005-05-05 16:16:09 -07005568static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569{
5570 mdk_thread_t *thread = arg;
5571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 /*
5573 * md_thread is a 'system-thread', it's priority should be very
5574 * high. We avoid resource deadlocks individually in each
5575 * raid personality. (RAID5 does preallocation) We also use RR and
5576 * the very same RT priority as kswapd, thus we will never get
5577 * into a priority inversion deadlock.
5578 *
5579 * we definitely have to have equal or higher priority than
5580 * bdflush, otherwise bdflush will deadlock if there are too
5581 * many dirty RAID5 blocks.
5582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
NeilBrown6985c432005-10-19 21:23:47 -07005584 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005585 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
NeilBrown93588e22005-11-15 00:09:12 -08005587 /* We need to wait INTERRUPTIBLE so that
5588 * we don't add to the load-average.
5589 * That means we need to be sure no signals are
5590 * pending
5591 */
5592 if (signal_pending(current))
5593 flush_signals(current);
5594
5595 wait_event_interruptible_timeout
5596 (thread->wqueue,
5597 test_bit(THREAD_WAKEUP, &thread->flags)
5598 || kthread_should_stop(),
5599 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
5601 clear_bit(THREAD_WAKEUP, &thread->flags);
5602
NeilBrown787453c2005-11-08 21:39:43 -08005603 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005605
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return 0;
5607}
5608
5609void md_wakeup_thread(mdk_thread_t *thread)
5610{
5611 if (thread) {
5612 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5613 set_bit(THREAD_WAKEUP, &thread->flags);
5614 wake_up(&thread->wqueue);
5615 }
5616}
5617
5618mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5619 const char *name)
5620{
5621 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
NeilBrown9ffae0c2006-01-06 00:20:32 -08005623 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 if (!thread)
5625 return NULL;
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 init_waitqueue_head(&thread->wqueue);
5628
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 thread->run = run;
5630 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005631 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005632 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005633 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 kfree(thread);
5635 return NULL;
5636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 return thread;
5638}
5639
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640void md_unregister_thread(mdk_thread_t *thread)
5641{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005642 if (!thread)
5643 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005644 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005645
5646 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 kfree(thread);
5648}
5649
5650void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5651{
5652 if (!mddev) {
5653 MD_BUG();
5654 return;
5655 }
5656
NeilBrownb2d444d2005-11-08 21:39:31 -08005657 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005659
5660 if (mddev->external)
5661 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005662/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5664 mdname(mddev),
5665 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5666 __builtin_return_address(0),__builtin_return_address(1),
5667 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005668*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005669 if (!mddev->pers)
5670 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 if (!mddev->pers->error_handler)
5672 return;
5673 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005674 if (mddev->degraded)
5675 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005676 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5678 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5679 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005680 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681}
5682
5683/* seq_file implementation /proc/mdstat */
5684
5685static void status_unused(struct seq_file *seq)
5686{
5687 int i = 0;
5688 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689
5690 seq_printf(seq, "unused devices: ");
5691
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005692 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 char b[BDEVNAME_SIZE];
5694 i++;
5695 seq_printf(seq, "%s ",
5696 bdevname(rdev->bdev,b));
5697 }
5698 if (!i)
5699 seq_printf(seq, "<none>");
5700
5701 seq_printf(seq, "\n");
5702}
5703
5704
5705static void status_resync(struct seq_file *seq, mddev_t * mddev)
5706{
NeilBrowndd71cf62009-05-07 12:49:35 +10005707 sector_t max_sectors, resync, res;
5708 unsigned long dt, db;
5709 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005710 int scale;
5711 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
NeilBrowndd71cf62009-05-07 12:49:35 +10005713 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
5715 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005716 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005718 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
5720 /*
5721 * Should not happen.
5722 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005723 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 MD_BUG();
5725 return;
5726 }
NeilBrown4588b422006-03-27 01:18:04 -08005727 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005728 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005729 * u32, as those are the requirements for sector_div.
5730 * Thus 'scale' must be at least 10
5731 */
5732 scale = 10;
5733 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005734 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005735 scale++;
5736 }
5737 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005738 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005739
5740 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 {
NeilBrown4588b422006-03-27 01:18:04 -08005742 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 seq_printf(seq, "[");
5744 for (i = 0; i < x; i++)
5745 seq_printf(seq, "=");
5746 seq_printf(seq, ">");
5747 for (i = 0; i < y; i++)
5748 seq_printf(seq, ".");
5749 seq_printf(seq, "] ");
5750 }
NeilBrown4588b422006-03-27 01:18:04 -08005751 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005752 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5753 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005754 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5755 "check" :
5756 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5757 "resync" : "recovery"))),
5758 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005759 (unsigned long long) resync/2,
5760 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 * dt: time from mark until now
5764 * db: blocks written from mark until now
5765 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005766 *
5767 * rt is a sector_t, so could be 32bit or 64bit.
5768 * So we divide before multiply in case it is 32bit and close
5769 * to the limit.
5770 * We scale the divisor (db) by 32 to avoid loosing precision
5771 * near the end of resync when the number of remaining sectors
5772 * is close to 'db'.
5773 * We then divide rt by 32 after multiplying by db to compensate.
5774 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 */
5776 dt = ((jiffies - mddev->resync_mark) / HZ);
5777 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005778 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5779 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
NeilBrowndd71cf62009-05-07 12:49:35 +10005781 rt = max_sectors - resync; /* number of remaining sectors */
5782 sector_div(rt, db/32+1);
5783 rt *= dt;
5784 rt >>= 5;
5785
5786 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5787 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
NeilBrownff4e8d92006-07-10 04:44:16 -07005789 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790}
5791
5792static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5793{
5794 struct list_head *tmp;
5795 loff_t l = *pos;
5796 mddev_t *mddev;
5797
5798 if (l >= 0x10000)
5799 return NULL;
5800 if (!l--)
5801 /* header */
5802 return (void*)1;
5803
5804 spin_lock(&all_mddevs_lock);
5805 list_for_each(tmp,&all_mddevs)
5806 if (!l--) {
5807 mddev = list_entry(tmp, mddev_t, all_mddevs);
5808 mddev_get(mddev);
5809 spin_unlock(&all_mddevs_lock);
5810 return mddev;
5811 }
5812 spin_unlock(&all_mddevs_lock);
5813 if (!l--)
5814 return (void*)2;/* tail */
5815 return NULL;
5816}
5817
5818static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5819{
5820 struct list_head *tmp;
5821 mddev_t *next_mddev, *mddev = v;
5822
5823 ++*pos;
5824 if (v == (void*)2)
5825 return NULL;
5826
5827 spin_lock(&all_mddevs_lock);
5828 if (v == (void*)1)
5829 tmp = all_mddevs.next;
5830 else
5831 tmp = mddev->all_mddevs.next;
5832 if (tmp != &all_mddevs)
5833 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5834 else {
5835 next_mddev = (void*)2;
5836 *pos = 0x10000;
5837 }
5838 spin_unlock(&all_mddevs_lock);
5839
5840 if (v != (void*)1)
5841 mddev_put(mddev);
5842 return next_mddev;
5843
5844}
5845
5846static void md_seq_stop(struct seq_file *seq, void *v)
5847{
5848 mddev_t *mddev = v;
5849
5850 if (mddev && v != (void*)1 && v != (void*)2)
5851 mddev_put(mddev);
5852}
5853
NeilBrownd7603b72006-01-06 00:20:30 -08005854struct mdstat_info {
5855 int event;
5856};
5857
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858static int md_seq_show(struct seq_file *seq, void *v)
5859{
5860 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005861 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005863 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005864 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
5866 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005867 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 seq_printf(seq, "Personalities : ");
5869 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005870 list_for_each_entry(pers, &pers_list, list)
5871 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 spin_unlock(&pers_lock);
5874 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005875 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return 0;
5877 }
5878 if (v == (void*)2) {
5879 status_unused(seq);
5880 return 0;
5881 }
5882
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005883 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005885
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5887 seq_printf(seq, "%s : %sactive", mdname(mddev),
5888 mddev->pers ? "" : "in");
5889 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005890 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005892 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005893 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 seq_printf(seq, " %s", mddev->pers->name);
5895 }
5896
Andre Nolldd8ac332009-03-31 14:33:13 +11005897 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005898 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 char b[BDEVNAME_SIZE];
5900 seq_printf(seq, " %s[%d]",
5901 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005902 if (test_bit(WriteMostly, &rdev->flags))
5903 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005904 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 seq_printf(seq, "(F)");
5906 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005907 } else if (rdev->raid_disk < 0)
5908 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005909 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 }
5911
5912 if (!list_empty(&mddev->disks)) {
5913 if (mddev->pers)
5914 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005915 (unsigned long long)
5916 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 else
5918 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005919 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005921 if (mddev->persistent) {
5922 if (mddev->major_version != 0 ||
5923 mddev->minor_version != 90) {
5924 seq_printf(seq," super %d.%d",
5925 mddev->major_version,
5926 mddev->minor_version);
5927 }
NeilBrowne6910632008-02-06 01:39:51 -08005928 } else if (mddev->external)
5929 seq_printf(seq, " super external:%s",
5930 mddev->metadata_type);
5931 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005932 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005935 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005937 if (mddev->pers->sync_request) {
5938 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005939 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005940 seq_printf(seq, "\n ");
5941 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5942 seq_printf(seq, "\tresync=DELAYED\n ");
5943 else if (mddev->recovery_cp < MaxSector)
5944 seq_printf(seq, "\tresync=PENDING\n ");
5945 }
NeilBrown32a76272005-06-21 17:17:14 -07005946 } else
5947 seq_printf(seq, "\n ");
5948
5949 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005950 unsigned long chunk_kb;
5951 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005952 spin_lock_irqsave(&bitmap->lock, flags);
5953 chunk_kb = bitmap->chunksize >> 10;
5954 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5955 "%lu%s chunk",
5956 bitmap->pages - bitmap->missing_pages,
5957 bitmap->pages,
5958 (bitmap->pages - bitmap->missing_pages)
5959 << (PAGE_SHIFT - 10),
5960 chunk_kb ? chunk_kb : bitmap->chunksize,
5961 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005962 if (bitmap->file) {
5963 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005964 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005965 }
NeilBrown78d742d2005-06-21 17:17:15 -07005966
NeilBrown32a76272005-06-21 17:17:14 -07005967 seq_printf(seq, "\n");
5968 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 }
5970
5971 seq_printf(seq, "\n");
5972 }
5973 mddev_unlock(mddev);
5974
5975 return 0;
5976}
5977
Jan Engelhardt110518b2009-05-07 12:49:37 +10005978static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 .start = md_seq_start,
5980 .next = md_seq_next,
5981 .stop = md_seq_stop,
5982 .show = md_seq_show,
5983};
5984
5985static int md_seq_open(struct inode *inode, struct file *file)
5986{
5987 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005988 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5989 if (mi == NULL)
5990 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
5992 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005993 if (error)
5994 kfree(mi);
5995 else {
5996 struct seq_file *p = file->private_data;
5997 p->private = mi;
5998 mi->event = atomic_read(&md_event_count);
5999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 return error;
6001}
6002
NeilBrownd7603b72006-01-06 00:20:30 -08006003static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6004{
6005 struct seq_file *m = filp->private_data;
6006 struct mdstat_info *mi = m->private;
6007 int mask;
6008
6009 poll_wait(filp, &md_event_waiters, wait);
6010
6011 /* always allow read */
6012 mask = POLLIN | POLLRDNORM;
6013
6014 if (mi->event != atomic_read(&md_event_count))
6015 mask |= POLLERR | POLLPRI;
6016 return mask;
6017}
6018
Arjan van de Venfa027c22007-02-12 00:55:33 -08006019static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006020 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 .open = md_seq_open,
6022 .read = seq_read,
6023 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006024 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006025 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026};
6027
NeilBrown2604b702006-01-06 00:20:36 -08006028int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006031 list_add_tail(&p->list, &pers_list);
6032 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 spin_unlock(&pers_lock);
6034 return 0;
6035}
6036
NeilBrown2604b702006-01-06 00:20:36 -08006037int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038{
NeilBrown2604b702006-01-06 00:20:36 -08006039 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006041 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 spin_unlock(&pers_lock);
6043 return 0;
6044}
6045
NeilBrowneea1bf32009-03-31 14:27:02 +11006046static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047{
6048 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006050 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006053 rcu_read_lock();
6054 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006056 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6057 (int)part_stat_read(&disk->part0, sectors[1]) -
6058 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006059 /* sync IO will cause sync_io to increase before the disk_stats
6060 * as sync_io is counted when a request starts, and
6061 * disk_stats is counted when it completes.
6062 * So resync activity will cause curr_events to be smaller than
6063 * when there was no such activity.
6064 * non-sync IO will cause disk_stat to increase without
6065 * increasing sync_io so curr_events will (eventually)
6066 * be larger than it was before. Once it becomes
6067 * substantially larger, the test below will cause
6068 * the array to appear non-idle, and resync will slow
6069 * down.
6070 * If there is a lot of outstanding resync activity when
6071 * we set last_event to curr_events, then all that activity
6072 * completing might cause the array to appear non-idle
6073 * and resync will be slowed down even though there might
6074 * not have been non-resync activity. This will only
6075 * happen once though. 'last_events' will soon reflect
6076 * the state where there is little or no outstanding
6077 * resync requests, and further resync activity will
6078 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006079 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006081 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 rdev->last_events = curr_events;
6083 idle = 0;
6084 }
6085 }
NeilBrown4b809912008-07-21 17:05:25 +10006086 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 return idle;
6088}
6089
6090void md_done_sync(mddev_t *mddev, int blocks, int ok)
6091{
6092 /* another "blocks" (512byte) blocks have been synced */
6093 atomic_sub(blocks, &mddev->recovery_active);
6094 wake_up(&mddev->recovery_wait);
6095 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006096 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 md_wakeup_thread(mddev->thread);
6098 // stop recovery, signal do_sync ....
6099 }
6100}
6101
6102
NeilBrown06d91a52005-06-21 17:17:12 -07006103/* md_write_start(mddev, bi)
6104 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006105 * in superblock) before writing, schedule a superblock update
6106 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006107 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006108void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109{
Neil Brown0fd62b82008-06-28 08:31:36 +10006110 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006111 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006112 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006113
NeilBrownf91de922005-11-08 21:39:36 -08006114 BUG_ON(mddev->ro == 1);
6115 if (mddev->ro == 2) {
6116 /* need to switch to read/write */
6117 mddev->ro = 0;
6118 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6119 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006120 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006121 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006122 }
NeilBrown06d91a52005-06-21 17:17:12 -07006123 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006124 if (mddev->safemode == 1)
6125 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006126 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006127 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006128 if (mddev->in_sync) {
6129 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006130 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006131 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006132 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006133 }
NeilBrowna9701a32005-11-08 21:39:34 -08006134 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006135 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006136 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006137 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006138 wait_event(mddev->sb_wait,
6139 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6140 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141}
6142
6143void md_write_end(mddev_t *mddev)
6144{
6145 if (atomic_dec_and_test(&mddev->writes_pending)) {
6146 if (mddev->safemode == 2)
6147 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006148 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6150 }
6151}
6152
NeilBrown2a2275d2007-01-26 00:57:11 -08006153/* md_allow_write(mddev)
6154 * Calling this ensures that the array is marked 'active' so that writes
6155 * may proceed without blocking. It is important to call this before
6156 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6157 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006158 *
6159 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6160 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006161 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006162int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006163{
6164 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006165 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006166 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006167 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006168 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006169 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006170
6171 spin_lock_irq(&mddev->write_lock);
6172 if (mddev->in_sync) {
6173 mddev->in_sync = 0;
6174 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6175 if (mddev->safemode_delay &&
6176 mddev->safemode == 0)
6177 mddev->safemode = 1;
6178 spin_unlock_irq(&mddev->write_lock);
6179 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006180 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006181 } else
6182 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006183
6184 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6185 return -EAGAIN;
6186 else
6187 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006188}
6189EXPORT_SYMBOL_GPL(md_allow_write);
6190
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191#define SYNC_MARKS 10
6192#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006193void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194{
6195 mddev_t *mddev2;
6196 unsigned int currspeed = 0,
6197 window;
NeilBrown57afd892005-06-21 17:17:13 -07006198 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 unsigned long mark[SYNC_MARKS];
6200 sector_t mark_cnt[SYNC_MARKS];
6201 int last_mark,m;
6202 struct list_head *tmp;
6203 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006204 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006205 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006206 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
6208 /* just incase thread restarts... */
6209 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6210 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006211 if (mddev->ro) /* never try to sync a read-only array */
6212 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
NeilBrown61df9d92006-10-03 01:15:57 -07006214 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6215 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6216 desc = "data-check";
6217 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6218 desc = "requested-resync";
6219 else
6220 desc = "resync";
6221 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6222 desc = "reshape";
6223 else
6224 desc = "recovery";
6225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 /* we overload curr_resync somewhat here.
6227 * 0 == not engaged in resync at all
6228 * 2 == checking that there is no conflict with another sync
6229 * 1 == like 2, but have yielded to allow conflicting resync to
6230 * commense
6231 * other == active in resync - this many blocks
6232 *
6233 * Before starting a resync we must have set curr_resync to
6234 * 2, and then checked that every "conflicting" array has curr_resync
6235 * less than ours. When we find one that is the same or higher
6236 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6237 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6238 * This will mean we have to start checking from the beginning again.
6239 *
6240 */
6241
6242 do {
6243 mddev->curr_resync = 2;
6244
6245 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006246 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006247 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 goto skip;
6249 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006250 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 if (mddev2 == mddev)
6252 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006253 if (!mddev->parallel_resync
6254 && mddev2->curr_resync
6255 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 DEFINE_WAIT(wq);
6257 if (mddev < mddev2 && mddev->curr_resync == 2) {
6258 /* arbitrarily yield */
6259 mddev->curr_resync = 1;
6260 wake_up(&resync_wait);
6261 }
6262 if (mddev > mddev2 && mddev->curr_resync == 1)
6263 /* no need to wait here, we can wait the next
6264 * time 'round when curr_resync == 2
6265 */
6266 continue;
NeilBrown97441972008-09-19 11:49:54 +10006267 /* We need to wait 'interruptible' so as not to
6268 * contribute to the load average, and not to
6269 * be caught by 'softlockup'
6270 */
6271 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006272 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006273 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006274 printk(KERN_INFO "md: delaying %s of %s"
6275 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006277 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006279 if (signal_pending(current))
6280 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 schedule();
6282 finish_wait(&resync_wait, &wq);
6283 goto try_again;
6284 }
6285 finish_wait(&resync_wait, &wq);
6286 }
6287 }
6288 } while (mddev->curr_resync < 2);
6289
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006290 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006291 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006293 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 */
6295 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006296 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006297 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006298 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6299 j = mddev->resync_min;
6300 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006301 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006302
NeilBrownccfcc3c2006-03-27 01:18:09 -08006303 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006304 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006305 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006307 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006308 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006309 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006310 if (rdev->raid_disk >= 0 &&
6311 !test_bit(Faulty, &rdev->flags) &&
6312 !test_bit(In_sync, &rdev->flags) &&
6313 rdev->recovery_offset < j)
6314 j = rdev->recovery_offset;
6315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
NeilBrown61df9d92006-10-03 01:15:57 -07006317 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6318 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6319 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006320 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006321 "(but not more than %d KB/sec) for %s.\n",
6322 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323
NeilBrowneea1bf32009-03-31 14:27:02 +11006324 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006325
NeilBrown57afd892005-06-21 17:17:13 -07006326 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 for (m = 0; m < SYNC_MARKS; m++) {
6328 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006329 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 }
6331 last_mark = 0;
6332 mddev->resync_mark = mark[last_mark];
6333 mddev->resync_mark_cnt = mark_cnt[last_mark];
6334
6335 /*
6336 * Tune reconstruction:
6337 */
6338 window = 32*(PAGE_SIZE/512);
6339 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6340 window/2,(unsigned long long) max_sectors/2);
6341
6342 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 last_check = 0;
6344
6345 if (j>2) {
6346 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006347 "md: resuming %s of %s from checkpoint.\n",
6348 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 mddev->curr_resync = j;
6350 }
6351
6352 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006353 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
NeilBrown57afd892005-06-21 17:17:13 -07006355 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006356
NeilBrown7a91ee12009-05-26 12:57:21 +10006357 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6358 ((mddev->curr_resync > mddev->curr_resync_completed &&
6359 (mddev->curr_resync - mddev->curr_resync_completed)
6360 > (max_sectors >> 4)) ||
6361 (j - mddev->curr_resync_completed)*2
6362 >= mddev->resync_max - mddev->curr_resync_completed
6363 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006364 /* time to update curr_resync_completed */
6365 blk_unplug(mddev->queue);
6366 wait_event(mddev->recovery_wait,
6367 atomic_read(&mddev->recovery_active) == 0);
6368 mddev->curr_resync_completed =
6369 mddev->curr_resync;
6370 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006371 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006372 }
NeilBrownacb180b2009-04-14 16:28:34 +10006373
NeilBrowne62e58a2009-07-01 13:15:35 +10006374 while (j >= mddev->resync_max && !kthread_should_stop()) {
6375 /* As this condition is controlled by user-space,
6376 * we can block indefinitely, so use '_interruptible'
6377 * to avoid triggering warnings.
6378 */
6379 flush_signals(current); /* just in case */
6380 wait_event_interruptible(mddev->recovery_wait,
6381 mddev->resync_max > j
6382 || kthread_should_stop());
6383 }
NeilBrownacb180b2009-04-14 16:28:34 +10006384
6385 if (kthread_should_stop())
6386 goto interrupted;
6387
NeilBrown57afd892005-06-21 17:17:13 -07006388 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006389 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006390 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006391 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 goto out;
6393 }
NeilBrown57afd892005-06-21 17:17:13 -07006394
6395 if (!skipped) { /* actual IO requested */
6396 io_sectors += sectors;
6397 atomic_add(sectors, &mddev->recovery_active);
6398 }
6399
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 j += sectors;
6401 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006402 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006403 if (last_check == 0)
6404 /* this is the earliers that rebuilt will be
6405 * visible in /proc/mdstat
6406 */
6407 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006408
6409 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 continue;
6411
NeilBrown57afd892005-06-21 17:17:13 -07006412 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
NeilBrowndfc70642008-05-23 13:04:39 -07006414 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 break;
6416
6417 repeat:
6418 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6419 /* step marks */
6420 int next = (last_mark+1) % SYNC_MARKS;
6421
6422 mddev->resync_mark = mark[next];
6423 mddev->resync_mark_cnt = mark_cnt[next];
6424 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006425 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 last_mark = next;
6427 }
6428
6429
NeilBrownc6207272008-02-06 01:39:52 -08006430 if (kthread_should_stop())
6431 goto interrupted;
6432
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433
6434 /*
6435 * this loop exits only if either when we are slower than
6436 * the 'hard' speed limit, or the system was IO-idle for
6437 * a jiffy.
6438 * the system might be non-idle CPU-wise, but we only care
6439 * about not overloading the IO subsystem. (things like an
6440 * e2fsck being done on the RAID array should execute fast)
6441 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006442 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 cond_resched();
6444
NeilBrown57afd892005-06-21 17:17:13 -07006445 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6446 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
NeilBrown88202a02006-01-06 00:21:36 -08006448 if (currspeed > speed_min(mddev)) {
6449 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006450 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006451 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 goto repeat;
6453 }
6454 }
6455 }
NeilBrown61df9d92006-10-03 01:15:57 -07006456 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 /*
6458 * this also signals 'finished resyncing' to md_stop
6459 */
6460 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006461 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462
6463 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6464
6465 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006466 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
NeilBrowndfc70642008-05-23 13:04:39 -07006468 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006469 mddev->curr_resync > 2) {
6470 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6471 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6472 if (mddev->curr_resync >= mddev->recovery_cp) {
6473 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006474 "md: checkpointing %s of %s.\n",
6475 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006476 mddev->recovery_cp = mddev->curr_resync;
6477 }
6478 } else
6479 mddev->recovery_cp = MaxSector;
6480 } else {
6481 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6482 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006483 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006484 if (rdev->raid_disk >= 0 &&
6485 !test_bit(Faulty, &rdev->flags) &&
6486 !test_bit(In_sync, &rdev->flags) &&
6487 rdev->recovery_offset < mddev->curr_resync)
6488 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 }
NeilBrown17571282006-12-10 02:20:52 -08006491 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 skip:
6494 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006495 mddev->curr_resync_completed = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006496 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006497 mddev->resync_max = MaxSector;
6498 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 wake_up(&resync_wait);
6500 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6501 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006502 return;
6503
6504 interrupted:
6505 /*
6506 * got a signal, exit.
6507 */
6508 printk(KERN_INFO
6509 "md: md_do_sync() got signal ... exiting\n");
6510 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6511 goto out;
6512
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513}
NeilBrown29269552006-03-27 01:18:10 -08006514EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515
6516
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006517static int remove_and_add_spares(mddev_t *mddev)
6518{
6519 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006520 int spares = 0;
6521
NeilBrown97e4f422009-03-31 14:33:13 +11006522 mddev->curr_resync_completed = 0;
6523
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006524 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006525 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006526 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006527 (test_bit(Faulty, &rdev->flags) ||
6528 ! test_bit(In_sync, &rdev->flags)) &&
6529 atomic_read(&rdev->nr_pending)==0) {
6530 if (mddev->pers->hot_remove_disk(
6531 mddev, rdev->raid_disk)==0) {
6532 char nm[20];
6533 sprintf(nm,"rd%d", rdev->raid_disk);
6534 sysfs_remove_link(&mddev->kobj, nm);
6535 rdev->raid_disk = -1;
6536 }
6537 }
6538
NeilBrown4044ba52009-01-09 08:31:11 +11006539 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006540 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006541 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006542 !test_bit(In_sync, &rdev->flags) &&
6543 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006544 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006545 if (rdev->raid_disk < 0
6546 && !test_bit(Faulty, &rdev->flags)) {
6547 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006548 if (mddev->pers->
6549 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006550 char nm[20];
6551 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006552 if (sysfs_create_link(&mddev->kobj,
6553 &rdev->kobj, nm))
6554 printk(KERN_WARNING
6555 "md: cannot register "
6556 "%s for %s\n",
6557 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006558 spares++;
6559 md_new_event(mddev);
6560 } else
6561 break;
6562 }
NeilBrowndfc70642008-05-23 13:04:39 -07006563 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006564 }
6565 return spares;
6566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567/*
6568 * This routine is regularly called by all per-raid-array threads to
6569 * deal with generic issues like resync and super-block update.
6570 * Raid personalities that don't have a thread (linear/raid0) do not
6571 * need this as they never do any recovery or update the superblock.
6572 *
6573 * It does not do any resync itself, but rather "forks" off other threads
6574 * to do that as needed.
6575 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6576 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006577 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 * and wakeups up this thread which will reap the thread and finish up.
6579 * This thread also removes any faulty devices (with nr_pending == 0).
6580 *
6581 * The overall approach is:
6582 * 1/ if the superblock needs updating, update it.
6583 * 2/ If a recovery thread is running, don't do anything else.
6584 * 3/ If recovery has finished, clean up, possibly marking spares active.
6585 * 4/ If there are any faulty devices, remove them.
6586 * 5/ If array is degraded, try to add spares devices
6587 * 6/ If array has spares or is not in-sync, start a resync thread.
6588 */
6589void md_check_recovery(mddev_t *mddev)
6590{
6591 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
6593
NeilBrown5f404022005-06-21 17:17:16 -07006594 if (mddev->bitmap)
6595 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596
6597 if (mddev->ro)
6598 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006599
6600 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006601 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006602 printk(KERN_INFO "md: %s in immediate safe mode\n",
6603 mdname(mddev));
6604 mddev->safemode = 2;
6605 }
6606 flush_signals(current);
6607 }
6608
NeilBrownc89a8ee2008-08-05 15:54:13 +10006609 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006612 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006614 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006615 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006616 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6617 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 ))
6619 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006620
NeilBrowndf5b89b2006-03-27 01:18:20 -08006621 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006622 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006623
NeilBrownc89a8ee2008-08-05 15:54:13 +10006624 if (mddev->ro) {
6625 /* Only thing we do on a ro array is remove
6626 * failed devices.
6627 */
6628 remove_and_add_spares(mddev);
6629 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6630 goto unlock;
6631 }
6632
NeilBrown31a59e32008-04-30 00:52:30 -07006633 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006634 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006635 spin_lock_irq(&mddev->write_lock);
6636 if (mddev->safemode &&
6637 !atomic_read(&mddev->writes_pending) &&
6638 !mddev->in_sync &&
6639 mddev->recovery_cp == MaxSector) {
6640 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006641 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006642 if (mddev->persistent)
6643 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6644 }
6645 if (mddev->safemode == 1)
6646 mddev->safemode = 0;
6647 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006648 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006649 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006650 }
NeilBrownfca4d842005-06-21 17:17:11 -07006651
NeilBrown850b2b42006-10-03 01:15:46 -07006652 if (mddev->flags)
6653 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006654
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006655 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006656 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006657 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006658
NeilBrown06d91a52005-06-21 17:17:12 -07006659
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6661 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6662 /* resync/recovery still happening */
6663 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6664 goto unlock;
6665 }
6666 if (mddev->sync_thread) {
6667 /* resync has finished, collect result */
6668 md_unregister_thread(mddev->sync_thread);
6669 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006670 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6671 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 /* success...*/
6673 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006674 if (mddev->pers->spare_active(mddev))
6675 sysfs_notify(&mddev->kobj, NULL,
6676 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 }
NeilBrowncea9c222009-03-31 15:15:05 +11006678 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6679 mddev->pers->finish_reshape)
6680 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006681 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006682
6683 /* if array is no-longer degraded, then any saved_raid_disk
6684 * information must be scrapped
6685 */
6686 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006687 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006688 rdev->saved_raid_disk = -1;
6689
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 mddev->recovery = 0;
6691 /* flag recovery needed just to double check */
6692 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006693 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006694 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 goto unlock;
6696 }
Neil Brown72a23c22008-06-28 08:31:41 +10006697 /* Set RUNNING before clearing NEEDED to avoid
6698 * any transients in the value of "sync_action".
6699 */
6700 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6701 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006702 /* Clear some bits that don't mean anything, but
6703 * might be left set
6704 */
NeilBrown24dd4692005-11-08 21:39:26 -08006705 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6706 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006708 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6709 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 /* no recovery is running.
6711 * remove any failed drives, then
6712 * add spares if possible.
6713 * Spare are also removed and re-added, to allow
6714 * the personality to fail the re-add.
6715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006717 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006718 if (mddev->pers->check_reshape == NULL ||
6719 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006720 /* Cannot proceed */
6721 goto unlock;
6722 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006723 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006724 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006725 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6726 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006727 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006728 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006729 } else if (mddev->recovery_cp < MaxSector) {
6730 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006731 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006732 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6733 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006735
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006737 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6738 /* We are adding a device or devices to an array
6739 * which has the bitmap stored on all devices.
6740 * So make sure all bitmap pages get written
6741 */
6742 bitmap_write_all(mddev->bitmap);
6743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 mddev->sync_thread = md_register_thread(md_do_sync,
6745 mddev,
6746 "%s_resync");
6747 if (!mddev->sync_thread) {
6748 printk(KERN_ERR "%s: could not start resync"
6749 " thread...\n",
6750 mdname(mddev));
6751 /* leave the spares where they are, it shouldn't hurt */
6752 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006753 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006755 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006756 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 }
6758 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006759 if (!mddev->sync_thread) {
6760 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6761 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6762 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006763 if (mddev->sysfs_action)
6764 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766 mddev_unlock(mddev);
6767 }
6768}
6769
Dan Williams6bfe0b42008-04-30 00:52:32 -07006770void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6771{
NeilBrown3c0ee632008-10-21 13:25:28 +11006772 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006773 wait_event_timeout(rdev->blocked_wait,
6774 !test_bit(Blocked, &rdev->flags),
6775 msecs_to_jiffies(5000));
6776 rdev_dec_pending(rdev, mddev);
6777}
6778EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6779
Adrian Bunk75c96f82005-05-05 16:16:09 -07006780static int md_notify_reboot(struct notifier_block *this,
6781 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782{
6783 struct list_head *tmp;
6784 mddev_t *mddev;
6785
6786 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6787
6788 printk(KERN_INFO "md: stopping all md devices.\n");
6789
NeilBrown29ac4aa2008-02-06 01:39:58 -08006790 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006791 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006792 /* Force a switch to readonly even array
6793 * appears to still be in use. Hence
6794 * the '100'.
6795 */
NeilBrownd710e132008-10-13 11:55:12 +11006796 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006797 mddev_unlock(mddev);
6798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799 /*
6800 * certain more exotic SCSI devices are known to be
6801 * volatile wrt too early system reboots. While the
6802 * right place to handle this issue is the given
6803 * driver, we do want to have a safe RAID driver ...
6804 */
6805 mdelay(1000*1);
6806 }
6807 return NOTIFY_DONE;
6808}
6809
Adrian Bunk75c96f82005-05-05 16:16:09 -07006810static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 .notifier_call = md_notify_reboot,
6812 .next = NULL,
6813 .priority = INT_MAX, /* before any real devices */
6814};
6815
6816static void md_geninit(void)
6817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6819
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006820 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821}
6822
Adrian Bunk75c96f82005-05-05 16:16:09 -07006823static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006825 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 return -1;
6827 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006828 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 return -1;
6830 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006831 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006832 md_probe, NULL, NULL);
6833 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 md_probe, NULL, NULL);
6835
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006837 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841}
6842
6843
6844#ifndef MODULE
6845
6846/*
6847 * Searches all registered partitions for autorun RAID arrays
6848 * at boot time.
6849 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006850
6851static LIST_HEAD(all_detected_devices);
6852struct detected_devices_node {
6853 struct list_head list;
6854 dev_t dev;
6855};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
6857void md_autodetect_dev(dev_t dev)
6858{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006859 struct detected_devices_node *node_detected_dev;
6860
6861 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6862 if (node_detected_dev) {
6863 node_detected_dev->dev = dev;
6864 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6865 } else {
6866 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6867 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869}
6870
6871
6872static void autostart_arrays(int part)
6873{
6874 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006875 struct detected_devices_node *node_detected_dev;
6876 dev_t dev;
6877 int i_scanned, i_passed;
6878
6879 i_scanned = 0;
6880 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
6882 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6883
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006884 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6885 i_scanned++;
6886 node_detected_dev = list_entry(all_detected_devices.next,
6887 struct detected_devices_node, list);
6888 list_del(&node_detected_dev->list);
6889 dev = node_detected_dev->dev;
6890 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006891 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 if (IS_ERR(rdev))
6893 continue;
6894
NeilBrownb2d444d2005-11-08 21:39:31 -08006895 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 MD_BUG();
6897 continue;
6898 }
NeilBrownd0fae182008-03-04 14:29:31 -08006899 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006901 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006903
6904 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6905 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
6907 autorun_devices(part);
6908}
6909
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006910#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
6912static __exit void md_exit(void)
6913{
6914 mddev_t *mddev;
6915 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006916
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006917 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006918 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006920 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 unregister_blkdev(mdp_major, "mdp");
6922 unregister_reboot_notifier(&md_notifier);
6923 unregister_sysctl_table(raid_table_header);
6924 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006925 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006927 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 }
6929}
6930
Dan Williams685784a2007-07-09 11:56:42 -07006931subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932module_exit(md_exit)
6933
NeilBrownf91de922005-11-08 21:39:36 -08006934static int get_ro(char *buffer, struct kernel_param *kp)
6935{
6936 return sprintf(buffer, "%d", start_readonly);
6937}
6938static int set_ro(const char *val, struct kernel_param *kp)
6939{
6940 char *e;
6941 int num = simple_strtoul(val, &e, 10);
6942 if (*val && (*e == '\0' || *e == '\n')) {
6943 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006944 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006945 }
6946 return -EINVAL;
6947}
6948
NeilBrown80ca3a42006-07-10 04:44:18 -07006949module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6950module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006951
NeilBrownefeb53c2009-01-09 08:31:10 +11006952module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006953
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954EXPORT_SYMBOL(register_md_personality);
6955EXPORT_SYMBOL(unregister_md_personality);
6956EXPORT_SYMBOL(md_error);
6957EXPORT_SYMBOL(md_done_sync);
6958EXPORT_SYMBOL(md_write_start);
6959EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960EXPORT_SYMBOL(md_register_thread);
6961EXPORT_SYMBOL(md_unregister_thread);
6962EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963EXPORT_SYMBOL(md_check_recovery);
6964MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006965MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006966MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);