blob: c50931352b238d0e3f16ce0479eec6fa884bc92b [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"
NeilBrown32a76272005-06-21 17:17:14 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#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
NeilBrownd3374822009-01-09 08:31:10 +1100272static void mddev_delayed_delete(struct work_struct *ws)
273{
274 mddev_t *mddev = container_of(ws, mddev_t, del_work);
275 kobject_del(&mddev->kobj);
276 kobject_put(&mddev->kobj);
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static void mddev_put(mddev_t *mddev)
280{
281 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
282 return;
NeilBrownd3374822009-01-09 08:31:10 +1100283 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
284 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100286 if (mddev->gendisk) {
287 /* we did a probe so need to clean up.
288 * Call schedule_work inside the spinlock
289 * so that flush_scheduled_work() after
290 * mddev_find will succeed in waiting for the
291 * work to be done.
292 */
293 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
294 schedule_work(&mddev->del_work);
295 } else
296 kfree(mddev);
297 }
298 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static mddev_t * mddev_find(dev_t unit)
302{
303 mddev_t *mddev, *new = NULL;
304
305 retry:
306 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrownefeb53c2009-01-09 08:31:10 +1100308 if (unit) {
309 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
310 if (mddev->unit == unit) {
311 mddev_get(mddev);
312 spin_unlock(&all_mddevs_lock);
313 kfree(new);
314 return mddev;
315 }
316
317 if (new) {
318 list_add(&new->all_mddevs, &all_mddevs);
319 spin_unlock(&all_mddevs_lock);
320 new->hold_active = UNTIL_IOCTL;
321 return new;
322 }
323 } else if (new) {
324 /* find an unused unit number */
325 static int next_minor = 512;
326 int start = next_minor;
327 int is_free = 0;
328 int dev = 0;
329 while (!is_free) {
330 dev = MKDEV(MD_MAJOR, next_minor);
331 next_minor++;
332 if (next_minor > MINORMASK)
333 next_minor = 0;
334 if (next_minor == start) {
335 /* Oh dear, all in use. */
336 spin_unlock(&all_mddevs_lock);
337 kfree(new);
338 return NULL;
339 }
340
341 is_free = 1;
342 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
343 if (mddev->unit == dev) {
344 is_free = 0;
345 break;
346 }
347 }
348 new->unit = dev;
349 new->md_minor = MINOR(dev);
350 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list_add(&new->all_mddevs, &all_mddevs);
352 spin_unlock(&all_mddevs_lock);
353 return new;
354 }
355 spin_unlock(&all_mddevs_lock);
356
NeilBrown9ffae0c2006-01-06 00:20:32 -0800357 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!new)
359 return NULL;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 new->unit = unit;
362 if (MAJOR(unit) == MD_MAJOR)
363 new->md_minor = MINOR(unit);
364 else
365 new->md_minor = MINOR(unit) >> MdpMinorShift;
366
NeilBrowndf5b89b2006-03-27 01:18:20 -0800367 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 INIT_LIST_HEAD(&new->disks);
369 INIT_LIST_HEAD(&new->all_mddevs);
370 init_timer(&new->safemode_timer);
371 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000372 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100373 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700374 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700375 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700376 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700377 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000378 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800379 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700380 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 goto retry;
383}
384
385static inline int mddev_lock(mddev_t * mddev)
386{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800387 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Dan Williamsb522adc2009-03-31 15:00:31 +1100390static inline int mddev_is_locked(mddev_t *mddev)
391{
392 return mutex_is_locked(&mddev->reconfig_mutex);
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static inline int mddev_trylock(mddev_t * mddev)
396{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800397 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static inline void mddev_unlock(mddev_t * mddev)
401{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800402 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
NeilBrown005eca52005-08-22 13:11:08 -0700404 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
NeilBrown2989ddb2006-01-06 00:20:43 -0800407static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100409 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100411 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (rdev->desc_nr == nr)
413 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return NULL;
416}
417
418static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
419{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 mdk_rdev_t *rdev;
421
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100422 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (rdev->bdev->bd_dev == dev)
424 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return NULL;
427}
428
NeilBrownd9d166c2006-01-06 00:20:51 -0800429static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800430{
431 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800432 list_for_each_entry(pers, &pers_list, list) {
433 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800434 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800435 if (strcmp(pers->name, clevel)==0)
436 return pers;
437 }
NeilBrown2604b702006-01-06 00:20:36 -0800438 return NULL;
439}
440
Andre Nollb73df2d2008-07-11 22:02:23 +1000441/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700442static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Andre Nollb73df2d2008-07-11 22:02:23 +1000444 sector_t num_sectors = bdev->bd_inode->i_size / 512;
445 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Andre Nolle7debaa2008-07-11 22:02:23 +1000448static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Andre Noll0f420352008-07-11 22:02:23 +1000450 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000453 num_sectors &= ~((sector_t)chunk_size/512 - 1);
454 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static int alloc_disk_sb(mdk_rdev_t * rdev)
458{
459 if (rdev->sb_page)
460 MD_BUG();
461
462 rdev->sb_page = alloc_page(GFP_KERNEL);
463 if (!rdev->sb_page) {
464 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000465 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 return 0;
469}
470
471static void free_disk_sb(mdk_rdev_t * rdev)
472{
473 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800474 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 rdev->sb_loaded = 0;
476 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000477 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100478 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480}
481
482
NeilBrown6712ecf2007-09-27 12:47:43 +0200483static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700484{
485 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800486 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700487
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700488 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
489 printk("md: super_written gets error=%d, uptodate=%d\n",
490 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
491 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800492 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700493 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700494
NeilBrowna9701a32005-11-08 21:39:34 -0800495 if (atomic_dec_and_test(&mddev->pending_writes))
496 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700497 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700498}
499
NeilBrown6712ecf2007-09-27 12:47:43 +0200500static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800501{
502 struct bio *bio2 = bio->bi_private;
503 mdk_rdev_t *rdev = bio2->bi_private;
504 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800505
506 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
507 error == -EOPNOTSUPP) {
508 unsigned long flags;
509 /* barriers don't appear to be supported :-( */
510 set_bit(BarriersNotsupp, &rdev->flags);
511 mddev->barriers_work = 0;
512 spin_lock_irqsave(&mddev->write_lock, flags);
513 bio2->bi_next = mddev->biolist;
514 mddev->biolist = bio2;
515 spin_unlock_irqrestore(&mddev->write_lock, flags);
516 wake_up(&mddev->sb_wait);
517 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200518 } else {
519 bio_put(bio2);
520 bio->bi_private = rdev;
521 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800522 }
NeilBrowna9701a32005-11-08 21:39:34 -0800523}
524
NeilBrown7bfa19f2005-06-21 17:17:28 -0700525void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
526 sector_t sector, int size, struct page *page)
527{
528 /* write first size bytes of page to sector of rdev
529 * Increment mddev->pending_writes before returning
530 * and decrement it on completion, waking up sb_wait
531 * if zero is reached.
532 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800533 *
534 * As we might need to resubmit the request if BIO_RW_BARRIER
535 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700536 */
537 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100538 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700539
540 bio->bi_bdev = rdev->bdev;
541 bio->bi_sector = sector;
542 bio_add_page(bio, page, size, 0);
543 bio->bi_private = rdev;
544 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800545 bio->bi_rw = rw;
546
NeilBrown7bfa19f2005-06-21 17:17:28 -0700547 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800548 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
549 struct bio *rbio;
550 rw |= (1<<BIO_RW_BARRIER);
551 rbio = bio_clone(bio, GFP_NOIO);
552 rbio->bi_private = bio;
553 rbio->bi_end_io = super_written_barrier;
554 submit_bio(rw, rbio);
555 } else
556 submit_bio(rw, bio);
557}
558
559void md_super_wait(mddev_t *mddev)
560{
561 /* wait for all superblock writes that were scheduled to complete.
562 * if any had to be retried (due to BARRIER problems), retry them
563 */
564 DEFINE_WAIT(wq);
565 for(;;) {
566 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
567 if (atomic_read(&mddev->pending_writes)==0)
568 break;
569 while (mddev->biolist) {
570 struct bio *bio;
571 spin_lock_irq(&mddev->write_lock);
572 bio = mddev->biolist;
573 mddev->biolist = bio->bi_next ;
574 bio->bi_next = NULL;
575 spin_unlock_irq(&mddev->write_lock);
576 submit_bio(bio->bi_rw, bio);
577 }
578 schedule();
579 }
580 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700581}
582
NeilBrown6712ecf2007-09-27 12:47:43 +0200583static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
NeilBrowna654b9d82005-06-21 17:17:27 -0700588int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct page *page, int rw)
590{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700591 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct completion event;
593 int ret;
594
Jens Axboe93dbb392009-02-16 10:25:40 +0100595 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 bio->bi_bdev = bdev;
598 bio->bi_sector = sector;
599 bio_add_page(bio, page, size, 0);
600 init_completion(&event);
601 bio->bi_private = &event;
602 bio->bi_end_io = bi_complete;
603 submit_bio(rw, bio);
604 wait_for_completion(&event);
605
606 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
607 bio_put(bio);
608 return ret;
609}
NeilBrowna8745db2006-01-06 00:20:34 -0800610EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
NeilBrown0002b272005-09-09 16:23:53 -0700612static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 char b[BDEVNAME_SIZE];
615 if (!rdev->sb_page) {
616 MD_BUG();
617 return -EINVAL;
618 }
619 if (rdev->sb_loaded)
620 return 0;
621
622
Andre Noll0f420352008-07-11 22:02:23 +1000623 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto fail;
625 rdev->sb_loaded = 1;
626 return 0;
627
628fail:
629 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
630 bdevname(rdev->bdev,b));
631 return -EINVAL;
632}
633
634static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
635{
Andre Noll05710462008-07-11 22:02:20 +1000636 return sb1->set_uuid0 == sb2->set_uuid0 &&
637 sb1->set_uuid1 == sb2->set_uuid1 &&
638 sb1->set_uuid2 == sb2->set_uuid2 &&
639 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
643{
644 int ret;
645 mdp_super_t *tmp1, *tmp2;
646
647 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
648 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
649
650 if (!tmp1 || !tmp2) {
651 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100652 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto abort;
654 }
655
656 *tmp1 = *sb1;
657 *tmp2 = *sb2;
658
659 /*
660 * nr_disks is not constant
661 */
662 tmp1->nr_disks = 0;
663 tmp2->nr_disks = 0;
664
Andre Nollce0c8e02008-07-11 22:02:20 +1000665 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700667 kfree(tmp1);
668 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return ret;
670}
671
NeilBrown4d167f02007-05-09 02:35:37 -0700672
673static u32 md_csum_fold(u32 csum)
674{
675 csum = (csum & 0xffff) + (csum >> 16);
676 return (csum & 0xffff) + (csum >> 16);
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static unsigned int calc_sb_csum(mdp_super_t * sb)
680{
NeilBrown4d167f02007-05-09 02:35:37 -0700681 u64 newcsum = 0;
682 u32 *sb32 = (u32*)sb;
683 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned int disk_csum, csum;
685
686 disk_csum = sb->sb_csum;
687 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700688
689 for (i = 0; i < MD_SB_BYTES/4 ; i++)
690 newcsum += sb32[i];
691 csum = (newcsum & 0xffffffff) + (newcsum>>32);
692
693
694#ifdef CONFIG_ALPHA
695 /* This used to use csum_partial, which was wrong for several
696 * reasons including that different results are returned on
697 * different architectures. It isn't critical that we get exactly
698 * the same return value as before (we always csum_fold before
699 * testing, and that removes any differences). However as we
700 * know that csum_partial always returned a 16bit value on
701 * alphas, do a fold to maximise conformity to previous behaviour.
702 */
703 sb->sb_csum = md_csum_fold(disk_csum);
704#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700706#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return csum;
708}
709
710
711/*
712 * Handle superblock details.
713 * We want to be able to handle multiple superblock formats
714 * so we have a common interface to them all, and an array of
715 * different handlers.
716 * We rely on user-space to write the initial superblock, and support
717 * reading and updating of superblocks.
718 * Interface methods are:
719 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
720 * loads and validates a superblock on dev.
721 * if refdev != NULL, compare superblocks on both devices
722 * Return:
723 * 0 - dev has a superblock that is compatible with refdev
724 * 1 - dev has a superblock that is compatible and newer than refdev
725 * so dev should be used as the refdev in future
726 * -EINVAL superblock incompatible or invalid
727 * -othererror e.g. -EIO
728 *
729 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
730 * Verify that dev is acceptable into mddev.
731 * The first time, mddev->raid_disks will be 0, and data from
732 * dev should be merged in. Subsequent calls check that dev
733 * is new enough. Return 0 or -EINVAL
734 *
735 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
736 * Update the superblock for rdev with data in mddev
737 * This does not write to disc.
738 *
739 */
740
741struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000742 char *name;
743 struct module *owner;
744 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
745 int minor_version);
746 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
747 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
748 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000749 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750};
751
752/*
753 * load_super for 0.90.0
754 */
755static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
756{
757 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
758 mdp_super_t *sb;
759 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /*
Andre Noll0f420352008-07-11 22:02:23 +1000762 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * it's at the end of the disk.
764 *
765 * It also happens to be a multiple of 4Kb.
766 */
Andre Noll0f420352008-07-11 22:02:23 +1000767 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
NeilBrown0002b272005-09-09 16:23:53 -0700769 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (ret) return ret;
771
772 ret = -EINVAL;
773
774 bdevname(rdev->bdev, b);
775 sb = (mdp_super_t*)page_address(rdev->sb_page);
776
777 if (sb->md_magic != MD_SB_MAGIC) {
778 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
779 b);
780 goto abort;
781 }
782
783 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800784 sb->minor_version < 90 ||
785 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
787 sb->major_version, sb->minor_version,
788 b);
789 goto abort;
790 }
791
792 if (sb->raid_disks <= 0)
793 goto abort;
794
NeilBrown4d167f02007-05-09 02:35:37 -0700795 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
797 b);
798 goto abort;
799 }
800
801 rdev->preferred_minor = sb->md_minor;
802 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700803 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
NeilBrowne11e93f2007-05-09 02:35:36 -0700805 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
806 if (sb->level != 1 && sb->level != 4
807 && sb->level != 5 && sb->level != 6
808 && sb->level != 10) {
809 /* FIXME use a better test */
810 printk(KERN_WARNING
811 "md: bitmaps not supported for this level.\n");
812 goto abort;
813 }
814 }
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (sb->level == LEVEL_MULTIPATH)
817 rdev->desc_nr = -1;
818 else
819 rdev->desc_nr = sb->this_disk.number;
820
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700821 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700823 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 __u64 ev1, ev2;
825 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
826 if (!uuid_equal(refsb, sb)) {
827 printk(KERN_WARNING "md: %s has different UUID to %s\n",
828 b, bdevname(refdev->bdev,b2));
829 goto abort;
830 }
831 if (!sb_equal(refsb, sb)) {
832 printk(KERN_WARNING "md: %s has same UUID"
833 " but different superblock to %s\n",
834 b, bdevname(refdev->bdev, b2));
835 goto abort;
836 }
837 ev1 = md_event(sb);
838 ev2 = md_event(refsb);
839 if (ev1 > ev2)
840 ret = 1;
841 else
842 ret = 0;
843 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100844 rdev->sectors = calc_num_sectors(rdev, sb->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Andre Nolldd8ac332009-03-31 14:33:13 +1100846 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800847 /* "this cannot possibly happen" ... */
848 ret = -EINVAL;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 abort:
851 return ret;
852}
853
854/*
855 * validate_super for 0.90.0
856 */
857static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
858{
859 mdp_disk_t *desc;
860 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700861 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
NeilBrown41158c72005-06-21 17:17:25 -0700863 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800864 clear_bit(Faulty, &rdev->flags);
865 clear_bit(In_sync, &rdev->flags);
866 clear_bit(WriteMostly, &rdev->flags);
867 clear_bit(BarriersNotsupp, &rdev->flags);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (mddev->raid_disks == 0) {
870 mddev->major_version = 0;
871 mddev->minor_version = sb->minor_version;
872 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800873 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 mddev->chunk_size = sb->chunk_size;
875 mddev->ctime = sb->ctime;
876 mddev->utime = sb->utime;
877 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800878 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 mddev->layout = sb->layout;
880 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100881 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700882 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700883 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700884 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
NeilBrownf6705572006-03-27 01:18:11 -0800886 if (mddev->minor_version >= 91) {
887 mddev->reshape_position = sb->reshape_position;
888 mddev->delta_disks = sb->delta_disks;
889 mddev->new_level = sb->new_level;
890 mddev->new_layout = sb->new_layout;
891 mddev->new_chunk = sb->new_chunk;
892 } else {
893 mddev->reshape_position = MaxSector;
894 mddev->delta_disks = 0;
895 mddev->new_level = mddev->level;
896 mddev->new_layout = mddev->layout;
897 mddev->new_chunk = mddev->chunk_size;
898 }
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (sb->state & (1<<MD_SB_CLEAN))
901 mddev->recovery_cp = MaxSector;
902 else {
903 if (sb->events_hi == sb->cp_events_hi &&
904 sb->events_lo == sb->cp_events_lo) {
905 mddev->recovery_cp = sb->recovery_cp;
906 } else
907 mddev->recovery_cp = 0;
908 }
909
910 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
911 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
912 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
913 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
914
915 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700916
917 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700918 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700919 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700920
NeilBrown41158c72005-06-21 17:17:25 -0700921 } else if (mddev->pers == NULL) {
922 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 ++ev1;
924 if (ev1 < mddev->events)
925 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700926 } else if (mddev->bitmap) {
927 /* if adding to array with a bitmap, then we can accept an
928 * older device ... but not too old.
929 */
NeilBrown41158c72005-06-21 17:17:25 -0700930 if (ev1 < mddev->bitmap->events_cleared)
931 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700932 } else {
933 if (ev1 < mddev->events)
934 /* just a hot-add of a new device, leave raid_disk at -1 */
935 return 0;
936 }
NeilBrown41158c72005-06-21 17:17:25 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 desc = sb->disks + rdev->desc_nr;
940
941 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800942 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700943 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
944 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800945 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 rdev->raid_disk = desc->raid_disk;
947 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700948 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
949 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700950 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800951 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
955/*
956 * sync_super for 0.90.0
957 */
958static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
959{
960 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 mdk_rdev_t *rdev2;
962 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* make rdev->sb match mddev data..
966 *
967 * 1/ zero out disks
968 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
969 * 3/ any empty disks < next_spare become removed
970 *
971 * disks[0] gets initialised to REMOVED because
972 * we cannot be sure from other fields if it has
973 * been initialised or not.
974 */
975 int i;
976 int active=0, working=0,failed=0,spare=0,nr_disks=0;
977
NeilBrown61181562005-09-09 16:24:02 -0700978 rdev->sb_size = MD_SB_BYTES;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sb = (mdp_super_t*)page_address(rdev->sb_page);
981
982 memset(sb, 0, sizeof(*sb));
983
984 sb->md_magic = MD_SB_MAGIC;
985 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 sb->patch_version = mddev->patch_version;
987 sb->gvalid_words = 0; /* ignored */
988 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
989 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
990 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
991 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
992
993 sb->ctime = mddev->ctime;
994 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +1100995 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 sb->raid_disks = mddev->raid_disks;
997 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800998 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 sb->utime = mddev->utime;
1000 sb->state = 0;
1001 sb->events_hi = (mddev->events>>32);
1002 sb->events_lo = (u32)mddev->events;
1003
NeilBrownf6705572006-03-27 01:18:11 -08001004 if (mddev->reshape_position == MaxSector)
1005 sb->minor_version = 90;
1006 else {
1007 sb->minor_version = 91;
1008 sb->reshape_position = mddev->reshape_position;
1009 sb->new_level = mddev->new_level;
1010 sb->delta_disks = mddev->delta_disks;
1011 sb->new_layout = mddev->new_layout;
1012 sb->new_chunk = mddev->new_chunk;
1013 }
1014 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (mddev->in_sync)
1016 {
1017 sb->recovery_cp = mddev->recovery_cp;
1018 sb->cp_events_hi = (mddev->events>>32);
1019 sb->cp_events_lo = (u32)mddev->events;
1020 if (mddev->recovery_cp == MaxSector)
1021 sb->state = (1<< MD_SB_CLEAN);
1022 } else
1023 sb->recovery_cp = 0;
1024
1025 sb->layout = mddev->layout;
1026 sb->chunk_size = mddev->chunk_size;
1027
NeilBrowna654b9d82005-06-21 17:17:27 -07001028 if (mddev->bitmap && mddev->bitmap_file == NULL)
1029 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001032 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001034 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001035 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1036 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -08001037 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001039 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001040 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 d = &sb->disks[rdev2->desc_nr];
1042 nr_disks++;
1043 d->number = rdev2->desc_nr;
1044 d->major = MAJOR(rdev2->bdev->bd_dev);
1045 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001046 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1047 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 d->raid_disk = rdev2->raid_disk;
1049 else
1050 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001051 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -08001053 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 d->state = (1<<MD_DISK_ACTIVE);
1055 d->state |= (1<<MD_DISK_SYNC);
1056 active++;
1057 working++;
1058 } else {
1059 d->state = 0;
1060 spare++;
1061 working++;
1062 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001063 if (test_bit(WriteMostly, &rdev2->flags))
1064 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* now set the "removed" and "faulty" bits on any missing devices */
1067 for (i=0 ; i < mddev->raid_disks ; i++) {
1068 mdp_disk_t *d = &sb->disks[i];
1069 if (d->state == 0 && d->number == 0) {
1070 d->number = i;
1071 d->raid_disk = i;
1072 d->state = (1<<MD_DISK_REMOVED);
1073 d->state |= (1<<MD_DISK_FAULTY);
1074 failed++;
1075 }
1076 }
1077 sb->nr_disks = nr_disks;
1078 sb->active_disks = active;
1079 sb->working_disks = working;
1080 sb->failed_disks = failed;
1081 sb->spare_disks = spare;
1082
1083 sb->this_disk = sb->disks[rdev->desc_nr];
1084 sb->sb_csum = calc_sb_csum(sb);
1085}
1086
1087/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001088 * rdev_size_change for 0.90.0
1089 */
1090static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001091super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001092{
Andre Noll58c0fed2009-03-31 14:33:13 +11001093 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001094 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001095 if (rdev->mddev->bitmap_offset)
1096 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001097 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001098 if (!num_sectors || num_sectors > rdev->sb_start)
1099 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001100 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001101 rdev->sb_page);
1102 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001103 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001104}
1105
1106
1107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * version 1 superblock
1109 */
1110
NeilBrown1c05b4b2006-10-21 10:24:08 -07001111static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001113 __le32 disk_csum;
1114 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 unsigned long long newcsum;
1116 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001117 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int i;
1119
1120 disk_csum = sb->sb_csum;
1121 sb->sb_csum = 0;
1122 newcsum = 0;
1123 for (i=0; size>=4; size -= 4 )
1124 newcsum += le32_to_cpu(*isuper++);
1125
1126 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001127 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1130 sb->sb_csum = disk_csum;
1131 return cpu_to_le32(csum);
1132}
1133
1134static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1135{
1136 struct mdp_superblock_1 *sb;
1137 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001138 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001140 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /*
Andre Noll0f420352008-07-11 22:02:23 +10001143 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * It is always aligned to a 4K boundary and
1145 * depeding on minor_version, it can be:
1146 * 0: At least 8K, but less than 12K, from end of device
1147 * 1: At start of device
1148 * 2: 4K from start of device.
1149 */
1150 switch(minor_version) {
1151 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001152 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1153 sb_start -= 8*2;
1154 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001157 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001160 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162 default:
1163 return -EINVAL;
1164 }
Andre Noll0f420352008-07-11 22:02:23 +10001165 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
NeilBrown0002b272005-09-09 16:23:53 -07001167 /* superblock is rarely larger than 1K, but it can be larger,
1168 * and it is safe to read 4k, so we do that
1169 */
1170 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (ret) return ret;
1172
1173
1174 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1175
1176 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1177 sb->major_version != cpu_to_le32(1) ||
1178 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001179 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001180 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return -EINVAL;
1182
1183 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1184 printk("md: invalid superblock checksum on %s\n",
1185 bdevname(rdev->bdev,b));
1186 return -EINVAL;
1187 }
1188 if (le64_to_cpu(sb->data_size) < 10) {
1189 printk("md: data_size too small on %s\n",
1190 bdevname(rdev->bdev,b));
1191 return -EINVAL;
1192 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001193 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1194 if (sb->level != cpu_to_le32(1) &&
1195 sb->level != cpu_to_le32(4) &&
1196 sb->level != cpu_to_le32(5) &&
1197 sb->level != cpu_to_le32(6) &&
1198 sb->level != cpu_to_le32(10)) {
1199 printk(KERN_WARNING
1200 "md: bitmaps not supported for this level.\n");
1201 return -EINVAL;
1202 }
1203 }
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 rdev->preferred_minor = 0xffff;
1206 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001207 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
NeilBrown0002b272005-09-09 16:23:53 -07001209 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001210 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001211 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001212 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1213
1214 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001215 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001216 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001217
NeilBrown31b65a02006-07-10 04:44:14 -07001218 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1219 rdev->desc_nr = -1;
1220 else
1221 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1222
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001223 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001224 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001225 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 __u64 ev1, ev2;
1227 struct mdp_superblock_1 *refsb =
1228 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1229
1230 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1231 sb->level != refsb->level ||
1232 sb->layout != refsb->layout ||
1233 sb->chunksize != refsb->chunksize) {
1234 printk(KERN_WARNING "md: %s has strangely different"
1235 " superblock to %s\n",
1236 bdevname(rdev->bdev,b),
1237 bdevname(refdev->bdev,b2));
1238 return -EINVAL;
1239 }
1240 ev1 = le64_to_cpu(sb->events);
1241 ev2 = le64_to_cpu(refsb->events);
1242
1243 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001244 ret = 1;
1245 else
1246 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
NeilBrowna1801f82008-03-04 14:29:31 -08001248 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001249 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1250 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001252 rdev->sectors = rdev->sb_start;
1253 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001255 rdev->sectors = le64_to_cpu(sb->data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (le32_to_cpu(sb->chunksize))
Andre Nolldd8ac332009-03-31 14:33:13 +11001257 rdev->sectors &= ~((sector_t)le32_to_cpu(sb->chunksize) - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001258
Andre Nolldd8ac332009-03-31 14:33:13 +11001259 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001260 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001261 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1265{
1266 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001267 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
NeilBrown41158c72005-06-21 17:17:25 -07001269 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001270 clear_bit(Faulty, &rdev->flags);
1271 clear_bit(In_sync, &rdev->flags);
1272 clear_bit(WriteMostly, &rdev->flags);
1273 clear_bit(BarriersNotsupp, &rdev->flags);
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (mddev->raid_disks == 0) {
1276 mddev->major_version = 1;
1277 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001278 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1280 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1281 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1282 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001283 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 mddev->layout = le32_to_cpu(sb->layout);
1285 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001286 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001287 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001288 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001289 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1292 memcpy(mddev->uuid, sb->set_uuid, 16);
1293
1294 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001295
NeilBrown71c08052005-09-09 16:23:51 -07001296 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001297 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001298 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001299
NeilBrownf6705572006-03-27 01:18:11 -08001300 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1301 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1302 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1303 mddev->new_level = le32_to_cpu(sb->new_level);
1304 mddev->new_layout = le32_to_cpu(sb->new_layout);
1305 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1306 } else {
1307 mddev->reshape_position = MaxSector;
1308 mddev->delta_disks = 0;
1309 mddev->new_level = mddev->level;
1310 mddev->new_layout = mddev->layout;
1311 mddev->new_chunk = mddev->chunk_size;
1312 }
1313
NeilBrown41158c72005-06-21 17:17:25 -07001314 } else if (mddev->pers == NULL) {
1315 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 ++ev1;
1317 if (ev1 < mddev->events)
1318 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001319 } else if (mddev->bitmap) {
1320 /* If adding to array with a bitmap, then we can accept an
1321 * older device, but not too old.
1322 */
NeilBrown41158c72005-06-21 17:17:25 -07001323 if (ev1 < mddev->bitmap->events_cleared)
1324 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001325 } else {
1326 if (ev1 < mddev->events)
1327 /* just a hot-add of a new device, leave raid_disk at -1 */
1328 return 0;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (mddev->level != LEVEL_MULTIPATH) {
1331 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1333 switch(role) {
1334 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001337 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
1339 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001340 if ((le32_to_cpu(sb->feature_map) &
1341 MD_FEATURE_RECOVERY_OFFSET))
1342 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1343 else
1344 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 rdev->raid_disk = role;
1346 break;
1347 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001348 if (sb->devflags & WriteMostly1)
1349 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001350 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001351 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return 0;
1354}
1355
1356static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1357{
1358 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 mdk_rdev_t *rdev2;
1360 int max_dev, i;
1361 /* make rdev->sb match mddev and rdev data. */
1362
1363 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1364
1365 sb->feature_map = 0;
1366 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001367 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 memset(sb->pad1, 0, sizeof(sb->pad1));
1369 memset(sb->pad2, 0, sizeof(sb->pad2));
1370 memset(sb->pad3, 0, sizeof(sb->pad3));
1371
1372 sb->utime = cpu_to_le64((__u64)mddev->utime);
1373 sb->events = cpu_to_le64(mddev->events);
1374 if (mddev->in_sync)
1375 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1376 else
1377 sb->resync_offset = cpu_to_le64(0);
1378
NeilBrown1c05b4b2006-10-21 10:24:08 -07001379 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001380
NeilBrownf0ca3402006-02-02 14:28:04 -08001381 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001382 sb->size = cpu_to_le64(mddev->dev_sectors);
NeilBrownf0ca3402006-02-02 14:28:04 -08001383
NeilBrowna654b9d82005-06-21 17:17:27 -07001384 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1385 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001386 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001387 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001388
1389 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001390 !test_bit(In_sync, &rdev->flags)) {
1391 if (mddev->curr_resync_completed > rdev->recovery_offset)
1392 rdev->recovery_offset = mddev->curr_resync_completed;
1393 if (rdev->recovery_offset > 0) {
1394 sb->feature_map |=
1395 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1396 sb->recovery_offset =
1397 cpu_to_le64(rdev->recovery_offset);
1398 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001399 }
1400
NeilBrownf6705572006-03-27 01:18:11 -08001401 if (mddev->reshape_position != MaxSector) {
1402 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1403 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1404 sb->new_layout = cpu_to_le32(mddev->new_layout);
1405 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1406 sb->new_level = cpu_to_le32(mddev->new_level);
1407 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1408 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001411 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (rdev2->desc_nr+1 > max_dev)
1413 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001414
1415 if (max_dev > le32_to_cpu(sb->max_dev))
1416 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 for (i=0; i<max_dev;i++)
1418 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1419
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001420 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001422 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001424 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001426 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1427 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 else
1429 sb->dev_roles[i] = cpu_to_le16(0xffff);
1430 }
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 sb->sb_csum = calc_sb_1_csum(sb);
1433}
1434
Chris Webb0cd17fe2008-06-28 08:31:46 +10001435static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001436super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001437{
1438 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001439 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001440 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001441 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001442 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001443 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001444 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1445 max_sectors -= rdev->data_offset;
1446 if (!num_sectors || num_sectors > max_sectors)
1447 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001448 } else if (rdev->mddev->bitmap_offset) {
1449 /* minor version 0 with bitmap we can't move */
1450 return 0;
1451 } else {
1452 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001453 sector_t sb_start;
1454 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1455 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001456 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001457 if (!num_sectors || num_sectors > max_sectors)
1458 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001459 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001460 }
1461 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001462 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001463 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001464 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001465 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001466 rdev->sb_page);
1467 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001468 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001469}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Adrian Bunk75c96f82005-05-05 16:16:09 -07001471static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 [0] = {
1473 .name = "0.90.0",
1474 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001475 .load_super = super_90_load,
1476 .validate_super = super_90_validate,
1477 .sync_super = super_90_sync,
1478 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 },
1480 [1] = {
1481 .name = "md-1",
1482 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001483 .load_super = super_1_load,
1484 .validate_super = super_1_validate,
1485 .sync_super = super_1_sync,
1486 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 },
1488};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1491{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001492 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
NeilBrown4b809912008-07-21 17:05:25 +10001494 rcu_read_lock();
1495 rdev_for_each_rcu(rdev, mddev1)
1496 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001497 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001498 rdev2->bdev->bd_contains) {
1499 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001500 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001501 }
1502 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
1504}
1505
1506static LIST_HEAD(pending_raid_disks);
1507
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001508static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev)
1509{
1510 struct mdk_personality *pers = mddev->pers;
1511 struct gendisk *disk = mddev->gendisk;
1512 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1513 struct blk_integrity *bi_mddev = blk_get_integrity(disk);
1514
1515 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1516 if (pers && pers->level >= 4 && pers->level <= 6)
1517 return;
1518
1519 /* If rdev is integrity capable, register profile for mddev */
1520 if (!bi_mddev && bi_rdev) {
1521 if (blk_integrity_register(disk, bi_rdev))
1522 printk(KERN_ERR "%s: %s Could not register integrity!\n",
1523 __func__, disk->disk_name);
1524 else
1525 printk(KERN_NOTICE "Enabling data integrity on %s\n",
1526 disk->disk_name);
1527 return;
1528 }
1529
1530 /* Check that mddev and rdev have matching profiles */
1531 if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) {
1532 printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__,
1533 disk->disk_name, rdev->bdev->bd_disk->disk_name);
1534 printk(KERN_NOTICE "Disabling data integrity on %s\n",
1535 disk->disk_name);
1536 blk_integrity_unregister(disk);
1537 }
1538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1541{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001542 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001543 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001544 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001545 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 if (rdev->mddev) {
1548 MD_BUG();
1549 return -EINVAL;
1550 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001551
1552 /* prevent duplicates */
1553 if (find_rdev(mddev, rdev->bdev->bd_dev))
1554 return -EEXIST;
1555
Andre Nolldd8ac332009-03-31 14:33:13 +11001556 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1557 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1558 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001559 if (mddev->pers) {
1560 /* Cannot change size, so fail
1561 * If mddev->level <= 0, then we don't care
1562 * about aligning sizes (e.g. linear)
1563 */
1564 if (mddev->level > 0)
1565 return -ENOSPC;
1566 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001567 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /* Verify rdev->desc_nr is unique.
1571 * If it is -1, assign a free number, else
1572 * check number is not in use
1573 */
1574 if (rdev->desc_nr < 0) {
1575 int choice = 0;
1576 if (mddev->pers) choice = mddev->raid_disks;
1577 while (find_rdev_nr(mddev, choice))
1578 choice++;
1579 rdev->desc_nr = choice;
1580 } else {
1581 if (find_rdev_nr(mddev, rdev->desc_nr))
1582 return -EBUSY;
1583 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001584 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1585 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1586 mdname(mddev), mddev->max_disks);
1587 return -EBUSY;
1588 }
NeilBrown19133a42005-11-08 21:39:35 -08001589 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001590 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001591 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001594 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001595
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001596 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001597 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001598
Tejun Heo0762b8b2008-08-25 19:56:12 +09001599 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001600 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1601 kobject_del(&rdev->kobj);
1602 goto fail;
1603 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001604 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1605
NeilBrown4b809912008-07-21 17:05:25 +10001606 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001607 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001608
1609 /* May as well allow recovery to be retried once */
1610 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001611
1612 md_integrity_check(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001614
1615 fail:
1616 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1617 b, mdname(mddev));
1618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
NeilBrown177a99b2008-02-06 01:39:56 -08001621static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001622{
1623 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1624 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001625 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001626}
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1629{
1630 char b[BDEVNAME_SIZE];
1631 if (!rdev->mddev) {
1632 MD_BUG();
1633 return;
1634 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001635 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001636 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1638 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001639 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001640 sysfs_put(rdev->sysfs_state);
1641 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001642 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001643 * writing to 'remove' to "dev/state". We also need
1644 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001645 */
NeilBrown4b809912008-07-21 17:05:25 +10001646 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001647 INIT_WORK(&rdev->del_work, md_delayed_delete);
1648 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001649 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
1652/*
1653 * prevent the device from being mounted, repartitioned or
1654 * otherwise reused by a RAID array (or any other kernel
1655 * subsystem), by bd_claiming the device.
1656 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001657static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 int err = 0;
1660 struct block_device *bdev;
1661 char b[BDEVNAME_SIZE];
1662
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001663 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (IS_ERR(bdev)) {
1665 printk(KERN_ERR "md: could not open %s.\n",
1666 __bdevname(dev, b));
1667 return PTR_ERR(bdev);
1668 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001669 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (err) {
1671 printk(KERN_ERR "md: could not bd_claim %s.\n",
1672 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001673 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return err;
1675 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001676 if (!shared)
1677 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 rdev->bdev = bdev;
1679 return err;
1680}
1681
1682static void unlock_rdev(mdk_rdev_t *rdev)
1683{
1684 struct block_device *bdev = rdev->bdev;
1685 rdev->bdev = NULL;
1686 if (!bdev)
1687 MD_BUG();
1688 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001689 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692void md_autodetect_dev(dev_t dev);
1693
1694static void export_rdev(mdk_rdev_t * rdev)
1695{
1696 char b[BDEVNAME_SIZE];
1697 printk(KERN_INFO "md: export_rdev(%s)\n",
1698 bdevname(rdev->bdev,b));
1699 if (rdev->mddev)
1700 MD_BUG();
1701 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001703 if (test_bit(AutoDetected, &rdev->flags))
1704 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705#endif
1706 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001707 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710static void kick_rdev_from_array(mdk_rdev_t * rdev)
1711{
1712 unbind_rdev_from_array(rdev);
1713 export_rdev(rdev);
1714}
1715
1716static void export_array(mddev_t *mddev)
1717{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001718 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
NeilBrownd089c6a2008-02-06 01:39:59 -08001720 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (!rdev->mddev) {
1722 MD_BUG();
1723 continue;
1724 }
1725 kick_rdev_from_array(rdev);
1726 }
1727 if (!list_empty(&mddev->disks))
1728 MD_BUG();
1729 mddev->raid_disks = 0;
1730 mddev->major_version = 0;
1731}
1732
1733static void print_desc(mdp_disk_t *desc)
1734{
1735 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1736 desc->major,desc->minor,desc->raid_disk,desc->state);
1737}
1738
Cheng Renquancd2ac932009-01-09 08:31:08 +11001739static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 int i;
1742
1743 printk(KERN_INFO
1744 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1745 sb->major_version, sb->minor_version, sb->patch_version,
1746 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1747 sb->ctime);
1748 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1749 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1750 sb->md_minor, sb->layout, sb->chunk_size);
1751 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1752 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1753 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1754 sb->failed_disks, sb->spare_disks,
1755 sb->sb_csum, (unsigned long)sb->events_lo);
1756
1757 printk(KERN_INFO);
1758 for (i = 0; i < MD_SB_DISKS; i++) {
1759 mdp_disk_t *desc;
1760
1761 desc = sb->disks + i;
1762 if (desc->number || desc->major || desc->minor ||
1763 desc->raid_disk || (desc->state && (desc->state != 4))) {
1764 printk(" D %2d: ", i);
1765 print_desc(desc);
1766 }
1767 }
1768 printk(KERN_INFO "md: THIS: ");
1769 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Cheng Renquancd2ac932009-01-09 08:31:08 +11001772static void print_sb_1(struct mdp_superblock_1 *sb)
1773{
1774 __u8 *uuid;
1775
1776 uuid = sb->set_uuid;
1777 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1778 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1779 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1780 le32_to_cpu(sb->major_version),
1781 le32_to_cpu(sb->feature_map),
1782 uuid[0], uuid[1], uuid[2], uuid[3],
1783 uuid[4], uuid[5], uuid[6], uuid[7],
1784 uuid[8], uuid[9], uuid[10], uuid[11],
1785 uuid[12], uuid[13], uuid[14], uuid[15],
1786 sb->set_name,
1787 (unsigned long long)le64_to_cpu(sb->ctime)
1788 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1789
1790 uuid = sb->device_uuid;
1791 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1792 " RO:%llu\n"
1793 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1794 ":%02x%02x%02x%02x%02x%02x\n"
1795 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1796 KERN_INFO "md: (MaxDev:%u) \n",
1797 le32_to_cpu(sb->level),
1798 (unsigned long long)le64_to_cpu(sb->size),
1799 le32_to_cpu(sb->raid_disks),
1800 le32_to_cpu(sb->layout),
1801 le32_to_cpu(sb->chunksize),
1802 (unsigned long long)le64_to_cpu(sb->data_offset),
1803 (unsigned long long)le64_to_cpu(sb->data_size),
1804 (unsigned long long)le64_to_cpu(sb->super_offset),
1805 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1806 le32_to_cpu(sb->dev_number),
1807 uuid[0], uuid[1], uuid[2], uuid[3],
1808 uuid[4], uuid[5], uuid[6], uuid[7],
1809 uuid[8], uuid[9], uuid[10], uuid[11],
1810 uuid[12], uuid[13], uuid[14], uuid[15],
1811 sb->devflags,
1812 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1813 (unsigned long long)le64_to_cpu(sb->events),
1814 (unsigned long long)le64_to_cpu(sb->resync_offset),
1815 le32_to_cpu(sb->sb_csum),
1816 le32_to_cpu(sb->max_dev)
1817 );
1818}
1819
1820static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
1822 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001823 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1824 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001825 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1826 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001828 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1829 switch (major_version) {
1830 case 0:
1831 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1832 break;
1833 case 1:
1834 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1835 break;
1836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 } else
1838 printk(KERN_INFO "md: no rdev superblock!\n");
1839}
1840
Adrian Bunk5e563412006-06-26 00:27:42 -07001841static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001843 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 mdk_rdev_t *rdev;
1845 mddev_t *mddev;
1846 char b[BDEVNAME_SIZE];
1847
1848 printk("\n");
1849 printk("md: **********************************\n");
1850 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1851 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001852 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
NeilBrown32a76272005-06-21 17:17:14 -07001854 if (mddev->bitmap)
1855 bitmap_print_sb(mddev->bitmap);
1856 else
1857 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001858 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 printk("<%s>", bdevname(rdev->bdev,b));
1860 printk("\n");
1861
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001862 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001863 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 printk("md: **********************************\n");
1866 printk("\n");
1867}
1868
1869
NeilBrown42543762006-06-26 00:27:57 -07001870static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
NeilBrown42543762006-06-26 00:27:57 -07001872 /* Update each superblock (in-memory image), but
1873 * if we are allowed to, skip spares which already
1874 * have the right event counter, or have one earlier
1875 * (which would mean they aren't being marked as dirty
1876 * with the rest of the array)
1877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001880 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001881 if (rdev->sb_events == mddev->events ||
1882 (nospares &&
1883 rdev->raid_disk < 0 &&
1884 (rdev->sb_events&1)==0 &&
1885 rdev->sb_events+1 == mddev->events)) {
1886 /* Don't update this superblock */
1887 rdev->sb_loaded = 2;
1888 } else {
1889 super_types[mddev->major_version].
1890 sync_super(mddev, rdev);
1891 rdev->sb_loaded = 1;
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894}
1895
NeilBrown850b2b422006-10-03 01:15:46 -07001896static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001899 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001900 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
NeilBrown8377bc82008-04-30 00:52:28 -07001902 if (mddev->external)
1903 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001905 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001906
NeilBrown850b2b422006-10-03 01:15:46 -07001907 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1908 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1909 force_change = 1;
1910 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1911 /* just a clean<-> dirty transition, possibly leave spares alone,
1912 * though if events isn't the right even/odd, we will have to do
1913 * spares after all
1914 */
1915 nospares = 1;
1916 if (force_change)
1917 nospares = 0;
1918 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001919 /* If the array is degraded, then skipping spares is both
1920 * dangerous and fairly pointless.
1921 * Dangerous because a device that was removed from the array
1922 * might have a event_count that still looks up-to-date,
1923 * so it can be re-added without a resync.
1924 * Pointless because if there are any spares to skip,
1925 * then a recovery will happen and soon that array won't
1926 * be degraded any more and the spare can go back to sleep then.
1927 */
NeilBrown850b2b422006-10-03 01:15:46 -07001928 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001929
NeilBrown06d91a52005-06-21 17:17:12 -07001930 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001932
1933 /* If this is just a dirty<->clean transition, and the array is clean
1934 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07001935 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001936 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001937 && (mddev->events & 1)
1938 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001939 mddev->events--;
1940 else {
1941 /* otherwise we have to go forward and ... */
1942 mddev->events ++;
1943 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1944 /* .. if the array isn't clean, insist on an odd 'events' */
1945 if ((mddev->events&1)==0) {
1946 mddev->events++;
1947 nospares = 0;
1948 }
1949 } else {
1950 /* otherwise insist on an even 'events' (for clean states) */
1951 if ((mddev->events&1)) {
1952 mddev->events++;
1953 nospares = 0;
1954 }
1955 }
1956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 if (!mddev->events) {
1959 /*
1960 * oops, this 64-bit counter should never wrap.
1961 * Either we are in around ~1 trillion A.C., assuming
1962 * 1 reboot per second, or we have a bug:
1963 */
1964 MD_BUG();
1965 mddev->events --;
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /*
1969 * do not write anything to disk if using
1970 * nonpersistent superblocks
1971 */
NeilBrown06d91a52005-06-21 17:17:12 -07001972 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001973 if (!mddev->external)
1974 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1975
NeilBrowna9701a32005-11-08 21:39:34 -08001976 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001977 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001979 }
NeilBrowne6910632008-02-06 01:39:51 -08001980 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001981 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 dprintk(KERN_INFO
1984 "md: updating %s RAID superblock on device (in sync %d)\n",
1985 mdname(mddev),mddev->in_sync);
1986
NeilBrown4ad13662007-07-17 04:06:13 -07001987 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001988 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 char b[BDEVNAME_SIZE];
1990 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001991 if (rdev->sb_loaded != 1)
1992 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001993 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 dprintk("(skipping faulty ");
1995
1996 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001997 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001998 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001999 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002000 rdev->sb_page);
2001 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2002 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002003 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002004 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 } else
2007 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002008 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /* only need to write one superblock... */
2010 break;
2011 }
NeilBrowna9701a32005-11-08 21:39:34 -08002012 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002013 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002014
NeilBrowna9701a32005-11-08 21:39:34 -08002015 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002016 if (mddev->in_sync != sync_req ||
2017 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002018 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002019 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002020 goto repeat;
2021 }
NeilBrown850b2b422006-10-03 01:15:46 -07002022 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002023 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002024 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Andre Noll7f6ce762008-03-23 18:34:54 +01002028/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002029 * We want to accept with case. For this we use cmd_match.
2030 */
2031static int cmd_match(const char *cmd, const char *str)
2032{
2033 /* See if cmd, written into a sysfs file, matches
2034 * str. They must either be the same, or cmd can
2035 * have a trailing newline
2036 */
2037 while (*cmd && *str && *cmd == *str) {
2038 cmd++;
2039 str++;
2040 }
2041 if (*cmd == '\n')
2042 cmd++;
2043 if (*str || *cmd)
2044 return 0;
2045 return 1;
2046}
2047
NeilBrown86e6ffd2005-11-08 21:39:24 -08002048struct rdev_sysfs_entry {
2049 struct attribute attr;
2050 ssize_t (*show)(mdk_rdev_t *, char *);
2051 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2052};
2053
2054static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002055state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002056{
2057 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002058 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002059
NeilBrownb2d444d2005-11-08 21:39:31 -08002060 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002061 len+= sprintf(page+len, "%sfaulty",sep);
2062 sep = ",";
2063 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002064 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002065 len += sprintf(page+len, "%sin_sync",sep);
2066 sep = ",";
2067 }
NeilBrownf6556752006-06-26 00:28:01 -07002068 if (test_bit(WriteMostly, &rdev->flags)) {
2069 len += sprintf(page+len, "%swrite_mostly",sep);
2070 sep = ",";
2071 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002072 if (test_bit(Blocked, &rdev->flags)) {
2073 len += sprintf(page+len, "%sblocked", sep);
2074 sep = ",";
2075 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002076 if (!test_bit(Faulty, &rdev->flags) &&
2077 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002078 len += sprintf(page+len, "%sspare", sep);
2079 sep = ",";
2080 }
2081 return len+sprintf(page+len, "\n");
2082}
2083
NeilBrown45dc2de2006-06-26 00:27:58 -07002084static ssize_t
2085state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2086{
2087 /* can write
2088 * faulty - simulates and error
2089 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002090 * writemostly - sets write_mostly
2091 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002092 * blocked - sets the Blocked flag
2093 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07002094 */
2095 int err = -EINVAL;
2096 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2097 md_error(rdev->mddev, rdev);
2098 err = 0;
2099 } else if (cmd_match(buf, "remove")) {
2100 if (rdev->raid_disk >= 0)
2101 err = -EBUSY;
2102 else {
2103 mddev_t *mddev = rdev->mddev;
2104 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002105 if (mddev->pers)
2106 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002107 md_new_event(mddev);
2108 err = 0;
2109 }
NeilBrownf6556752006-06-26 00:28:01 -07002110 } else if (cmd_match(buf, "writemostly")) {
2111 set_bit(WriteMostly, &rdev->flags);
2112 err = 0;
2113 } else if (cmd_match(buf, "-writemostly")) {
2114 clear_bit(WriteMostly, &rdev->flags);
2115 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002116 } else if (cmd_match(buf, "blocked")) {
2117 set_bit(Blocked, &rdev->flags);
2118 err = 0;
2119 } else if (cmd_match(buf, "-blocked")) {
2120 clear_bit(Blocked, &rdev->flags);
2121 wake_up(&rdev->blocked_wait);
2122 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2123 md_wakeup_thread(rdev->mddev->thread);
2124
2125 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002126 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002127 if (!err && rdev->sysfs_state)
2128 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002129 return err ? err : len;
2130}
NeilBrown80ca3a42006-07-10 04:44:18 -07002131static struct rdev_sysfs_entry rdev_state =
2132__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002133
2134static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002135errors_show(mdk_rdev_t *rdev, char *page)
2136{
2137 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2138}
2139
2140static ssize_t
2141errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2142{
2143 char *e;
2144 unsigned long n = simple_strtoul(buf, &e, 10);
2145 if (*buf && (*e == 0 || *e == '\n')) {
2146 atomic_set(&rdev->corrected_errors, n);
2147 return len;
2148 }
2149 return -EINVAL;
2150}
2151static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002152__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002153
NeilBrown014236d2006-01-06 00:20:55 -08002154static ssize_t
2155slot_show(mdk_rdev_t *rdev, char *page)
2156{
2157 if (rdev->raid_disk < 0)
2158 return sprintf(page, "none\n");
2159 else
2160 return sprintf(page, "%d\n", rdev->raid_disk);
2161}
2162
2163static ssize_t
2164slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2165{
2166 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002167 int err;
2168 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002169 int slot = simple_strtoul(buf, &e, 10);
2170 if (strncmp(buf, "none", 4)==0)
2171 slot = -1;
2172 else if (e==buf || (*e && *e!= '\n'))
2173 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002174 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002175 /* Setting 'slot' on an active array requires also
2176 * updating the 'rd%d' link, and communicating
2177 * with the personality with ->hot_*_disk.
2178 * For now we only support removing
2179 * failed/spare devices. This normally happens automatically,
2180 * but not when the metadata is externally managed.
2181 */
NeilBrownc303da62008-02-06 01:39:51 -08002182 if (rdev->raid_disk == -1)
2183 return -EEXIST;
2184 /* personality does all needed checks */
2185 if (rdev->mddev->pers->hot_add_disk == NULL)
2186 return -EINVAL;
2187 err = rdev->mddev->pers->
2188 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2189 if (err)
2190 return err;
2191 sprintf(nm, "rd%d", rdev->raid_disk);
2192 sysfs_remove_link(&rdev->mddev->kobj, nm);
2193 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2194 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002195 } else if (rdev->mddev->pers) {
2196 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002197 /* Activating a spare .. or possibly reactivating
2198 * if we every get bitmaps working here.
2199 */
2200
2201 if (rdev->raid_disk != -1)
2202 return -EBUSY;
2203
2204 if (rdev->mddev->pers->hot_add_disk == NULL)
2205 return -EINVAL;
2206
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002207 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002208 if (rdev2->raid_disk == slot)
2209 return -EEXIST;
2210
2211 rdev->raid_disk = slot;
2212 if (test_bit(In_sync, &rdev->flags))
2213 rdev->saved_raid_disk = slot;
2214 else
2215 rdev->saved_raid_disk = -1;
2216 err = rdev->mddev->pers->
2217 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002218 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002219 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002220 return err;
Neil Brown52664732008-06-28 08:31:44 +10002221 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002222 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002223 sprintf(nm, "rd%d", rdev->raid_disk);
2224 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2225 printk(KERN_WARNING
2226 "md: cannot register "
2227 "%s for %s\n",
2228 nm, mdname(rdev->mddev));
2229
2230 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002231 } else {
2232 if (slot >= rdev->mddev->raid_disks)
2233 return -ENOSPC;
2234 rdev->raid_disk = slot;
2235 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002236 clear_bit(Faulty, &rdev->flags);
2237 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002238 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002239 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002240 }
NeilBrown014236d2006-01-06 00:20:55 -08002241 return len;
2242}
2243
2244
2245static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002246__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002247
NeilBrown93c8cad2006-01-06 00:20:56 -08002248static ssize_t
2249offset_show(mdk_rdev_t *rdev, char *page)
2250{
Andrew Morton6961ece2006-01-06 00:20:59 -08002251 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002252}
2253
2254static ssize_t
2255offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2256{
2257 char *e;
2258 unsigned long long offset = simple_strtoull(buf, &e, 10);
2259 if (e==buf || (*e && *e != '\n'))
2260 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002261 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002262 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002263 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002264 /* Must set offset before size, so overlap checks
2265 * can be sane */
2266 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002267 rdev->data_offset = offset;
2268 return len;
2269}
2270
2271static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002272__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002273
NeilBrown83303b62006-01-06 00:21:06 -08002274static ssize_t
2275rdev_size_show(mdk_rdev_t *rdev, char *page)
2276{
Andre Nolldd8ac332009-03-31 14:33:13 +11002277 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002278}
2279
NeilBrownc5d79ad2008-02-06 01:39:54 -08002280static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2281{
2282 /* check if two start/length pairs overlap */
2283 if (s1+l1 <= s2)
2284 return 0;
2285 if (s2+l2 <= s1)
2286 return 0;
2287 return 1;
2288}
2289
Dan Williamsb522adc2009-03-31 15:00:31 +11002290static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2291{
2292 unsigned long long blocks;
2293 sector_t new;
2294
2295 if (strict_strtoull(buf, 10, &blocks) < 0)
2296 return -EINVAL;
2297
2298 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2299 return -EINVAL; /* sector conversion overflow */
2300
2301 new = blocks * 2;
2302 if (new != blocks * 2)
2303 return -EINVAL; /* unsigned long long to sector_t overflow */
2304
2305 *sectors = new;
2306 return 0;
2307}
2308
NeilBrown83303b62006-01-06 00:21:06 -08002309static ssize_t
2310rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2311{
NeilBrown27c529b2008-03-04 14:29:33 -08002312 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002313 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002314 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002315
Dan Williamsb522adc2009-03-31 15:00:31 +11002316 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002317 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002318 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002319 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002320 sectors = super_types[my_mddev->major_version].
2321 rdev_size_change(rdev, sectors);
2322 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002323 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002324 } else if (!sectors)
2325 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2326 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002327 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002328 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002329 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002330
Andre Nolldd8ac332009-03-31 14:33:13 +11002331 rdev->sectors = sectors;
2332 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002333 /* need to check that all other rdevs with the same ->bdev
2334 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002335 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002336 * we have to change it back, we will have the lock again.
2337 */
2338 mddev_t *mddev;
2339 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002340 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002341
NeilBrown27c529b2008-03-04 14:29:33 -08002342 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002343 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002344 mdk_rdev_t *rdev2;
2345
2346 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002347 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002348 if (test_bit(AllReserved, &rdev2->flags) ||
2349 (rdev->bdev == rdev2->bdev &&
2350 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002351 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002352 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002353 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002354 overlap = 1;
2355 break;
2356 }
2357 mddev_unlock(mddev);
2358 if (overlap) {
2359 mddev_put(mddev);
2360 break;
2361 }
2362 }
NeilBrown27c529b2008-03-04 14:29:33 -08002363 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002364 if (overlap) {
2365 /* Someone else could have slipped in a size
2366 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002367 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002368 * safe, and trust userspace not to race with
2369 * itself
2370 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002371 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002372 return -EBUSY;
2373 }
2374 }
NeilBrown83303b62006-01-06 00:21:06 -08002375 return len;
2376}
2377
2378static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002379__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002380
NeilBrown86e6ffd2005-11-08 21:39:24 -08002381static struct attribute *rdev_default_attrs[] = {
2382 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002383 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002384 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002385 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002386 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002387 NULL,
2388};
2389static ssize_t
2390rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2391{
2392 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2393 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002394 mddev_t *mddev = rdev->mddev;
2395 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002396
2397 if (!entry->show)
2398 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002399
2400 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2401 if (!rv) {
2402 if (rdev->mddev == NULL)
2403 rv = -EBUSY;
2404 else
2405 rv = entry->show(rdev, page);
2406 mddev_unlock(mddev);
2407 }
2408 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002409}
2410
2411static ssize_t
2412rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2413 const char *page, size_t length)
2414{
2415 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2416 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002417 ssize_t rv;
2418 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002419
2420 if (!entry->store)
2421 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002422 if (!capable(CAP_SYS_ADMIN))
2423 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002424 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002425 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002426 if (rdev->mddev == NULL)
2427 rv = -EBUSY;
2428 else
2429 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002430 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002431 }
2432 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002433}
2434
2435static void rdev_free(struct kobject *ko)
2436{
2437 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2438 kfree(rdev);
2439}
2440static struct sysfs_ops rdev_sysfs_ops = {
2441 .show = rdev_attr_show,
2442 .store = rdev_attr_store,
2443};
2444static struct kobj_type rdev_ktype = {
2445 .release = rdev_free,
2446 .sysfs_ops = &rdev_sysfs_ops,
2447 .default_attrs = rdev_default_attrs,
2448};
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450/*
2451 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2452 *
2453 * mark the device faulty if:
2454 *
2455 * - the device is nonexistent (zero size)
2456 * - the device has no valid superblock
2457 *
2458 * a faulty rdev _never_ has rdev->sb set.
2459 */
2460static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2461{
2462 char b[BDEVNAME_SIZE];
2463 int err;
2464 mdk_rdev_t *rdev;
2465 sector_t size;
2466
NeilBrown9ffae0c2006-01-06 00:20:32 -08002467 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (!rdev) {
2469 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2470 return ERR_PTR(-ENOMEM);
2471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 if ((err = alloc_disk_sb(rdev)))
2474 goto abort_free;
2475
NeilBrownc5d79ad2008-02-06 01:39:54 -08002476 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (err)
2478 goto abort_free;
2479
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002480 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002483 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002484 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002485 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002487 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002489 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002490 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2493 if (!size) {
2494 printk(KERN_WARNING
2495 "md: %s has zero or unknown size, marking faulty!\n",
2496 bdevname(rdev->bdev,b));
2497 err = -EINVAL;
2498 goto abort_free;
2499 }
2500
2501 if (super_format >= 0) {
2502 err = super_types[super_format].
2503 load_super(rdev, NULL, super_minor);
2504 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002505 printk(KERN_WARNING
2506 "md: %s does not have a valid v%d.%d "
2507 "superblock, not importing!\n",
2508 bdevname(rdev->bdev,b),
2509 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 goto abort_free;
2511 }
2512 if (err < 0) {
2513 printk(KERN_WARNING
2514 "md: could not read %s's sb, not importing!\n",
2515 bdevname(rdev->bdev,b));
2516 goto abort_free;
2517 }
2518 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002521 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 return rdev;
2524
2525abort_free:
2526 if (rdev->sb_page) {
2527 if (rdev->bdev)
2528 unlock_rdev(rdev);
2529 free_disk_sb(rdev);
2530 }
2531 kfree(rdev);
2532 return ERR_PTR(err);
2533}
2534
2535/*
2536 * Check a full RAID array for plausibility
2537 */
2538
2539
NeilBrowna757e642005-04-16 15:26:42 -07002540static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002543 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 char b[BDEVNAME_SIZE];
2545
2546 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002547 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 switch (super_types[mddev->major_version].
2549 load_super(rdev, freshest, mddev->minor_version)) {
2550 case 1:
2551 freshest = rdev;
2552 break;
2553 case 0:
2554 break;
2555 default:
2556 printk( KERN_ERR \
2557 "md: fatal superblock inconsistency in %s"
2558 " -- removing from array\n",
2559 bdevname(rdev->bdev,b));
2560 kick_rdev_from_array(rdev);
2561 }
2562
2563
2564 super_types[mddev->major_version].
2565 validate_super(mddev, freshest);
2566
2567 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002568 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002569 if (rdev->desc_nr >= mddev->max_disks ||
2570 i > mddev->max_disks) {
2571 printk(KERN_WARNING
2572 "md: %s: %s: only %d devices permitted\n",
2573 mdname(mddev), bdevname(rdev->bdev, b),
2574 mddev->max_disks);
2575 kick_rdev_from_array(rdev);
2576 continue;
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (rdev != freshest)
2579 if (super_types[mddev->major_version].
2580 validate_super(mddev, rdev)) {
2581 printk(KERN_WARNING "md: kicking non-fresh %s"
2582 " from array!\n",
2583 bdevname(rdev->bdev,b));
2584 kick_rdev_from_array(rdev);
2585 continue;
2586 }
2587 if (mddev->level == LEVEL_MULTIPATH) {
2588 rdev->desc_nr = i++;
2589 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002590 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002591 } else if (rdev->raid_disk >= mddev->raid_disks) {
2592 rdev->raid_disk = -1;
2593 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595 }
2596
2597
2598
2599 if (mddev->recovery_cp != MaxSector &&
2600 mddev->level >= 1)
2601 printk(KERN_ERR "md: %s: raid array is not clean"
2602 " -- starting background reconstruction\n",
2603 mdname(mddev));
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605}
2606
NeilBrown19052c02008-08-05 15:54:13 +10002607static void md_safemode_timeout(unsigned long data);
2608
NeilBrowneae17012005-11-08 21:39:23 -08002609static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002610safe_delay_show(mddev_t *mddev, char *page)
2611{
2612 int msec = (mddev->safemode_delay*1000)/HZ;
2613 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2614}
2615static ssize_t
2616safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2617{
2618 int scale=1;
2619 int dot=0;
2620 int i;
2621 unsigned long msec;
2622 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002623
NeilBrown16f17b32006-06-26 00:27:37 -07002624 /* remove a period, and count digits after it */
2625 if (len >= sizeof(buf))
2626 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002627 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002628 for (i=0; i<len; i++) {
2629 if (dot) {
2630 if (isdigit(buf[i])) {
2631 buf[i-1] = buf[i];
2632 scale *= 10;
2633 }
2634 buf[i] = 0;
2635 } else if (buf[i] == '.') {
2636 dot=1;
2637 buf[i] = 0;
2638 }
2639 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002640 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002641 return -EINVAL;
2642 msec = (msec * 1000) / scale;
2643 if (msec == 0)
2644 mddev->safemode_delay = 0;
2645 else {
NeilBrown19052c02008-08-05 15:54:13 +10002646 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002647 mddev->safemode_delay = (msec*HZ)/1000;
2648 if (mddev->safemode_delay == 0)
2649 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002650 if (mddev->safemode_delay < old_delay)
2651 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002652 }
2653 return len;
2654}
2655static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002656__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002657
2658static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002659level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002660{
NeilBrown2604b702006-01-06 00:20:36 -08002661 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002662 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002663 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002664 else if (mddev->clevel[0])
2665 return sprintf(page, "%s\n", mddev->clevel);
2666 else if (mddev->level != LEVEL_NONE)
2667 return sprintf(page, "%d\n", mddev->level);
2668 else
2669 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002670}
2671
NeilBrownd9d166c2006-01-06 00:20:51 -08002672static ssize_t
2673level_store(mddev_t *mddev, const char *buf, size_t len)
2674{
NeilBrown245f46c2009-03-31 14:39:39 +11002675 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002676 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002677 struct mdk_personality *pers;
2678 void *priv;
2679
2680 if (mddev->pers == NULL) {
2681 if (len == 0)
2682 return 0;
2683 if (len >= sizeof(mddev->clevel))
2684 return -ENOSPC;
2685 strncpy(mddev->clevel, buf, len);
2686 if (mddev->clevel[len-1] == '\n')
2687 len--;
2688 mddev->clevel[len] = 0;
2689 mddev->level = LEVEL_NONE;
2690 return rv;
2691 }
2692
2693 /* request to change the personality. Need to ensure:
2694 * - array is not engaged in resync/recovery/reshape
2695 * - old personality can be suspended
2696 * - new personality will access other array.
2697 */
2698
2699 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002700 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002701
2702 if (!mddev->pers->quiesce) {
2703 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2704 mdname(mddev), mddev->pers->name);
2705 return -EINVAL;
2706 }
2707
2708 /* Now find the new personality */
2709 if (len == 0 || len >= sizeof(level))
2710 return -EINVAL;
2711 strncpy(level, buf, len);
2712 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002713 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002714 level[len] = 0;
2715
2716 request_module("md-%s", level);
2717 spin_lock(&pers_lock);
2718 pers = find_pers(LEVEL_NONE, level);
2719 if (!pers || !try_module_get(pers->owner)) {
2720 spin_unlock(&pers_lock);
2721 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2722 return -EINVAL;
2723 }
2724 spin_unlock(&pers_lock);
2725
2726 if (pers == mddev->pers) {
2727 /* Nothing to do! */
2728 module_put(pers->owner);
2729 return rv;
2730 }
2731 if (!pers->takeover) {
2732 module_put(pers->owner);
2733 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2734 mdname(mddev), level);
2735 return -EINVAL;
2736 }
2737
2738 /* ->takeover must set new_* and/or delta_disks
2739 * if it succeeds, and may set them when it fails.
2740 */
2741 priv = pers->takeover(mddev);
2742 if (IS_ERR(priv)) {
2743 mddev->new_level = mddev->level;
2744 mddev->new_layout = mddev->layout;
2745 mddev->new_chunk = mddev->chunk_size;
2746 mddev->raid_disks -= mddev->delta_disks;
2747 mddev->delta_disks = 0;
2748 module_put(pers->owner);
2749 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2750 mdname(mddev), level);
2751 return PTR_ERR(priv);
2752 }
2753
2754 /* Looks like we have a winner */
2755 mddev_suspend(mddev);
2756 mddev->pers->stop(mddev);
2757 module_put(mddev->pers->owner);
2758 mddev->pers = pers;
2759 mddev->private = priv;
2760 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2761 mddev->level = mddev->new_level;
2762 mddev->layout = mddev->new_layout;
2763 mddev->chunk_size = mddev->new_chunk;
2764 mddev->delta_disks = 0;
2765 pers->run(mddev);
2766 mddev_resume(mddev);
2767 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2768 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2769 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002770 return rv;
2771}
2772
2773static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002774__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002775
NeilBrownd4dbd022006-06-26 00:27:59 -07002776
2777static ssize_t
2778layout_show(mddev_t *mddev, char *page)
2779{
2780 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002781 if (mddev->reshape_position != MaxSector &&
2782 mddev->layout != mddev->new_layout)
2783 return sprintf(page, "%d (%d)\n",
2784 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002785 return sprintf(page, "%d\n", mddev->layout);
2786}
2787
2788static ssize_t
2789layout_store(mddev_t *mddev, const char *buf, size_t len)
2790{
2791 char *e;
2792 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002793
2794 if (!*buf || (*e && *e != '\n'))
2795 return -EINVAL;
2796
NeilBrownb3546032009-03-31 14:56:41 +11002797 if (mddev->pers) {
2798 int err;
2799 if (mddev->pers->reconfig == NULL)
2800 return -EBUSY;
2801 err = mddev->pers->reconfig(mddev, n, -1);
2802 if (err)
2803 return err;
2804 } else {
2805 mddev->new_layout = n;
2806 if (mddev->reshape_position == MaxSector)
2807 mddev->layout = n;
2808 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002809 return len;
2810}
2811static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002812__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002813
2814
NeilBrowneae17012005-11-08 21:39:23 -08002815static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002816raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002817{
NeilBrownbb636542005-11-08 21:39:45 -08002818 if (mddev->raid_disks == 0)
2819 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002820 if (mddev->reshape_position != MaxSector &&
2821 mddev->delta_disks != 0)
2822 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2823 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002824 return sprintf(page, "%d\n", mddev->raid_disks);
2825}
2826
NeilBrownda943b992006-01-06 00:20:54 -08002827static int update_raid_disks(mddev_t *mddev, int raid_disks);
2828
2829static ssize_t
2830raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2831{
NeilBrownda943b992006-01-06 00:20:54 -08002832 char *e;
2833 int rv = 0;
2834 unsigned long n = simple_strtoul(buf, &e, 10);
2835
2836 if (!*buf || (*e && *e != '\n'))
2837 return -EINVAL;
2838
2839 if (mddev->pers)
2840 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002841 else if (mddev->reshape_position != MaxSector) {
2842 int olddisks = mddev->raid_disks - mddev->delta_disks;
2843 mddev->delta_disks = n - olddisks;
2844 mddev->raid_disks = n;
2845 } else
NeilBrownda943b992006-01-06 00:20:54 -08002846 mddev->raid_disks = n;
2847 return rv ? rv : len;
2848}
2849static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002850__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002851
NeilBrown24dd4692005-11-08 21:39:26 -08002852static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002853chunk_size_show(mddev_t *mddev, char *page)
2854{
NeilBrown08a02ec2007-05-09 02:35:38 -07002855 if (mddev->reshape_position != MaxSector &&
2856 mddev->chunk_size != mddev->new_chunk)
2857 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2858 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002859 return sprintf(page, "%d\n", mddev->chunk_size);
2860}
2861
2862static ssize_t
2863chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2864{
NeilBrown3b343802006-01-06 00:20:47 -08002865 char *e;
2866 unsigned long n = simple_strtoul(buf, &e, 10);
2867
NeilBrown3b343802006-01-06 00:20:47 -08002868 if (!*buf || (*e && *e != '\n'))
2869 return -EINVAL;
2870
NeilBrownb3546032009-03-31 14:56:41 +11002871 if (mddev->pers) {
2872 int err;
2873 if (mddev->pers->reconfig == NULL)
2874 return -EBUSY;
2875 err = mddev->pers->reconfig(mddev, -1, n);
2876 if (err)
2877 return err;
2878 } else {
2879 mddev->new_chunk = n;
2880 if (mddev->reshape_position == MaxSector)
2881 mddev->chunk_size = n;
2882 }
NeilBrown3b343802006-01-06 00:20:47 -08002883 return len;
2884}
2885static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002886__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002887
NeilBrowna94213b2006-06-26 00:28:00 -07002888static ssize_t
2889resync_start_show(mddev_t *mddev, char *page)
2890{
2891 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2892}
2893
2894static ssize_t
2895resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2896{
NeilBrowna94213b2006-06-26 00:28:00 -07002897 char *e;
2898 unsigned long long n = simple_strtoull(buf, &e, 10);
2899
2900 if (mddev->pers)
2901 return -EBUSY;
2902 if (!*buf || (*e && *e != '\n'))
2903 return -EINVAL;
2904
2905 mddev->recovery_cp = n;
2906 return len;
2907}
2908static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002909__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002910
NeilBrown9e653b62006-06-26 00:27:58 -07002911/*
2912 * The array state can be:
2913 *
2914 * clear
2915 * No devices, no size, no level
2916 * Equivalent to STOP_ARRAY ioctl
2917 * inactive
2918 * May have some settings, but array is not active
2919 * all IO results in error
2920 * When written, doesn't tear down array, but just stops it
2921 * suspended (not supported yet)
2922 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002923 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002924 * readonly
2925 * no resync can happen. no superblocks get written.
2926 * write requests fail
2927 * read-auto
2928 * like readonly, but behaves like 'clean' on a write request.
2929 *
2930 * clean - no pending writes, but otherwise active.
2931 * When written to inactive array, starts without resync
2932 * If a write request arrives then
2933 * if metadata is known, mark 'dirty' and switch to 'active'.
2934 * if not known, block and switch to write-pending
2935 * If written to an active array that has pending writes, then fails.
2936 * active
2937 * fully active: IO and resync can be happening.
2938 * When written to inactive array, starts with resync
2939 *
2940 * write-pending
2941 * clean, but writes are blocked waiting for 'active' to be written.
2942 *
2943 * active-idle
2944 * like active, but no writes have been seen for a while (100msec).
2945 *
2946 */
2947enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2948 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002949static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002950 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2951 "write-pending", "active-idle", NULL };
2952
2953static int match_word(const char *word, char **list)
2954{
2955 int n;
2956 for (n=0; list[n]; n++)
2957 if (cmd_match(word, list[n]))
2958 break;
2959 return n;
2960}
2961
2962static ssize_t
2963array_state_show(mddev_t *mddev, char *page)
2964{
2965 enum array_state st = inactive;
2966
2967 if (mddev->pers)
2968 switch(mddev->ro) {
2969 case 1:
2970 st = readonly;
2971 break;
2972 case 2:
2973 st = read_auto;
2974 break;
2975 case 0:
2976 if (mddev->in_sync)
2977 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002978 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2979 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002980 else if (mddev->safemode)
2981 st = active_idle;
2982 else
2983 st = active;
2984 }
2985 else {
2986 if (list_empty(&mddev->disks) &&
2987 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11002988 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002989 st = clear;
2990 else
2991 st = inactive;
2992 }
2993 return sprintf(page, "%s\n", array_states[st]);
2994}
2995
Neil Browndf5b20c2008-07-11 22:02:22 +10002996static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002997static int do_md_run(mddev_t * mddev);
2998static int restart_array(mddev_t *mddev);
2999
3000static ssize_t
3001array_state_store(mddev_t *mddev, const char *buf, size_t len)
3002{
3003 int err = -EINVAL;
3004 enum array_state st = match_word(buf, array_states);
3005 switch(st) {
3006 case bad_word:
3007 break;
3008 case clear:
3009 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003010 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003011 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003012 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003013 break;
3014 case inactive:
3015 /* stopping an active array */
3016 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003017 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003018 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003019 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003020 } else
3021 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003022 break;
3023 case suspended:
3024 break; /* not supported yet */
3025 case readonly:
3026 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003027 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003028 else {
3029 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003030 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003031 err = do_md_run(mddev);
3032 }
3033 break;
3034 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003035 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003036 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003037 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003038 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003039 err = restart_array(mddev);
3040 if (err == 0) {
3041 mddev->ro = 2;
3042 set_disk_ro(mddev->gendisk, 0);
3043 }
NeilBrown9e653b62006-06-26 00:27:58 -07003044 } else {
3045 mddev->ro = 2;
3046 err = do_md_run(mddev);
3047 }
3048 break;
3049 case clean:
3050 if (mddev->pers) {
3051 restart_array(mddev);
3052 spin_lock_irq(&mddev->write_lock);
3053 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003054 if (mddev->in_sync == 0) {
3055 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003056 if (mddev->safemode == 1)
3057 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003058 if (mddev->persistent)
3059 set_bit(MD_CHANGE_CLEAN,
3060 &mddev->flags);
3061 }
3062 err = 0;
3063 } else
3064 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003065 spin_unlock_irq(&mddev->write_lock);
3066 } else {
3067 mddev->ro = 0;
3068 mddev->recovery_cp = MaxSector;
3069 err = do_md_run(mddev);
3070 }
3071 break;
3072 case active:
3073 if (mddev->pers) {
3074 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003075 if (mddev->external)
3076 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003077 wake_up(&mddev->sb_wait);
3078 err = 0;
3079 } else {
3080 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003081 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003082 err = do_md_run(mddev);
3083 }
3084 break;
3085 case write_pending:
3086 case active_idle:
3087 /* these cannot be set */
3088 break;
3089 }
3090 if (err)
3091 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003092 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003093 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003094 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003095 }
NeilBrown9e653b62006-06-26 00:27:58 -07003096}
NeilBrown80ca3a42006-07-10 04:44:18 -07003097static struct md_sysfs_entry md_array_state =
3098__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003099
NeilBrown6d7ff7382006-01-06 00:21:16 -08003100static ssize_t
3101null_show(mddev_t *mddev, char *page)
3102{
3103 return -EINVAL;
3104}
3105
3106static ssize_t
3107new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3108{
3109 /* buf must be %d:%d\n? giving major and minor numbers */
3110 /* The new device is added to the array.
3111 * If the array has a persistent superblock, we read the
3112 * superblock to initialise info and check validity.
3113 * Otherwise, only checking done is that in bind_rdev_to_array,
3114 * which mainly checks size.
3115 */
3116 char *e;
3117 int major = simple_strtoul(buf, &e, 10);
3118 int minor;
3119 dev_t dev;
3120 mdk_rdev_t *rdev;
3121 int err;
3122
3123 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3124 return -EINVAL;
3125 minor = simple_strtoul(e+1, &e, 10);
3126 if (*e && *e != '\n')
3127 return -EINVAL;
3128 dev = MKDEV(major, minor);
3129 if (major != MAJOR(dev) ||
3130 minor != MINOR(dev))
3131 return -EOVERFLOW;
3132
3133
3134 if (mddev->persistent) {
3135 rdev = md_import_device(dev, mddev->major_version,
3136 mddev->minor_version);
3137 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3138 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3139 mdk_rdev_t, same_set);
3140 err = super_types[mddev->major_version]
3141 .load_super(rdev, rdev0, mddev->minor_version);
3142 if (err < 0)
3143 goto out;
3144 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003145 } else if (mddev->external)
3146 rdev = md_import_device(dev, -2, -1);
3147 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003148 rdev = md_import_device(dev, -1, -1);
3149
3150 if (IS_ERR(rdev))
3151 return PTR_ERR(rdev);
3152 err = bind_rdev_to_array(rdev, mddev);
3153 out:
3154 if (err)
3155 export_rdev(rdev);
3156 return err ? err : len;
3157}
3158
3159static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003160__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003161
3162static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003163bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3164{
3165 char *end;
3166 unsigned long chunk, end_chunk;
3167
3168 if (!mddev->bitmap)
3169 goto out;
3170 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3171 while (*buf) {
3172 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3173 if (buf == end) break;
3174 if (*end == '-') { /* range */
3175 buf = end + 1;
3176 end_chunk = simple_strtoul(buf, &end, 0);
3177 if (buf == end) break;
3178 }
3179 if (*end && !isspace(*end)) break;
3180 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3181 buf = end;
3182 while (isspace(*buf)) buf++;
3183 }
3184 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3185out:
3186 return len;
3187}
3188
3189static struct md_sysfs_entry md_bitmap =
3190__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3191
3192static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003193size_show(mddev_t *mddev, char *page)
3194{
Andre Noll58c0fed2009-03-31 14:33:13 +11003195 return sprintf(page, "%llu\n",
3196 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003197}
3198
Andre Nolld71f9f82008-07-11 22:02:22 +10003199static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003200
3201static ssize_t
3202size_store(mddev_t *mddev, const char *buf, size_t len)
3203{
3204 /* If array is inactive, we can reduce the component size, but
3205 * not increase it (except from 0).
3206 * If array is active, we can try an on-line resize
3207 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003208 sector_t sectors;
3209 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003210
Andre Noll58c0fed2009-03-31 14:33:13 +11003211 if (err < 0)
3212 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003213 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003214 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003215 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003216 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003217 if (mddev->dev_sectors == 0 ||
3218 mddev->dev_sectors > sectors)
3219 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003220 else
3221 err = -ENOSPC;
3222 }
3223 return err ? err : len;
3224}
3225
3226static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003227__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003228
NeilBrown8bb93aa2006-01-06 00:20:50 -08003229
3230/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003231 * This is one of
3232 * 'none' for arrays with no metadata (good luck...)
3233 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003234 * or N.M for internally known formats
3235 */
3236static ssize_t
3237metadata_show(mddev_t *mddev, char *page)
3238{
3239 if (mddev->persistent)
3240 return sprintf(page, "%d.%d\n",
3241 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003242 else if (mddev->external)
3243 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003244 else
3245 return sprintf(page, "none\n");
3246}
3247
3248static ssize_t
3249metadata_store(mddev_t *mddev, const char *buf, size_t len)
3250{
3251 int major, minor;
3252 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003253 /* Changing the details of 'external' metadata is
3254 * always permitted. Otherwise there must be
3255 * no devices attached to the array.
3256 */
3257 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3258 ;
3259 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003260 return -EBUSY;
3261
3262 if (cmd_match(buf, "none")) {
3263 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003264 mddev->external = 0;
3265 mddev->major_version = 0;
3266 mddev->minor_version = 90;
3267 return len;
3268 }
3269 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003270 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003271 if (namelen >= sizeof(mddev->metadata_type))
3272 namelen = sizeof(mddev->metadata_type)-1;
3273 strncpy(mddev->metadata_type, buf+9, namelen);
3274 mddev->metadata_type[namelen] = 0;
3275 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3276 mddev->metadata_type[--namelen] = 0;
3277 mddev->persistent = 0;
3278 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003279 mddev->major_version = 0;
3280 mddev->minor_version = 90;
3281 return len;
3282 }
3283 major = simple_strtoul(buf, &e, 10);
3284 if (e==buf || *e != '.')
3285 return -EINVAL;
3286 buf = e+1;
3287 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003288 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003289 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003290 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003291 return -ENOENT;
3292 mddev->major_version = major;
3293 mddev->minor_version = minor;
3294 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003295 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003296 return len;
3297}
3298
3299static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003300__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003301
NeilBrowna35b0d62006-01-06 00:20:49 -08003302static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003303action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003304{
NeilBrown7eec3142005-11-08 21:39:44 -08003305 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08003306 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003307 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003308 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3309 type = "reshape";
3310 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003311 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3312 type = "resync";
3313 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3314 type = "check";
3315 else
3316 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003317 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003318 type = "recover";
3319 }
3320 return sprintf(page, "%s\n", type);
3321}
3322
3323static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003324action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003325{
NeilBrown7eec3142005-11-08 21:39:44 -08003326 if (!mddev->pers || !mddev->pers->sync_request)
3327 return -EINVAL;
3328
NeilBrownbce74da2006-01-06 00:20:41 -08003329 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003330 if (mddev->sync_thread) {
3331 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3332 md_unregister_thread(mddev->sync_thread);
3333 mddev->sync_thread = NULL;
3334 mddev->recovery = 0;
3335 }
NeilBrown03c902e2006-01-06 00:20:46 -08003336 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3337 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003338 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003339 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003340 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003341 else if (cmd_match(page, "recover")) {
3342 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3344 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003345 int err;
3346 if (mddev->pers->start_reshape == NULL)
3347 return -EINVAL;
3348 err = mddev->pers->start_reshape(mddev);
3349 if (err)
3350 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003351 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003352 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003353 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003354 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003355 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003356 return -EINVAL;
3357 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3358 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003359 }
NeilBrown03c902e2006-01-06 00:20:46 -08003360 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003361 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003362 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003363 return len;
3364}
3365
NeilBrown9d888832005-11-08 21:39:26 -08003366static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003367mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003368{
3369 return sprintf(page, "%llu\n",
3370 (unsigned long long) mddev->resync_mismatches);
3371}
3372
NeilBrown80ca3a42006-07-10 04:44:18 -07003373static struct md_sysfs_entry md_scan_mode =
3374__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003375
NeilBrown96de1e62005-11-08 21:39:39 -08003376
NeilBrown80ca3a42006-07-10 04:44:18 -07003377static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003378
NeilBrown88202a02006-01-06 00:21:36 -08003379static ssize_t
3380sync_min_show(mddev_t *mddev, char *page)
3381{
3382 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3383 mddev->sync_speed_min ? "local": "system");
3384}
3385
3386static ssize_t
3387sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3388{
3389 int min;
3390 char *e;
3391 if (strncmp(buf, "system", 6)==0) {
3392 mddev->sync_speed_min = 0;
3393 return len;
3394 }
3395 min = simple_strtoul(buf, &e, 10);
3396 if (buf == e || (*e && *e != '\n') || min <= 0)
3397 return -EINVAL;
3398 mddev->sync_speed_min = min;
3399 return len;
3400}
3401
3402static struct md_sysfs_entry md_sync_min =
3403__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3404
3405static ssize_t
3406sync_max_show(mddev_t *mddev, char *page)
3407{
3408 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3409 mddev->sync_speed_max ? "local": "system");
3410}
3411
3412static ssize_t
3413sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3414{
3415 int max;
3416 char *e;
3417 if (strncmp(buf, "system", 6)==0) {
3418 mddev->sync_speed_max = 0;
3419 return len;
3420 }
3421 max = simple_strtoul(buf, &e, 10);
3422 if (buf == e || (*e && *e != '\n') || max <= 0)
3423 return -EINVAL;
3424 mddev->sync_speed_max = max;
3425 return len;
3426}
3427
3428static struct md_sysfs_entry md_sync_max =
3429__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3430
Iustin Popd7f3d292007-10-16 23:30:54 -07003431static ssize_t
3432degraded_show(mddev_t *mddev, char *page)
3433{
3434 return sprintf(page, "%d\n", mddev->degraded);
3435}
3436static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003437
3438static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003439sync_force_parallel_show(mddev_t *mddev, char *page)
3440{
3441 return sprintf(page, "%d\n", mddev->parallel_resync);
3442}
3443
3444static ssize_t
3445sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3446{
3447 long n;
3448
3449 if (strict_strtol(buf, 10, &n))
3450 return -EINVAL;
3451
3452 if (n != 0 && n != 1)
3453 return -EINVAL;
3454
3455 mddev->parallel_resync = n;
3456
3457 if (mddev->sync_thread)
3458 wake_up(&resync_wait);
3459
3460 return len;
3461}
3462
3463/* force parallel resync, even with shared block devices */
3464static struct md_sysfs_entry md_sync_force_parallel =
3465__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3466 sync_force_parallel_show, sync_force_parallel_store);
3467
3468static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003469sync_speed_show(mddev_t *mddev, char *page)
3470{
3471 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003472 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3473 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003474 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003475 db = resync - mddev->resync_mark_cnt;
3476 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003477}
3478
NeilBrown80ca3a42006-07-10 04:44:18 -07003479static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003480
3481static ssize_t
3482sync_completed_show(mddev_t *mddev, char *page)
3483{
Andre Noll58c0fed2009-03-31 14:33:13 +11003484 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003485
3486 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003487 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003488 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003489 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003490
3491 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
Andre Noll58c0fed2009-03-31 14:33:13 +11003492 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003493}
3494
NeilBrown80ca3a42006-07-10 04:44:18 -07003495static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003496
NeilBrowne464eaf2006-03-27 01:18:14 -08003497static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003498min_sync_show(mddev_t *mddev, char *page)
3499{
3500 return sprintf(page, "%llu\n",
3501 (unsigned long long)mddev->resync_min);
3502}
3503static ssize_t
3504min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3505{
3506 unsigned long long min;
3507 if (strict_strtoull(buf, 10, &min))
3508 return -EINVAL;
3509 if (min > mddev->resync_max)
3510 return -EINVAL;
3511 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3512 return -EBUSY;
3513
3514 /* Must be a multiple of chunk_size */
3515 if (mddev->chunk_size) {
3516 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3517 return -EINVAL;
3518 }
3519 mddev->resync_min = min;
3520
3521 return len;
3522}
3523
3524static struct md_sysfs_entry md_min_sync =
3525__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3526
3527static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003528max_sync_show(mddev_t *mddev, char *page)
3529{
3530 if (mddev->resync_max == MaxSector)
3531 return sprintf(page, "max\n");
3532 else
3533 return sprintf(page, "%llu\n",
3534 (unsigned long long)mddev->resync_max);
3535}
3536static ssize_t
3537max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3538{
3539 if (strncmp(buf, "max", 3) == 0)
3540 mddev->resync_max = MaxSector;
3541 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003542 unsigned long long max;
3543 if (strict_strtoull(buf, 10, &max))
3544 return -EINVAL;
3545 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003546 return -EINVAL;
3547 if (max < mddev->resync_max &&
3548 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3549 return -EBUSY;
3550
3551 /* Must be a multiple of chunk_size */
3552 if (mddev->chunk_size) {
3553 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3554 return -EINVAL;
3555 }
3556 mddev->resync_max = max;
3557 }
3558 wake_up(&mddev->recovery_wait);
3559 return len;
3560}
3561
3562static struct md_sysfs_entry md_max_sync =
3563__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3564
3565static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003566suspend_lo_show(mddev_t *mddev, char *page)
3567{
3568 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3569}
3570
3571static ssize_t
3572suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3573{
3574 char *e;
3575 unsigned long long new = simple_strtoull(buf, &e, 10);
3576
3577 if (mddev->pers->quiesce == NULL)
3578 return -EINVAL;
3579 if (buf == e || (*e && *e != '\n'))
3580 return -EINVAL;
3581 if (new >= mddev->suspend_hi ||
3582 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3583 mddev->suspend_lo = new;
3584 mddev->pers->quiesce(mddev, 2);
3585 return len;
3586 } else
3587 return -EINVAL;
3588}
3589static struct md_sysfs_entry md_suspend_lo =
3590__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3591
3592
3593static ssize_t
3594suspend_hi_show(mddev_t *mddev, char *page)
3595{
3596 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3597}
3598
3599static ssize_t
3600suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3601{
3602 char *e;
3603 unsigned long long new = simple_strtoull(buf, &e, 10);
3604
3605 if (mddev->pers->quiesce == NULL)
3606 return -EINVAL;
3607 if (buf == e || (*e && *e != '\n'))
3608 return -EINVAL;
3609 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3610 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3611 mddev->suspend_hi = new;
3612 mddev->pers->quiesce(mddev, 1);
3613 mddev->pers->quiesce(mddev, 0);
3614 return len;
3615 } else
3616 return -EINVAL;
3617}
3618static struct md_sysfs_entry md_suspend_hi =
3619__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3620
NeilBrown08a02ec2007-05-09 02:35:38 -07003621static ssize_t
3622reshape_position_show(mddev_t *mddev, char *page)
3623{
3624 if (mddev->reshape_position != MaxSector)
3625 return sprintf(page, "%llu\n",
3626 (unsigned long long)mddev->reshape_position);
3627 strcpy(page, "none\n");
3628 return 5;
3629}
3630
3631static ssize_t
3632reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3633{
3634 char *e;
3635 unsigned long long new = simple_strtoull(buf, &e, 10);
3636 if (mddev->pers)
3637 return -EBUSY;
3638 if (buf == e || (*e && *e != '\n'))
3639 return -EINVAL;
3640 mddev->reshape_position = new;
3641 mddev->delta_disks = 0;
3642 mddev->new_level = mddev->level;
3643 mddev->new_layout = mddev->layout;
3644 mddev->new_chunk = mddev->chunk_size;
3645 return len;
3646}
3647
3648static struct md_sysfs_entry md_reshape_position =
3649__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3650 reshape_position_store);
3651
Dan Williamsb522adc2009-03-31 15:00:31 +11003652static ssize_t
3653array_size_show(mddev_t *mddev, char *page)
3654{
3655 if (mddev->external_size)
3656 return sprintf(page, "%llu\n",
3657 (unsigned long long)mddev->array_sectors/2);
3658 else
3659 return sprintf(page, "default\n");
3660}
3661
3662static ssize_t
3663array_size_store(mddev_t *mddev, const char *buf, size_t len)
3664{
3665 sector_t sectors;
3666
3667 if (strncmp(buf, "default", 7) == 0) {
3668 if (mddev->pers)
3669 sectors = mddev->pers->size(mddev, 0, 0);
3670 else
3671 sectors = mddev->array_sectors;
3672
3673 mddev->external_size = 0;
3674 } else {
3675 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3676 return -EINVAL;
3677 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3678 return -EINVAL;
3679
3680 mddev->external_size = 1;
3681 }
3682
3683 mddev->array_sectors = sectors;
3684 set_capacity(mddev->gendisk, mddev->array_sectors);
3685 if (mddev->pers) {
3686 struct block_device *bdev = bdget_disk(mddev->gendisk, 0);
3687
3688 if (bdev) {
3689 mutex_lock(&bdev->bd_inode->i_mutex);
3690 i_size_write(bdev->bd_inode,
3691 (loff_t)mddev->array_sectors << 9);
3692 mutex_unlock(&bdev->bd_inode->i_mutex);
3693 bdput(bdev);
3694 }
3695 }
3696
3697 return len;
3698}
3699
3700static struct md_sysfs_entry md_array_size =
3701__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3702 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003703
NeilBrowneae17012005-11-08 21:39:23 -08003704static struct attribute *md_default_attrs[] = {
3705 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003706 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003707 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003708 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003709 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003710 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003711 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003712 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003713 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003714 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003715 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003716 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003717 NULL,
3718};
3719
3720static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003721 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003722 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003723 &md_sync_min.attr,
3724 &md_sync_max.attr,
3725 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003726 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003727 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003728 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003729 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003730 &md_suspend_lo.attr,
3731 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003732 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003733 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003734 NULL,
3735};
NeilBrown411036f2005-11-08 21:39:40 -08003736static struct attribute_group md_redundancy_group = {
3737 .name = NULL,
3738 .attrs = md_redundancy_attrs,
3739};
3740
NeilBrowneae17012005-11-08 21:39:23 -08003741
3742static ssize_t
3743md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3744{
3745 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3746 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003747 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003748
3749 if (!entry->show)
3750 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003751 rv = mddev_lock(mddev);
3752 if (!rv) {
3753 rv = entry->show(mddev, page);
3754 mddev_unlock(mddev);
3755 }
NeilBrown96de1e62005-11-08 21:39:39 -08003756 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003757}
3758
3759static ssize_t
3760md_attr_store(struct kobject *kobj, struct attribute *attr,
3761 const char *page, size_t length)
3762{
3763 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3764 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003765 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003766
3767 if (!entry->store)
3768 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003769 if (!capable(CAP_SYS_ADMIN))
3770 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003771 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003772 if (mddev->hold_active == UNTIL_IOCTL)
3773 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003774 if (!rv) {
3775 rv = entry->store(mddev, page, length);
3776 mddev_unlock(mddev);
3777 }
NeilBrown96de1e62005-11-08 21:39:39 -08003778 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003779}
3780
3781static void md_free(struct kobject *ko)
3782{
3783 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003784
3785 if (mddev->sysfs_state)
3786 sysfs_put(mddev->sysfs_state);
3787
3788 if (mddev->gendisk) {
3789 del_gendisk(mddev->gendisk);
3790 put_disk(mddev->gendisk);
3791 }
3792 if (mddev->queue)
3793 blk_cleanup_queue(mddev->queue);
3794
NeilBrowneae17012005-11-08 21:39:23 -08003795 kfree(mddev);
3796}
3797
3798static struct sysfs_ops md_sysfs_ops = {
3799 .show = md_attr_show,
3800 .store = md_attr_store,
3801};
3802static struct kobj_type md_ktype = {
3803 .release = md_free,
3804 .sysfs_ops = &md_sysfs_ops,
3805 .default_attrs = md_default_attrs,
3806};
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808int mdp_major = 0;
3809
NeilBrownefeb53c2009-01-09 08:31:10 +11003810static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003812 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 mddev_t *mddev = mddev_find(dev);
3814 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003815 int partitioned;
3816 int shift;
3817 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003818 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003821 return -ENODEV;
3822
3823 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3824 shift = partitioned ? MdpMinorShift : 0;
3825 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
NeilBrownd3374822009-01-09 08:31:10 +11003827 /* wait for any previous instance if this device
3828 * to be completed removed (mddev_delayed_delete).
3829 */
3830 flush_scheduled_work();
3831
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003832 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003834 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003836 return -EEXIST;
3837 }
3838
3839 if (name) {
3840 /* Need to ensure that 'name' is not a duplicate.
3841 */
3842 mddev_t *mddev2;
3843 spin_lock(&all_mddevs_lock);
3844
3845 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3846 if (mddev2->gendisk &&
3847 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3848 spin_unlock(&all_mddevs_lock);
3849 return -EEXIST;
3850 }
3851 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
NeilBrown8b765392009-01-09 08:31:08 +11003853
3854 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3855 if (!mddev->queue) {
3856 mutex_unlock(&disks_mutex);
3857 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003858 return -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003859 }
NeilBrown409c57f2009-03-31 14:39:39 +11003860 mddev->queue->queuedata = mddev;
3861
NeilBrown8b765392009-01-09 08:31:08 +11003862 /* Can be unlocked because the queue is new: no concurrency */
3863 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3864
NeilBrown409c57f2009-03-31 14:39:39 +11003865 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 disk = alloc_disk(1 << shift);
3868 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003869 mutex_unlock(&disks_mutex);
NeilBrown8b765392009-01-09 08:31:08 +11003870 blk_cleanup_queue(mddev->queue);
3871 mddev->queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003873 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003875 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003877 if (name)
3878 strcpy(disk->disk_name, name);
3879 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003881 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 disk->fops = &md_fops;
3884 disk->private_data = mddev;
3885 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003886 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003887 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003888 * remove it now.
3889 */
3890 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 add_disk(disk);
3892 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003893 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3894 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003895 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003896 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003897 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3898 disk->disk_name);
NeilBrownb62b7592008-10-21 13:25:21 +11003899 else {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003900 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003901 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3902 }
NeilBrownd3374822009-01-09 08:31:10 +11003903 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003904 return 0;
3905}
3906
3907static struct kobject *md_probe(dev_t dev, int *part, void *data)
3908{
3909 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return NULL;
3911}
3912
NeilBrownefeb53c2009-01-09 08:31:10 +11003913static int add_named_array(const char *val, struct kernel_param *kp)
3914{
3915 /* val must be "md_*" where * is not all digits.
3916 * We allocate an array with a large free minor number, and
3917 * set the name to val. val must not already be an active name.
3918 */
3919 int len = strlen(val);
3920 char buf[DISK_NAME_LEN];
3921
3922 while (len && val[len-1] == '\n')
3923 len--;
3924 if (len >= DISK_NAME_LEN)
3925 return -E2BIG;
3926 strlcpy(buf, val, len+1);
3927 if (strncmp(buf, "md_", 3) != 0)
3928 return -EINVAL;
3929 return md_alloc(0, buf);
3930}
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932static void md_safemode_timeout(unsigned long data)
3933{
3934 mddev_t *mddev = (mddev_t *) data;
3935
Neil Brown0fd62b82008-06-28 08:31:36 +10003936 if (!atomic_read(&mddev->writes_pending)) {
3937 mddev->safemode = 1;
3938 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11003939 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 md_wakeup_thread(mddev->thread);
3942}
3943
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003944static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946static int do_md_run(mddev_t * mddev)
3947{
NeilBrown2604b702006-01-06 00:20:36 -08003948 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 int chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 mdk_rdev_t *rdev;
3951 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003952 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 char b[BDEVNAME_SIZE];
3954
NeilBrowna757e642005-04-16 15:26:42 -07003955 if (list_empty(&mddev->disks))
3956 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
3959 if (mddev->pers)
3960 return -EBUSY;
3961
3962 /*
3963 * Analyze all RAID superblock(s)
3964 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003965 if (!mddev->raid_disks) {
3966 if (!mddev->persistent)
3967 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003968 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972
NeilBrown2604b702006-01-06 00:20:36 -08003973 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 if (chunk_size > MAX_CHUNK_SIZE) {
3975 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3976 chunk_size, MAX_CHUNK_SIZE);
3977 return -EINVAL;
3978 }
3979 /*
NeilBrown4bbf3772008-10-13 11:55:12 +11003980 * chunk-size has to be a power of 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 */
3982 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003983 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 return -EINVAL;
3985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
3987 /* devices must have minimum size of one chunk */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003988 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003989 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 continue;
Andre Nolldd8ac332009-03-31 14:33:13 +11003991 if (rdev->sectors < chunk_size / 512) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 printk(KERN_WARNING
3993 "md: Dev %s smaller than chunk_size:"
Andre Nolldd8ac332009-03-31 14:33:13 +11003994 " %llu < %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 bdevname(rdev->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +11003996 (unsigned long long)rdev->sectors,
3997 chunk_size / 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return -EINVAL;
3999 }
4000 }
4001 }
4002
NeilBrownd9d166c2006-01-06 00:20:51 -08004003 if (mddev->level != LEVEL_NONE)
4004 request_module("md-level-%d", mddev->level);
4005 else if (mddev->clevel[0])
4006 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 /*
4009 * Drop all container device buffers, from now on
4010 * the only valid external interface is through the md
4011 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004013 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004014 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 continue;
4016 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004017 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004018
4019 /* perform some consistency tests on the device.
4020 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004021 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004022 */
Andre Noll0f420352008-07-11 22:02:23 +10004023 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004024 if (mddev->dev_sectors &&
4025 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004026 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004027 printk("md: %s: data overlaps metadata\n",
4028 mdname(mddev));
4029 return -EINVAL;
4030 }
4031 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004032 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004033 > rdev->data_offset) {
4034 printk("md: %s: metadata overlaps data\n",
4035 mdname(mddev));
4036 return -EINVAL;
4037 }
4038 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004039 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
4041
4042 md_probe(mddev->unit, NULL, NULL);
4043 disk = mddev->gendisk;
4044 if (!disk)
4045 return -ENOMEM;
4046
4047 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004048 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004049 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004051 if (mddev->level != LEVEL_NONE)
4052 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4053 mddev->level);
4054 else
4055 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4056 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return -EINVAL;
4058 }
NeilBrown2604b702006-01-06 00:20:36 -08004059 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004061 if (mddev->level != pers->level) {
4062 mddev->level = pers->level;
4063 mddev->new_level = pers->level;
4064 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004065 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004067 if (pers->level >= 4 && pers->level <= 6)
4068 /* Cannot support integrity (yet) */
4069 blk_integrity_unregister(mddev->gendisk);
4070
NeilBrownf6705572006-03-27 01:18:11 -08004071 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004072 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004073 /* This personality cannot handle reshaping... */
4074 mddev->pers = NULL;
4075 module_put(pers->owner);
4076 return -EINVAL;
4077 }
4078
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004079 if (pers->sync_request) {
4080 /* Warn if this is a potentially silly
4081 * configuration.
4082 */
4083 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4084 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004085 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004086
4087 list_for_each_entry(rdev, &mddev->disks, same_set)
4088 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004089 if (rdev < rdev2 &&
4090 rdev->bdev->bd_contains ==
4091 rdev2->bdev->bd_contains) {
4092 printk(KERN_WARNING
4093 "%s: WARNING: %s appears to be"
4094 " on the same physical disk as"
4095 " %s.\n",
4096 mdname(mddev),
4097 bdevname(rdev->bdev,b),
4098 bdevname(rdev2->bdev,b2));
4099 warned = 1;
4100 }
4101 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004102
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004103 if (warned)
4104 printk(KERN_WARNING
4105 "True protection against single-disk"
4106 " failure might be compromised.\n");
4107 }
4108
NeilBrown657390d2005-08-26 18:34:16 -07004109 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004110 /* may be over-ridden by personality */
4111 mddev->resync_max_sectors = mddev->dev_sectors;
4112
NeilBrowna9701a32005-11-08 21:39:34 -08004113 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004114 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
NeilBrownf91de922005-11-08 21:39:36 -08004116 if (start_readonly)
4117 mddev->ro = 2; /* read-only, but switch on first write */
4118
NeilBrownb15c2e52006-01-06 00:20:16 -08004119 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004120 if (err)
4121 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004122 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4123 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4124 " but 'external_size' not in effect?\n", __func__);
4125 printk(KERN_ERR
4126 "md: invalid array_size %llu > default size %llu\n",
4127 (unsigned long long)mddev->array_sectors / 2,
4128 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4129 err = -EINVAL;
4130 mddev->pers->stop(mddev);
4131 }
4132 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004133 err = bitmap_create(mddev);
4134 if (err) {
4135 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4136 mdname(mddev), err);
4137 mddev->pers->stop(mddev);
4138 }
4139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 module_put(mddev->pers->owner);
4142 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004143 bitmap_destroy(mddev);
4144 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004146 if (mddev->pers->sync_request) {
4147 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4148 printk(KERN_WARNING
4149 "md: cannot register extra attributes for %s\n",
4150 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004151 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004152 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004153 mddev->ro = 0;
4154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 atomic_set(&mddev->writes_pending,0);
4156 mddev->safemode = 0;
4157 mddev->safemode_timer.function = md_safemode_timeout;
4158 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004159 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004161
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004162 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004163 if (rdev->raid_disk >= 0) {
4164 char nm[20];
4165 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004166 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4167 printk("md: cannot register %s for %s\n",
4168 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
4171 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4172
NeilBrown850b2b422006-10-03 01:15:46 -07004173 if (mddev->flags)
4174 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Andre Nollf233ea52008-07-21 17:05:22 +10004176 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004178 /* If there is a partially-recovered drive we need to
4179 * start recovery here. If we leave it to md_check_recovery,
4180 * it will remove the drives and not do the right thing
4181 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004182 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004183 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004184 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004185 if (rdev->raid_disk >= 0 &&
4186 !test_bit(In_sync, &rdev->flags) &&
4187 !test_bit(Faulty, &rdev->flags))
4188 /* complete an interrupted recovery */
4189 spares++;
4190 if (spares && mddev->pers->sync_request) {
4191 mddev->recovery = 0;
4192 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4193 mddev->sync_thread = md_register_thread(md_do_sync,
4194 mddev,
4195 "%s_resync");
4196 if (!mddev->sync_thread) {
4197 printk(KERN_ERR "%s: could not start resync"
4198 " thread...\n",
4199 mdname(mddev));
4200 /* leave the spares where they are, it shouldn't hurt */
4201 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004202 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004203 }
4204 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004205 md_wakeup_thread(mddev->thread);
4206 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004207
Linus Torvalds44ce62942007-05-09 18:51:36 -07004208 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004209 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004210 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004211 if (mddev->sysfs_action)
4212 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004213 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004214 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 return 0;
4216}
4217
4218static int restart_array(mddev_t *mddev)
4219{
4220 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Andre Noll80fab1d2008-07-11 22:02:21 +10004222 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004224 return -ENXIO;
4225 if (!mddev->pers)
4226 return -EINVAL;
4227 if (!mddev->ro)
4228 return -EBUSY;
4229 mddev->safemode = 0;
4230 mddev->ro = 0;
4231 set_disk_ro(disk, 0);
4232 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4233 mdname(mddev));
4234 /* Kick recovery or resync if necessary */
4235 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4236 md_wakeup_thread(mddev->thread);
4237 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004238 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
NeilBrownacc55e22006-06-26 00:27:47 -07004242/* similar to deny_write_access, but accounts for our holding a reference
4243 * to the file ourselves */
4244static int deny_bitmap_write_access(struct file * file)
4245{
4246 struct inode *inode = file->f_mapping->host;
4247
4248 spin_lock(&inode->i_lock);
4249 if (atomic_read(&inode->i_writecount) > 1) {
4250 spin_unlock(&inode->i_lock);
4251 return -ETXTBSY;
4252 }
4253 atomic_set(&inode->i_writecount, -1);
4254 spin_unlock(&inode->i_lock);
4255
4256 return 0;
4257}
4258
4259static void restore_bitmap_write_access(struct file *file)
4260{
4261 struct inode *inode = file->f_mapping->host;
4262
4263 spin_lock(&inode->i_lock);
4264 atomic_set(&inode->i_writecount, 1);
4265 spin_unlock(&inode->i_lock);
4266}
4267
NeilBrown9e653b62006-06-26 00:27:58 -07004268/* mode:
4269 * 0 - completely stop and dis-assemble array
4270 * 1 - switch to readonly
4271 * 2 - stop but do not disassemble array
4272 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004273static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
4275 int err = 0;
4276 struct gendisk *disk = mddev->gendisk;
4277
NeilBrownf2ea68c2008-07-21 17:05:25 +10004278 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004279 printk("md: %s still in use.\n",mdname(mddev));
4280 return -EBUSY;
4281 }
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004286 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4288 md_unregister_thread(mddev->sync_thread);
4289 mddev->sync_thread = NULL;
4290 }
4291
4292 del_timer_sync(&mddev->safemode_timer);
4293
NeilBrown9e653b62006-06-26 00:27:58 -07004294 switch(mode) {
4295 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004297 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 goto out;
4299 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004300 break;
4301 case 0: /* disassemble */
4302 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004303 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004304 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (mddev->ro)
4306 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004307
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004309 mddev->queue->merge_bvec_fn = NULL;
4310 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004311 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown0c3573f2009-01-09 08:31:05 +11004312 if (mddev->pers->sync_request) {
NeilBrown411036f2005-11-08 21:39:40 -08004313 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
NeilBrown0c3573f2009-01-09 08:31:05 +11004314 if (mddev->sysfs_action)
4315 sysfs_put(mddev->sysfs_action);
4316 mddev->sysfs_action = NULL;
4317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 module_put(mddev->pers->owner);
4319 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004320 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004321 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004322
4323 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004324 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 if (mddev->ro)
4327 mddev->ro = 0;
4328 }
NeilBrown850b2b422006-10-03 01:15:46 -07004329 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 /* mark array as shutdown cleanly */
4331 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07004332 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 }
NeilBrown9e653b62006-06-26 00:27:58 -07004334 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004336 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 }
NeilBrown32a76272005-06-21 17:17:14 -07004338
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 /*
4340 * Free resources if final stop
4341 */
NeilBrown9e653b62006-06-26 00:27:58 -07004342 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004343 mdk_rdev_t *rdev;
NeilBrown0d4ca602006-12-10 02:20:44 -08004344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4346
NeilBrown978f9462006-02-02 14:28:05 -08004347 bitmap_destroy(mddev);
4348 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004349 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004350 fput(mddev->bitmap_file);
4351 mddev->bitmap_file = NULL;
4352 }
4353 mddev->bitmap_offset = 0;
4354
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004355 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004356 if (rdev->raid_disk >= 0) {
4357 char nm[20];
4358 sprintf(nm, "rd%d", rdev->raid_disk);
4359 sysfs_remove_link(&mddev->kobj, nm);
4360 }
4361
NeilBrown177a99b2008-02-06 01:39:56 -08004362 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004363 flush_scheduled_work();
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 export_array(mddev);
4366
Andre Nollf233ea52008-07-21 17:05:22 +10004367 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004368 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004369 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004370 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004371 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004372 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004373 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004374 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004375 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004376 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004377 mddev->level = LEVEL_NONE;
4378 mddev->clevel[0] = 0;
4379 mddev->flags = 0;
4380 mddev->ro = 0;
4381 mddev->metadata_type[0] = 0;
4382 mddev->chunk_size = 0;
4383 mddev->ctime = mddev->utime = 0;
4384 mddev->layout = 0;
4385 mddev->max_disks = 0;
4386 mddev->events = 0;
4387 mddev->delta_disks = 0;
4388 mddev->new_level = LEVEL_NONE;
4389 mddev->new_layout = 0;
4390 mddev->new_chunk = 0;
4391 mddev->curr_resync = 0;
4392 mddev->resync_mismatches = 0;
4393 mddev->suspend_lo = mddev->suspend_hi = 0;
4394 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4395 mddev->recovery = 0;
4396 mddev->in_sync = 0;
4397 mddev->changed = 0;
4398 mddev->degraded = 0;
4399 mddev->barriers_work = 0;
4400 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004401 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004402 if (mddev->hold_active == UNTIL_STOP)
4403 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004404
NeilBrowna8a55c32006-06-26 00:27:39 -07004405 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4407 mdname(mddev));
4408 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004409 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004410 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004411 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412out:
4413 return err;
4414}
4415
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004416#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417static void autorun_array(mddev_t *mddev)
4418{
4419 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 int err;
4421
NeilBrowna757e642005-04-16 15:26:42 -07004422 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 printk(KERN_INFO "md: running: ");
4426
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004427 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 char b[BDEVNAME_SIZE];
4429 printk("<%s>", bdevname(rdev->bdev,b));
4430 }
4431 printk("\n");
4432
NeilBrownd710e132008-10-13 11:55:12 +11004433 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 if (err) {
4435 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004436 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 }
4438}
4439
4440/*
4441 * lets try to run arrays based on all disks that have arrived
4442 * until now. (those are in pending_raid_disks)
4443 *
4444 * the method: pick the first pending disk, collect all disks with
4445 * the same UUID, remove all from the pending list and put them into
4446 * the 'same_array' list. Then order this list based on superblock
4447 * update time (freshest comes first), kick out 'old' disks and
4448 * compare superblocks. If everything's fine then run it.
4449 *
4450 * If "unit" is allocated, then bump its reference count
4451 */
4452static void autorun_devices(int part)
4453{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004454 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 mddev_t *mddev;
4456 char b[BDEVNAME_SIZE];
4457
4458 printk(KERN_INFO "md: autorun ...\n");
4459 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004460 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004462 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 rdev0 = list_entry(pending_raid_disks.next,
4464 mdk_rdev_t, same_set);
4465
4466 printk(KERN_INFO "md: considering %s ...\n",
4467 bdevname(rdev0->bdev,b));
4468 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004469 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 if (super_90_load(rdev, rdev0, 0) >= 0) {
4471 printk(KERN_INFO "md: adding %s ...\n",
4472 bdevname(rdev->bdev,b));
4473 list_move(&rdev->same_set, &candidates);
4474 }
4475 /*
4476 * now we have a set of devices, with all of them having
4477 * mostly sane superblocks. It's time to allocate the
4478 * mddev.
4479 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004480 if (part) {
4481 dev = MKDEV(mdp_major,
4482 rdev0->preferred_minor << MdpMinorShift);
4483 unit = MINOR(dev) >> MdpMinorShift;
4484 } else {
4485 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4486 unit = MINOR(dev);
4487 }
4488 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4490 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4491 break;
4492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
4494 md_probe(dev, NULL, NULL);
4495 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004496 if (!mddev || !mddev->gendisk) {
4497 if (mddev)
4498 mddev_put(mddev);
4499 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 "md: cannot allocate memory for md drive.\n");
4501 break;
4502 }
4503 if (mddev_lock(mddev))
4504 printk(KERN_WARNING "md: %s locked, cannot run\n",
4505 mdname(mddev));
4506 else if (mddev->raid_disks || mddev->major_version
4507 || !list_empty(&mddev->disks)) {
4508 printk(KERN_WARNING
4509 "md: %s already running, cannot run %s\n",
4510 mdname(mddev), bdevname(rdev0->bdev,b));
4511 mddev_unlock(mddev);
4512 } else {
4513 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004514 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004515 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 list_del_init(&rdev->same_set);
4517 if (bind_rdev_to_array(rdev, mddev))
4518 export_rdev(rdev);
4519 }
4520 autorun_array(mddev);
4521 mddev_unlock(mddev);
4522 }
4523 /* on success, candidates will be empty, on error
4524 * it won't...
4525 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004526 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004527 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 mddev_put(mddev);
4531 }
4532 printk(KERN_INFO "md: ... autorun DONE.\n");
4533}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004534#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536static int get_version(void __user * arg)
4537{
4538 mdu_version_t ver;
4539
4540 ver.major = MD_MAJOR_VERSION;
4541 ver.minor = MD_MINOR_VERSION;
4542 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4543
4544 if (copy_to_user(arg, &ver, sizeof(ver)))
4545 return -EFAULT;
4546
4547 return 0;
4548}
4549
4550static int get_array_info(mddev_t * mddev, void __user * arg)
4551{
4552 mdu_array_info_t info;
4553 int nr,working,active,failed,spare;
4554 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
4556 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004557 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004559 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 failed++;
4561 else {
4562 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004563 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 active++;
4565 else
4566 spare++;
4567 }
4568 }
4569
4570 info.major_version = mddev->major_version;
4571 info.minor_version = mddev->minor_version;
4572 info.patch_version = MD_PATCHLEVEL_VERSION;
4573 info.ctime = mddev->ctime;
4574 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004575 info.size = mddev->dev_sectors / 2;
4576 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004577 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 info.nr_disks = nr;
4579 info.raid_disks = mddev->raid_disks;
4580 info.md_minor = mddev->md_minor;
4581 info.not_persistent= !mddev->persistent;
4582
4583 info.utime = mddev->utime;
4584 info.state = 0;
4585 if (mddev->in_sync)
4586 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004587 if (mddev->bitmap && mddev->bitmap_offset)
4588 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 info.active_disks = active;
4590 info.working_disks = working;
4591 info.failed_disks = failed;
4592 info.spare_disks = spare;
4593
4594 info.layout = mddev->layout;
4595 info.chunk_size = mddev->chunk_size;
4596
4597 if (copy_to_user(arg, &info, sizeof(info)))
4598 return -EFAULT;
4599
4600 return 0;
4601}
4602
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004603static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004604{
4605 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4606 char *ptr, *buf = NULL;
4607 int err = -ENOMEM;
4608
Dan Williamsb5470dc2008-06-27 21:44:04 -07004609 if (md_allow_write(mddev))
4610 file = kmalloc(sizeof(*file), GFP_NOIO);
4611 else
4612 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004613
NeilBrown32a76272005-06-21 17:17:14 -07004614 if (!file)
4615 goto out;
4616
4617 /* bitmap disabled, zero the first byte and copy out */
4618 if (!mddev->bitmap || !mddev->bitmap->file) {
4619 file->pathname[0] = '\0';
4620 goto copy_out;
4621 }
4622
4623 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4624 if (!buf)
4625 goto out;
4626
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004627 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4628 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004629 goto out;
4630
4631 strcpy(file->pathname, ptr);
4632
4633copy_out:
4634 err = 0;
4635 if (copy_to_user(arg, file, sizeof(*file)))
4636 err = -EFAULT;
4637out:
4638 kfree(buf);
4639 kfree(file);
4640 return err;
4641}
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643static int get_disk_info(mddev_t * mddev, void __user * arg)
4644{
4645 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 mdk_rdev_t *rdev;
4647
4648 if (copy_from_user(&info, arg, sizeof(info)))
4649 return -EFAULT;
4650
Andre Noll26ef3792008-07-11 22:02:21 +10004651 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 if (rdev) {
4653 info.major = MAJOR(rdev->bdev->bd_dev);
4654 info.minor = MINOR(rdev->bdev->bd_dev);
4655 info.raid_disk = rdev->raid_disk;
4656 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004657 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004659 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 info.state |= (1<<MD_DISK_ACTIVE);
4661 info.state |= (1<<MD_DISK_SYNC);
4662 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004663 if (test_bit(WriteMostly, &rdev->flags))
4664 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 } else {
4666 info.major = info.minor = 0;
4667 info.raid_disk = -1;
4668 info.state = (1<<MD_DISK_REMOVED);
4669 }
4670
4671 if (copy_to_user(arg, &info, sizeof(info)))
4672 return -EFAULT;
4673
4674 return 0;
4675}
4676
4677static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4678{
4679 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4680 mdk_rdev_t *rdev;
4681 dev_t dev = MKDEV(info->major,info->minor);
4682
4683 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4684 return -EOVERFLOW;
4685
4686 if (!mddev->raid_disks) {
4687 int err;
4688 /* expecting a device which has a superblock */
4689 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4690 if (IS_ERR(rdev)) {
4691 printk(KERN_WARNING
4692 "md: md_import_device returned %ld\n",
4693 PTR_ERR(rdev));
4694 return PTR_ERR(rdev);
4695 }
4696 if (!list_empty(&mddev->disks)) {
4697 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4698 mdk_rdev_t, same_set);
4699 int err = super_types[mddev->major_version]
4700 .load_super(rdev, rdev0, mddev->minor_version);
4701 if (err < 0) {
4702 printk(KERN_WARNING
4703 "md: %s has different UUID to %s\n",
4704 bdevname(rdev->bdev,b),
4705 bdevname(rdev0->bdev,b2));
4706 export_rdev(rdev);
4707 return -EINVAL;
4708 }
4709 }
4710 err = bind_rdev_to_array(rdev, mddev);
4711 if (err)
4712 export_rdev(rdev);
4713 return err;
4714 }
4715
4716 /*
4717 * add_new_disk can be used once the array is assembled
4718 * to add "hot spares". They must already have a superblock
4719 * written
4720 */
4721 if (mddev->pers) {
4722 int err;
4723 if (!mddev->pers->hot_add_disk) {
4724 printk(KERN_WARNING
4725 "%s: personality does not support diskops!\n",
4726 mdname(mddev));
4727 return -EINVAL;
4728 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004729 if (mddev->persistent)
4730 rdev = md_import_device(dev, mddev->major_version,
4731 mddev->minor_version);
4732 else
4733 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 if (IS_ERR(rdev)) {
4735 printk(KERN_WARNING
4736 "md: md_import_device returned %ld\n",
4737 PTR_ERR(rdev));
4738 return PTR_ERR(rdev);
4739 }
NeilBrown41158c72005-06-21 17:17:25 -07004740 /* set save_raid_disk if appropriate */
4741 if (!mddev->persistent) {
4742 if (info->state & (1<<MD_DISK_SYNC) &&
4743 info->raid_disk < mddev->raid_disks)
4744 rdev->raid_disk = info->raid_disk;
4745 else
4746 rdev->raid_disk = -1;
4747 } else
4748 super_types[mddev->major_version].
4749 validate_super(mddev, rdev);
4750 rdev->saved_raid_disk = rdev->raid_disk;
4751
NeilBrownb2d444d2005-11-08 21:39:31 -08004752 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004753 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4754 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004755 else
4756 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 rdev->raid_disk = -1;
4759 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004760 if (!err && !mddev->pers->hot_remove_disk) {
4761 /* If there is hot_add_disk but no hot_remove_disk
4762 * then added disks for geometry changes,
4763 * and should be added immediately.
4764 */
4765 super_types[mddev->major_version].
4766 validate_super(mddev, rdev);
4767 err = mddev->pers->hot_add_disk(mddev, rdev);
4768 if (err)
4769 unbind_rdev_from_array(rdev);
4770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 if (err)
4772 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004773 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004774 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004775
NeilBrown17571282006-12-10 02:20:52 -08004776 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004777 if (mddev->degraded)
4778 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004779 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004780 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 return err;
4782 }
4783
4784 /* otherwise, add_new_disk is only allowed
4785 * for major_version==0 superblocks
4786 */
4787 if (mddev->major_version != 0) {
4788 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4789 mdname(mddev));
4790 return -EINVAL;
4791 }
4792
4793 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4794 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004795 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 if (IS_ERR(rdev)) {
4797 printk(KERN_WARNING
4798 "md: error, md_import_device() returned %ld\n",
4799 PTR_ERR(rdev));
4800 return PTR_ERR(rdev);
4801 }
4802 rdev->desc_nr = info->number;
4803 if (info->raid_disk < mddev->raid_disks)
4804 rdev->raid_disk = info->raid_disk;
4805 else
4806 rdev->raid_disk = -1;
4807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004809 if (info->state & (1<<MD_DISK_SYNC))
4810 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004812 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4813 set_bit(WriteMostly, &rdev->flags);
4814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 if (!mddev->persistent) {
4816 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004817 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 } else
Andre Noll0f420352008-07-11 22:02:23 +10004819 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolldd8ac332009-03-31 14:33:13 +11004820 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
NeilBrown2bf071b2006-01-06 00:20:55 -08004822 err = bind_rdev_to_array(rdev, mddev);
4823 if (err) {
4824 export_rdev(rdev);
4825 return err;
4826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 }
4828
4829 return 0;
4830}
4831
4832static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4833{
4834 char b[BDEVNAME_SIZE];
4835 mdk_rdev_t *rdev;
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 rdev = find_rdev(mddev, dev);
4838 if (!rdev)
4839 return -ENXIO;
4840
4841 if (rdev->raid_disk >= 0)
4842 goto busy;
4843
4844 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07004845 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004846 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 return 0;
4849busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004850 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 bdevname(rdev->bdev,b), mdname(mddev));
4852 return -EBUSY;
4853}
4854
4855static int hot_add_disk(mddev_t * mddev, dev_t dev)
4856{
4857 char b[BDEVNAME_SIZE];
4858 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 mdk_rdev_t *rdev;
4860
4861 if (!mddev->pers)
4862 return -ENODEV;
4863
4864 if (mddev->major_version != 0) {
4865 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4866 " version-0 superblocks.\n",
4867 mdname(mddev));
4868 return -EINVAL;
4869 }
4870 if (!mddev->pers->hot_add_disk) {
4871 printk(KERN_WARNING
4872 "%s: personality does not support diskops!\n",
4873 mdname(mddev));
4874 return -EINVAL;
4875 }
4876
NeilBrownd710e132008-10-13 11:55:12 +11004877 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 if (IS_ERR(rdev)) {
4879 printk(KERN_WARNING
4880 "md: error, md_import_device() returned %ld\n",
4881 PTR_ERR(rdev));
4882 return -EINVAL;
4883 }
4884
4885 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004886 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 else
Andre Noll0f420352008-07-11 22:02:23 +10004888 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Andre Nolldd8ac332009-03-31 14:33:13 +11004890 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
NeilBrownb2d444d2005-11-08 21:39:31 -08004892 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 printk(KERN_WARNING
4894 "md: can not hot-add faulty %s disk to %s!\n",
4895 bdevname(rdev->bdev,b), mdname(mddev));
4896 err = -EINVAL;
4897 goto abort_export;
4898 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004899 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004901 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004902 err = bind_rdev_to_array(rdev, mddev);
4903 if (err)
4904 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
4906 /*
4907 * The rest should better be atomic, we can have disk failures
4908 * noticed in interrupt contexts ...
4909 */
4910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 rdev->raid_disk = -1;
4912
NeilBrown850b2b422006-10-03 01:15:46 -07004913 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 /*
4916 * Kick recovery, maybe this spare has to be added to the
4917 * array immediately.
4918 */
4919 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4920 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004921 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 return 0;
4923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924abort_export:
4925 export_rdev(rdev);
4926 return err;
4927}
4928
NeilBrown32a76272005-06-21 17:17:14 -07004929static int set_bitmap_file(mddev_t *mddev, int fd)
4930{
4931 int err;
4932
NeilBrown36fa3062005-09-09 16:23:45 -07004933 if (mddev->pers) {
4934 if (!mddev->pers->quiesce)
4935 return -EBUSY;
4936 if (mddev->recovery || mddev->sync_thread)
4937 return -EBUSY;
4938 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004939 }
4940
NeilBrown36fa3062005-09-09 16:23:45 -07004941
4942 if (fd >= 0) {
4943 if (mddev->bitmap)
4944 return -EEXIST; /* cannot add when bitmap is present */
4945 mddev->bitmap_file = fget(fd);
4946
4947 if (mddev->bitmap_file == NULL) {
4948 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4949 mdname(mddev));
4950 return -EBADF;
4951 }
4952
4953 err = deny_bitmap_write_access(mddev->bitmap_file);
4954 if (err) {
4955 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4956 mdname(mddev));
4957 fput(mddev->bitmap_file);
4958 mddev->bitmap_file = NULL;
4959 return err;
4960 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004961 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004962 } else if (mddev->bitmap == NULL)
4963 return -ENOENT; /* cannot remove what isn't there */
4964 err = 0;
4965 if (mddev->pers) {
4966 mddev->pers->quiesce(mddev, 1);
4967 if (fd >= 0)
4968 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004969 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004970 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004971 fd = -1; /* make sure to put the file */
4972 }
NeilBrown36fa3062005-09-09 16:23:45 -07004973 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004974 }
4975 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004976 if (mddev->bitmap_file) {
4977 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004978 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004979 }
NeilBrown36fa3062005-09-09 16:23:45 -07004980 mddev->bitmap_file = NULL;
4981 }
4982
NeilBrown32a76272005-06-21 17:17:14 -07004983 return err;
4984}
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986/*
4987 * set_array_info is used two different ways
4988 * The original usage is when creating a new array.
4989 * In this usage, raid_disks is > 0 and it together with
4990 * level, size, not_persistent,layout,chunksize determine the
4991 * shape of the array.
4992 * This will always create an array with a type-0.90.0 superblock.
4993 * The newer usage is when assembling an array.
4994 * In this case raid_disks will be 0, and the major_version field is
4995 * use to determine which style super-blocks are to be found on the devices.
4996 * The minor and patch _version numbers are also kept incase the
4997 * super_block handler wishes to interpret them.
4998 */
4999static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5000{
5001
5002 if (info->raid_disks == 0) {
5003 /* just setting version number for superblock loading */
5004 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005005 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 super_types[info->major_version].name == NULL) {
5007 /* maybe try to auto-load a module? */
5008 printk(KERN_INFO
5009 "md: superblock version %d not known\n",
5010 info->major_version);
5011 return -EINVAL;
5012 }
5013 mddev->major_version = info->major_version;
5014 mddev->minor_version = info->minor_version;
5015 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005016 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 return 0;
5018 }
5019 mddev->major_version = MD_MAJOR_VERSION;
5020 mddev->minor_version = MD_MINOR_VERSION;
5021 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5022 mddev->ctime = get_seconds();
5023
5024 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005025 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005026 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 mddev->raid_disks = info->raid_disks;
5028 /* don't set md_minor, it is determined by which /dev/md* was
5029 * openned
5030 */
5031 if (info->state & (1<<MD_SB_CLEAN))
5032 mddev->recovery_cp = MaxSector;
5033 else
5034 mddev->recovery_cp = 0;
5035 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005036 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
5038 mddev->layout = info->layout;
5039 mddev->chunk_size = info->chunk_size;
5040
5041 mddev->max_disks = MD_SB_DISKS;
5042
NeilBrowne6910632008-02-06 01:39:51 -08005043 if (mddev->persistent)
5044 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005045 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
NeilBrownb2a27032005-11-28 13:44:12 -08005047 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5048 mddev->bitmap_offset = 0;
5049
NeilBrownf6705572006-03-27 01:18:11 -08005050 mddev->reshape_position = MaxSector;
5051
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 /*
5053 * Generate a 128 bit UUID
5054 */
5055 get_random_bytes(mddev->uuid, 16);
5056
NeilBrownf6705572006-03-27 01:18:11 -08005057 mddev->new_level = mddev->level;
5058 mddev->new_chunk = mddev->chunk_size;
5059 mddev->new_layout = mddev->layout;
5060 mddev->delta_disks = 0;
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 return 0;
5063}
5064
Dan Williams1f403622009-03-31 14:59:03 +11005065void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5066{
Dan Williamsb522adc2009-03-31 15:00:31 +11005067 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5068
5069 if (mddev->external_size)
5070 return;
5071
Dan Williams1f403622009-03-31 14:59:03 +11005072 mddev->array_sectors = array_sectors;
5073}
5074EXPORT_SYMBOL(md_set_array_sectors);
5075
Andre Nolld71f9f82008-07-11 22:02:22 +10005076static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005077{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005078 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005079 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005080 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005081
5082 if (mddev->pers->resize == NULL)
5083 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005084 /* The "num_sectors" is the number of sectors of each device that
5085 * is used. This can only make sense for arrays with redundancy.
5086 * linear and raid0 always use whatever space is available. We can only
5087 * consider changing this number if no resync or reconstruction is
5088 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005089 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005090 * of each device. If num_sectors is zero, we find the largest size
5091 * that fits.
5092
NeilBrowna35b0d62006-01-06 00:20:49 -08005093 */
5094 if (mddev->sync_thread)
5095 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005096 if (mddev->bitmap)
5097 /* Sorry, cannot grow a bitmap yet, just remove it,
5098 * grow, and re-add.
5099 */
5100 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005101 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005102 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005103
Andre Nolld71f9f82008-07-11 22:02:22 +10005104 if (fit && (num_sectors == 0 || num_sectors > avail))
5105 num_sectors = avail;
5106 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005107 return -ENOSPC;
5108 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005109 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08005110 if (!rv) {
5111 struct block_device *bdev;
5112
5113 bdev = bdget_disk(mddev->gendisk, 0);
5114 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005115 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10005116 i_size_write(bdev->bd_inode,
5117 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005118 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08005119 bdput(bdev);
5120 }
5121 }
5122 return rv;
5123}
5124
NeilBrownda943b992006-01-06 00:20:54 -08005125static int update_raid_disks(mddev_t *mddev, int raid_disks)
5126{
5127 int rv;
5128 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005129 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005130 return -EINVAL;
5131 if (raid_disks <= 0 ||
5132 raid_disks >= mddev->max_disks)
5133 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005134 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005135 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005136 mddev->delta_disks = raid_disks - mddev->raid_disks;
5137
5138 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005139 return rv;
5140}
5141
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143/*
5144 * update_array_info is used to change the configuration of an
5145 * on-line array.
5146 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5147 * fields in the info are checked against the array.
5148 * Any differences that cannot be handled will cause an error.
5149 * Normally, only one change can be managed at a time.
5150 */
5151static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5152{
5153 int rv = 0;
5154 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005155 int state = 0;
5156
5157 /* calculate expected state,ignoring low bits */
5158 if (mddev->bitmap && mddev->bitmap_offset)
5159 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 if (mddev->major_version != info->major_version ||
5162 mddev->minor_version != info->minor_version ||
5163/* mddev->patch_version != info->patch_version || */
5164 mddev->ctime != info->ctime ||
5165 mddev->level != info->level ||
5166/* mddev->layout != info->layout || */
5167 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07005168 mddev->chunk_size != info->chunk_size ||
5169 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5170 ((state^info->state) & 0xfffffe00)
5171 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 return -EINVAL;
5173 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005174 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5175 cnt++;
5176 if (mddev->raid_disks != info->raid_disks)
5177 cnt++;
5178 if (mddev->layout != info->layout)
5179 cnt++;
5180 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5181 cnt++;
5182 if (cnt == 0)
5183 return 0;
5184 if (cnt > 1)
5185 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
5187 if (mddev->layout != info->layout) {
5188 /* Change layout
5189 * we don't need to do anything at the md level, the
5190 * personality will take care of it all.
5191 */
5192 if (mddev->pers->reconfig == NULL)
5193 return -EINVAL;
5194 else
5195 return mddev->pers->reconfig(mddev, info->layout, -1);
5196 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005197 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005198 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
NeilBrownda943b992006-01-06 00:20:54 -08005200 if (mddev->raid_disks != info->raid_disks)
5201 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
NeilBrown36fa3062005-09-09 16:23:45 -07005203 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5204 if (mddev->pers->quiesce == NULL)
5205 return -EINVAL;
5206 if (mddev->recovery || mddev->sync_thread)
5207 return -EBUSY;
5208 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5209 /* add the bitmap */
5210 if (mddev->bitmap)
5211 return -EEXIST;
5212 if (mddev->default_bitmap_offset == 0)
5213 return -EINVAL;
5214 mddev->bitmap_offset = mddev->default_bitmap_offset;
5215 mddev->pers->quiesce(mddev, 1);
5216 rv = bitmap_create(mddev);
5217 if (rv)
5218 bitmap_destroy(mddev);
5219 mddev->pers->quiesce(mddev, 0);
5220 } else {
5221 /* remove the bitmap */
5222 if (!mddev->bitmap)
5223 return -ENOENT;
5224 if (mddev->bitmap->file)
5225 return -EINVAL;
5226 mddev->pers->quiesce(mddev, 1);
5227 bitmap_destroy(mddev);
5228 mddev->pers->quiesce(mddev, 0);
5229 mddev->bitmap_offset = 0;
5230 }
5231 }
NeilBrown850b2b422006-10-03 01:15:46 -07005232 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 return rv;
5234}
5235
5236static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5237{
5238 mdk_rdev_t *rdev;
5239
5240 if (mddev->pers == NULL)
5241 return -ENODEV;
5242
5243 rdev = find_rdev(mddev, dev);
5244 if (!rdev)
5245 return -ENODEV;
5246
5247 md_error(mddev, rdev);
5248 return 0;
5249}
5250
Andre Noll2f9618c2008-04-25 18:57:58 +02005251/*
5252 * We have a problem here : there is no easy way to give a CHS
5253 * virtual geometry. We currently pretend that we have a 2 heads
5254 * 4 sectors (with a BIG number of cylinders...). This drives
5255 * dosfs just mad... ;-)
5256 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005257static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5258{
5259 mddev_t *mddev = bdev->bd_disk->private_data;
5260
5261 geo->heads = 2;
5262 geo->sectors = 4;
5263 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5264 return 0;
5265}
5266
Al Viroa39907f2008-03-02 10:31:15 -05005267static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 unsigned int cmd, unsigned long arg)
5269{
5270 int err = 0;
5271 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 mddev_t *mddev = NULL;
5273
5274 if (!capable(CAP_SYS_ADMIN))
5275 return -EACCES;
5276
5277 /*
5278 * Commands dealing with the RAID driver but not any
5279 * particular array:
5280 */
5281 switch (cmd)
5282 {
5283 case RAID_VERSION:
5284 err = get_version(argp);
5285 goto done;
5286
5287 case PRINT_RAID_DEBUG:
5288 err = 0;
5289 md_print_devices();
5290 goto done;
5291
5292#ifndef MODULE
5293 case RAID_AUTORUN:
5294 err = 0;
5295 autostart_arrays(arg);
5296 goto done;
5297#endif
5298 default:;
5299 }
5300
5301 /*
5302 * Commands creating/starting a new array:
5303 */
5304
Al Viroa39907f2008-03-02 10:31:15 -05005305 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
5307 if (!mddev) {
5308 BUG();
5309 goto abort;
5310 }
5311
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 err = mddev_lock(mddev);
5313 if (err) {
5314 printk(KERN_INFO
5315 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5316 err, cmd);
5317 goto abort;
5318 }
5319
5320 switch (cmd)
5321 {
5322 case SET_ARRAY_INFO:
5323 {
5324 mdu_array_info_t info;
5325 if (!arg)
5326 memset(&info, 0, sizeof(info));
5327 else if (copy_from_user(&info, argp, sizeof(info))) {
5328 err = -EFAULT;
5329 goto abort_unlock;
5330 }
5331 if (mddev->pers) {
5332 err = update_array_info(mddev, &info);
5333 if (err) {
5334 printk(KERN_WARNING "md: couldn't update"
5335 " array info. %d\n", err);
5336 goto abort_unlock;
5337 }
5338 goto done_unlock;
5339 }
5340 if (!list_empty(&mddev->disks)) {
5341 printk(KERN_WARNING
5342 "md: array %s already has disks!\n",
5343 mdname(mddev));
5344 err = -EBUSY;
5345 goto abort_unlock;
5346 }
5347 if (mddev->raid_disks) {
5348 printk(KERN_WARNING
5349 "md: array %s already initialised!\n",
5350 mdname(mddev));
5351 err = -EBUSY;
5352 goto abort_unlock;
5353 }
5354 err = set_array_info(mddev, &info);
5355 if (err) {
5356 printk(KERN_WARNING "md: couldn't set"
5357 " array info. %d\n", err);
5358 goto abort_unlock;
5359 }
5360 }
5361 goto done_unlock;
5362
5363 default:;
5364 }
5365
5366 /*
5367 * Commands querying/configuring an existing array:
5368 */
NeilBrown32a76272005-06-21 17:17:14 -07005369 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005370 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005371 if ((!mddev->raid_disks && !mddev->external)
5372 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5373 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5374 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 err = -ENODEV;
5376 goto abort_unlock;
5377 }
5378
5379 /*
5380 * Commands even a read-only array can execute:
5381 */
5382 switch (cmd)
5383 {
5384 case GET_ARRAY_INFO:
5385 err = get_array_info(mddev, argp);
5386 goto done_unlock;
5387
NeilBrown32a76272005-06-21 17:17:14 -07005388 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005389 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005390 goto done_unlock;
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 case GET_DISK_INFO:
5393 err = get_disk_info(mddev, argp);
5394 goto done_unlock;
5395
5396 case RESTART_ARRAY_RW:
5397 err = restart_array(mddev);
5398 goto done_unlock;
5399
5400 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005401 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 goto done_unlock;
5403
5404 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005405 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 goto done_unlock;
5407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 }
5409
5410 /*
5411 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005412 * superblock, so we do not allow them on read-only arrays.
5413 * However non-MD ioctls (e.g. get-size) will still come through
5414 * here and hit the 'default' below, so only disallow
5415 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005417 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005418 if (mddev->ro == 2) {
5419 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005420 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005421 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5422 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005423 } else {
5424 err = -EROFS;
5425 goto abort_unlock;
5426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428
5429 switch (cmd)
5430 {
5431 case ADD_NEW_DISK:
5432 {
5433 mdu_disk_info_t info;
5434 if (copy_from_user(&info, argp, sizeof(info)))
5435 err = -EFAULT;
5436 else
5437 err = add_new_disk(mddev, &info);
5438 goto done_unlock;
5439 }
5440
5441 case HOT_REMOVE_DISK:
5442 err = hot_remove_disk(mddev, new_decode_dev(arg));
5443 goto done_unlock;
5444
5445 case HOT_ADD_DISK:
5446 err = hot_add_disk(mddev, new_decode_dev(arg));
5447 goto done_unlock;
5448
5449 case SET_DISK_FAULTY:
5450 err = set_disk_faulty(mddev, new_decode_dev(arg));
5451 goto done_unlock;
5452
5453 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005454 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 goto done_unlock;
5456
NeilBrown32a76272005-06-21 17:17:14 -07005457 case SET_BITMAP_FILE:
5458 err = set_bitmap_file(mddev, (int)arg);
5459 goto done_unlock;
5460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 err = -EINVAL;
5463 goto abort_unlock;
5464 }
5465
5466done_unlock:
5467abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005468 if (mddev->hold_active == UNTIL_IOCTL &&
5469 err != -EINVAL)
5470 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 mddev_unlock(mddev);
5472
5473 return err;
5474done:
5475 if (err)
5476 MD_BUG();
5477abort:
5478 return err;
5479}
5480
Al Viroa39907f2008-03-02 10:31:15 -05005481static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
5483 /*
5484 * Succeed if we can lock the mddev, which confirms that
5485 * it isn't being stopped right now.
5486 */
NeilBrownd3374822009-01-09 08:31:10 +11005487 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 int err;
5489
NeilBrownd3374822009-01-09 08:31:10 +11005490 if (mddev->gendisk != bdev->bd_disk) {
5491 /* we are racing with mddev_put which is discarding this
5492 * bd_disk.
5493 */
5494 mddev_put(mddev);
5495 /* Wait until bdev->bd_disk is definitely gone */
5496 flush_scheduled_work();
5497 /* Then retry the open from the top */
5498 return -ERESTARTSYS;
5499 }
5500 BUG_ON(mddev != bdev->bd_disk->private_data);
5501
NeilBrownd63a5a72006-12-08 02:36:17 -08005502 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 goto out;
5504
5505 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005506 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 mddev_unlock(mddev);
5508
Al Viroa39907f2008-03-02 10:31:15 -05005509 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 out:
5511 return err;
5512}
5513
Al Viroa39907f2008-03-02 10:31:15 -05005514static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
Al Viroa39907f2008-03-02 10:31:15 -05005516 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005518 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005519 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 mddev_put(mddev);
5521
5522 return 0;
5523}
5524
Linus Torvalds44ce62942007-05-09 18:51:36 -07005525static int md_media_changed(struct gendisk *disk)
5526{
5527 mddev_t *mddev = disk->private_data;
5528
5529 return mddev->changed;
5530}
5531
5532static int md_revalidate(struct gendisk *disk)
5533{
5534 mddev_t *mddev = disk->private_data;
5535
5536 mddev->changed = 0;
5537 return 0;
5538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539static struct block_device_operations md_fops =
5540{
5541 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005542 .open = md_open,
5543 .release = md_release,
5544 .locked_ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005545 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005546 .media_changed = md_media_changed,
5547 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548};
5549
Adrian Bunk75c96f82005-05-05 16:16:09 -07005550static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551{
5552 mdk_thread_t *thread = arg;
5553
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 /*
5555 * md_thread is a 'system-thread', it's priority should be very
5556 * high. We avoid resource deadlocks individually in each
5557 * raid personality. (RAID5 does preallocation) We also use RR and
5558 * the very same RT priority as kswapd, thus we will never get
5559 * into a priority inversion deadlock.
5560 *
5561 * we definitely have to have equal or higher priority than
5562 * bdflush, otherwise bdflush will deadlock if there are too
5563 * many dirty RAID5 blocks.
5564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
NeilBrown6985c432005-10-19 21:23:47 -07005566 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005567 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
NeilBrown93588e22005-11-15 00:09:12 -08005569 /* We need to wait INTERRUPTIBLE so that
5570 * we don't add to the load-average.
5571 * That means we need to be sure no signals are
5572 * pending
5573 */
5574 if (signal_pending(current))
5575 flush_signals(current);
5576
5577 wait_event_interruptible_timeout
5578 (thread->wqueue,
5579 test_bit(THREAD_WAKEUP, &thread->flags)
5580 || kthread_should_stop(),
5581 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 clear_bit(THREAD_WAKEUP, &thread->flags);
5584
NeilBrown787453c2005-11-08 21:39:43 -08005585 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 return 0;
5589}
5590
5591void md_wakeup_thread(mdk_thread_t *thread)
5592{
5593 if (thread) {
5594 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5595 set_bit(THREAD_WAKEUP, &thread->flags);
5596 wake_up(&thread->wqueue);
5597 }
5598}
5599
5600mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5601 const char *name)
5602{
5603 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
NeilBrown9ffae0c2006-01-06 00:20:32 -08005605 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 if (!thread)
5607 return NULL;
5608
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 init_waitqueue_head(&thread->wqueue);
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 thread->run = run;
5612 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005613 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005614 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005615 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 kfree(thread);
5617 return NULL;
5618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 return thread;
5620}
5621
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622void md_unregister_thread(mdk_thread_t *thread)
5623{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005624 if (!thread)
5625 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005626 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005627
5628 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 kfree(thread);
5630}
5631
5632void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5633{
5634 if (!mddev) {
5635 MD_BUG();
5636 return;
5637 }
5638
NeilBrownb2d444d2005-11-08 21:39:31 -08005639 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005641
5642 if (mddev->external)
5643 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005644/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5646 mdname(mddev),
5647 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5648 __builtin_return_address(0),__builtin_return_address(1),
5649 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005650*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005651 if (!mddev->pers)
5652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 if (!mddev->pers->error_handler)
5654 return;
5655 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005656 if (mddev->degraded)
5657 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005658 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5660 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5661 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005662 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663}
5664
5665/* seq_file implementation /proc/mdstat */
5666
5667static void status_unused(struct seq_file *seq)
5668{
5669 int i = 0;
5670 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
5672 seq_printf(seq, "unused devices: ");
5673
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005674 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 char b[BDEVNAME_SIZE];
5676 i++;
5677 seq_printf(seq, "%s ",
5678 bdevname(rdev->bdev,b));
5679 }
5680 if (!i)
5681 seq_printf(seq, "<none>");
5682
5683 seq_printf(seq, "\n");
5684}
5685
5686
5687static void status_resync(struct seq_file *seq, mddev_t * mddev)
5688{
NeilBrown4588b422006-03-27 01:18:04 -08005689 sector_t max_blocks, resync, res;
5690 unsigned long dt, db, rt;
5691 int scale;
5692 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
5694 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5695
5696 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5697 max_blocks = mddev->resync_max_sectors >> 1;
5698 else
Andre Noll58c0fed2009-03-31 14:33:13 +11005699 max_blocks = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
5701 /*
5702 * Should not happen.
5703 */
5704 if (!max_blocks) {
5705 MD_BUG();
5706 return;
5707 }
NeilBrown4588b422006-03-27 01:18:04 -08005708 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5709 * in a sector_t, and (max_blocks>>scale) will fit in a
5710 * u32, as those are the requirements for sector_div.
5711 * Thus 'scale' must be at least 10
5712 */
5713 scale = 10;
5714 if (sizeof(sector_t) > sizeof(unsigned long)) {
5715 while ( max_blocks/2 > (1ULL<<(scale+32)))
5716 scale++;
5717 }
5718 res = (resync>>scale)*1000;
5719 sector_div(res, (u32)((max_blocks>>scale)+1));
5720
5721 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 {
NeilBrown4588b422006-03-27 01:18:04 -08005723 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 seq_printf(seq, "[");
5725 for (i = 0; i < x; i++)
5726 seq_printf(seq, "=");
5727 seq_printf(seq, ">");
5728 for (i = 0; i < y; i++)
5729 seq_printf(seq, ".");
5730 seq_printf(seq, "] ");
5731 }
NeilBrown4588b422006-03-27 01:18:04 -08005732 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005733 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5734 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005735 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5736 "check" :
5737 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5738 "resync" : "recovery"))),
5739 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005740 (unsigned long long) resync,
5741 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
5743 /*
5744 * We do not want to overflow, so the order of operands and
5745 * the * 100 / 100 trick are important. We do a +1 to be
5746 * safe against division by zero. We only estimate anyway.
5747 *
5748 * dt: time from mark until now
5749 * db: blocks written from mark until now
5750 * rt: remaining time
5751 */
5752 dt = ((jiffies - mddev->resync_mark) / HZ);
5753 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005754 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5755 - mddev->resync_mark_cnt;
5756 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
5758 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5759
NeilBrownff4e8d92006-07-10 04:44:16 -07005760 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761}
5762
5763static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5764{
5765 struct list_head *tmp;
5766 loff_t l = *pos;
5767 mddev_t *mddev;
5768
5769 if (l >= 0x10000)
5770 return NULL;
5771 if (!l--)
5772 /* header */
5773 return (void*)1;
5774
5775 spin_lock(&all_mddevs_lock);
5776 list_for_each(tmp,&all_mddevs)
5777 if (!l--) {
5778 mddev = list_entry(tmp, mddev_t, all_mddevs);
5779 mddev_get(mddev);
5780 spin_unlock(&all_mddevs_lock);
5781 return mddev;
5782 }
5783 spin_unlock(&all_mddevs_lock);
5784 if (!l--)
5785 return (void*)2;/* tail */
5786 return NULL;
5787}
5788
5789static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5790{
5791 struct list_head *tmp;
5792 mddev_t *next_mddev, *mddev = v;
5793
5794 ++*pos;
5795 if (v == (void*)2)
5796 return NULL;
5797
5798 spin_lock(&all_mddevs_lock);
5799 if (v == (void*)1)
5800 tmp = all_mddevs.next;
5801 else
5802 tmp = mddev->all_mddevs.next;
5803 if (tmp != &all_mddevs)
5804 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5805 else {
5806 next_mddev = (void*)2;
5807 *pos = 0x10000;
5808 }
5809 spin_unlock(&all_mddevs_lock);
5810
5811 if (v != (void*)1)
5812 mddev_put(mddev);
5813 return next_mddev;
5814
5815}
5816
5817static void md_seq_stop(struct seq_file *seq, void *v)
5818{
5819 mddev_t *mddev = v;
5820
5821 if (mddev && v != (void*)1 && v != (void*)2)
5822 mddev_put(mddev);
5823}
5824
NeilBrownd7603b72006-01-06 00:20:30 -08005825struct mdstat_info {
5826 int event;
5827};
5828
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829static int md_seq_show(struct seq_file *seq, void *v)
5830{
5831 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005832 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005834 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005835 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
5837 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005838 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 seq_printf(seq, "Personalities : ");
5840 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005841 list_for_each_entry(pers, &pers_list, list)
5842 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
5844 spin_unlock(&pers_lock);
5845 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005846 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 return 0;
5848 }
5849 if (v == (void*)2) {
5850 status_unused(seq);
5851 return 0;
5852 }
5853
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005854 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005856
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5858 seq_printf(seq, "%s : %sactive", mdname(mddev),
5859 mddev->pers ? "" : "in");
5860 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005861 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005863 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005864 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 seq_printf(seq, " %s", mddev->pers->name);
5866 }
5867
Andre Nolldd8ac332009-03-31 14:33:13 +11005868 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005869 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 char b[BDEVNAME_SIZE];
5871 seq_printf(seq, " %s[%d]",
5872 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005873 if (test_bit(WriteMostly, &rdev->flags))
5874 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005875 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 seq_printf(seq, "(F)");
5877 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005878 } else if (rdev->raid_disk < 0)
5879 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005880 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 }
5882
5883 if (!list_empty(&mddev->disks)) {
5884 if (mddev->pers)
5885 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005886 (unsigned long long)
5887 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 else
5889 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005890 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005892 if (mddev->persistent) {
5893 if (mddev->major_version != 0 ||
5894 mddev->minor_version != 90) {
5895 seq_printf(seq," super %d.%d",
5896 mddev->major_version,
5897 mddev->minor_version);
5898 }
NeilBrowne6910632008-02-06 01:39:51 -08005899 } else if (mddev->external)
5900 seq_printf(seq, " super external:%s",
5901 mddev->metadata_type);
5902 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005903 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
5905 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005906 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005908 if (mddev->pers->sync_request) {
5909 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005910 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005911 seq_printf(seq, "\n ");
5912 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5913 seq_printf(seq, "\tresync=DELAYED\n ");
5914 else if (mddev->recovery_cp < MaxSector)
5915 seq_printf(seq, "\tresync=PENDING\n ");
5916 }
NeilBrown32a76272005-06-21 17:17:14 -07005917 } else
5918 seq_printf(seq, "\n ");
5919
5920 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005921 unsigned long chunk_kb;
5922 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005923 spin_lock_irqsave(&bitmap->lock, flags);
5924 chunk_kb = bitmap->chunksize >> 10;
5925 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5926 "%lu%s chunk",
5927 bitmap->pages - bitmap->missing_pages,
5928 bitmap->pages,
5929 (bitmap->pages - bitmap->missing_pages)
5930 << (PAGE_SHIFT - 10),
5931 chunk_kb ? chunk_kb : bitmap->chunksize,
5932 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005933 if (bitmap->file) {
5934 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005935 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005936 }
NeilBrown78d742d2005-06-21 17:17:15 -07005937
NeilBrown32a76272005-06-21 17:17:14 -07005938 seq_printf(seq, "\n");
5939 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 }
5941
5942 seq_printf(seq, "\n");
5943 }
5944 mddev_unlock(mddev);
5945
5946 return 0;
5947}
5948
5949static struct seq_operations md_seq_ops = {
5950 .start = md_seq_start,
5951 .next = md_seq_next,
5952 .stop = md_seq_stop,
5953 .show = md_seq_show,
5954};
5955
5956static int md_seq_open(struct inode *inode, struct file *file)
5957{
5958 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005959 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5960 if (mi == NULL)
5961 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
5963 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005964 if (error)
5965 kfree(mi);
5966 else {
5967 struct seq_file *p = file->private_data;
5968 p->private = mi;
5969 mi->event = atomic_read(&md_event_count);
5970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 return error;
5972}
5973
NeilBrownd7603b72006-01-06 00:20:30 -08005974static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5975{
5976 struct seq_file *m = filp->private_data;
5977 struct mdstat_info *mi = m->private;
5978 int mask;
5979
5980 poll_wait(filp, &md_event_waiters, wait);
5981
5982 /* always allow read */
5983 mask = POLLIN | POLLRDNORM;
5984
5985 if (mi->event != atomic_read(&md_event_count))
5986 mask |= POLLERR | POLLPRI;
5987 return mask;
5988}
5989
Arjan van de Venfa027c22007-02-12 00:55:33 -08005990static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005991 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 .open = md_seq_open,
5993 .read = seq_read,
5994 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005995 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005996 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997};
5998
NeilBrown2604b702006-01-06 00:20:36 -08005999int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006002 list_add_tail(&p->list, &pers_list);
6003 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 spin_unlock(&pers_lock);
6005 return 0;
6006}
6007
NeilBrown2604b702006-01-06 00:20:36 -08006008int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009{
NeilBrown2604b702006-01-06 00:20:36 -08006010 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006012 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 spin_unlock(&pers_lock);
6014 return 0;
6015}
6016
NeilBrowneea1bf32009-03-31 14:27:02 +11006017static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018{
6019 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006021 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
6023 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006024 rcu_read_lock();
6025 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006027 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6028 (int)part_stat_read(&disk->part0, sectors[1]) -
6029 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006030 /* sync IO will cause sync_io to increase before the disk_stats
6031 * as sync_io is counted when a request starts, and
6032 * disk_stats is counted when it completes.
6033 * So resync activity will cause curr_events to be smaller than
6034 * when there was no such activity.
6035 * non-sync IO will cause disk_stat to increase without
6036 * increasing sync_io so curr_events will (eventually)
6037 * be larger than it was before. Once it becomes
6038 * substantially larger, the test below will cause
6039 * the array to appear non-idle, and resync will slow
6040 * down.
6041 * If there is a lot of outstanding resync activity when
6042 * we set last_event to curr_events, then all that activity
6043 * completing might cause the array to appear non-idle
6044 * and resync will be slowed down even though there might
6045 * not have been non-resync activity. This will only
6046 * happen once though. 'last_events' will soon reflect
6047 * the state where there is little or no outstanding
6048 * resync requests, and further resync activity will
6049 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006050 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006052 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 rdev->last_events = curr_events;
6054 idle = 0;
6055 }
6056 }
NeilBrown4b809912008-07-21 17:05:25 +10006057 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 return idle;
6059}
6060
6061void md_done_sync(mddev_t *mddev, int blocks, int ok)
6062{
6063 /* another "blocks" (512byte) blocks have been synced */
6064 atomic_sub(blocks, &mddev->recovery_active);
6065 wake_up(&mddev->recovery_wait);
6066 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006067 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 md_wakeup_thread(mddev->thread);
6069 // stop recovery, signal do_sync ....
6070 }
6071}
6072
6073
NeilBrown06d91a52005-06-21 17:17:12 -07006074/* md_write_start(mddev, bi)
6075 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006076 * in superblock) before writing, schedule a superblock update
6077 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006078 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006079void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
Neil Brown0fd62b82008-06-28 08:31:36 +10006081 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006082 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006083 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006084
NeilBrownf91de922005-11-08 21:39:36 -08006085 BUG_ON(mddev->ro == 1);
6086 if (mddev->ro == 2) {
6087 /* need to switch to read/write */
6088 mddev->ro = 0;
6089 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6090 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006091 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006092 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006093 }
NeilBrown06d91a52005-06-21 17:17:12 -07006094 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006095 if (mddev->safemode == 1)
6096 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006097 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006098 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006099 if (mddev->in_sync) {
6100 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006101 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006102 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006103 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006104 }
NeilBrowna9701a32005-11-08 21:39:34 -08006105 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006106 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006107 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006108 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006109 wait_event(mddev->sb_wait,
6110 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6111 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112}
6113
6114void md_write_end(mddev_t *mddev)
6115{
6116 if (atomic_dec_and_test(&mddev->writes_pending)) {
6117 if (mddev->safemode == 2)
6118 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006119 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6121 }
6122}
6123
NeilBrown2a2275d2007-01-26 00:57:11 -08006124/* md_allow_write(mddev)
6125 * Calling this ensures that the array is marked 'active' so that writes
6126 * may proceed without blocking. It is important to call this before
6127 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6128 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006129 *
6130 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6131 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006132 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006133int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006134{
6135 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006136 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006137 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006138 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006139 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006140 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006141
6142 spin_lock_irq(&mddev->write_lock);
6143 if (mddev->in_sync) {
6144 mddev->in_sync = 0;
6145 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6146 if (mddev->safemode_delay &&
6147 mddev->safemode == 0)
6148 mddev->safemode = 1;
6149 spin_unlock_irq(&mddev->write_lock);
6150 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006151 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006152 } else
6153 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006154
6155 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6156 return -EAGAIN;
6157 else
6158 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006159}
6160EXPORT_SYMBOL_GPL(md_allow_write);
6161
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162#define SYNC_MARKS 10
6163#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006164void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165{
6166 mddev_t *mddev2;
6167 unsigned int currspeed = 0,
6168 window;
NeilBrown57afd892005-06-21 17:17:13 -07006169 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 unsigned long mark[SYNC_MARKS];
6171 sector_t mark_cnt[SYNC_MARKS];
6172 int last_mark,m;
6173 struct list_head *tmp;
6174 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006175 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006176 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006177 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
6179 /* just incase thread restarts... */
6180 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6181 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006182 if (mddev->ro) /* never try to sync a read-only array */
6183 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
NeilBrown61df9d92006-10-03 01:15:57 -07006185 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6186 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6187 desc = "data-check";
6188 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6189 desc = "requested-resync";
6190 else
6191 desc = "resync";
6192 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6193 desc = "reshape";
6194 else
6195 desc = "recovery";
6196
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 /* we overload curr_resync somewhat here.
6198 * 0 == not engaged in resync at all
6199 * 2 == checking that there is no conflict with another sync
6200 * 1 == like 2, but have yielded to allow conflicting resync to
6201 * commense
6202 * other == active in resync - this many blocks
6203 *
6204 * Before starting a resync we must have set curr_resync to
6205 * 2, and then checked that every "conflicting" array has curr_resync
6206 * less than ours. When we find one that is the same or higher
6207 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6208 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6209 * This will mean we have to start checking from the beginning again.
6210 *
6211 */
6212
6213 do {
6214 mddev->curr_resync = 2;
6215
6216 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006217 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006218 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 goto skip;
6220 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006221 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 if (mddev2 == mddev)
6223 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006224 if (!mddev->parallel_resync
6225 && mddev2->curr_resync
6226 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 DEFINE_WAIT(wq);
6228 if (mddev < mddev2 && mddev->curr_resync == 2) {
6229 /* arbitrarily yield */
6230 mddev->curr_resync = 1;
6231 wake_up(&resync_wait);
6232 }
6233 if (mddev > mddev2 && mddev->curr_resync == 1)
6234 /* no need to wait here, we can wait the next
6235 * time 'round when curr_resync == 2
6236 */
6237 continue;
NeilBrown97441972008-09-19 11:49:54 +10006238 /* We need to wait 'interruptible' so as not to
6239 * contribute to the load average, and not to
6240 * be caught by 'softlockup'
6241 */
6242 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006243 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006244 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006245 printk(KERN_INFO "md: delaying %s of %s"
6246 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006248 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006250 if (signal_pending(current))
6251 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 schedule();
6253 finish_wait(&resync_wait, &wq);
6254 goto try_again;
6255 }
6256 finish_wait(&resync_wait, &wq);
6257 }
6258 }
6259 } while (mddev->curr_resync < 2);
6260
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006261 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006262 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006264 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 */
6266 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006267 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006268 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006269 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6270 j = mddev->resync_min;
6271 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006272 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006273
NeilBrownccfcc3c2006-03-27 01:18:09 -08006274 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006275 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006276 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006278 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006279 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006280 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006281 if (rdev->raid_disk >= 0 &&
6282 !test_bit(Faulty, &rdev->flags) &&
6283 !test_bit(In_sync, &rdev->flags) &&
6284 rdev->recovery_offset < j)
6285 j = rdev->recovery_offset;
6286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
NeilBrown61df9d92006-10-03 01:15:57 -07006288 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6289 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6290 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006291 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006292 "(but not more than %d KB/sec) for %s.\n",
6293 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
NeilBrowneea1bf32009-03-31 14:27:02 +11006295 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006296
NeilBrown57afd892005-06-21 17:17:13 -07006297 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 for (m = 0; m < SYNC_MARKS; m++) {
6299 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006300 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 }
6302 last_mark = 0;
6303 mddev->resync_mark = mark[last_mark];
6304 mddev->resync_mark_cnt = mark_cnt[last_mark];
6305
6306 /*
6307 * Tune reconstruction:
6308 */
6309 window = 32*(PAGE_SIZE/512);
6310 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6311 window/2,(unsigned long long) max_sectors/2);
6312
6313 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 last_check = 0;
6315
6316 if (j>2) {
6317 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006318 "md: resuming %s of %s from checkpoint.\n",
6319 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 mddev->curr_resync = j;
6321 }
6322
6323 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006324 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325
NeilBrown57afd892005-06-21 17:17:13 -07006326 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006327 if (j >= mddev->resync_max) {
6328 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6329 wait_event(mddev->recovery_wait,
6330 mddev->resync_max > j
6331 || kthread_should_stop());
6332 }
6333 if (kthread_should_stop())
6334 goto interrupted;
NeilBrown97e4f422009-03-31 14:33:13 +11006335
6336 if (mddev->curr_resync > mddev->curr_resync_completed &&
6337 (mddev->curr_resync - mddev->curr_resync_completed)
6338 > (max_sectors >> 4)) {
6339 /* time to update curr_resync_completed */
6340 blk_unplug(mddev->queue);
6341 wait_event(mddev->recovery_wait,
6342 atomic_read(&mddev->recovery_active) == 0);
6343 mddev->curr_resync_completed =
6344 mddev->curr_resync;
6345 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6346 }
NeilBrown57afd892005-06-21 17:17:13 -07006347 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006348 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006349 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006350 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 goto out;
6352 }
NeilBrown57afd892005-06-21 17:17:13 -07006353
6354 if (!skipped) { /* actual IO requested */
6355 io_sectors += sectors;
6356 atomic_add(sectors, &mddev->recovery_active);
6357 }
6358
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 j += sectors;
6360 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006361 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006362 if (last_check == 0)
6363 /* this is the earliers that rebuilt will be
6364 * visible in /proc/mdstat
6365 */
6366 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006367
6368 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 continue;
6370
NeilBrown57afd892005-06-21 17:17:13 -07006371 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
NeilBrowndfc70642008-05-23 13:04:39 -07006373 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 break;
6375
6376 repeat:
6377 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6378 /* step marks */
6379 int next = (last_mark+1) % SYNC_MARKS;
6380
6381 mddev->resync_mark = mark[next];
6382 mddev->resync_mark_cnt = mark_cnt[next];
6383 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006384 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 last_mark = next;
6386 }
6387
6388
NeilBrownc6207272008-02-06 01:39:52 -08006389 if (kthread_should_stop())
6390 goto interrupted;
6391
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
6393 /*
6394 * this loop exits only if either when we are slower than
6395 * the 'hard' speed limit, or the system was IO-idle for
6396 * a jiffy.
6397 * the system might be non-idle CPU-wise, but we only care
6398 * about not overloading the IO subsystem. (things like an
6399 * e2fsck being done on the RAID array should execute fast)
6400 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006401 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 cond_resched();
6403
NeilBrown57afd892005-06-21 17:17:13 -07006404 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6405 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
NeilBrown88202a02006-01-06 00:21:36 -08006407 if (currspeed > speed_min(mddev)) {
6408 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006409 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006410 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 goto repeat;
6412 }
6413 }
6414 }
NeilBrown61df9d92006-10-03 01:15:57 -07006415 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 /*
6417 * this also signals 'finished resyncing' to md_stop
6418 */
6419 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006420 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
6422 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6423
6424 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006425 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
NeilBrowndfc70642008-05-23 13:04:39 -07006427 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006428 mddev->curr_resync > 2) {
6429 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6430 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6431 if (mddev->curr_resync >= mddev->recovery_cp) {
6432 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006433 "md: checkpointing %s of %s.\n",
6434 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006435 mddev->recovery_cp = mddev->curr_resync;
6436 }
6437 } else
6438 mddev->recovery_cp = MaxSector;
6439 } else {
6440 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6441 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006442 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006443 if (rdev->raid_disk >= 0 &&
6444 !test_bit(Faulty, &rdev->flags) &&
6445 !test_bit(In_sync, &rdev->flags) &&
6446 rdev->recovery_offset < mddev->curr_resync)
6447 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 }
NeilBrown17571282006-12-10 02:20:52 -08006450 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 skip:
6453 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006454 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006455 mddev->resync_max = MaxSector;
6456 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 wake_up(&resync_wait);
6458 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6459 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006460 return;
6461
6462 interrupted:
6463 /*
6464 * got a signal, exit.
6465 */
6466 printk(KERN_INFO
6467 "md: md_do_sync() got signal ... exiting\n");
6468 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6469 goto out;
6470
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471}
NeilBrown29269552006-03-27 01:18:10 -08006472EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473
6474
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006475static int remove_and_add_spares(mddev_t *mddev)
6476{
6477 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006478 int spares = 0;
6479
NeilBrown97e4f422009-03-31 14:33:13 +11006480 mddev->curr_resync_completed = 0;
6481
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006482 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006483 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006484 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006485 (test_bit(Faulty, &rdev->flags) ||
6486 ! test_bit(In_sync, &rdev->flags)) &&
6487 atomic_read(&rdev->nr_pending)==0) {
6488 if (mddev->pers->hot_remove_disk(
6489 mddev, rdev->raid_disk)==0) {
6490 char nm[20];
6491 sprintf(nm,"rd%d", rdev->raid_disk);
6492 sysfs_remove_link(&mddev->kobj, nm);
6493 rdev->raid_disk = -1;
6494 }
6495 }
6496
NeilBrown4044ba52009-01-09 08:31:11 +11006497 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006498 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006499 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006500 !test_bit(In_sync, &rdev->flags) &&
6501 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006502 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006503 if (rdev->raid_disk < 0
6504 && !test_bit(Faulty, &rdev->flags)) {
6505 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006506 if (mddev->pers->
6507 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006508 char nm[20];
6509 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006510 if (sysfs_create_link(&mddev->kobj,
6511 &rdev->kobj, nm))
6512 printk(KERN_WARNING
6513 "md: cannot register "
6514 "%s for %s\n",
6515 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006516 spares++;
6517 md_new_event(mddev);
6518 } else
6519 break;
6520 }
NeilBrowndfc70642008-05-23 13:04:39 -07006521 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006522 }
6523 return spares;
6524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525/*
6526 * This routine is regularly called by all per-raid-array threads to
6527 * deal with generic issues like resync and super-block update.
6528 * Raid personalities that don't have a thread (linear/raid0) do not
6529 * need this as they never do any recovery or update the superblock.
6530 *
6531 * It does not do any resync itself, but rather "forks" off other threads
6532 * to do that as needed.
6533 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6534 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006535 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 * and wakeups up this thread which will reap the thread and finish up.
6537 * This thread also removes any faulty devices (with nr_pending == 0).
6538 *
6539 * The overall approach is:
6540 * 1/ if the superblock needs updating, update it.
6541 * 2/ If a recovery thread is running, don't do anything else.
6542 * 3/ If recovery has finished, clean up, possibly marking spares active.
6543 * 4/ If there are any faulty devices, remove them.
6544 * 5/ If array is degraded, try to add spares devices
6545 * 6/ If array has spares or is not in-sync, start a resync thread.
6546 */
6547void md_check_recovery(mddev_t *mddev)
6548{
6549 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
6551
NeilBrown5f404022005-06-21 17:17:16 -07006552 if (mddev->bitmap)
6553 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
6555 if (mddev->ro)
6556 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006557
6558 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006559 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006560 printk(KERN_INFO "md: %s in immediate safe mode\n",
6561 mdname(mddev));
6562 mddev->safemode = 2;
6563 }
6564 flush_signals(current);
6565 }
6566
NeilBrownc89a8ee2008-08-05 15:54:13 +10006567 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6568 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006570 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006572 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006573 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006574 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6575 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 ))
6577 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006578
NeilBrowndf5b89b2006-03-27 01:18:20 -08006579 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006580 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006581
NeilBrownc89a8ee2008-08-05 15:54:13 +10006582 if (mddev->ro) {
6583 /* Only thing we do on a ro array is remove
6584 * failed devices.
6585 */
6586 remove_and_add_spares(mddev);
6587 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6588 goto unlock;
6589 }
6590
NeilBrown31a59e32008-04-30 00:52:30 -07006591 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006592 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006593 spin_lock_irq(&mddev->write_lock);
6594 if (mddev->safemode &&
6595 !atomic_read(&mddev->writes_pending) &&
6596 !mddev->in_sync &&
6597 mddev->recovery_cp == MaxSector) {
6598 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006599 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006600 if (mddev->persistent)
6601 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6602 }
6603 if (mddev->safemode == 1)
6604 mddev->safemode = 0;
6605 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006606 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006607 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006608 }
NeilBrownfca4d842005-06-21 17:17:11 -07006609
NeilBrown850b2b422006-10-03 01:15:46 -07006610 if (mddev->flags)
6611 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006612
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006613 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006614 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006615 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006616
NeilBrown06d91a52005-06-21 17:17:12 -07006617
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6619 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6620 /* resync/recovery still happening */
6621 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6622 goto unlock;
6623 }
6624 if (mddev->sync_thread) {
6625 /* resync has finished, collect result */
6626 md_unregister_thread(mddev->sync_thread);
6627 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006628 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6629 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 /* success...*/
6631 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006632 if (mddev->pers->spare_active(mddev))
6633 sysfs_notify(&mddev->kobj, NULL,
6634 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 }
NeilBrowncea9c222009-03-31 15:15:05 +11006636 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6637 mddev->pers->finish_reshape)
6638 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07006639 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006640
6641 /* if array is no-longer degraded, then any saved_raid_disk
6642 * information must be scrapped
6643 */
6644 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006645 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006646 rdev->saved_raid_disk = -1;
6647
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 mddev->recovery = 0;
6649 /* flag recovery needed just to double check */
6650 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006651 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006652 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 goto unlock;
6654 }
Neil Brown72a23c22008-06-28 08:31:41 +10006655 /* Set RUNNING before clearing NEEDED to avoid
6656 * any transients in the value of "sync_action".
6657 */
6658 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6659 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006660 /* Clear some bits that don't mean anything, but
6661 * might be left set
6662 */
NeilBrown24dd4692005-11-08 21:39:26 -08006663 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6664 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006666 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6667 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 /* no recovery is running.
6669 * remove any failed drives, then
6670 * add spares if possible.
6671 * Spare are also removed and re-added, to allow
6672 * the personality to fail the re-add.
6673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006675 if (mddev->reshape_position != MaxSector) {
6676 if (mddev->pers->check_reshape(mddev) != 0)
6677 /* Cannot proceed */
6678 goto unlock;
6679 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006680 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006681 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006682 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6683 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006684 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006685 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006686 } else if (mddev->recovery_cp < MaxSector) {
6687 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006688 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006689 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6690 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006692
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006694 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6695 /* We are adding a device or devices to an array
6696 * which has the bitmap stored on all devices.
6697 * So make sure all bitmap pages get written
6698 */
6699 bitmap_write_all(mddev->bitmap);
6700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 mddev->sync_thread = md_register_thread(md_do_sync,
6702 mddev,
6703 "%s_resync");
6704 if (!mddev->sync_thread) {
6705 printk(KERN_ERR "%s: could not start resync"
6706 " thread...\n",
6707 mdname(mddev));
6708 /* leave the spares where they are, it shouldn't hurt */
6709 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006710 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006712 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006713 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 }
6715 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006716 if (!mddev->sync_thread) {
6717 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6718 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6719 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006720 if (mddev->sysfs_action)
6721 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 mddev_unlock(mddev);
6724 }
6725}
6726
Dan Williams6bfe0b42008-04-30 00:52:32 -07006727void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6728{
NeilBrown3c0ee632008-10-21 13:25:28 +11006729 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006730 wait_event_timeout(rdev->blocked_wait,
6731 !test_bit(Blocked, &rdev->flags),
6732 msecs_to_jiffies(5000));
6733 rdev_dec_pending(rdev, mddev);
6734}
6735EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6736
Adrian Bunk75c96f82005-05-05 16:16:09 -07006737static int md_notify_reboot(struct notifier_block *this,
6738 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739{
6740 struct list_head *tmp;
6741 mddev_t *mddev;
6742
6743 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6744
6745 printk(KERN_INFO "md: stopping all md devices.\n");
6746
NeilBrown29ac4aa2008-02-06 01:39:58 -08006747 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006748 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006749 /* Force a switch to readonly even array
6750 * appears to still be in use. Hence
6751 * the '100'.
6752 */
NeilBrownd710e132008-10-13 11:55:12 +11006753 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006754 mddev_unlock(mddev);
6755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 /*
6757 * certain more exotic SCSI devices are known to be
6758 * volatile wrt too early system reboots. While the
6759 * right place to handle this issue is the given
6760 * driver, we do want to have a safe RAID driver ...
6761 */
6762 mdelay(1000*1);
6763 }
6764 return NOTIFY_DONE;
6765}
6766
Adrian Bunk75c96f82005-05-05 16:16:09 -07006767static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 .notifier_call = md_notify_reboot,
6769 .next = NULL,
6770 .priority = INT_MAX, /* before any real devices */
6771};
6772
6773static void md_geninit(void)
6774{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6776
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006777 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778}
6779
Adrian Bunk75c96f82005-05-05 16:16:09 -07006780static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006782 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 return -1;
6784 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006785 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 return -1;
6787 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006788 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006789 md_probe, NULL, NULL);
6790 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 md_probe, NULL, NULL);
6792
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006794 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
6796 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798}
6799
6800
6801#ifndef MODULE
6802
6803/*
6804 * Searches all registered partitions for autorun RAID arrays
6805 * at boot time.
6806 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006807
6808static LIST_HEAD(all_detected_devices);
6809struct detected_devices_node {
6810 struct list_head list;
6811 dev_t dev;
6812};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813
6814void md_autodetect_dev(dev_t dev)
6815{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006816 struct detected_devices_node *node_detected_dev;
6817
6818 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6819 if (node_detected_dev) {
6820 node_detected_dev->dev = dev;
6821 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6822 } else {
6823 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6824 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826}
6827
6828
6829static void autostart_arrays(int part)
6830{
6831 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006832 struct detected_devices_node *node_detected_dev;
6833 dev_t dev;
6834 int i_scanned, i_passed;
6835
6836 i_scanned = 0;
6837 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6840
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006841 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6842 i_scanned++;
6843 node_detected_dev = list_entry(all_detected_devices.next,
6844 struct detected_devices_node, list);
6845 list_del(&node_detected_dev->list);
6846 dev = node_detected_dev->dev;
6847 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006848 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 if (IS_ERR(rdev))
6850 continue;
6851
NeilBrownb2d444d2005-11-08 21:39:31 -08006852 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 MD_BUG();
6854 continue;
6855 }
NeilBrownd0fae182008-03-04 14:29:31 -08006856 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006858 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006860
6861 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6862 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
6864 autorun_devices(part);
6865}
6866
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006867#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
6869static __exit void md_exit(void)
6870{
6871 mddev_t *mddev;
6872 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006873
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006874 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006875 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006877 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 unregister_blkdev(mdp_major, "mdp");
6879 unregister_reboot_notifier(&md_notifier);
6880 unregister_sysctl_table(raid_table_header);
6881 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006882 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006884 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 }
6886}
6887
Dan Williams685784a2007-07-09 11:56:42 -07006888subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889module_exit(md_exit)
6890
NeilBrownf91de922005-11-08 21:39:36 -08006891static int get_ro(char *buffer, struct kernel_param *kp)
6892{
6893 return sprintf(buffer, "%d", start_readonly);
6894}
6895static int set_ro(const char *val, struct kernel_param *kp)
6896{
6897 char *e;
6898 int num = simple_strtoul(val, &e, 10);
6899 if (*val && (*e == '\0' || *e == '\n')) {
6900 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006901 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006902 }
6903 return -EINVAL;
6904}
6905
NeilBrown80ca3a42006-07-10 04:44:18 -07006906module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6907module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006908
NeilBrownefeb53c2009-01-09 08:31:10 +11006909module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006910
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911EXPORT_SYMBOL(register_md_personality);
6912EXPORT_SYMBOL(unregister_md_personality);
6913EXPORT_SYMBOL(md_error);
6914EXPORT_SYMBOL(md_done_sync);
6915EXPORT_SYMBOL(md_write_start);
6916EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917EXPORT_SYMBOL(md_register_thread);
6918EXPORT_SYMBOL(md_unregister_thread);
6919EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920EXPORT_SYMBOL(md_check_recovery);
6921MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006922MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006923MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);