blob: c194955aecaeb4a284ad35f0779ea66a52ab95d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700105 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .proc_handler = &proc_dointvec,
107 },
108 {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .proc_handler = &proc_dointvec,
115 },
116 { .ctl_name = 0 }
117};
118
119static ctl_table raid_dir_table[] = {
120 {
121 .ctl_name = DEV_RAID,
122 .procname = "raid",
123 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .child = raid_table,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_root_table[] = {
131 {
132 .ctl_name = CTL_DEV,
133 .procname = "dev",
134 .maxlen = 0,
135 .mode = 0555,
136 .child = raid_dir_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static struct block_device_operations md_fops;
142
NeilBrownf91de922005-11-08 21:39:36 -0800143static int start_readonly;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
NeilBrownd7603b72006-01-06 00:20:30 -0800146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
149 * count increases.
150 *
151 * Events are:
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
154 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800155static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800156static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800157void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800158{
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
161}
NeilBrown29269552006-03-27 01:18:10 -0800162EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800163
NeilBrownc331eb02006-05-30 21:27:13 -0700164/* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
166 */
Adrian Bunk05381952006-06-26 00:28:01 -0700167static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700168{
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171}
172
NeilBrownd7603b72006-01-06 00:20:30 -0800173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
176 */
177static LIST_HEAD(all_mddevs);
178static DEFINE_SPINLOCK(all_mddevs_lock);
179
180
181/*
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
187 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800188#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
192 mddev = NULL;}); \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
200 tmp = tmp->next;}) \
201 )
202
203
NeilBrown409c57f2009-03-31 14:39:39 +1100204/* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
210 */
211static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
NeilBrown409c57f2009-03-31 14:39:39 +1100213 mddev_t *mddev = q->queuedata;
214 int rv;
215 if (mddev == NULL || mddev->pers == NULL) {
216 bio_io_error(bio);
217 return 0;
218 }
219 rcu_read_lock();
220 if (mddev->suspended) {
221 DEFINE_WAIT(__wait);
222 for (;;) {
223 prepare_to_wait(&mddev->sb_wait, &__wait,
224 TASK_UNINTERRUPTIBLE);
225 if (!mddev->suspended)
226 break;
227 rcu_read_unlock();
228 schedule();
229 rcu_read_lock();
230 }
231 finish_wait(&mddev->sb_wait, &__wait);
232 }
233 atomic_inc(&mddev->active_io);
234 rcu_read_unlock();
235 rv = mddev->pers->make_request(q, bio);
236 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
237 wake_up(&mddev->sb_wait);
238
239 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
NeilBrown409c57f2009-03-31 14:39:39 +1100242static void mddev_suspend(mddev_t *mddev)
243{
244 BUG_ON(mddev->suspended);
245 mddev->suspended = 1;
246 synchronize_rcu();
247 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
248 mddev->pers->quiesce(mddev, 1);
249 md_unregister_thread(mddev->thread);
250 mddev->thread = NULL;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
254 * unloaded
255 */
256}
257
258static void mddev_resume(mddev_t *mddev)
259{
260 mddev->suspended = 0;
261 wake_up(&mddev->sb_wait);
262 mddev->pers->quiesce(mddev, 0);
263}
264
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static inline mddev_t *mddev_get(mddev_t *mddev)
267{
268 atomic_inc(&mddev->active);
269 return mddev;
270}
271
Dan Williams5fd3a172009-03-04 00:57:25 -0700272static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static void mddev_put(mddev_t *mddev)
275{
276 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
277 return;
NeilBrownd3374822009-01-09 08:31:10 +1100278 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
279 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100281 if (mddev->gendisk) {
282 /* we did a probe so need to clean up.
283 * Call schedule_work inside the spinlock
284 * so that flush_scheduled_work() after
285 * mddev_find will succeed in waiting for the
286 * work to be done.
287 */
288 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
289 schedule_work(&mddev->del_work);
290 } else
291 kfree(mddev);
292 }
293 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static mddev_t * mddev_find(dev_t unit)
297{
298 mddev_t *mddev, *new = NULL;
299
300 retry:
301 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
NeilBrownefeb53c2009-01-09 08:31:10 +1100303 if (unit) {
304 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
305 if (mddev->unit == unit) {
306 mddev_get(mddev);
307 spin_unlock(&all_mddevs_lock);
308 kfree(new);
309 return mddev;
310 }
311
312 if (new) {
313 list_add(&new->all_mddevs, &all_mddevs);
314 spin_unlock(&all_mddevs_lock);
315 new->hold_active = UNTIL_IOCTL;
316 return new;
317 }
318 } else if (new) {
319 /* find an unused unit number */
320 static int next_minor = 512;
321 int start = next_minor;
322 int is_free = 0;
323 int dev = 0;
324 while (!is_free) {
325 dev = MKDEV(MD_MAJOR, next_minor);
326 next_minor++;
327 if (next_minor > MINORMASK)
328 next_minor = 0;
329 if (next_minor == start) {
330 /* Oh dear, all in use. */
331 spin_unlock(&all_mddevs_lock);
332 kfree(new);
333 return NULL;
334 }
335
336 is_free = 1;
337 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
338 if (mddev->unit == dev) {
339 is_free = 0;
340 break;
341 }
342 }
343 new->unit = dev;
344 new->md_minor = MINOR(dev);
345 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 list_add(&new->all_mddevs, &all_mddevs);
347 spin_unlock(&all_mddevs_lock);
348 return new;
349 }
350 spin_unlock(&all_mddevs_lock);
351
NeilBrown9ffae0c2006-01-06 00:20:32 -0800352 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!new)
354 return NULL;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 new->unit = unit;
357 if (MAJOR(unit) == MD_MAJOR)
358 new->md_minor = MINOR(unit);
359 else
360 new->md_minor = MINOR(unit) >> MdpMinorShift;
361
NeilBrowndf5b89b2006-03-27 01:18:20 -0800362 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 INIT_LIST_HEAD(&new->disks);
364 INIT_LIST_HEAD(&new->all_mddevs);
365 init_timer(&new->safemode_timer);
366 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000367 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100368 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700369 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700370 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700371 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700372 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000373 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800374 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700375 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto retry;
378}
379
380static inline int mddev_lock(mddev_t * mddev)
381{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800382 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Dan Williamsb522adc2009-03-31 15:00:31 +1100385static inline int mddev_is_locked(mddev_t *mddev)
386{
387 return mutex_is_locked(&mddev->reconfig_mutex);
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static inline int mddev_trylock(mddev_t * mddev)
391{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800392 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static inline void mddev_unlock(mddev_t * mddev)
396{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800397 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
NeilBrown005eca52005-08-22 13:11:08 -0700399 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
NeilBrown2989ddb2006-01-06 00:20:43 -0800402static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100404 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100406 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (rdev->desc_nr == nr)
408 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return NULL;
411}
412
413static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
414{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 mdk_rdev_t *rdev;
416
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100417 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (rdev->bdev->bd_dev == dev)
419 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return NULL;
422}
423
NeilBrownd9d166c2006-01-06 00:20:51 -0800424static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800425{
426 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800427 list_for_each_entry(pers, &pers_list, list) {
428 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800429 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800430 if (strcmp(pers->name, clevel)==0)
431 return pers;
432 }
NeilBrown2604b702006-01-06 00:20:36 -0800433 return NULL;
434}
435
Andre Nollb73df2d2008-07-11 22:02:23 +1000436/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700437static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Andre Nollb73df2d2008-07-11 22:02:23 +1000439 sector_t num_sectors = bdev->bd_inode->i_size / 512;
440 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static int alloc_disk_sb(mdk_rdev_t * rdev)
444{
445 if (rdev->sb_page)
446 MD_BUG();
447
448 rdev->sb_page = alloc_page(GFP_KERNEL);
449 if (!rdev->sb_page) {
450 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000451 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
454 return 0;
455}
456
457static void free_disk_sb(mdk_rdev_t * rdev)
458{
459 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800460 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 rdev->sb_loaded = 0;
462 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000463 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100464 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466}
467
468
NeilBrown6712ecf2007-09-27 12:47:43 +0200469static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700470{
471 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800472 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700473
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700474 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
475 printk("md: super_written gets error=%d, uptodate=%d\n",
476 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
477 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800478 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700479 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700480
NeilBrowna9701a32005-11-08 21:39:34 -0800481 if (atomic_dec_and_test(&mddev->pending_writes))
482 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700483 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700484}
485
NeilBrown6712ecf2007-09-27 12:47:43 +0200486static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800487{
488 struct bio *bio2 = bio->bi_private;
489 mdk_rdev_t *rdev = bio2->bi_private;
490 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800491
492 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
493 error == -EOPNOTSUPP) {
494 unsigned long flags;
495 /* barriers don't appear to be supported :-( */
496 set_bit(BarriersNotsupp, &rdev->flags);
497 mddev->barriers_work = 0;
498 spin_lock_irqsave(&mddev->write_lock, flags);
499 bio2->bi_next = mddev->biolist;
500 mddev->biolist = bio2;
501 spin_unlock_irqrestore(&mddev->write_lock, flags);
502 wake_up(&mddev->sb_wait);
503 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200504 } else {
505 bio_put(bio2);
506 bio->bi_private = rdev;
507 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800508 }
NeilBrowna9701a32005-11-08 21:39:34 -0800509}
510
NeilBrown7bfa19f2005-06-21 17:17:28 -0700511void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
512 sector_t sector, int size, struct page *page)
513{
514 /* write first size bytes of page to sector of rdev
515 * Increment mddev->pending_writes before returning
516 * and decrement it on completion, waking up sb_wait
517 * if zero is reached.
518 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800519 *
520 * As we might need to resubmit the request if BIO_RW_BARRIER
521 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700522 */
523 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100524 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700525
526 bio->bi_bdev = rdev->bdev;
527 bio->bi_sector = sector;
528 bio_add_page(bio, page, size, 0);
529 bio->bi_private = rdev;
530 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800531 bio->bi_rw = rw;
532
NeilBrown7bfa19f2005-06-21 17:17:28 -0700533 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800534 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
535 struct bio *rbio;
536 rw |= (1<<BIO_RW_BARRIER);
537 rbio = bio_clone(bio, GFP_NOIO);
538 rbio->bi_private = bio;
539 rbio->bi_end_io = super_written_barrier;
540 submit_bio(rw, rbio);
541 } else
542 submit_bio(rw, bio);
543}
544
545void md_super_wait(mddev_t *mddev)
546{
547 /* wait for all superblock writes that were scheduled to complete.
548 * if any had to be retried (due to BARRIER problems), retry them
549 */
550 DEFINE_WAIT(wq);
551 for(;;) {
552 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
553 if (atomic_read(&mddev->pending_writes)==0)
554 break;
555 while (mddev->biolist) {
556 struct bio *bio;
557 spin_lock_irq(&mddev->write_lock);
558 bio = mddev->biolist;
559 mddev->biolist = bio->bi_next ;
560 bio->bi_next = NULL;
561 spin_unlock_irq(&mddev->write_lock);
562 submit_bio(bio->bi_rw, bio);
563 }
564 schedule();
565 }
566 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700567}
568
NeilBrown6712ecf2007-09-27 12:47:43 +0200569static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
NeilBrowna654b9d82005-06-21 17:17:27 -0700574int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct page *page, int rw)
576{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700577 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct completion event;
579 int ret;
580
Jens Axboe93dbb392009-02-16 10:25:40 +0100581 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 bio->bi_bdev = bdev;
584 bio->bi_sector = sector;
585 bio_add_page(bio, page, size, 0);
586 init_completion(&event);
587 bio->bi_private = &event;
588 bio->bi_end_io = bi_complete;
589 submit_bio(rw, bio);
590 wait_for_completion(&event);
591
592 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
593 bio_put(bio);
594 return ret;
595}
NeilBrowna8745db2006-01-06 00:20:34 -0800596EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
NeilBrown0002b272005-09-09 16:23:53 -0700598static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 char b[BDEVNAME_SIZE];
601 if (!rdev->sb_page) {
602 MD_BUG();
603 return -EINVAL;
604 }
605 if (rdev->sb_loaded)
606 return 0;
607
608
Andre Noll0f420352008-07-11 22:02:23 +1000609 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto fail;
611 rdev->sb_loaded = 1;
612 return 0;
613
614fail:
615 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
616 bdevname(rdev->bdev,b));
617 return -EINVAL;
618}
619
620static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
621{
Andre Noll05710462008-07-11 22:02:20 +1000622 return sb1->set_uuid0 == sb2->set_uuid0 &&
623 sb1->set_uuid1 == sb2->set_uuid1 &&
624 sb1->set_uuid2 == sb2->set_uuid2 &&
625 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
629{
630 int ret;
631 mdp_super_t *tmp1, *tmp2;
632
633 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
634 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
635
636 if (!tmp1 || !tmp2) {
637 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100638 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto abort;
640 }
641
642 *tmp1 = *sb1;
643 *tmp2 = *sb2;
644
645 /*
646 * nr_disks is not constant
647 */
648 tmp1->nr_disks = 0;
649 tmp2->nr_disks = 0;
650
Andre Nollce0c8e02008-07-11 22:02:20 +1000651 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700653 kfree(tmp1);
654 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return ret;
656}
657
NeilBrown4d167f02007-05-09 02:35:37 -0700658
659static u32 md_csum_fold(u32 csum)
660{
661 csum = (csum & 0xffff) + (csum >> 16);
662 return (csum & 0xffff) + (csum >> 16);
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static unsigned int calc_sb_csum(mdp_super_t * sb)
666{
NeilBrown4d167f02007-05-09 02:35:37 -0700667 u64 newcsum = 0;
668 u32 *sb32 = (u32*)sb;
669 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned int disk_csum, csum;
671
672 disk_csum = sb->sb_csum;
673 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700674
675 for (i = 0; i < MD_SB_BYTES/4 ; i++)
676 newcsum += sb32[i];
677 csum = (newcsum & 0xffffffff) + (newcsum>>32);
678
679
680#ifdef CONFIG_ALPHA
681 /* This used to use csum_partial, which was wrong for several
682 * reasons including that different results are returned on
683 * different architectures. It isn't critical that we get exactly
684 * the same return value as before (we always csum_fold before
685 * testing, and that removes any differences). However as we
686 * know that csum_partial always returned a 16bit value on
687 * alphas, do a fold to maximise conformity to previous behaviour.
688 */
689 sb->sb_csum = md_csum_fold(disk_csum);
690#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return csum;
694}
695
696
697/*
698 * Handle superblock details.
699 * We want to be able to handle multiple superblock formats
700 * so we have a common interface to them all, and an array of
701 * different handlers.
702 * We rely on user-space to write the initial superblock, and support
703 * reading and updating of superblocks.
704 * Interface methods are:
705 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
706 * loads and validates a superblock on dev.
707 * if refdev != NULL, compare superblocks on both devices
708 * Return:
709 * 0 - dev has a superblock that is compatible with refdev
710 * 1 - dev has a superblock that is compatible and newer than refdev
711 * so dev should be used as the refdev in future
712 * -EINVAL superblock incompatible or invalid
713 * -othererror e.g. -EIO
714 *
715 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
716 * Verify that dev is acceptable into mddev.
717 * The first time, mddev->raid_disks will be 0, and data from
718 * dev should be merged in. Subsequent calls check that dev
719 * is new enough. Return 0 or -EINVAL
720 *
721 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
722 * Update the superblock for rdev with data in mddev
723 * This does not write to disc.
724 *
725 */
726
727struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000728 char *name;
729 struct module *owner;
730 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
731 int minor_version);
732 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
733 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
734 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000735 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736};
737
738/*
Andre Noll0894cc32009-06-18 08:49:23 +1000739 * Check that the given mddev has no bitmap.
740 *
741 * This function is called from the run method of all personalities that do not
742 * support bitmaps. It prints an error message and returns non-zero if mddev
743 * has a bitmap. Otherwise, it returns 0.
744 *
745 */
746int md_check_no_bitmap(mddev_t *mddev)
747{
748 if (!mddev->bitmap_file && !mddev->bitmap_offset)
749 return 0;
750 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
751 mdname(mddev), mddev->pers->name);
752 return 1;
753}
754EXPORT_SYMBOL(md_check_no_bitmap);
755
756/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * load_super for 0.90.0
758 */
759static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
760{
761 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
762 mdp_super_t *sb;
763 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /*
Andre Noll0f420352008-07-11 22:02:23 +1000766 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 * it's at the end of the disk.
768 *
769 * It also happens to be a multiple of 4Kb.
770 */
Andre Noll0f420352008-07-11 22:02:23 +1000771 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
NeilBrown0002b272005-09-09 16:23:53 -0700773 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (ret) return ret;
775
776 ret = -EINVAL;
777
778 bdevname(rdev->bdev, b);
779 sb = (mdp_super_t*)page_address(rdev->sb_page);
780
781 if (sb->md_magic != MD_SB_MAGIC) {
782 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
783 b);
784 goto abort;
785 }
786
787 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800788 sb->minor_version < 90 ||
789 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
791 sb->major_version, sb->minor_version,
792 b);
793 goto abort;
794 }
795
796 if (sb->raid_disks <= 0)
797 goto abort;
798
NeilBrown4d167f02007-05-09 02:35:37 -0700799 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
801 b);
802 goto abort;
803 }
804
805 rdev->preferred_minor = sb->md_minor;
806 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700807 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (sb->level == LEVEL_MULTIPATH)
810 rdev->desc_nr = -1;
811 else
812 rdev->desc_nr = sb->this_disk.number;
813
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700814 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700816 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 __u64 ev1, ev2;
818 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
819 if (!uuid_equal(refsb, sb)) {
820 printk(KERN_WARNING "md: %s has different UUID to %s\n",
821 b, bdevname(refdev->bdev,b2));
822 goto abort;
823 }
824 if (!sb_equal(refsb, sb)) {
825 printk(KERN_WARNING "md: %s has same UUID"
826 " but different superblock to %s\n",
827 b, bdevname(refdev->bdev, b2));
828 goto abort;
829 }
830 ev1 = md_event(sb);
831 ev2 = md_event(refsb);
832 if (ev1 > ev2)
833 ret = 1;
834 else
835 ret = 0;
836 }
NeilBrown8190e752009-06-18 08:48:58 +1000837 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Andre Nolldd8ac332009-03-31 14:33:13 +1100839 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800840 /* "this cannot possibly happen" ... */
841 ret = -EINVAL;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 abort:
844 return ret;
845}
846
847/*
848 * validate_super for 0.90.0
849 */
850static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
851{
852 mdp_disk_t *desc;
853 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700854 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
NeilBrown41158c72005-06-21 17:17:25 -0700856 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800857 clear_bit(Faulty, &rdev->flags);
858 clear_bit(In_sync, &rdev->flags);
859 clear_bit(WriteMostly, &rdev->flags);
860 clear_bit(BarriersNotsupp, &rdev->flags);
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (mddev->raid_disks == 0) {
863 mddev->major_version = 0;
864 mddev->minor_version = sb->minor_version;
865 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800866 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000867 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 mddev->ctime = sb->ctime;
869 mddev->utime = sb->utime;
870 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800871 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 mddev->layout = sb->layout;
873 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100874 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700875 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700876 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700877 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
NeilBrownf6705572006-03-27 01:18:11 -0800879 if (mddev->minor_version >= 91) {
880 mddev->reshape_position = sb->reshape_position;
881 mddev->delta_disks = sb->delta_disks;
882 mddev->new_level = sb->new_level;
883 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000884 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800885 } else {
886 mddev->reshape_position = MaxSector;
887 mddev->delta_disks = 0;
888 mddev->new_level = mddev->level;
889 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000890 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800891 }
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (sb->state & (1<<MD_SB_CLEAN))
894 mddev->recovery_cp = MaxSector;
895 else {
896 if (sb->events_hi == sb->cp_events_hi &&
897 sb->events_lo == sb->cp_events_lo) {
898 mddev->recovery_cp = sb->recovery_cp;
899 } else
900 mddev->recovery_cp = 0;
901 }
902
903 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
904 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
905 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
906 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
907
908 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700909
910 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700911 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700912 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700913
NeilBrown41158c72005-06-21 17:17:25 -0700914 } else if (mddev->pers == NULL) {
915 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 ++ev1;
917 if (ev1 < mddev->events)
918 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700919 } else if (mddev->bitmap) {
920 /* if adding to array with a bitmap, then we can accept an
921 * older device ... but not too old.
922 */
NeilBrown41158c72005-06-21 17:17:25 -0700923 if (ev1 < mddev->bitmap->events_cleared)
924 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700925 } else {
926 if (ev1 < mddev->events)
927 /* just a hot-add of a new device, leave raid_disk at -1 */
928 return 0;
929 }
NeilBrown41158c72005-06-21 17:17:25 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 desc = sb->disks + rdev->desc_nr;
933
934 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800935 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700936 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
937 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800938 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 rdev->raid_disk = desc->raid_disk;
940 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700941 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
942 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700943 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800944 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946}
947
948/*
949 * sync_super for 0.90.0
950 */
951static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
952{
953 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 mdk_rdev_t *rdev2;
955 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 /* make rdev->sb match mddev data..
959 *
960 * 1/ zero out disks
961 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
962 * 3/ any empty disks < next_spare become removed
963 *
964 * disks[0] gets initialised to REMOVED because
965 * we cannot be sure from other fields if it has
966 * been initialised or not.
967 */
968 int i;
969 int active=0, working=0,failed=0,spare=0,nr_disks=0;
970
NeilBrown61181562005-09-09 16:24:02 -0700971 rdev->sb_size = MD_SB_BYTES;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 sb = (mdp_super_t*)page_address(rdev->sb_page);
974
975 memset(sb, 0, sizeof(*sb));
976
977 sb->md_magic = MD_SB_MAGIC;
978 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 sb->patch_version = mddev->patch_version;
980 sb->gvalid_words = 0; /* ignored */
981 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
982 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
983 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
984 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
985
986 sb->ctime = mddev->ctime;
987 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +1100988 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 sb->raid_disks = mddev->raid_disks;
990 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800991 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 sb->utime = mddev->utime;
993 sb->state = 0;
994 sb->events_hi = (mddev->events>>32);
995 sb->events_lo = (u32)mddev->events;
996
NeilBrownf6705572006-03-27 01:18:11 -0800997 if (mddev->reshape_position == MaxSector)
998 sb->minor_version = 90;
999 else {
1000 sb->minor_version = 91;
1001 sb->reshape_position = mddev->reshape_position;
1002 sb->new_level = mddev->new_level;
1003 sb->delta_disks = mddev->delta_disks;
1004 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001005 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001006 }
1007 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (mddev->in_sync)
1009 {
1010 sb->recovery_cp = mddev->recovery_cp;
1011 sb->cp_events_hi = (mddev->events>>32);
1012 sb->cp_events_lo = (u32)mddev->events;
1013 if (mddev->recovery_cp == MaxSector)
1014 sb->state = (1<< MD_SB_CLEAN);
1015 } else
1016 sb->recovery_cp = 0;
1017
1018 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001019 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
NeilBrowna654b9d82005-06-21 17:17:27 -07001021 if (mddev->bitmap && mddev->bitmap_file == NULL)
1022 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001025 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001027 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001028 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1029 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -08001030 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001032 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001033 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 d = &sb->disks[rdev2->desc_nr];
1035 nr_disks++;
1036 d->number = rdev2->desc_nr;
1037 d->major = MAJOR(rdev2->bdev->bd_dev);
1038 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001039 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1040 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 d->raid_disk = rdev2->raid_disk;
1042 else
1043 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001044 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -08001046 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 d->state = (1<<MD_DISK_ACTIVE);
1048 d->state |= (1<<MD_DISK_SYNC);
1049 active++;
1050 working++;
1051 } else {
1052 d->state = 0;
1053 spare++;
1054 working++;
1055 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001056 if (test_bit(WriteMostly, &rdev2->flags))
1057 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* now set the "removed" and "faulty" bits on any missing devices */
1060 for (i=0 ; i < mddev->raid_disks ; i++) {
1061 mdp_disk_t *d = &sb->disks[i];
1062 if (d->state == 0 && d->number == 0) {
1063 d->number = i;
1064 d->raid_disk = i;
1065 d->state = (1<<MD_DISK_REMOVED);
1066 d->state |= (1<<MD_DISK_FAULTY);
1067 failed++;
1068 }
1069 }
1070 sb->nr_disks = nr_disks;
1071 sb->active_disks = active;
1072 sb->working_disks = working;
1073 sb->failed_disks = failed;
1074 sb->spare_disks = spare;
1075
1076 sb->this_disk = sb->disks[rdev->desc_nr];
1077 sb->sb_csum = calc_sb_csum(sb);
1078}
1079
1080/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001081 * rdev_size_change for 0.90.0
1082 */
1083static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001084super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001085{
Andre Noll58c0fed2009-03-31 14:33:13 +11001086 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001087 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001088 if (rdev->mddev->bitmap_offset)
1089 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001090 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001091 if (!num_sectors || num_sectors > rdev->sb_start)
1092 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001093 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001094 rdev->sb_page);
1095 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001096 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001097}
1098
1099
1100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 * version 1 superblock
1102 */
1103
NeilBrown1c05b4b2006-10-21 10:24:08 -07001104static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001106 __le32 disk_csum;
1107 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 unsigned long long newcsum;
1109 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001110 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 int i;
1112
1113 disk_csum = sb->sb_csum;
1114 sb->sb_csum = 0;
1115 newcsum = 0;
1116 for (i=0; size>=4; size -= 4 )
1117 newcsum += le32_to_cpu(*isuper++);
1118
1119 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001120 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1123 sb->sb_csum = disk_csum;
1124 return cpu_to_le32(csum);
1125}
1126
1127static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1128{
1129 struct mdp_superblock_1 *sb;
1130 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001131 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001133 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /*
Andre Noll0f420352008-07-11 22:02:23 +10001136 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * It is always aligned to a 4K boundary and
1138 * depeding on minor_version, it can be:
1139 * 0: At least 8K, but less than 12K, from end of device
1140 * 1: At start of device
1141 * 2: 4K from start of device.
1142 */
1143 switch(minor_version) {
1144 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001145 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1146 sb_start -= 8*2;
1147 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001150 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001153 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 default:
1156 return -EINVAL;
1157 }
Andre Noll0f420352008-07-11 22:02:23 +10001158 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
NeilBrown0002b272005-09-09 16:23:53 -07001160 /* superblock is rarely larger than 1K, but it can be larger,
1161 * and it is safe to read 4k, so we do that
1162 */
1163 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (ret) return ret;
1165
1166
1167 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1168
1169 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1170 sb->major_version != cpu_to_le32(1) ||
1171 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001172 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001173 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return -EINVAL;
1175
1176 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1177 printk("md: invalid superblock checksum on %s\n",
1178 bdevname(rdev->bdev,b));
1179 return -EINVAL;
1180 }
1181 if (le64_to_cpu(sb->data_size) < 10) {
1182 printk("md: data_size too small on %s\n",
1183 bdevname(rdev->bdev,b));
1184 return -EINVAL;
1185 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 rdev->preferred_minor = 0xffff;
1188 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001189 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
NeilBrown0002b272005-09-09 16:23:53 -07001191 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001192 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001193 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001194 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1195
1196 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001197 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001198 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001199
NeilBrown31b65a02006-07-10 04:44:14 -07001200 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1201 rdev->desc_nr = -1;
1202 else
1203 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1204
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001205 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001206 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001207 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 __u64 ev1, ev2;
1209 struct mdp_superblock_1 *refsb =
1210 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1211
1212 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1213 sb->level != refsb->level ||
1214 sb->layout != refsb->layout ||
1215 sb->chunksize != refsb->chunksize) {
1216 printk(KERN_WARNING "md: %s has strangely different"
1217 " superblock to %s\n",
1218 bdevname(rdev->bdev,b),
1219 bdevname(refdev->bdev,b2));
1220 return -EINVAL;
1221 }
1222 ev1 = le64_to_cpu(sb->events);
1223 ev2 = le64_to_cpu(refsb->events);
1224
1225 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001226 ret = 1;
1227 else
1228 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
NeilBrowna1801f82008-03-04 14:29:31 -08001230 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001231 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1232 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001234 rdev->sectors = rdev->sb_start;
1235 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001237 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001238 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001239 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1244{
1245 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001246 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
NeilBrown41158c72005-06-21 17:17:25 -07001248 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001249 clear_bit(Faulty, &rdev->flags);
1250 clear_bit(In_sync, &rdev->flags);
1251 clear_bit(WriteMostly, &rdev->flags);
1252 clear_bit(BarriersNotsupp, &rdev->flags);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (mddev->raid_disks == 0) {
1255 mddev->major_version = 1;
1256 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001257 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001258 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1260 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1261 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001262 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 mddev->layout = le32_to_cpu(sb->layout);
1264 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001265 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001266 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001267 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001268 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1271 memcpy(mddev->uuid, sb->set_uuid, 16);
1272
1273 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001274
NeilBrown71c08052005-09-09 16:23:51 -07001275 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001276 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001277 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001278
NeilBrownf6705572006-03-27 01:18:11 -08001279 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1280 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1281 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1282 mddev->new_level = le32_to_cpu(sb->new_level);
1283 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001284 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001285 } else {
1286 mddev->reshape_position = MaxSector;
1287 mddev->delta_disks = 0;
1288 mddev->new_level = mddev->level;
1289 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001290 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001291 }
1292
NeilBrown41158c72005-06-21 17:17:25 -07001293 } else if (mddev->pers == NULL) {
1294 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ++ev1;
1296 if (ev1 < mddev->events)
1297 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001298 } else if (mddev->bitmap) {
1299 /* If adding to array with a bitmap, then we can accept an
1300 * older device, but not too old.
1301 */
NeilBrown41158c72005-06-21 17:17:25 -07001302 if (ev1 < mddev->bitmap->events_cleared)
1303 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001304 } else {
1305 if (ev1 < mddev->events)
1306 /* just a hot-add of a new device, leave raid_disk at -1 */
1307 return 0;
1308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (mddev->level != LEVEL_MULTIPATH) {
1310 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1312 switch(role) {
1313 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 break;
1315 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001316 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 break;
1318 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001319 if ((le32_to_cpu(sb->feature_map) &
1320 MD_FEATURE_RECOVERY_OFFSET))
1321 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1322 else
1323 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 rdev->raid_disk = role;
1325 break;
1326 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001327 if (sb->devflags & WriteMostly1)
1328 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001329 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001330 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 0;
1333}
1334
1335static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1336{
1337 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 mdk_rdev_t *rdev2;
1339 int max_dev, i;
1340 /* make rdev->sb match mddev and rdev data. */
1341
1342 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1343
1344 sb->feature_map = 0;
1345 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001346 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 memset(sb->pad1, 0, sizeof(sb->pad1));
1348 memset(sb->pad2, 0, sizeof(sb->pad2));
1349 memset(sb->pad3, 0, sizeof(sb->pad3));
1350
1351 sb->utime = cpu_to_le64((__u64)mddev->utime);
1352 sb->events = cpu_to_le64(mddev->events);
1353 if (mddev->in_sync)
1354 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1355 else
1356 sb->resync_offset = cpu_to_le64(0);
1357
NeilBrown1c05b4b2006-10-21 10:24:08 -07001358 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001359
NeilBrownf0ca3402006-02-02 14:28:04 -08001360 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001361 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001362 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001363 sb->level = cpu_to_le32(mddev->level);
1364 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001365
NeilBrowna654b9d82005-06-21 17:17:27 -07001366 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1367 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001368 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001369 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001370
1371 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001372 !test_bit(In_sync, &rdev->flags)) {
1373 if (mddev->curr_resync_completed > rdev->recovery_offset)
1374 rdev->recovery_offset = mddev->curr_resync_completed;
1375 if (rdev->recovery_offset > 0) {
1376 sb->feature_map |=
1377 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1378 sb->recovery_offset =
1379 cpu_to_le64(rdev->recovery_offset);
1380 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001381 }
1382
NeilBrownf6705572006-03-27 01:18:11 -08001383 if (mddev->reshape_position != MaxSector) {
1384 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1385 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1386 sb->new_layout = cpu_to_le32(mddev->new_layout);
1387 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1388 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001389 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001390 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001393 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (rdev2->desc_nr+1 > max_dev)
1395 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001396
1397 if (max_dev > le32_to_cpu(sb->max_dev))
1398 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 for (i=0; i<max_dev;i++)
1400 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1401
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001402 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001404 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001406 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001408 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1409 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 else
1411 sb->dev_roles[i] = cpu_to_le16(0xffff);
1412 }
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 sb->sb_csum = calc_sb_1_csum(sb);
1415}
1416
Chris Webb0cd17fe2008-06-28 08:31:46 +10001417static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001418super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001419{
1420 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001421 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001422 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001423 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001424 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001425 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001426 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1427 max_sectors -= rdev->data_offset;
1428 if (!num_sectors || num_sectors > max_sectors)
1429 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001430 } else if (rdev->mddev->bitmap_offset) {
1431 /* minor version 0 with bitmap we can't move */
1432 return 0;
1433 } else {
1434 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001435 sector_t sb_start;
1436 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1437 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001438 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001439 if (!num_sectors || num_sectors > max_sectors)
1440 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001441 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001442 }
1443 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001444 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001445 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001446 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001447 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001448 rdev->sb_page);
1449 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001450 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Adrian Bunk75c96f82005-05-05 16:16:09 -07001453static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 [0] = {
1455 .name = "0.90.0",
1456 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001457 .load_super = super_90_load,
1458 .validate_super = super_90_validate,
1459 .sync_super = super_90_sync,
1460 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 },
1462 [1] = {
1463 .name = "md-1",
1464 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001465 .load_super = super_1_load,
1466 .validate_super = super_1_validate,
1467 .sync_super = super_1_sync,
1468 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 },
1470};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1473{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001474 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
NeilBrown4b809912008-07-21 17:05:25 +10001476 rcu_read_lock();
1477 rdev_for_each_rcu(rdev, mddev1)
1478 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001479 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001480 rdev2->bdev->bd_contains) {
1481 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001482 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001483 }
1484 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return 0;
1486}
1487
1488static LIST_HEAD(pending_raid_disks);
1489
Andre Nollac5e7112009-08-03 10:59:47 +10001490/*
1491 * Try to register data integrity profile for an mddev
1492 *
1493 * This is called when an array is started and after a disk has been kicked
1494 * from the array. It only succeeds if all working and active component devices
1495 * are integrity capable with matching profiles.
1496 */
1497int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001498{
Andre Nollac5e7112009-08-03 10:59:47 +10001499 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001500
Andre Nollac5e7112009-08-03 10:59:47 +10001501 if (list_empty(&mddev->disks))
1502 return 0; /* nothing to do */
1503 if (blk_get_integrity(mddev->gendisk))
1504 return 0; /* already registered */
1505 list_for_each_entry(rdev, &mddev->disks, same_set) {
1506 /* skip spares and non-functional disks */
1507 if (test_bit(Faulty, &rdev->flags))
1508 continue;
1509 if (rdev->raid_disk < 0)
1510 continue;
1511 /*
1512 * If at least one rdev is not integrity capable, we can not
1513 * enable data integrity for the md device.
1514 */
1515 if (!bdev_get_integrity(rdev->bdev))
1516 return -EINVAL;
1517 if (!reference) {
1518 /* Use the first rdev as the reference */
1519 reference = rdev;
1520 continue;
1521 }
1522 /* does this rdev's profile match the reference profile? */
1523 if (blk_integrity_compare(reference->bdev->bd_disk,
1524 rdev->bdev->bd_disk) < 0)
1525 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001526 }
Andre Nollac5e7112009-08-03 10:59:47 +10001527 /*
1528 * All component devices are integrity capable and have matching
1529 * profiles, register the common profile for the md device.
1530 */
1531 if (blk_integrity_register(mddev->gendisk,
1532 bdev_get_integrity(reference->bdev)) != 0) {
1533 printk(KERN_ERR "md: failed to register integrity for %s\n",
1534 mdname(mddev));
1535 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001536 }
Andre Nollac5e7112009-08-03 10:59:47 +10001537 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1538 mdname(mddev));
1539 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001540}
Andre Nollac5e7112009-08-03 10:59:47 +10001541EXPORT_SYMBOL(md_integrity_register);
1542
1543/* Disable data integrity if non-capable/non-matching disk is being added */
1544void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1545{
1546 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1547 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1548
1549 if (!bi_mddev) /* nothing to do */
1550 return;
1551 if (rdev->raid_disk < 0) /* skip spares */
1552 return;
1553 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1554 rdev->bdev->bd_disk) >= 0)
1555 return;
1556 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1557 blk_integrity_unregister(mddev->gendisk);
1558}
1559EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1562{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001563 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001564 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001565 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001566 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (rdev->mddev) {
1569 MD_BUG();
1570 return -EINVAL;
1571 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001572
1573 /* prevent duplicates */
1574 if (find_rdev(mddev, rdev->bdev->bd_dev))
1575 return -EEXIST;
1576
Andre Nolldd8ac332009-03-31 14:33:13 +11001577 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1578 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1579 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001580 if (mddev->pers) {
1581 /* Cannot change size, so fail
1582 * If mddev->level <= 0, then we don't care
1583 * about aligning sizes (e.g. linear)
1584 */
1585 if (mddev->level > 0)
1586 return -ENOSPC;
1587 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001588 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 /* Verify rdev->desc_nr is unique.
1592 * If it is -1, assign a free number, else
1593 * check number is not in use
1594 */
1595 if (rdev->desc_nr < 0) {
1596 int choice = 0;
1597 if (mddev->pers) choice = mddev->raid_disks;
1598 while (find_rdev_nr(mddev, choice))
1599 choice++;
1600 rdev->desc_nr = choice;
1601 } else {
1602 if (find_rdev_nr(mddev, rdev->desc_nr))
1603 return -EBUSY;
1604 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001605 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1606 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1607 mdname(mddev), mddev->max_disks);
1608 return -EBUSY;
1609 }
NeilBrown19133a42005-11-08 21:39:35 -08001610 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001611 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001612 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001615 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001616
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001617 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001618 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001619
Tejun Heo0762b8b2008-08-25 19:56:12 +09001620 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001621 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1622 kobject_del(&rdev->kobj);
1623 goto fail;
1624 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001625 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1626
NeilBrown4b809912008-07-21 17:05:25 +10001627 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001628 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001629
1630 /* May as well allow recovery to be retried once */
1631 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001634
1635 fail:
1636 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1637 b, mdname(mddev));
1638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
NeilBrown177a99b2008-02-06 01:39:56 -08001641static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001642{
1643 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1644 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001645 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001646}
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1649{
1650 char b[BDEVNAME_SIZE];
1651 if (!rdev->mddev) {
1652 MD_BUG();
1653 return;
1654 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001655 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001656 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1658 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001659 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001660 sysfs_put(rdev->sysfs_state);
1661 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001662 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001663 * writing to 'remove' to "dev/state". We also need
1664 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001665 */
NeilBrown4b809912008-07-21 17:05:25 +10001666 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001667 INIT_WORK(&rdev->del_work, md_delayed_delete);
1668 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001669 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672/*
1673 * prevent the device from being mounted, repartitioned or
1674 * otherwise reused by a RAID array (or any other kernel
1675 * subsystem), by bd_claiming the device.
1676 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001677static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 int err = 0;
1680 struct block_device *bdev;
1681 char b[BDEVNAME_SIZE];
1682
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001683 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (IS_ERR(bdev)) {
1685 printk(KERN_ERR "md: could not open %s.\n",
1686 __bdevname(dev, b));
1687 return PTR_ERR(bdev);
1688 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001689 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (err) {
1691 printk(KERN_ERR "md: could not bd_claim %s.\n",
1692 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001693 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return err;
1695 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001696 if (!shared)
1697 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 rdev->bdev = bdev;
1699 return err;
1700}
1701
1702static void unlock_rdev(mdk_rdev_t *rdev)
1703{
1704 struct block_device *bdev = rdev->bdev;
1705 rdev->bdev = NULL;
1706 if (!bdev)
1707 MD_BUG();
1708 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001709 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712void md_autodetect_dev(dev_t dev);
1713
1714static void export_rdev(mdk_rdev_t * rdev)
1715{
1716 char b[BDEVNAME_SIZE];
1717 printk(KERN_INFO "md: export_rdev(%s)\n",
1718 bdevname(rdev->bdev,b));
1719 if (rdev->mddev)
1720 MD_BUG();
1721 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001723 if (test_bit(AutoDetected, &rdev->flags))
1724 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725#endif
1726 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001727 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730static void kick_rdev_from_array(mdk_rdev_t * rdev)
1731{
1732 unbind_rdev_from_array(rdev);
1733 export_rdev(rdev);
1734}
1735
1736static void export_array(mddev_t *mddev)
1737{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001738 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
NeilBrownd089c6a2008-02-06 01:39:59 -08001740 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (!rdev->mddev) {
1742 MD_BUG();
1743 continue;
1744 }
1745 kick_rdev_from_array(rdev);
1746 }
1747 if (!list_empty(&mddev->disks))
1748 MD_BUG();
1749 mddev->raid_disks = 0;
1750 mddev->major_version = 0;
1751}
1752
1753static void print_desc(mdp_disk_t *desc)
1754{
1755 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1756 desc->major,desc->minor,desc->raid_disk,desc->state);
1757}
1758
Cheng Renquancd2ac932009-01-09 08:31:08 +11001759static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 int i;
1762
1763 printk(KERN_INFO
1764 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1765 sb->major_version, sb->minor_version, sb->patch_version,
1766 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1767 sb->ctime);
1768 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1769 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1770 sb->md_minor, sb->layout, sb->chunk_size);
1771 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1772 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1773 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1774 sb->failed_disks, sb->spare_disks,
1775 sb->sb_csum, (unsigned long)sb->events_lo);
1776
1777 printk(KERN_INFO);
1778 for (i = 0; i < MD_SB_DISKS; i++) {
1779 mdp_disk_t *desc;
1780
1781 desc = sb->disks + i;
1782 if (desc->number || desc->major || desc->minor ||
1783 desc->raid_disk || (desc->state && (desc->state != 4))) {
1784 printk(" D %2d: ", i);
1785 print_desc(desc);
1786 }
1787 }
1788 printk(KERN_INFO "md: THIS: ");
1789 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Cheng Renquancd2ac932009-01-09 08:31:08 +11001792static void print_sb_1(struct mdp_superblock_1 *sb)
1793{
1794 __u8 *uuid;
1795
1796 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001797 printk(KERN_INFO
1798 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1799 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1800 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001801 le32_to_cpu(sb->major_version),
1802 le32_to_cpu(sb->feature_map),
1803 uuid[0], uuid[1], uuid[2], uuid[3],
1804 uuid[4], uuid[5], uuid[6], uuid[7],
1805 uuid[8], uuid[9], uuid[10], uuid[11],
1806 uuid[12], uuid[13], uuid[14], uuid[15],
1807 sb->set_name,
1808 (unsigned long long)le64_to_cpu(sb->ctime)
1809 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1810
1811 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001812 printk(KERN_INFO
1813 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001814 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001815 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1816 ":%02x%02x%02x%02x%02x%02x\n"
1817 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1818 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001819 le32_to_cpu(sb->level),
1820 (unsigned long long)le64_to_cpu(sb->size),
1821 le32_to_cpu(sb->raid_disks),
1822 le32_to_cpu(sb->layout),
1823 le32_to_cpu(sb->chunksize),
1824 (unsigned long long)le64_to_cpu(sb->data_offset),
1825 (unsigned long long)le64_to_cpu(sb->data_size),
1826 (unsigned long long)le64_to_cpu(sb->super_offset),
1827 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1828 le32_to_cpu(sb->dev_number),
1829 uuid[0], uuid[1], uuid[2], uuid[3],
1830 uuid[4], uuid[5], uuid[6], uuid[7],
1831 uuid[8], uuid[9], uuid[10], uuid[11],
1832 uuid[12], uuid[13], uuid[14], uuid[15],
1833 sb->devflags,
1834 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1835 (unsigned long long)le64_to_cpu(sb->events),
1836 (unsigned long long)le64_to_cpu(sb->resync_offset),
1837 le32_to_cpu(sb->sb_csum),
1838 le32_to_cpu(sb->max_dev)
1839 );
1840}
1841
1842static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001845 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1846 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001847 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1848 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001850 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1851 switch (major_version) {
1852 case 0:
1853 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1854 break;
1855 case 1:
1856 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1857 break;
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 } else
1860 printk(KERN_INFO "md: no rdev superblock!\n");
1861}
1862
Adrian Bunk5e563412006-06-26 00:27:42 -07001863static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001865 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 mdk_rdev_t *rdev;
1867 mddev_t *mddev;
1868 char b[BDEVNAME_SIZE];
1869
1870 printk("\n");
1871 printk("md: **********************************\n");
1872 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1873 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001874 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
NeilBrown32a76272005-06-21 17:17:14 -07001876 if (mddev->bitmap)
1877 bitmap_print_sb(mddev->bitmap);
1878 else
1879 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001880 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 printk("<%s>", bdevname(rdev->bdev,b));
1882 printk("\n");
1883
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001884 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001885 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887 printk("md: **********************************\n");
1888 printk("\n");
1889}
1890
1891
NeilBrown42543762006-06-26 00:27:57 -07001892static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
NeilBrown42543762006-06-26 00:27:57 -07001894 /* Update each superblock (in-memory image), but
1895 * if we are allowed to, skip spares which already
1896 * have the right event counter, or have one earlier
1897 * (which would mean they aren't being marked as dirty
1898 * with the rest of the array)
1899 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001902 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001903 if (rdev->sb_events == mddev->events ||
1904 (nospares &&
1905 rdev->raid_disk < 0 &&
1906 (rdev->sb_events&1)==0 &&
1907 rdev->sb_events+1 == mddev->events)) {
1908 /* Don't update this superblock */
1909 rdev->sb_loaded = 2;
1910 } else {
1911 super_types[mddev->major_version].
1912 sync_super(mddev, rdev);
1913 rdev->sb_loaded = 1;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916}
1917
NeilBrown850b2b42006-10-03 01:15:46 -07001918static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001921 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001922 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrown1b57f132009-06-18 08:48:19 +10001924 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07001925 if (mddev->external)
1926 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001928 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001929
NeilBrown850b2b42006-10-03 01:15:46 -07001930 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1931 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1932 force_change = 1;
1933 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1934 /* just a clean<-> dirty transition, possibly leave spares alone,
1935 * though if events isn't the right even/odd, we will have to do
1936 * spares after all
1937 */
1938 nospares = 1;
1939 if (force_change)
1940 nospares = 0;
1941 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001942 /* If the array is degraded, then skipping spares is both
1943 * dangerous and fairly pointless.
1944 * Dangerous because a device that was removed from the array
1945 * might have a event_count that still looks up-to-date,
1946 * so it can be re-added without a resync.
1947 * Pointless because if there are any spares to skip,
1948 * then a recovery will happen and soon that array won't
1949 * be degraded any more and the spare can go back to sleep then.
1950 */
NeilBrown850b2b42006-10-03 01:15:46 -07001951 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001952
NeilBrown06d91a52005-06-21 17:17:12 -07001953 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07001954
1955 /* If this is just a dirty<->clean transition, and the array is clean
1956 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001957 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001958 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001959 && (mddev->events & 1)
1960 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001961 mddev->events--;
1962 else {
1963 /* otherwise we have to go forward and ... */
1964 mddev->events ++;
1965 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1966 /* .. if the array isn't clean, insist on an odd 'events' */
1967 if ((mddev->events&1)==0) {
1968 mddev->events++;
1969 nospares = 0;
1970 }
1971 } else {
1972 /* otherwise insist on an even 'events' (for clean states) */
1973 if ((mddev->events&1)) {
1974 mddev->events++;
1975 nospares = 0;
1976 }
1977 }
1978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 if (!mddev->events) {
1981 /*
1982 * oops, this 64-bit counter should never wrap.
1983 * Either we are in around ~1 trillion A.C., assuming
1984 * 1 reboot per second, or we have a bug:
1985 */
1986 MD_BUG();
1987 mddev->events --;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
1991 * do not write anything to disk if using
1992 * nonpersistent superblocks
1993 */
NeilBrown06d91a52005-06-21 17:17:12 -07001994 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001995 if (!mddev->external)
1996 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1997
NeilBrowna9701a32005-11-08 21:39:34 -08001998 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001999 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002001 }
NeilBrowne6910632008-02-06 01:39:51 -08002002 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002003 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 dprintk(KERN_INFO
2006 "md: updating %s RAID superblock on device (in sync %d)\n",
2007 mdname(mddev),mddev->in_sync);
2008
NeilBrown4ad13662007-07-17 04:06:13 -07002009 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002010 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 char b[BDEVNAME_SIZE];
2012 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002013 if (rdev->sb_loaded != 1)
2014 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002015 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 dprintk("(skipping faulty ");
2017
2018 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002019 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002020 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002021 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002022 rdev->sb_page);
2023 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2024 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002025 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002026 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 } else
2029 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002030 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /* only need to write one superblock... */
2032 break;
2033 }
NeilBrowna9701a32005-11-08 21:39:34 -08002034 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002035 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002036
NeilBrowna9701a32005-11-08 21:39:34 -08002037 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002038 if (mddev->in_sync != sync_req ||
2039 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002040 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002041 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002042 goto repeat;
2043 }
NeilBrown850b2b42006-10-03 01:15:46 -07002044 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002045 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002046 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002047 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2048 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Andre Noll7f6ce762008-03-23 18:34:54 +01002052/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002053 * We want to accept with case. For this we use cmd_match.
2054 */
2055static int cmd_match(const char *cmd, const char *str)
2056{
2057 /* See if cmd, written into a sysfs file, matches
2058 * str. They must either be the same, or cmd can
2059 * have a trailing newline
2060 */
2061 while (*cmd && *str && *cmd == *str) {
2062 cmd++;
2063 str++;
2064 }
2065 if (*cmd == '\n')
2066 cmd++;
2067 if (*str || *cmd)
2068 return 0;
2069 return 1;
2070}
2071
NeilBrown86e6ffd2005-11-08 21:39:24 -08002072struct rdev_sysfs_entry {
2073 struct attribute attr;
2074 ssize_t (*show)(mdk_rdev_t *, char *);
2075 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2076};
2077
2078static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002079state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002080{
2081 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002082 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002083
NeilBrownb2d444d2005-11-08 21:39:31 -08002084 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002085 len+= sprintf(page+len, "%sfaulty",sep);
2086 sep = ",";
2087 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002088 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002089 len += sprintf(page+len, "%sin_sync",sep);
2090 sep = ",";
2091 }
NeilBrownf6556752006-06-26 00:28:01 -07002092 if (test_bit(WriteMostly, &rdev->flags)) {
2093 len += sprintf(page+len, "%swrite_mostly",sep);
2094 sep = ",";
2095 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002096 if (test_bit(Blocked, &rdev->flags)) {
2097 len += sprintf(page+len, "%sblocked", sep);
2098 sep = ",";
2099 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002100 if (!test_bit(Faulty, &rdev->flags) &&
2101 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002102 len += sprintf(page+len, "%sspare", sep);
2103 sep = ",";
2104 }
2105 return len+sprintf(page+len, "\n");
2106}
2107
NeilBrown45dc2de2006-06-26 00:27:58 -07002108static ssize_t
2109state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2110{
2111 /* can write
2112 * faulty - simulates and error
2113 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002114 * writemostly - sets write_mostly
2115 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002116 * blocked - sets the Blocked flag
2117 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002118 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002119 */
2120 int err = -EINVAL;
2121 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2122 md_error(rdev->mddev, rdev);
2123 err = 0;
2124 } else if (cmd_match(buf, "remove")) {
2125 if (rdev->raid_disk >= 0)
2126 err = -EBUSY;
2127 else {
2128 mddev_t *mddev = rdev->mddev;
2129 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002130 if (mddev->pers)
2131 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002132 md_new_event(mddev);
2133 err = 0;
2134 }
NeilBrownf6556752006-06-26 00:28:01 -07002135 } else if (cmd_match(buf, "writemostly")) {
2136 set_bit(WriteMostly, &rdev->flags);
2137 err = 0;
2138 } else if (cmd_match(buf, "-writemostly")) {
2139 clear_bit(WriteMostly, &rdev->flags);
2140 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002141 } else if (cmd_match(buf, "blocked")) {
2142 set_bit(Blocked, &rdev->flags);
2143 err = 0;
2144 } else if (cmd_match(buf, "-blocked")) {
2145 clear_bit(Blocked, &rdev->flags);
2146 wake_up(&rdev->blocked_wait);
2147 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2148 md_wakeup_thread(rdev->mddev->thread);
2149
2150 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002151 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2152 set_bit(In_sync, &rdev->flags);
2153 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002154 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002155 if (!err && rdev->sysfs_state)
2156 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002157 return err ? err : len;
2158}
NeilBrown80ca3a42006-07-10 04:44:18 -07002159static struct rdev_sysfs_entry rdev_state =
2160__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002161
2162static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002163errors_show(mdk_rdev_t *rdev, char *page)
2164{
2165 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2166}
2167
2168static ssize_t
2169errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2170{
2171 char *e;
2172 unsigned long n = simple_strtoul(buf, &e, 10);
2173 if (*buf && (*e == 0 || *e == '\n')) {
2174 atomic_set(&rdev->corrected_errors, n);
2175 return len;
2176 }
2177 return -EINVAL;
2178}
2179static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002180__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002181
NeilBrown014236d2006-01-06 00:20:55 -08002182static ssize_t
2183slot_show(mdk_rdev_t *rdev, char *page)
2184{
2185 if (rdev->raid_disk < 0)
2186 return sprintf(page, "none\n");
2187 else
2188 return sprintf(page, "%d\n", rdev->raid_disk);
2189}
2190
2191static ssize_t
2192slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2193{
2194 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002195 int err;
2196 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002197 int slot = simple_strtoul(buf, &e, 10);
2198 if (strncmp(buf, "none", 4)==0)
2199 slot = -1;
2200 else if (e==buf || (*e && *e!= '\n'))
2201 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002202 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002203 /* Setting 'slot' on an active array requires also
2204 * updating the 'rd%d' link, and communicating
2205 * with the personality with ->hot_*_disk.
2206 * For now we only support removing
2207 * failed/spare devices. This normally happens automatically,
2208 * but not when the metadata is externally managed.
2209 */
NeilBrownc303da62008-02-06 01:39:51 -08002210 if (rdev->raid_disk == -1)
2211 return -EEXIST;
2212 /* personality does all needed checks */
2213 if (rdev->mddev->pers->hot_add_disk == NULL)
2214 return -EINVAL;
2215 err = rdev->mddev->pers->
2216 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2217 if (err)
2218 return err;
2219 sprintf(nm, "rd%d", rdev->raid_disk);
2220 sysfs_remove_link(&rdev->mddev->kobj, nm);
2221 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2222 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002223 } else if (rdev->mddev->pers) {
2224 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002225 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002226 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002227 */
2228
2229 if (rdev->raid_disk != -1)
2230 return -EBUSY;
2231
2232 if (rdev->mddev->pers->hot_add_disk == NULL)
2233 return -EINVAL;
2234
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002235 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002236 if (rdev2->raid_disk == slot)
2237 return -EEXIST;
2238
2239 rdev->raid_disk = slot;
2240 if (test_bit(In_sync, &rdev->flags))
2241 rdev->saved_raid_disk = slot;
2242 else
2243 rdev->saved_raid_disk = -1;
2244 err = rdev->mddev->pers->
2245 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002246 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002247 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002248 return err;
Neil Brown52664732008-06-28 08:31:44 +10002249 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002250 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002251 sprintf(nm, "rd%d", rdev->raid_disk);
2252 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2253 printk(KERN_WARNING
2254 "md: cannot register "
2255 "%s for %s\n",
2256 nm, mdname(rdev->mddev));
2257
2258 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002259 } else {
2260 if (slot >= rdev->mddev->raid_disks)
2261 return -ENOSPC;
2262 rdev->raid_disk = slot;
2263 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002264 clear_bit(Faulty, &rdev->flags);
2265 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002266 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002267 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002268 }
NeilBrown014236d2006-01-06 00:20:55 -08002269 return len;
2270}
2271
2272
2273static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002274__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002275
NeilBrown93c8cad2006-01-06 00:20:56 -08002276static ssize_t
2277offset_show(mdk_rdev_t *rdev, char *page)
2278{
Andrew Morton6961ece2006-01-06 00:20:59 -08002279 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002280}
2281
2282static ssize_t
2283offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2284{
2285 char *e;
2286 unsigned long long offset = simple_strtoull(buf, &e, 10);
2287 if (e==buf || (*e && *e != '\n'))
2288 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002289 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002290 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002291 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002292 /* Must set offset before size, so overlap checks
2293 * can be sane */
2294 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002295 rdev->data_offset = offset;
2296 return len;
2297}
2298
2299static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002300__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002301
NeilBrown83303b62006-01-06 00:21:06 -08002302static ssize_t
2303rdev_size_show(mdk_rdev_t *rdev, char *page)
2304{
Andre Nolldd8ac332009-03-31 14:33:13 +11002305 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002306}
2307
NeilBrownc5d79ad2008-02-06 01:39:54 -08002308static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2309{
2310 /* check if two start/length pairs overlap */
2311 if (s1+l1 <= s2)
2312 return 0;
2313 if (s2+l2 <= s1)
2314 return 0;
2315 return 1;
2316}
2317
Dan Williamsb522adc2009-03-31 15:00:31 +11002318static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2319{
2320 unsigned long long blocks;
2321 sector_t new;
2322
2323 if (strict_strtoull(buf, 10, &blocks) < 0)
2324 return -EINVAL;
2325
2326 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2327 return -EINVAL; /* sector conversion overflow */
2328
2329 new = blocks * 2;
2330 if (new != blocks * 2)
2331 return -EINVAL; /* unsigned long long to sector_t overflow */
2332
2333 *sectors = new;
2334 return 0;
2335}
2336
NeilBrown83303b62006-01-06 00:21:06 -08002337static ssize_t
2338rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2339{
NeilBrown27c529b2008-03-04 14:29:33 -08002340 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002341 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002342 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002343
Dan Williamsb522adc2009-03-31 15:00:31 +11002344 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002345 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002346 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002347 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002348 sectors = super_types[my_mddev->major_version].
2349 rdev_size_change(rdev, sectors);
2350 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002351 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002352 } else if (!sectors)
2353 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2354 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002355 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002356 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002357 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002358
Andre Nolldd8ac332009-03-31 14:33:13 +11002359 rdev->sectors = sectors;
2360 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002361 /* need to check that all other rdevs with the same ->bdev
2362 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002363 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002364 * we have to change it back, we will have the lock again.
2365 */
2366 mddev_t *mddev;
2367 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002368 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002369
NeilBrown27c529b2008-03-04 14:29:33 -08002370 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002371 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002372 mdk_rdev_t *rdev2;
2373
2374 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002375 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002376 if (test_bit(AllReserved, &rdev2->flags) ||
2377 (rdev->bdev == rdev2->bdev &&
2378 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002379 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002380 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002381 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002382 overlap = 1;
2383 break;
2384 }
2385 mddev_unlock(mddev);
2386 if (overlap) {
2387 mddev_put(mddev);
2388 break;
2389 }
2390 }
NeilBrown27c529b2008-03-04 14:29:33 -08002391 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002392 if (overlap) {
2393 /* Someone else could have slipped in a size
2394 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002395 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002396 * safe, and trust userspace not to race with
2397 * itself
2398 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002399 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002400 return -EBUSY;
2401 }
2402 }
NeilBrown83303b62006-01-06 00:21:06 -08002403 return len;
2404}
2405
2406static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002407__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002408
NeilBrown86e6ffd2005-11-08 21:39:24 -08002409static struct attribute *rdev_default_attrs[] = {
2410 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002411 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002412 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002413 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002414 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002415 NULL,
2416};
2417static ssize_t
2418rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2419{
2420 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2421 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002422 mddev_t *mddev = rdev->mddev;
2423 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002424
2425 if (!entry->show)
2426 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002427
2428 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2429 if (!rv) {
2430 if (rdev->mddev == NULL)
2431 rv = -EBUSY;
2432 else
2433 rv = entry->show(rdev, page);
2434 mddev_unlock(mddev);
2435 }
2436 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002437}
2438
2439static ssize_t
2440rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2441 const char *page, size_t length)
2442{
2443 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2444 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002445 ssize_t rv;
2446 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002447
2448 if (!entry->store)
2449 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002450 if (!capable(CAP_SYS_ADMIN))
2451 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002452 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002453 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002454 if (rdev->mddev == NULL)
2455 rv = -EBUSY;
2456 else
2457 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002458 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002459 }
2460 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002461}
2462
2463static void rdev_free(struct kobject *ko)
2464{
2465 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2466 kfree(rdev);
2467}
2468static struct sysfs_ops rdev_sysfs_ops = {
2469 .show = rdev_attr_show,
2470 .store = rdev_attr_store,
2471};
2472static struct kobj_type rdev_ktype = {
2473 .release = rdev_free,
2474 .sysfs_ops = &rdev_sysfs_ops,
2475 .default_attrs = rdev_default_attrs,
2476};
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/*
2479 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2480 *
2481 * mark the device faulty if:
2482 *
2483 * - the device is nonexistent (zero size)
2484 * - the device has no valid superblock
2485 *
2486 * a faulty rdev _never_ has rdev->sb set.
2487 */
2488static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2489{
2490 char b[BDEVNAME_SIZE];
2491 int err;
2492 mdk_rdev_t *rdev;
2493 sector_t size;
2494
NeilBrown9ffae0c2006-01-06 00:20:32 -08002495 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (!rdev) {
2497 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2498 return ERR_PTR(-ENOMEM);
2499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 if ((err = alloc_disk_sb(rdev)))
2502 goto abort_free;
2503
NeilBrownc5d79ad2008-02-06 01:39:54 -08002504 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (err)
2506 goto abort_free;
2507
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002508 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002511 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002512 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002513 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002515 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002517 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002518 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2521 if (!size) {
2522 printk(KERN_WARNING
2523 "md: %s has zero or unknown size, marking faulty!\n",
2524 bdevname(rdev->bdev,b));
2525 err = -EINVAL;
2526 goto abort_free;
2527 }
2528
2529 if (super_format >= 0) {
2530 err = super_types[super_format].
2531 load_super(rdev, NULL, super_minor);
2532 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002533 printk(KERN_WARNING
2534 "md: %s does not have a valid v%d.%d "
2535 "superblock, not importing!\n",
2536 bdevname(rdev->bdev,b),
2537 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 goto abort_free;
2539 }
2540 if (err < 0) {
2541 printk(KERN_WARNING
2542 "md: could not read %s's sb, not importing!\n",
2543 bdevname(rdev->bdev,b));
2544 goto abort_free;
2545 }
2546 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002549 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 return rdev;
2552
2553abort_free:
2554 if (rdev->sb_page) {
2555 if (rdev->bdev)
2556 unlock_rdev(rdev);
2557 free_disk_sb(rdev);
2558 }
2559 kfree(rdev);
2560 return ERR_PTR(err);
2561}
2562
2563/*
2564 * Check a full RAID array for plausibility
2565 */
2566
2567
NeilBrowna757e642005-04-16 15:26:42 -07002568static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002571 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 char b[BDEVNAME_SIZE];
2573
2574 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002575 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 switch (super_types[mddev->major_version].
2577 load_super(rdev, freshest, mddev->minor_version)) {
2578 case 1:
2579 freshest = rdev;
2580 break;
2581 case 0:
2582 break;
2583 default:
2584 printk( KERN_ERR \
2585 "md: fatal superblock inconsistency in %s"
2586 " -- removing from array\n",
2587 bdevname(rdev->bdev,b));
2588 kick_rdev_from_array(rdev);
2589 }
2590
2591
2592 super_types[mddev->major_version].
2593 validate_super(mddev, freshest);
2594
2595 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002596 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002597 if (rdev->desc_nr >= mddev->max_disks ||
2598 i > mddev->max_disks) {
2599 printk(KERN_WARNING
2600 "md: %s: %s: only %d devices permitted\n",
2601 mdname(mddev), bdevname(rdev->bdev, b),
2602 mddev->max_disks);
2603 kick_rdev_from_array(rdev);
2604 continue;
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (rdev != freshest)
2607 if (super_types[mddev->major_version].
2608 validate_super(mddev, rdev)) {
2609 printk(KERN_WARNING "md: kicking non-fresh %s"
2610 " from array!\n",
2611 bdevname(rdev->bdev,b));
2612 kick_rdev_from_array(rdev);
2613 continue;
2614 }
2615 if (mddev->level == LEVEL_MULTIPATH) {
2616 rdev->desc_nr = i++;
2617 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002618 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002619 } else if (rdev->raid_disk >= mddev->raid_disks) {
2620 rdev->raid_disk = -1;
2621 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
NeilBrown19052c02008-08-05 15:54:13 +10002626static void md_safemode_timeout(unsigned long data);
2627
NeilBrowneae17012005-11-08 21:39:23 -08002628static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002629safe_delay_show(mddev_t *mddev, char *page)
2630{
2631 int msec = (mddev->safemode_delay*1000)/HZ;
2632 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2633}
2634static ssize_t
2635safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2636{
2637 int scale=1;
2638 int dot=0;
2639 int i;
2640 unsigned long msec;
2641 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002642
NeilBrown16f17b32006-06-26 00:27:37 -07002643 /* remove a period, and count digits after it */
2644 if (len >= sizeof(buf))
2645 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002646 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002647 for (i=0; i<len; i++) {
2648 if (dot) {
2649 if (isdigit(buf[i])) {
2650 buf[i-1] = buf[i];
2651 scale *= 10;
2652 }
2653 buf[i] = 0;
2654 } else if (buf[i] == '.') {
2655 dot=1;
2656 buf[i] = 0;
2657 }
2658 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002659 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002660 return -EINVAL;
2661 msec = (msec * 1000) / scale;
2662 if (msec == 0)
2663 mddev->safemode_delay = 0;
2664 else {
NeilBrown19052c02008-08-05 15:54:13 +10002665 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002666 mddev->safemode_delay = (msec*HZ)/1000;
2667 if (mddev->safemode_delay == 0)
2668 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002669 if (mddev->safemode_delay < old_delay)
2670 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002671 }
2672 return len;
2673}
2674static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002675__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002676
2677static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002678level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002679{
NeilBrown2604b702006-01-06 00:20:36 -08002680 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002681 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002682 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002683 else if (mddev->clevel[0])
2684 return sprintf(page, "%s\n", mddev->clevel);
2685 else if (mddev->level != LEVEL_NONE)
2686 return sprintf(page, "%d\n", mddev->level);
2687 else
2688 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002689}
2690
NeilBrownd9d166c2006-01-06 00:20:51 -08002691static ssize_t
2692level_store(mddev_t *mddev, const char *buf, size_t len)
2693{
NeilBrown245f46c2009-03-31 14:39:39 +11002694 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002695 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002696 struct mdk_personality *pers;
2697 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002698 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002699
2700 if (mddev->pers == NULL) {
2701 if (len == 0)
2702 return 0;
2703 if (len >= sizeof(mddev->clevel))
2704 return -ENOSPC;
2705 strncpy(mddev->clevel, buf, len);
2706 if (mddev->clevel[len-1] == '\n')
2707 len--;
2708 mddev->clevel[len] = 0;
2709 mddev->level = LEVEL_NONE;
2710 return rv;
2711 }
2712
2713 /* request to change the personality. Need to ensure:
2714 * - array is not engaged in resync/recovery/reshape
2715 * - old personality can be suspended
2716 * - new personality will access other array.
2717 */
2718
2719 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002720 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002721
2722 if (!mddev->pers->quiesce) {
2723 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2724 mdname(mddev), mddev->pers->name);
2725 return -EINVAL;
2726 }
2727
2728 /* Now find the new personality */
2729 if (len == 0 || len >= sizeof(level))
2730 return -EINVAL;
2731 strncpy(level, buf, len);
2732 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002733 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002734 level[len] = 0;
2735
2736 request_module("md-%s", level);
2737 spin_lock(&pers_lock);
2738 pers = find_pers(LEVEL_NONE, level);
2739 if (!pers || !try_module_get(pers->owner)) {
2740 spin_unlock(&pers_lock);
2741 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2742 return -EINVAL;
2743 }
2744 spin_unlock(&pers_lock);
2745
2746 if (pers == mddev->pers) {
2747 /* Nothing to do! */
2748 module_put(pers->owner);
2749 return rv;
2750 }
2751 if (!pers->takeover) {
2752 module_put(pers->owner);
2753 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2754 mdname(mddev), level);
2755 return -EINVAL;
2756 }
2757
2758 /* ->takeover must set new_* and/or delta_disks
2759 * if it succeeds, and may set them when it fails.
2760 */
2761 priv = pers->takeover(mddev);
2762 if (IS_ERR(priv)) {
2763 mddev->new_level = mddev->level;
2764 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002765 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002766 mddev->raid_disks -= mddev->delta_disks;
2767 mddev->delta_disks = 0;
2768 module_put(pers->owner);
2769 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2770 mdname(mddev), level);
2771 return PTR_ERR(priv);
2772 }
2773
2774 /* Looks like we have a winner */
2775 mddev_suspend(mddev);
2776 mddev->pers->stop(mddev);
2777 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002778 /* Invalidate devices that are now superfluous */
2779 list_for_each_entry(rdev, &mddev->disks, same_set)
2780 if (rdev->raid_disk >= mddev->raid_disks) {
2781 rdev->raid_disk = -1;
2782 clear_bit(In_sync, &rdev->flags);
2783 }
NeilBrown245f46c2009-03-31 14:39:39 +11002784 mddev->pers = pers;
2785 mddev->private = priv;
2786 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2787 mddev->level = mddev->new_level;
2788 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002789 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002790 mddev->delta_disks = 0;
2791 pers->run(mddev);
2792 mddev_resume(mddev);
2793 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2794 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2795 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002796 return rv;
2797}
2798
2799static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002800__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002801
NeilBrownd4dbd022006-06-26 00:27:59 -07002802
2803static ssize_t
2804layout_show(mddev_t *mddev, char *page)
2805{
2806 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002807 if (mddev->reshape_position != MaxSector &&
2808 mddev->layout != mddev->new_layout)
2809 return sprintf(page, "%d (%d)\n",
2810 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002811 return sprintf(page, "%d\n", mddev->layout);
2812}
2813
2814static ssize_t
2815layout_store(mddev_t *mddev, const char *buf, size_t len)
2816{
2817 char *e;
2818 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002819
2820 if (!*buf || (*e && *e != '\n'))
2821 return -EINVAL;
2822
NeilBrownb3546032009-03-31 14:56:41 +11002823 if (mddev->pers) {
2824 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002825 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002826 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002827 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002828 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002829 if (err) {
2830 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002831 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002832 }
NeilBrownb3546032009-03-31 14:56:41 +11002833 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002834 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002835 if (mddev->reshape_position == MaxSector)
2836 mddev->layout = n;
2837 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002838 return len;
2839}
2840static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002841__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002842
2843
NeilBrowneae17012005-11-08 21:39:23 -08002844static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002845raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002846{
NeilBrownbb636542005-11-08 21:39:45 -08002847 if (mddev->raid_disks == 0)
2848 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002849 if (mddev->reshape_position != MaxSector &&
2850 mddev->delta_disks != 0)
2851 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2852 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002853 return sprintf(page, "%d\n", mddev->raid_disks);
2854}
2855
NeilBrownda943b992006-01-06 00:20:54 -08002856static int update_raid_disks(mddev_t *mddev, int raid_disks);
2857
2858static ssize_t
2859raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2860{
NeilBrownda943b992006-01-06 00:20:54 -08002861 char *e;
2862 int rv = 0;
2863 unsigned long n = simple_strtoul(buf, &e, 10);
2864
2865 if (!*buf || (*e && *e != '\n'))
2866 return -EINVAL;
2867
2868 if (mddev->pers)
2869 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002870 else if (mddev->reshape_position != MaxSector) {
2871 int olddisks = mddev->raid_disks - mddev->delta_disks;
2872 mddev->delta_disks = n - olddisks;
2873 mddev->raid_disks = n;
2874 } else
NeilBrownda943b992006-01-06 00:20:54 -08002875 mddev->raid_disks = n;
2876 return rv ? rv : len;
2877}
2878static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002879__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002880
NeilBrown24dd4692005-11-08 21:39:26 -08002881static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002882chunk_size_show(mddev_t *mddev, char *page)
2883{
NeilBrown08a02ec2007-05-09 02:35:38 -07002884 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10002885 mddev->chunk_sectors != mddev->new_chunk_sectors)
2886 return sprintf(page, "%d (%d)\n",
2887 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10002888 mddev->chunk_sectors << 9);
2889 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08002890}
2891
2892static ssize_t
2893chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2894{
NeilBrown3b343802006-01-06 00:20:47 -08002895 char *e;
2896 unsigned long n = simple_strtoul(buf, &e, 10);
2897
NeilBrown3b343802006-01-06 00:20:47 -08002898 if (!*buf || (*e && *e != '\n'))
2899 return -EINVAL;
2900
NeilBrownb3546032009-03-31 14:56:41 +11002901 if (mddev->pers) {
2902 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002903 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002904 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002905 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10002906 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002907 if (err) {
2908 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11002909 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002910 }
NeilBrownb3546032009-03-31 14:56:41 +11002911 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10002912 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002913 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10002914 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002915 }
NeilBrown3b343802006-01-06 00:20:47 -08002916 return len;
2917}
2918static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002919__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002920
NeilBrowna94213b2006-06-26 00:28:00 -07002921static ssize_t
2922resync_start_show(mddev_t *mddev, char *page)
2923{
NeilBrownd1a7c502009-03-31 15:24:32 +11002924 if (mddev->recovery_cp == MaxSector)
2925 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002926 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2927}
2928
2929static ssize_t
2930resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2931{
NeilBrowna94213b2006-06-26 00:28:00 -07002932 char *e;
2933 unsigned long long n = simple_strtoull(buf, &e, 10);
2934
2935 if (mddev->pers)
2936 return -EBUSY;
2937 if (!*buf || (*e && *e != '\n'))
2938 return -EINVAL;
2939
2940 mddev->recovery_cp = n;
2941 return len;
2942}
2943static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002944__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002945
NeilBrown9e653b62006-06-26 00:27:58 -07002946/*
2947 * The array state can be:
2948 *
2949 * clear
2950 * No devices, no size, no level
2951 * Equivalent to STOP_ARRAY ioctl
2952 * inactive
2953 * May have some settings, but array is not active
2954 * all IO results in error
2955 * When written, doesn't tear down array, but just stops it
2956 * suspended (not supported yet)
2957 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002958 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002959 * readonly
2960 * no resync can happen. no superblocks get written.
2961 * write requests fail
2962 * read-auto
2963 * like readonly, but behaves like 'clean' on a write request.
2964 *
2965 * clean - no pending writes, but otherwise active.
2966 * When written to inactive array, starts without resync
2967 * If a write request arrives then
2968 * if metadata is known, mark 'dirty' and switch to 'active'.
2969 * if not known, block and switch to write-pending
2970 * If written to an active array that has pending writes, then fails.
2971 * active
2972 * fully active: IO and resync can be happening.
2973 * When written to inactive array, starts with resync
2974 *
2975 * write-pending
2976 * clean, but writes are blocked waiting for 'active' to be written.
2977 *
2978 * active-idle
2979 * like active, but no writes have been seen for a while (100msec).
2980 *
2981 */
2982enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2983 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002984static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002985 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2986 "write-pending", "active-idle", NULL };
2987
2988static int match_word(const char *word, char **list)
2989{
2990 int n;
2991 for (n=0; list[n]; n++)
2992 if (cmd_match(word, list[n]))
2993 break;
2994 return n;
2995}
2996
2997static ssize_t
2998array_state_show(mddev_t *mddev, char *page)
2999{
3000 enum array_state st = inactive;
3001
3002 if (mddev->pers)
3003 switch(mddev->ro) {
3004 case 1:
3005 st = readonly;
3006 break;
3007 case 2:
3008 st = read_auto;
3009 break;
3010 case 0:
3011 if (mddev->in_sync)
3012 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003013 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3014 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003015 else if (mddev->safemode)
3016 st = active_idle;
3017 else
3018 st = active;
3019 }
3020 else {
3021 if (list_empty(&mddev->disks) &&
3022 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003023 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003024 st = clear;
3025 else
3026 st = inactive;
3027 }
3028 return sprintf(page, "%s\n", array_states[st]);
3029}
3030
Neil Browndf5b20c2008-07-11 22:02:22 +10003031static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003032static int do_md_run(mddev_t * mddev);
3033static int restart_array(mddev_t *mddev);
3034
3035static ssize_t
3036array_state_store(mddev_t *mddev, const char *buf, size_t len)
3037{
3038 int err = -EINVAL;
3039 enum array_state st = match_word(buf, array_states);
3040 switch(st) {
3041 case bad_word:
3042 break;
3043 case clear:
3044 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003045 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003046 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003047 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003048 break;
3049 case inactive:
3050 /* stopping an active array */
3051 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003052 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003053 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003054 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003055 } else
3056 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003057 break;
3058 case suspended:
3059 break; /* not supported yet */
3060 case readonly:
3061 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003062 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003063 else {
3064 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003065 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003066 err = do_md_run(mddev);
3067 }
3068 break;
3069 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003070 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003071 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003072 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003073 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003074 err = restart_array(mddev);
3075 if (err == 0) {
3076 mddev->ro = 2;
3077 set_disk_ro(mddev->gendisk, 0);
3078 }
NeilBrown9e653b62006-06-26 00:27:58 -07003079 } else {
3080 mddev->ro = 2;
3081 err = do_md_run(mddev);
3082 }
3083 break;
3084 case clean:
3085 if (mddev->pers) {
3086 restart_array(mddev);
3087 spin_lock_irq(&mddev->write_lock);
3088 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003089 if (mddev->in_sync == 0) {
3090 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003091 if (mddev->safemode == 1)
3092 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003093 if (mddev->persistent)
3094 set_bit(MD_CHANGE_CLEAN,
3095 &mddev->flags);
3096 }
3097 err = 0;
3098 } else
3099 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003100 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003101 } else
3102 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003103 break;
3104 case active:
3105 if (mddev->pers) {
3106 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003107 if (mddev->external)
3108 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003109 wake_up(&mddev->sb_wait);
3110 err = 0;
3111 } else {
3112 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003113 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003114 err = do_md_run(mddev);
3115 }
3116 break;
3117 case write_pending:
3118 case active_idle:
3119 /* these cannot be set */
3120 break;
3121 }
3122 if (err)
3123 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003124 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003125 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003126 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003127 }
NeilBrown9e653b62006-06-26 00:27:58 -07003128}
NeilBrown80ca3a42006-07-10 04:44:18 -07003129static struct md_sysfs_entry md_array_state =
3130__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003131
NeilBrown6d7ff7382006-01-06 00:21:16 -08003132static ssize_t
3133null_show(mddev_t *mddev, char *page)
3134{
3135 return -EINVAL;
3136}
3137
3138static ssize_t
3139new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3140{
3141 /* buf must be %d:%d\n? giving major and minor numbers */
3142 /* The new device is added to the array.
3143 * If the array has a persistent superblock, we read the
3144 * superblock to initialise info and check validity.
3145 * Otherwise, only checking done is that in bind_rdev_to_array,
3146 * which mainly checks size.
3147 */
3148 char *e;
3149 int major = simple_strtoul(buf, &e, 10);
3150 int minor;
3151 dev_t dev;
3152 mdk_rdev_t *rdev;
3153 int err;
3154
3155 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3156 return -EINVAL;
3157 minor = simple_strtoul(e+1, &e, 10);
3158 if (*e && *e != '\n')
3159 return -EINVAL;
3160 dev = MKDEV(major, minor);
3161 if (major != MAJOR(dev) ||
3162 minor != MINOR(dev))
3163 return -EOVERFLOW;
3164
3165
3166 if (mddev->persistent) {
3167 rdev = md_import_device(dev, mddev->major_version,
3168 mddev->minor_version);
3169 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3170 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3171 mdk_rdev_t, same_set);
3172 err = super_types[mddev->major_version]
3173 .load_super(rdev, rdev0, mddev->minor_version);
3174 if (err < 0)
3175 goto out;
3176 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003177 } else if (mddev->external)
3178 rdev = md_import_device(dev, -2, -1);
3179 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003180 rdev = md_import_device(dev, -1, -1);
3181
3182 if (IS_ERR(rdev))
3183 return PTR_ERR(rdev);
3184 err = bind_rdev_to_array(rdev, mddev);
3185 out:
3186 if (err)
3187 export_rdev(rdev);
3188 return err ? err : len;
3189}
3190
3191static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003192__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003193
3194static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003195bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3196{
3197 char *end;
3198 unsigned long chunk, end_chunk;
3199
3200 if (!mddev->bitmap)
3201 goto out;
3202 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3203 while (*buf) {
3204 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3205 if (buf == end) break;
3206 if (*end == '-') { /* range */
3207 buf = end + 1;
3208 end_chunk = simple_strtoul(buf, &end, 0);
3209 if (buf == end) break;
3210 }
3211 if (*end && !isspace(*end)) break;
3212 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3213 buf = end;
3214 while (isspace(*buf)) buf++;
3215 }
3216 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3217out:
3218 return len;
3219}
3220
3221static struct md_sysfs_entry md_bitmap =
3222__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3223
3224static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003225size_show(mddev_t *mddev, char *page)
3226{
Andre Noll58c0fed2009-03-31 14:33:13 +11003227 return sprintf(page, "%llu\n",
3228 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003229}
3230
Andre Nolld71f9f82008-07-11 22:02:22 +10003231static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003232
3233static ssize_t
3234size_store(mddev_t *mddev, const char *buf, size_t len)
3235{
3236 /* If array is inactive, we can reduce the component size, but
3237 * not increase it (except from 0).
3238 * If array is active, we can try an on-line resize
3239 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003240 sector_t sectors;
3241 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003242
Andre Noll58c0fed2009-03-31 14:33:13 +11003243 if (err < 0)
3244 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003245 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003246 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003247 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003248 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003249 if (mddev->dev_sectors == 0 ||
3250 mddev->dev_sectors > sectors)
3251 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003252 else
3253 err = -ENOSPC;
3254 }
3255 return err ? err : len;
3256}
3257
3258static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003259__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003260
NeilBrown8bb93aa2006-01-06 00:20:50 -08003261
3262/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003263 * This is one of
3264 * 'none' for arrays with no metadata (good luck...)
3265 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003266 * or N.M for internally known formats
3267 */
3268static ssize_t
3269metadata_show(mddev_t *mddev, char *page)
3270{
3271 if (mddev->persistent)
3272 return sprintf(page, "%d.%d\n",
3273 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003274 else if (mddev->external)
3275 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003276 else
3277 return sprintf(page, "none\n");
3278}
3279
3280static ssize_t
3281metadata_store(mddev_t *mddev, const char *buf, size_t len)
3282{
3283 int major, minor;
3284 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003285 /* Changing the details of 'external' metadata is
3286 * always permitted. Otherwise there must be
3287 * no devices attached to the array.
3288 */
3289 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3290 ;
3291 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003292 return -EBUSY;
3293
3294 if (cmd_match(buf, "none")) {
3295 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003296 mddev->external = 0;
3297 mddev->major_version = 0;
3298 mddev->minor_version = 90;
3299 return len;
3300 }
3301 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003302 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003303 if (namelen >= sizeof(mddev->metadata_type))
3304 namelen = sizeof(mddev->metadata_type)-1;
3305 strncpy(mddev->metadata_type, buf+9, namelen);
3306 mddev->metadata_type[namelen] = 0;
3307 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3308 mddev->metadata_type[--namelen] = 0;
3309 mddev->persistent = 0;
3310 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003311 mddev->major_version = 0;
3312 mddev->minor_version = 90;
3313 return len;
3314 }
3315 major = simple_strtoul(buf, &e, 10);
3316 if (e==buf || *e != '.')
3317 return -EINVAL;
3318 buf = e+1;
3319 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003320 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003321 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003322 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003323 return -ENOENT;
3324 mddev->major_version = major;
3325 mddev->minor_version = minor;
3326 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003327 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003328 return len;
3329}
3330
3331static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003332__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003333
NeilBrowna35b0d62006-01-06 00:20:49 -08003334static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003335action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003336{
NeilBrown7eec3142005-11-08 21:39:44 -08003337 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003338 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3339 type = "frozen";
3340 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003341 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003342 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3343 type = "reshape";
3344 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003345 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3346 type = "resync";
3347 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3348 type = "check";
3349 else
3350 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003351 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003352 type = "recover";
3353 }
3354 return sprintf(page, "%s\n", type);
3355}
3356
3357static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003358action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003359{
NeilBrown7eec3142005-11-08 21:39:44 -08003360 if (!mddev->pers || !mddev->pers->sync_request)
3361 return -EINVAL;
3362
NeilBrownb6a9ce62009-05-26 09:41:17 +10003363 if (cmd_match(page, "frozen"))
3364 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3365 else
3366 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3367
3368 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003369 if (mddev->sync_thread) {
3370 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3371 md_unregister_thread(mddev->sync_thread);
3372 mddev->sync_thread = NULL;
3373 mddev->recovery = 0;
3374 }
NeilBrown03c902e2006-01-06 00:20:46 -08003375 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3376 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003377 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003378 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003379 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003380 else if (cmd_match(page, "recover")) {
3381 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3382 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3383 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003384 int err;
3385 if (mddev->pers->start_reshape == NULL)
3386 return -EINVAL;
3387 err = mddev->pers->start_reshape(mddev);
3388 if (err)
3389 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003390 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003391 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003392 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003393 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003394 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003395 return -EINVAL;
3396 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3397 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003398 }
NeilBrown03c902e2006-01-06 00:20:46 -08003399 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003400 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003401 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003402 return len;
3403}
3404
NeilBrown9d888832005-11-08 21:39:26 -08003405static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003406mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003407{
3408 return sprintf(page, "%llu\n",
3409 (unsigned long long) mddev->resync_mismatches);
3410}
3411
NeilBrown80ca3a42006-07-10 04:44:18 -07003412static struct md_sysfs_entry md_scan_mode =
3413__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003414
NeilBrown96de1e62005-11-08 21:39:39 -08003415
NeilBrown80ca3a42006-07-10 04:44:18 -07003416static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003417
NeilBrown88202a02006-01-06 00:21:36 -08003418static ssize_t
3419sync_min_show(mddev_t *mddev, char *page)
3420{
3421 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3422 mddev->sync_speed_min ? "local": "system");
3423}
3424
3425static ssize_t
3426sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3427{
3428 int min;
3429 char *e;
3430 if (strncmp(buf, "system", 6)==0) {
3431 mddev->sync_speed_min = 0;
3432 return len;
3433 }
3434 min = simple_strtoul(buf, &e, 10);
3435 if (buf == e || (*e && *e != '\n') || min <= 0)
3436 return -EINVAL;
3437 mddev->sync_speed_min = min;
3438 return len;
3439}
3440
3441static struct md_sysfs_entry md_sync_min =
3442__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3443
3444static ssize_t
3445sync_max_show(mddev_t *mddev, char *page)
3446{
3447 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3448 mddev->sync_speed_max ? "local": "system");
3449}
3450
3451static ssize_t
3452sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3453{
3454 int max;
3455 char *e;
3456 if (strncmp(buf, "system", 6)==0) {
3457 mddev->sync_speed_max = 0;
3458 return len;
3459 }
3460 max = simple_strtoul(buf, &e, 10);
3461 if (buf == e || (*e && *e != '\n') || max <= 0)
3462 return -EINVAL;
3463 mddev->sync_speed_max = max;
3464 return len;
3465}
3466
3467static struct md_sysfs_entry md_sync_max =
3468__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3469
Iustin Popd7f3d292007-10-16 23:30:54 -07003470static ssize_t
3471degraded_show(mddev_t *mddev, char *page)
3472{
3473 return sprintf(page, "%d\n", mddev->degraded);
3474}
3475static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003476
3477static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003478sync_force_parallel_show(mddev_t *mddev, char *page)
3479{
3480 return sprintf(page, "%d\n", mddev->parallel_resync);
3481}
3482
3483static ssize_t
3484sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3485{
3486 long n;
3487
3488 if (strict_strtol(buf, 10, &n))
3489 return -EINVAL;
3490
3491 if (n != 0 && n != 1)
3492 return -EINVAL;
3493
3494 mddev->parallel_resync = n;
3495
3496 if (mddev->sync_thread)
3497 wake_up(&resync_wait);
3498
3499 return len;
3500}
3501
3502/* force parallel resync, even with shared block devices */
3503static struct md_sysfs_entry md_sync_force_parallel =
3504__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3505 sync_force_parallel_show, sync_force_parallel_store);
3506
3507static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003508sync_speed_show(mddev_t *mddev, char *page)
3509{
3510 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003511 if (mddev->curr_resync == 0)
3512 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003513 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3514 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003515 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003516 db = resync - mddev->resync_mark_cnt;
3517 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003518}
3519
NeilBrown80ca3a42006-07-10 04:44:18 -07003520static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003521
3522static ssize_t
3523sync_completed_show(mddev_t *mddev, char *page)
3524{
Andre Noll58c0fed2009-03-31 14:33:13 +11003525 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003526
NeilBrownacb180b2009-04-14 16:28:34 +10003527 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3528 return sprintf(page, "none\n");
3529
NeilBrown88202a02006-01-06 00:21:36 -08003530 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003531 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003532 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003533 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003534
NeilBrownacb180b2009-04-14 16:28:34 +10003535 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003536 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003537}
3538
NeilBrown80ca3a42006-07-10 04:44:18 -07003539static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003540
NeilBrowne464eaf2006-03-27 01:18:14 -08003541static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003542min_sync_show(mddev_t *mddev, char *page)
3543{
3544 return sprintf(page, "%llu\n",
3545 (unsigned long long)mddev->resync_min);
3546}
3547static ssize_t
3548min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3549{
3550 unsigned long long min;
3551 if (strict_strtoull(buf, 10, &min))
3552 return -EINVAL;
3553 if (min > mddev->resync_max)
3554 return -EINVAL;
3555 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3556 return -EBUSY;
3557
3558 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003559 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003560 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003561 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003562 return -EINVAL;
3563 }
3564 mddev->resync_min = min;
3565
3566 return len;
3567}
3568
3569static struct md_sysfs_entry md_min_sync =
3570__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3571
3572static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003573max_sync_show(mddev_t *mddev, char *page)
3574{
3575 if (mddev->resync_max == MaxSector)
3576 return sprintf(page, "max\n");
3577 else
3578 return sprintf(page, "%llu\n",
3579 (unsigned long long)mddev->resync_max);
3580}
3581static ssize_t
3582max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3583{
3584 if (strncmp(buf, "max", 3) == 0)
3585 mddev->resync_max = MaxSector;
3586 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003587 unsigned long long max;
3588 if (strict_strtoull(buf, 10, &max))
3589 return -EINVAL;
3590 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003591 return -EINVAL;
3592 if (max < mddev->resync_max &&
3593 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3594 return -EBUSY;
3595
3596 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003597 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003598 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003599 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003600 return -EINVAL;
3601 }
3602 mddev->resync_max = max;
3603 }
3604 wake_up(&mddev->recovery_wait);
3605 return len;
3606}
3607
3608static struct md_sysfs_entry md_max_sync =
3609__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3610
3611static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003612suspend_lo_show(mddev_t *mddev, char *page)
3613{
3614 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3615}
3616
3617static ssize_t
3618suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3619{
3620 char *e;
3621 unsigned long long new = simple_strtoull(buf, &e, 10);
3622
NeilBrownb8d966e2009-07-01 11:14:04 +10003623 if (mddev->pers == NULL ||
3624 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003625 return -EINVAL;
3626 if (buf == e || (*e && *e != '\n'))
3627 return -EINVAL;
3628 if (new >= mddev->suspend_hi ||
3629 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3630 mddev->suspend_lo = new;
3631 mddev->pers->quiesce(mddev, 2);
3632 return len;
3633 } else
3634 return -EINVAL;
3635}
3636static struct md_sysfs_entry md_suspend_lo =
3637__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3638
3639
3640static ssize_t
3641suspend_hi_show(mddev_t *mddev, char *page)
3642{
3643 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3644}
3645
3646static ssize_t
3647suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3648{
3649 char *e;
3650 unsigned long long new = simple_strtoull(buf, &e, 10);
3651
NeilBrownb8d966e2009-07-01 11:14:04 +10003652 if (mddev->pers == NULL ||
3653 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003654 return -EINVAL;
3655 if (buf == e || (*e && *e != '\n'))
3656 return -EINVAL;
3657 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3658 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3659 mddev->suspend_hi = new;
3660 mddev->pers->quiesce(mddev, 1);
3661 mddev->pers->quiesce(mddev, 0);
3662 return len;
3663 } else
3664 return -EINVAL;
3665}
3666static struct md_sysfs_entry md_suspend_hi =
3667__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3668
NeilBrown08a02ec2007-05-09 02:35:38 -07003669static ssize_t
3670reshape_position_show(mddev_t *mddev, char *page)
3671{
3672 if (mddev->reshape_position != MaxSector)
3673 return sprintf(page, "%llu\n",
3674 (unsigned long long)mddev->reshape_position);
3675 strcpy(page, "none\n");
3676 return 5;
3677}
3678
3679static ssize_t
3680reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3681{
3682 char *e;
3683 unsigned long long new = simple_strtoull(buf, &e, 10);
3684 if (mddev->pers)
3685 return -EBUSY;
3686 if (buf == e || (*e && *e != '\n'))
3687 return -EINVAL;
3688 mddev->reshape_position = new;
3689 mddev->delta_disks = 0;
3690 mddev->new_level = mddev->level;
3691 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003692 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003693 return len;
3694}
3695
3696static struct md_sysfs_entry md_reshape_position =
3697__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3698 reshape_position_store);
3699
Dan Williamsb522adc2009-03-31 15:00:31 +11003700static ssize_t
3701array_size_show(mddev_t *mddev, char *page)
3702{
3703 if (mddev->external_size)
3704 return sprintf(page, "%llu\n",
3705 (unsigned long long)mddev->array_sectors/2);
3706 else
3707 return sprintf(page, "default\n");
3708}
3709
3710static ssize_t
3711array_size_store(mddev_t *mddev, const char *buf, size_t len)
3712{
3713 sector_t sectors;
3714
3715 if (strncmp(buf, "default", 7) == 0) {
3716 if (mddev->pers)
3717 sectors = mddev->pers->size(mddev, 0, 0);
3718 else
3719 sectors = mddev->array_sectors;
3720
3721 mddev->external_size = 0;
3722 } else {
3723 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3724 return -EINVAL;
3725 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003726 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003727
3728 mddev->external_size = 1;
3729 }
3730
3731 mddev->array_sectors = sectors;
3732 set_capacity(mddev->gendisk, mddev->array_sectors);
3733 if (mddev->pers) {
3734 struct block_device *bdev = bdget_disk(mddev->gendisk, 0);
3735
3736 if (bdev) {
3737 mutex_lock(&bdev->bd_inode->i_mutex);
3738 i_size_write(bdev->bd_inode,
3739 (loff_t)mddev->array_sectors << 9);
3740 mutex_unlock(&bdev->bd_inode->i_mutex);
3741 bdput(bdev);
3742 }
3743 }
3744
3745 return len;
3746}
3747
3748static struct md_sysfs_entry md_array_size =
3749__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3750 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003751
NeilBrowneae17012005-11-08 21:39:23 -08003752static struct attribute *md_default_attrs[] = {
3753 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003754 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003755 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003756 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003757 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003758 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003759 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003760 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003761 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003762 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003763 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003764 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003765 NULL,
3766};
3767
3768static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003769 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003770 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003771 &md_sync_min.attr,
3772 &md_sync_max.attr,
3773 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003774 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003775 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003776 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003777 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003778 &md_suspend_lo.attr,
3779 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003780 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003781 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003782 NULL,
3783};
NeilBrown411036f2005-11-08 21:39:40 -08003784static struct attribute_group md_redundancy_group = {
3785 .name = NULL,
3786 .attrs = md_redundancy_attrs,
3787};
3788
NeilBrowneae17012005-11-08 21:39:23 -08003789
3790static ssize_t
3791md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3792{
3793 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3794 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003795 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003796
3797 if (!entry->show)
3798 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003799 rv = mddev_lock(mddev);
3800 if (!rv) {
3801 rv = entry->show(mddev, page);
3802 mddev_unlock(mddev);
3803 }
NeilBrown96de1e62005-11-08 21:39:39 -08003804 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003805}
3806
3807static ssize_t
3808md_attr_store(struct kobject *kobj, struct attribute *attr,
3809 const char *page, size_t length)
3810{
3811 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3812 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003813 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003814
3815 if (!entry->store)
3816 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003817 if (!capable(CAP_SYS_ADMIN))
3818 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003819 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003820 if (mddev->hold_active == UNTIL_IOCTL)
3821 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003822 if (!rv) {
3823 rv = entry->store(mddev, page, length);
3824 mddev_unlock(mddev);
3825 }
NeilBrown96de1e62005-11-08 21:39:39 -08003826 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003827}
3828
3829static void md_free(struct kobject *ko)
3830{
3831 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003832
3833 if (mddev->sysfs_state)
3834 sysfs_put(mddev->sysfs_state);
3835
3836 if (mddev->gendisk) {
3837 del_gendisk(mddev->gendisk);
3838 put_disk(mddev->gendisk);
3839 }
3840 if (mddev->queue)
3841 blk_cleanup_queue(mddev->queue);
3842
NeilBrowneae17012005-11-08 21:39:23 -08003843 kfree(mddev);
3844}
3845
3846static struct sysfs_ops md_sysfs_ops = {
3847 .show = md_attr_show,
3848 .store = md_attr_store,
3849};
3850static struct kobj_type md_ktype = {
3851 .release = md_free,
3852 .sysfs_ops = &md_sysfs_ops,
3853 .default_attrs = md_default_attrs,
3854};
3855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856int mdp_major = 0;
3857
Dan Williams5fd3a172009-03-04 00:57:25 -07003858static void mddev_delayed_delete(struct work_struct *ws)
3859{
3860 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3861
3862 if (mddev->private == &md_redundancy_group) {
3863 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3864 if (mddev->sysfs_action)
3865 sysfs_put(mddev->sysfs_action);
3866 mddev->sysfs_action = NULL;
3867 mddev->private = NULL;
3868 }
3869 kobject_del(&mddev->kobj);
3870 kobject_put(&mddev->kobj);
3871}
3872
NeilBrownefeb53c2009-01-09 08:31:10 +11003873static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003875 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 mddev_t *mddev = mddev_find(dev);
3877 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003878 int partitioned;
3879 int shift;
3880 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003881 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
3883 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003884 return -ENODEV;
3885
3886 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3887 shift = partitioned ? MdpMinorShift : 0;
3888 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
NeilBrownd3374822009-01-09 08:31:10 +11003890 /* wait for any previous instance if this device
3891 * to be completed removed (mddev_delayed_delete).
3892 */
3893 flush_scheduled_work();
3894
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003895 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10003896 error = -EEXIST;
3897 if (mddev->gendisk)
3898 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003899
3900 if (name) {
3901 /* Need to ensure that 'name' is not a duplicate.
3902 */
3903 mddev_t *mddev2;
3904 spin_lock(&all_mddevs_lock);
3905
3906 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3907 if (mddev2->gendisk &&
3908 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3909 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10003910 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003911 }
3912 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
NeilBrown8b765392009-01-09 08:31:08 +11003914
NeilBrown0909dc42009-07-01 12:27:21 +10003915 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003916 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10003917 if (!mddev->queue)
3918 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11003919 mddev->queue->queuedata = mddev;
3920
NeilBrown8b765392009-01-09 08:31:08 +11003921 /* Can be unlocked because the queue is new: no concurrency */
3922 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3923
NeilBrown409c57f2009-03-31 14:39:39 +11003924 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 disk = alloc_disk(1 << shift);
3927 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11003928 blk_cleanup_queue(mddev->queue);
3929 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10003930 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003932 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003934 if (name)
3935 strcpy(disk->disk_name, name);
3936 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003938 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 disk->fops = &md_fops;
3941 disk->private_data = mddev;
3942 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003943 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003944 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003945 * remove it now.
3946 */
3947 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 add_disk(disk);
3949 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003950 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3951 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10003952 if (error) {
3953 /* This isn't possible, but as kobject_init_and_add is marked
3954 * __must_check, we must do something with the result
3955 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08003956 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3957 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10003958 error = 0;
3959 }
3960 abort:
3961 mutex_unlock(&disks_mutex);
3962 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003963 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003964 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3965 }
NeilBrownd3374822009-01-09 08:31:10 +11003966 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10003967 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11003968}
3969
3970static struct kobject *md_probe(dev_t dev, int *part, void *data)
3971{
3972 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return NULL;
3974}
3975
NeilBrownefeb53c2009-01-09 08:31:10 +11003976static int add_named_array(const char *val, struct kernel_param *kp)
3977{
3978 /* val must be "md_*" where * is not all digits.
3979 * We allocate an array with a large free minor number, and
3980 * set the name to val. val must not already be an active name.
3981 */
3982 int len = strlen(val);
3983 char buf[DISK_NAME_LEN];
3984
3985 while (len && val[len-1] == '\n')
3986 len--;
3987 if (len >= DISK_NAME_LEN)
3988 return -E2BIG;
3989 strlcpy(buf, val, len+1);
3990 if (strncmp(buf, "md_", 3) != 0)
3991 return -EINVAL;
3992 return md_alloc(0, buf);
3993}
3994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995static void md_safemode_timeout(unsigned long data)
3996{
3997 mddev_t *mddev = (mddev_t *) data;
3998
Neil Brown0fd62b82008-06-28 08:31:36 +10003999 if (!atomic_read(&mddev->writes_pending)) {
4000 mddev->safemode = 1;
4001 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004002 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 md_wakeup_thread(mddev->thread);
4005}
4006
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004007static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
4009static int do_md_run(mddev_t * mddev)
4010{
NeilBrown2604b702006-01-06 00:20:36 -08004011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 mdk_rdev_t *rdev;
4013 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004014 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
NeilBrowna757e642005-04-16 15:26:42 -07004016 if (list_empty(&mddev->disks))
4017 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020 if (mddev->pers)
4021 return -EBUSY;
4022
4023 /*
4024 * Analyze all RAID superblock(s)
4025 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004026 if (!mddev->raid_disks) {
4027 if (!mddev->persistent)
4028 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004029 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
NeilBrownd9d166c2006-01-06 00:20:51 -08004032 if (mddev->level != LEVEL_NONE)
4033 request_module("md-level-%d", mddev->level);
4034 else if (mddev->clevel[0])
4035 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
4037 /*
4038 * Drop all container device buffers, from now on
4039 * the only valid external interface is through the md
4040 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004042 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004043 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 continue;
4045 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004046 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004047
4048 /* perform some consistency tests on the device.
4049 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004050 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004051 */
Andre Noll0f420352008-07-11 22:02:23 +10004052 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004053 if (mddev->dev_sectors &&
4054 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004055 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004056 printk("md: %s: data overlaps metadata\n",
4057 mdname(mddev));
4058 return -EINVAL;
4059 }
4060 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004061 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004062 > rdev->data_offset) {
4063 printk("md: %s: metadata overlaps data\n",
4064 mdname(mddev));
4065 return -EINVAL;
4066 }
4067 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004068 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 }
4070
4071 md_probe(mddev->unit, NULL, NULL);
4072 disk = mddev->gendisk;
4073 if (!disk)
4074 return -ENOMEM;
4075
4076 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004077 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004078 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004080 if (mddev->level != LEVEL_NONE)
4081 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4082 mddev->level);
4083 else
4084 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4085 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 return -EINVAL;
4087 }
NeilBrown2604b702006-01-06 00:20:36 -08004088 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004090 if (mddev->level != pers->level) {
4091 mddev->level = pers->level;
4092 mddev->new_level = pers->level;
4093 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004094 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
NeilBrownf6705572006-03-27 01:18:11 -08004096 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004097 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004098 /* This personality cannot handle reshaping... */
4099 mddev->pers = NULL;
4100 module_put(pers->owner);
4101 return -EINVAL;
4102 }
4103
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004104 if (pers->sync_request) {
4105 /* Warn if this is a potentially silly
4106 * configuration.
4107 */
4108 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4109 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004110 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004111
4112 list_for_each_entry(rdev, &mddev->disks, same_set)
4113 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004114 if (rdev < rdev2 &&
4115 rdev->bdev->bd_contains ==
4116 rdev2->bdev->bd_contains) {
4117 printk(KERN_WARNING
4118 "%s: WARNING: %s appears to be"
4119 " on the same physical disk as"
4120 " %s.\n",
4121 mdname(mddev),
4122 bdevname(rdev->bdev,b),
4123 bdevname(rdev2->bdev,b2));
4124 warned = 1;
4125 }
4126 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004127
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004128 if (warned)
4129 printk(KERN_WARNING
4130 "True protection against single-disk"
4131 " failure might be compromised.\n");
4132 }
4133
NeilBrown657390d2005-08-26 18:34:16 -07004134 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004135 /* may be over-ridden by personality */
4136 mddev->resync_max_sectors = mddev->dev_sectors;
4137
NeilBrowna9701a32005-11-08 21:39:34 -08004138 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004139 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
NeilBrownf91de922005-11-08 21:39:36 -08004141 if (start_readonly)
4142 mddev->ro = 2; /* read-only, but switch on first write */
4143
NeilBrownb15c2e52006-01-06 00:20:16 -08004144 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004145 if (err)
4146 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004147 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4148 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4149 " but 'external_size' not in effect?\n", __func__);
4150 printk(KERN_ERR
4151 "md: invalid array_size %llu > default size %llu\n",
4152 (unsigned long long)mddev->array_sectors / 2,
4153 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4154 err = -EINVAL;
4155 mddev->pers->stop(mddev);
4156 }
4157 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004158 err = bitmap_create(mddev);
4159 if (err) {
4160 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4161 mdname(mddev), err);
4162 mddev->pers->stop(mddev);
4163 }
4164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 module_put(mddev->pers->owner);
4167 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004168 bitmap_destroy(mddev);
4169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004171 if (mddev->pers->sync_request) {
4172 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4173 printk(KERN_WARNING
4174 "md: cannot register extra attributes for %s\n",
4175 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004176 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004177 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004178 mddev->ro = 0;
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 atomic_set(&mddev->writes_pending,0);
4181 mddev->safemode = 0;
4182 mddev->safemode_timer.function = md_safemode_timeout;
4183 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004184 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004186
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004187 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004188 if (rdev->raid_disk >= 0) {
4189 char nm[20];
4190 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004191 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4192 printk("md: cannot register %s for %s\n",
4193 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4197
NeilBrown850b2b42006-10-03 01:15:46 -07004198 if (mddev->flags)
4199 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Andre Nollf233ea52008-07-21 17:05:22 +10004201 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004203 /* If there is a partially-recovered drive we need to
4204 * start recovery here. If we leave it to md_check_recovery,
4205 * it will remove the drives and not do the right thing
4206 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004207 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004208 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004209 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004210 if (rdev->raid_disk >= 0 &&
4211 !test_bit(In_sync, &rdev->flags) &&
4212 !test_bit(Faulty, &rdev->flags))
4213 /* complete an interrupted recovery */
4214 spares++;
4215 if (spares && mddev->pers->sync_request) {
4216 mddev->recovery = 0;
4217 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4218 mddev->sync_thread = md_register_thread(md_do_sync,
4219 mddev,
4220 "%s_resync");
4221 if (!mddev->sync_thread) {
4222 printk(KERN_ERR "%s: could not start resync"
4223 " thread...\n",
4224 mdname(mddev));
4225 /* leave the spares where they are, it shouldn't hurt */
4226 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004227 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004228 }
4229 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004230 md_wakeup_thread(mddev->thread);
4231 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004232
Linus Torvalds44ce62942007-05-09 18:51:36 -07004233 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004234 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004235 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004236 if (mddev->sysfs_action)
4237 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004238 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004239 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return 0;
4241}
4242
4243static int restart_array(mddev_t *mddev)
4244{
4245 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Andre Noll80fab1d2008-07-11 22:02:21 +10004247 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004249 return -ENXIO;
4250 if (!mddev->pers)
4251 return -EINVAL;
4252 if (!mddev->ro)
4253 return -EBUSY;
4254 mddev->safemode = 0;
4255 mddev->ro = 0;
4256 set_disk_ro(disk, 0);
4257 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4258 mdname(mddev));
4259 /* Kick recovery or resync if necessary */
4260 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4261 md_wakeup_thread(mddev->thread);
4262 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004263 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
NeilBrownacc55e22006-06-26 00:27:47 -07004267/* similar to deny_write_access, but accounts for our holding a reference
4268 * to the file ourselves */
4269static int deny_bitmap_write_access(struct file * file)
4270{
4271 struct inode *inode = file->f_mapping->host;
4272
4273 spin_lock(&inode->i_lock);
4274 if (atomic_read(&inode->i_writecount) > 1) {
4275 spin_unlock(&inode->i_lock);
4276 return -ETXTBSY;
4277 }
4278 atomic_set(&inode->i_writecount, -1);
4279 spin_unlock(&inode->i_lock);
4280
4281 return 0;
4282}
4283
4284static void restore_bitmap_write_access(struct file *file)
4285{
4286 struct inode *inode = file->f_mapping->host;
4287
4288 spin_lock(&inode->i_lock);
4289 atomic_set(&inode->i_writecount, 1);
4290 spin_unlock(&inode->i_lock);
4291}
4292
NeilBrown9e653b62006-06-26 00:27:58 -07004293/* mode:
4294 * 0 - completely stop and dis-assemble array
4295 * 1 - switch to readonly
4296 * 2 - stop but do not disassemble array
4297 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004298static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299{
4300 int err = 0;
4301 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004302 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
NeilBrownf2ea68c2008-07-21 17:05:25 +10004304 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004305 printk("md: %s still in use.\n",mdname(mddev));
4306 return -EBUSY;
4307 }
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004312 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4314 md_unregister_thread(mddev->sync_thread);
4315 mddev->sync_thread = NULL;
4316 }
4317
4318 del_timer_sync(&mddev->safemode_timer);
4319
NeilBrown9e653b62006-06-26 00:27:58 -07004320 switch(mode) {
4321 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004323 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 goto out;
4325 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004326 break;
4327 case 0: /* disassemble */
4328 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004329 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004330 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 if (mddev->ro)
4332 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004335 mddev->queue->merge_bvec_fn = NULL;
4336 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004337 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004339 if (mddev->pers->sync_request)
4340 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004342 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004343 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004344
NeilBrownc4647292009-05-07 12:51:06 +10004345 list_for_each_entry(rdev, &mddev->disks, same_set)
4346 if (rdev->raid_disk >= 0) {
4347 char nm[20];
4348 sprintf(nm, "rd%d", rdev->raid_disk);
4349 sysfs_remove_link(&mddev->kobj, nm);
4350 }
4351
NeilBrown0d4ca602006-12-10 02:20:44 -08004352 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004353 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (mddev->ro)
4356 mddev->ro = 0;
4357 }
NeilBrown850b2b42006-10-03 01:15:46 -07004358 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 /* mark array as shutdown cleanly */
4360 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004361 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
NeilBrown9e653b62006-06-26 00:27:58 -07004363 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004365 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
NeilBrown32a76272005-06-21 17:17:14 -07004367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 /*
4369 * Free resources if final stop
4370 */
NeilBrown9e653b62006-06-26 00:27:58 -07004371 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4374
NeilBrown978f9462006-02-02 14:28:05 -08004375 bitmap_destroy(mddev);
4376 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004377 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004378 fput(mddev->bitmap_file);
4379 mddev->bitmap_file = NULL;
4380 }
4381 mddev->bitmap_offset = 0;
4382
NeilBrown177a99b2008-02-06 01:39:56 -08004383 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004384 flush_scheduled_work();
4385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 export_array(mddev);
4387
Andre Nollf233ea52008-07-21 17:05:22 +10004388 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004389 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004390 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004391 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004392 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004393 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004394 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004395 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004396 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004397 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004398 mddev->level = LEVEL_NONE;
4399 mddev->clevel[0] = 0;
4400 mddev->flags = 0;
4401 mddev->ro = 0;
4402 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004403 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004404 mddev->ctime = mddev->utime = 0;
4405 mddev->layout = 0;
4406 mddev->max_disks = 0;
4407 mddev->events = 0;
4408 mddev->delta_disks = 0;
4409 mddev->new_level = LEVEL_NONE;
4410 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004411 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004412 mddev->curr_resync = 0;
4413 mddev->resync_mismatches = 0;
4414 mddev->suspend_lo = mddev->suspend_hi = 0;
4415 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4416 mddev->recovery = 0;
4417 mddev->in_sync = 0;
4418 mddev->changed = 0;
4419 mddev->degraded = 0;
4420 mddev->barriers_work = 0;
4421 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004422 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004423 if (mddev->hold_active == UNTIL_STOP)
4424 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004425
NeilBrowna8a55c32006-06-26 00:27:39 -07004426 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4428 mdname(mddev));
4429 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004430 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004431 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004432 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433out:
4434 return err;
4435}
4436
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004437#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438static void autorun_array(mddev_t *mddev)
4439{
4440 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 int err;
4442
NeilBrowna757e642005-04-16 15:26:42 -07004443 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 printk(KERN_INFO "md: running: ");
4447
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004448 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 char b[BDEVNAME_SIZE];
4450 printk("<%s>", bdevname(rdev->bdev,b));
4451 }
4452 printk("\n");
4453
NeilBrownd710e132008-10-13 11:55:12 +11004454 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (err) {
4456 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004457 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
4459}
4460
4461/*
4462 * lets try to run arrays based on all disks that have arrived
4463 * until now. (those are in pending_raid_disks)
4464 *
4465 * the method: pick the first pending disk, collect all disks with
4466 * the same UUID, remove all from the pending list and put them into
4467 * the 'same_array' list. Then order this list based on superblock
4468 * update time (freshest comes first), kick out 'old' disks and
4469 * compare superblocks. If everything's fine then run it.
4470 *
4471 * If "unit" is allocated, then bump its reference count
4472 */
4473static void autorun_devices(int part)
4474{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004475 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 mddev_t *mddev;
4477 char b[BDEVNAME_SIZE];
4478
4479 printk(KERN_INFO "md: autorun ...\n");
4480 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004481 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004483 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 rdev0 = list_entry(pending_raid_disks.next,
4485 mdk_rdev_t, same_set);
4486
4487 printk(KERN_INFO "md: considering %s ...\n",
4488 bdevname(rdev0->bdev,b));
4489 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004490 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 if (super_90_load(rdev, rdev0, 0) >= 0) {
4492 printk(KERN_INFO "md: adding %s ...\n",
4493 bdevname(rdev->bdev,b));
4494 list_move(&rdev->same_set, &candidates);
4495 }
4496 /*
4497 * now we have a set of devices, with all of them having
4498 * mostly sane superblocks. It's time to allocate the
4499 * mddev.
4500 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004501 if (part) {
4502 dev = MKDEV(mdp_major,
4503 rdev0->preferred_minor << MdpMinorShift);
4504 unit = MINOR(dev) >> MdpMinorShift;
4505 } else {
4506 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4507 unit = MINOR(dev);
4508 }
4509 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4511 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4512 break;
4513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
4515 md_probe(dev, NULL, NULL);
4516 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004517 if (!mddev || !mddev->gendisk) {
4518 if (mddev)
4519 mddev_put(mddev);
4520 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 "md: cannot allocate memory for md drive.\n");
4522 break;
4523 }
4524 if (mddev_lock(mddev))
4525 printk(KERN_WARNING "md: %s locked, cannot run\n",
4526 mdname(mddev));
4527 else if (mddev->raid_disks || mddev->major_version
4528 || !list_empty(&mddev->disks)) {
4529 printk(KERN_WARNING
4530 "md: %s already running, cannot run %s\n",
4531 mdname(mddev), bdevname(rdev0->bdev,b));
4532 mddev_unlock(mddev);
4533 } else {
4534 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004535 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004536 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 list_del_init(&rdev->same_set);
4538 if (bind_rdev_to_array(rdev, mddev))
4539 export_rdev(rdev);
4540 }
4541 autorun_array(mddev);
4542 mddev_unlock(mddev);
4543 }
4544 /* on success, candidates will be empty, on error
4545 * it won't...
4546 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004547 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004548 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 mddev_put(mddev);
4552 }
4553 printk(KERN_INFO "md: ... autorun DONE.\n");
4554}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004555#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557static int get_version(void __user * arg)
4558{
4559 mdu_version_t ver;
4560
4561 ver.major = MD_MAJOR_VERSION;
4562 ver.minor = MD_MINOR_VERSION;
4563 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4564
4565 if (copy_to_user(arg, &ver, sizeof(ver)))
4566 return -EFAULT;
4567
4568 return 0;
4569}
4570
4571static int get_array_info(mddev_t * mddev, void __user * arg)
4572{
4573 mdu_array_info_t info;
4574 int nr,working,active,failed,spare;
4575 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004578 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004580 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 failed++;
4582 else {
4583 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004584 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 active++;
4586 else
4587 spare++;
4588 }
4589 }
4590
4591 info.major_version = mddev->major_version;
4592 info.minor_version = mddev->minor_version;
4593 info.patch_version = MD_PATCHLEVEL_VERSION;
4594 info.ctime = mddev->ctime;
4595 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004596 info.size = mddev->dev_sectors / 2;
4597 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004598 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 info.nr_disks = nr;
4600 info.raid_disks = mddev->raid_disks;
4601 info.md_minor = mddev->md_minor;
4602 info.not_persistent= !mddev->persistent;
4603
4604 info.utime = mddev->utime;
4605 info.state = 0;
4606 if (mddev->in_sync)
4607 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004608 if (mddev->bitmap && mddev->bitmap_offset)
4609 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 info.active_disks = active;
4611 info.working_disks = working;
4612 info.failed_disks = failed;
4613 info.spare_disks = spare;
4614
4615 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004616 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 if (copy_to_user(arg, &info, sizeof(info)))
4619 return -EFAULT;
4620
4621 return 0;
4622}
4623
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004624static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004625{
4626 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4627 char *ptr, *buf = NULL;
4628 int err = -ENOMEM;
4629
Dan Williamsb5470dc2008-06-27 21:44:04 -07004630 if (md_allow_write(mddev))
4631 file = kmalloc(sizeof(*file), GFP_NOIO);
4632 else
4633 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004634
NeilBrown32a76272005-06-21 17:17:14 -07004635 if (!file)
4636 goto out;
4637
4638 /* bitmap disabled, zero the first byte and copy out */
4639 if (!mddev->bitmap || !mddev->bitmap->file) {
4640 file->pathname[0] = '\0';
4641 goto copy_out;
4642 }
4643
4644 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4645 if (!buf)
4646 goto out;
4647
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004648 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4649 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004650 goto out;
4651
4652 strcpy(file->pathname, ptr);
4653
4654copy_out:
4655 err = 0;
4656 if (copy_to_user(arg, file, sizeof(*file)))
4657 err = -EFAULT;
4658out:
4659 kfree(buf);
4660 kfree(file);
4661 return err;
4662}
4663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664static int get_disk_info(mddev_t * mddev, void __user * arg)
4665{
4666 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 mdk_rdev_t *rdev;
4668
4669 if (copy_from_user(&info, arg, sizeof(info)))
4670 return -EFAULT;
4671
Andre Noll26ef3792008-07-11 22:02:21 +10004672 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 if (rdev) {
4674 info.major = MAJOR(rdev->bdev->bd_dev);
4675 info.minor = MINOR(rdev->bdev->bd_dev);
4676 info.raid_disk = rdev->raid_disk;
4677 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004678 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004680 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 info.state |= (1<<MD_DISK_ACTIVE);
4682 info.state |= (1<<MD_DISK_SYNC);
4683 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004684 if (test_bit(WriteMostly, &rdev->flags))
4685 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 } else {
4687 info.major = info.minor = 0;
4688 info.raid_disk = -1;
4689 info.state = (1<<MD_DISK_REMOVED);
4690 }
4691
4692 if (copy_to_user(arg, &info, sizeof(info)))
4693 return -EFAULT;
4694
4695 return 0;
4696}
4697
4698static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4699{
4700 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4701 mdk_rdev_t *rdev;
4702 dev_t dev = MKDEV(info->major,info->minor);
4703
4704 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4705 return -EOVERFLOW;
4706
4707 if (!mddev->raid_disks) {
4708 int err;
4709 /* expecting a device which has a superblock */
4710 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4711 if (IS_ERR(rdev)) {
4712 printk(KERN_WARNING
4713 "md: md_import_device returned %ld\n",
4714 PTR_ERR(rdev));
4715 return PTR_ERR(rdev);
4716 }
4717 if (!list_empty(&mddev->disks)) {
4718 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4719 mdk_rdev_t, same_set);
4720 int err = super_types[mddev->major_version]
4721 .load_super(rdev, rdev0, mddev->minor_version);
4722 if (err < 0) {
4723 printk(KERN_WARNING
4724 "md: %s has different UUID to %s\n",
4725 bdevname(rdev->bdev,b),
4726 bdevname(rdev0->bdev,b2));
4727 export_rdev(rdev);
4728 return -EINVAL;
4729 }
4730 }
4731 err = bind_rdev_to_array(rdev, mddev);
4732 if (err)
4733 export_rdev(rdev);
4734 return err;
4735 }
4736
4737 /*
4738 * add_new_disk can be used once the array is assembled
4739 * to add "hot spares". They must already have a superblock
4740 * written
4741 */
4742 if (mddev->pers) {
4743 int err;
4744 if (!mddev->pers->hot_add_disk) {
4745 printk(KERN_WARNING
4746 "%s: personality does not support diskops!\n",
4747 mdname(mddev));
4748 return -EINVAL;
4749 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004750 if (mddev->persistent)
4751 rdev = md_import_device(dev, mddev->major_version,
4752 mddev->minor_version);
4753 else
4754 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (IS_ERR(rdev)) {
4756 printk(KERN_WARNING
4757 "md: md_import_device returned %ld\n",
4758 PTR_ERR(rdev));
4759 return PTR_ERR(rdev);
4760 }
NeilBrown41158c72005-06-21 17:17:25 -07004761 /* set save_raid_disk if appropriate */
4762 if (!mddev->persistent) {
4763 if (info->state & (1<<MD_DISK_SYNC) &&
4764 info->raid_disk < mddev->raid_disks)
4765 rdev->raid_disk = info->raid_disk;
4766 else
4767 rdev->raid_disk = -1;
4768 } else
4769 super_types[mddev->major_version].
4770 validate_super(mddev, rdev);
4771 rdev->saved_raid_disk = rdev->raid_disk;
4772
NeilBrownb2d444d2005-11-08 21:39:31 -08004773 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004774 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4775 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004776 else
4777 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 rdev->raid_disk = -1;
4780 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004781 if (!err && !mddev->pers->hot_remove_disk) {
4782 /* If there is hot_add_disk but no hot_remove_disk
4783 * then added disks for geometry changes,
4784 * and should be added immediately.
4785 */
4786 super_types[mddev->major_version].
4787 validate_super(mddev, rdev);
4788 err = mddev->pers->hot_add_disk(mddev, rdev);
4789 if (err)
4790 unbind_rdev_from_array(rdev);
4791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 if (err)
4793 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004794 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004795 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004796
NeilBrown17571282006-12-10 02:20:52 -08004797 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004798 if (mddev->degraded)
4799 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004801 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 return err;
4803 }
4804
4805 /* otherwise, add_new_disk is only allowed
4806 * for major_version==0 superblocks
4807 */
4808 if (mddev->major_version != 0) {
4809 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4810 mdname(mddev));
4811 return -EINVAL;
4812 }
4813
4814 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4815 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004816 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 if (IS_ERR(rdev)) {
4818 printk(KERN_WARNING
4819 "md: error, md_import_device() returned %ld\n",
4820 PTR_ERR(rdev));
4821 return PTR_ERR(rdev);
4822 }
4823 rdev->desc_nr = info->number;
4824 if (info->raid_disk < mddev->raid_disks)
4825 rdev->raid_disk = info->raid_disk;
4826 else
4827 rdev->raid_disk = -1;
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004830 if (info->state & (1<<MD_DISK_SYNC))
4831 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004833 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4834 set_bit(WriteMostly, &rdev->flags);
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (!mddev->persistent) {
4837 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004838 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 } else
Andre Noll0f420352008-07-11 22:02:23 +10004840 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004841 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
NeilBrown2bf071b2006-01-06 00:20:55 -08004843 err = bind_rdev_to_array(rdev, mddev);
4844 if (err) {
4845 export_rdev(rdev);
4846 return err;
4847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 }
4849
4850 return 0;
4851}
4852
4853static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4854{
4855 char b[BDEVNAME_SIZE];
4856 mdk_rdev_t *rdev;
4857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 rdev = find_rdev(mddev, dev);
4859 if (!rdev)
4860 return -ENXIO;
4861
4862 if (rdev->raid_disk >= 0)
4863 goto busy;
4864
4865 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004866 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004867 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 return 0;
4870busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004871 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 bdevname(rdev->bdev,b), mdname(mddev));
4873 return -EBUSY;
4874}
4875
4876static int hot_add_disk(mddev_t * mddev, dev_t dev)
4877{
4878 char b[BDEVNAME_SIZE];
4879 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 mdk_rdev_t *rdev;
4881
4882 if (!mddev->pers)
4883 return -ENODEV;
4884
4885 if (mddev->major_version != 0) {
4886 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4887 " version-0 superblocks.\n",
4888 mdname(mddev));
4889 return -EINVAL;
4890 }
4891 if (!mddev->pers->hot_add_disk) {
4892 printk(KERN_WARNING
4893 "%s: personality does not support diskops!\n",
4894 mdname(mddev));
4895 return -EINVAL;
4896 }
4897
NeilBrownd710e132008-10-13 11:55:12 +11004898 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 if (IS_ERR(rdev)) {
4900 printk(KERN_WARNING
4901 "md: error, md_import_device() returned %ld\n",
4902 PTR_ERR(rdev));
4903 return -EINVAL;
4904 }
4905
4906 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004907 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 else
Andre Noll0f420352008-07-11 22:02:23 +10004909 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
NeilBrown8190e752009-06-18 08:48:58 +10004911 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
NeilBrownb2d444d2005-11-08 21:39:31 -08004913 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 printk(KERN_WARNING
4915 "md: can not hot-add faulty %s disk to %s!\n",
4916 bdevname(rdev->bdev,b), mdname(mddev));
4917 err = -EINVAL;
4918 goto abort_export;
4919 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004920 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004922 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004923 err = bind_rdev_to_array(rdev, mddev);
4924 if (err)
4925 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 /*
4928 * The rest should better be atomic, we can have disk failures
4929 * noticed in interrupt contexts ...
4930 */
4931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 rdev->raid_disk = -1;
4933
NeilBrown850b2b42006-10-03 01:15:46 -07004934 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
4936 /*
4937 * Kick recovery, maybe this spare has to be added to the
4938 * array immediately.
4939 */
4940 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4941 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004942 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 return 0;
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945abort_export:
4946 export_rdev(rdev);
4947 return err;
4948}
4949
NeilBrown32a76272005-06-21 17:17:14 -07004950static int set_bitmap_file(mddev_t *mddev, int fd)
4951{
4952 int err;
4953
NeilBrown36fa3062005-09-09 16:23:45 -07004954 if (mddev->pers) {
4955 if (!mddev->pers->quiesce)
4956 return -EBUSY;
4957 if (mddev->recovery || mddev->sync_thread)
4958 return -EBUSY;
4959 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004960 }
4961
NeilBrown36fa3062005-09-09 16:23:45 -07004962
4963 if (fd >= 0) {
4964 if (mddev->bitmap)
4965 return -EEXIST; /* cannot add when bitmap is present */
4966 mddev->bitmap_file = fget(fd);
4967
4968 if (mddev->bitmap_file == NULL) {
4969 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4970 mdname(mddev));
4971 return -EBADF;
4972 }
4973
4974 err = deny_bitmap_write_access(mddev->bitmap_file);
4975 if (err) {
4976 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4977 mdname(mddev));
4978 fput(mddev->bitmap_file);
4979 mddev->bitmap_file = NULL;
4980 return err;
4981 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004982 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004983 } else if (mddev->bitmap == NULL)
4984 return -ENOENT; /* cannot remove what isn't there */
4985 err = 0;
4986 if (mddev->pers) {
4987 mddev->pers->quiesce(mddev, 1);
4988 if (fd >= 0)
4989 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004990 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004991 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004992 fd = -1; /* make sure to put the file */
4993 }
NeilBrown36fa3062005-09-09 16:23:45 -07004994 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004995 }
4996 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004997 if (mddev->bitmap_file) {
4998 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004999 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07005000 }
NeilBrown36fa3062005-09-09 16:23:45 -07005001 mddev->bitmap_file = NULL;
5002 }
5003
NeilBrown32a76272005-06-21 17:17:14 -07005004 return err;
5005}
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007/*
5008 * set_array_info is used two different ways
5009 * The original usage is when creating a new array.
5010 * In this usage, raid_disks is > 0 and it together with
5011 * level, size, not_persistent,layout,chunksize determine the
5012 * shape of the array.
5013 * This will always create an array with a type-0.90.0 superblock.
5014 * The newer usage is when assembling an array.
5015 * In this case raid_disks will be 0, and the major_version field is
5016 * use to determine which style super-blocks are to be found on the devices.
5017 * The minor and patch _version numbers are also kept incase the
5018 * super_block handler wishes to interpret them.
5019 */
5020static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5021{
5022
5023 if (info->raid_disks == 0) {
5024 /* just setting version number for superblock loading */
5025 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005026 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 super_types[info->major_version].name == NULL) {
5028 /* maybe try to auto-load a module? */
5029 printk(KERN_INFO
5030 "md: superblock version %d not known\n",
5031 info->major_version);
5032 return -EINVAL;
5033 }
5034 mddev->major_version = info->major_version;
5035 mddev->minor_version = info->minor_version;
5036 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005037 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 return 0;
5039 }
5040 mddev->major_version = MD_MAJOR_VERSION;
5041 mddev->minor_version = MD_MINOR_VERSION;
5042 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5043 mddev->ctime = get_seconds();
5044
5045 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005046 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005047 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 mddev->raid_disks = info->raid_disks;
5049 /* don't set md_minor, it is determined by which /dev/md* was
5050 * openned
5051 */
5052 if (info->state & (1<<MD_SB_CLEAN))
5053 mddev->recovery_cp = MaxSector;
5054 else
5055 mddev->recovery_cp = 0;
5056 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005057 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
5059 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005060 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
5062 mddev->max_disks = MD_SB_DISKS;
5063
NeilBrowne6910632008-02-06 01:39:51 -08005064 if (mddev->persistent)
5065 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005066 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
NeilBrownb2a27032005-11-28 13:44:12 -08005068 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5069 mddev->bitmap_offset = 0;
5070
NeilBrownf6705572006-03-27 01:18:11 -08005071 mddev->reshape_position = MaxSector;
5072
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 /*
5074 * Generate a 128 bit UUID
5075 */
5076 get_random_bytes(mddev->uuid, 16);
5077
NeilBrownf6705572006-03-27 01:18:11 -08005078 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005079 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005080 mddev->new_layout = mddev->layout;
5081 mddev->delta_disks = 0;
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return 0;
5084}
5085
Dan Williams1f403622009-03-31 14:59:03 +11005086void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5087{
Dan Williamsb522adc2009-03-31 15:00:31 +11005088 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5089
5090 if (mddev->external_size)
5091 return;
5092
Dan Williams1f403622009-03-31 14:59:03 +11005093 mddev->array_sectors = array_sectors;
5094}
5095EXPORT_SYMBOL(md_set_array_sectors);
5096
Andre Nolld71f9f82008-07-11 22:02:22 +10005097static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005098{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005099 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005100 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005101 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005102
5103 if (mddev->pers->resize == NULL)
5104 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005105 /* The "num_sectors" is the number of sectors of each device that
5106 * is used. This can only make sense for arrays with redundancy.
5107 * linear and raid0 always use whatever space is available. We can only
5108 * consider changing this number if no resync or reconstruction is
5109 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005110 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005111 * of each device. If num_sectors is zero, we find the largest size
5112 * that fits.
5113
NeilBrowna35b0d62006-01-06 00:20:49 -08005114 */
5115 if (mddev->sync_thread)
5116 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005117 if (mddev->bitmap)
5118 /* Sorry, cannot grow a bitmap yet, just remove it,
5119 * grow, and re-add.
5120 */
5121 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005122 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005123 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005124
Andre Nolld71f9f82008-07-11 22:02:22 +10005125 if (fit && (num_sectors == 0 || num_sectors > avail))
5126 num_sectors = avail;
5127 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005128 return -ENOSPC;
5129 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005130 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08005131 if (!rv) {
5132 struct block_device *bdev;
5133
5134 bdev = bdget_disk(mddev->gendisk, 0);
5135 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005136 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10005137 i_size_write(bdev->bd_inode,
5138 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005139 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08005140 bdput(bdev);
5141 }
5142 }
5143 return rv;
5144}
5145
NeilBrownda943b992006-01-06 00:20:54 -08005146static int update_raid_disks(mddev_t *mddev, int raid_disks)
5147{
5148 int rv;
5149 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005150 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005151 return -EINVAL;
5152 if (raid_disks <= 0 ||
5153 raid_disks >= mddev->max_disks)
5154 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005155 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005156 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005157 mddev->delta_disks = raid_disks - mddev->raid_disks;
5158
5159 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005160 return rv;
5161}
5162
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164/*
5165 * update_array_info is used to change the configuration of an
5166 * on-line array.
5167 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5168 * fields in the info are checked against the array.
5169 * Any differences that cannot be handled will cause an error.
5170 * Normally, only one change can be managed at a time.
5171 */
5172static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5173{
5174 int rv = 0;
5175 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005176 int state = 0;
5177
5178 /* calculate expected state,ignoring low bits */
5179 if (mddev->bitmap && mddev->bitmap_offset)
5180 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
5182 if (mddev->major_version != info->major_version ||
5183 mddev->minor_version != info->minor_version ||
5184/* mddev->patch_version != info->patch_version || */
5185 mddev->ctime != info->ctime ||
5186 mddev->level != info->level ||
5187/* mddev->layout != info->layout || */
5188 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005189 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005190 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5191 ((state^info->state) & 0xfffffe00)
5192 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 return -EINVAL;
5194 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005195 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5196 cnt++;
5197 if (mddev->raid_disks != info->raid_disks)
5198 cnt++;
5199 if (mddev->layout != info->layout)
5200 cnt++;
5201 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5202 cnt++;
5203 if (cnt == 0)
5204 return 0;
5205 if (cnt > 1)
5206 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 if (mddev->layout != info->layout) {
5209 /* Change layout
5210 * we don't need to do anything at the md level, the
5211 * personality will take care of it all.
5212 */
NeilBrown50ac1682009-06-18 08:47:55 +10005213 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005215 else {
5216 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005217 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005218 if (rv)
5219 mddev->new_layout = mddev->layout;
5220 return rv;
5221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005223 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005224 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
NeilBrownda943b992006-01-06 00:20:54 -08005226 if (mddev->raid_disks != info->raid_disks)
5227 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
NeilBrown36fa3062005-09-09 16:23:45 -07005229 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5230 if (mddev->pers->quiesce == NULL)
5231 return -EINVAL;
5232 if (mddev->recovery || mddev->sync_thread)
5233 return -EBUSY;
5234 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5235 /* add the bitmap */
5236 if (mddev->bitmap)
5237 return -EEXIST;
5238 if (mddev->default_bitmap_offset == 0)
5239 return -EINVAL;
5240 mddev->bitmap_offset = mddev->default_bitmap_offset;
5241 mddev->pers->quiesce(mddev, 1);
5242 rv = bitmap_create(mddev);
5243 if (rv)
5244 bitmap_destroy(mddev);
5245 mddev->pers->quiesce(mddev, 0);
5246 } else {
5247 /* remove the bitmap */
5248 if (!mddev->bitmap)
5249 return -ENOENT;
5250 if (mddev->bitmap->file)
5251 return -EINVAL;
5252 mddev->pers->quiesce(mddev, 1);
5253 bitmap_destroy(mddev);
5254 mddev->pers->quiesce(mddev, 0);
5255 mddev->bitmap_offset = 0;
5256 }
5257 }
NeilBrown850b2b42006-10-03 01:15:46 -07005258 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return rv;
5260}
5261
5262static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5263{
5264 mdk_rdev_t *rdev;
5265
5266 if (mddev->pers == NULL)
5267 return -ENODEV;
5268
5269 rdev = find_rdev(mddev, dev);
5270 if (!rdev)
5271 return -ENODEV;
5272
5273 md_error(mddev, rdev);
5274 return 0;
5275}
5276
Andre Noll2f9618c2008-04-25 18:57:58 +02005277/*
5278 * We have a problem here : there is no easy way to give a CHS
5279 * virtual geometry. We currently pretend that we have a 2 heads
5280 * 4 sectors (with a BIG number of cylinders...). This drives
5281 * dosfs just mad... ;-)
5282 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005283static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5284{
5285 mddev_t *mddev = bdev->bd_disk->private_data;
5286
5287 geo->heads = 2;
5288 geo->sectors = 4;
5289 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5290 return 0;
5291}
5292
Al Viroa39907f2008-03-02 10:31:15 -05005293static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 unsigned int cmd, unsigned long arg)
5295{
5296 int err = 0;
5297 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 mddev_t *mddev = NULL;
5299
5300 if (!capable(CAP_SYS_ADMIN))
5301 return -EACCES;
5302
5303 /*
5304 * Commands dealing with the RAID driver but not any
5305 * particular array:
5306 */
5307 switch (cmd)
5308 {
5309 case RAID_VERSION:
5310 err = get_version(argp);
5311 goto done;
5312
5313 case PRINT_RAID_DEBUG:
5314 err = 0;
5315 md_print_devices();
5316 goto done;
5317
5318#ifndef MODULE
5319 case RAID_AUTORUN:
5320 err = 0;
5321 autostart_arrays(arg);
5322 goto done;
5323#endif
5324 default:;
5325 }
5326
5327 /*
5328 * Commands creating/starting a new array:
5329 */
5330
Al Viroa39907f2008-03-02 10:31:15 -05005331 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
5333 if (!mddev) {
5334 BUG();
5335 goto abort;
5336 }
5337
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 err = mddev_lock(mddev);
5339 if (err) {
5340 printk(KERN_INFO
5341 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5342 err, cmd);
5343 goto abort;
5344 }
5345
5346 switch (cmd)
5347 {
5348 case SET_ARRAY_INFO:
5349 {
5350 mdu_array_info_t info;
5351 if (!arg)
5352 memset(&info, 0, sizeof(info));
5353 else if (copy_from_user(&info, argp, sizeof(info))) {
5354 err = -EFAULT;
5355 goto abort_unlock;
5356 }
5357 if (mddev->pers) {
5358 err = update_array_info(mddev, &info);
5359 if (err) {
5360 printk(KERN_WARNING "md: couldn't update"
5361 " array info. %d\n", err);
5362 goto abort_unlock;
5363 }
5364 goto done_unlock;
5365 }
5366 if (!list_empty(&mddev->disks)) {
5367 printk(KERN_WARNING
5368 "md: array %s already has disks!\n",
5369 mdname(mddev));
5370 err = -EBUSY;
5371 goto abort_unlock;
5372 }
5373 if (mddev->raid_disks) {
5374 printk(KERN_WARNING
5375 "md: array %s already initialised!\n",
5376 mdname(mddev));
5377 err = -EBUSY;
5378 goto abort_unlock;
5379 }
5380 err = set_array_info(mddev, &info);
5381 if (err) {
5382 printk(KERN_WARNING "md: couldn't set"
5383 " array info. %d\n", err);
5384 goto abort_unlock;
5385 }
5386 }
5387 goto done_unlock;
5388
5389 default:;
5390 }
5391
5392 /*
5393 * Commands querying/configuring an existing array:
5394 */
NeilBrown32a76272005-06-21 17:17:14 -07005395 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005396 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005397 if ((!mddev->raid_disks && !mddev->external)
5398 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5399 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5400 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 err = -ENODEV;
5402 goto abort_unlock;
5403 }
5404
5405 /*
5406 * Commands even a read-only array can execute:
5407 */
5408 switch (cmd)
5409 {
5410 case GET_ARRAY_INFO:
5411 err = get_array_info(mddev, argp);
5412 goto done_unlock;
5413
NeilBrown32a76272005-06-21 17:17:14 -07005414 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005415 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005416 goto done_unlock;
5417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 case GET_DISK_INFO:
5419 err = get_disk_info(mddev, argp);
5420 goto done_unlock;
5421
5422 case RESTART_ARRAY_RW:
5423 err = restart_array(mddev);
5424 goto done_unlock;
5425
5426 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005427 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 goto done_unlock;
5429
5430 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005431 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 goto done_unlock;
5433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 }
5435
5436 /*
5437 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005438 * superblock, so we do not allow them on read-only arrays.
5439 * However non-MD ioctls (e.g. get-size) will still come through
5440 * here and hit the 'default' below, so only disallow
5441 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005443 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005444 if (mddev->ro == 2) {
5445 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005446 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005447 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5448 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005449 } else {
5450 err = -EROFS;
5451 goto abort_unlock;
5452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454
5455 switch (cmd)
5456 {
5457 case ADD_NEW_DISK:
5458 {
5459 mdu_disk_info_t info;
5460 if (copy_from_user(&info, argp, sizeof(info)))
5461 err = -EFAULT;
5462 else
5463 err = add_new_disk(mddev, &info);
5464 goto done_unlock;
5465 }
5466
5467 case HOT_REMOVE_DISK:
5468 err = hot_remove_disk(mddev, new_decode_dev(arg));
5469 goto done_unlock;
5470
5471 case HOT_ADD_DISK:
5472 err = hot_add_disk(mddev, new_decode_dev(arg));
5473 goto done_unlock;
5474
5475 case SET_DISK_FAULTY:
5476 err = set_disk_faulty(mddev, new_decode_dev(arg));
5477 goto done_unlock;
5478
5479 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005480 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 goto done_unlock;
5482
NeilBrown32a76272005-06-21 17:17:14 -07005483 case SET_BITMAP_FILE:
5484 err = set_bitmap_file(mddev, (int)arg);
5485 goto done_unlock;
5486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 err = -EINVAL;
5489 goto abort_unlock;
5490 }
5491
5492done_unlock:
5493abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005494 if (mddev->hold_active == UNTIL_IOCTL &&
5495 err != -EINVAL)
5496 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 mddev_unlock(mddev);
5498
5499 return err;
5500done:
5501 if (err)
5502 MD_BUG();
5503abort:
5504 return err;
5505}
5506
Al Viroa39907f2008-03-02 10:31:15 -05005507static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
5509 /*
5510 * Succeed if we can lock the mddev, which confirms that
5511 * it isn't being stopped right now.
5512 */
NeilBrownd3374822009-01-09 08:31:10 +11005513 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 int err;
5515
NeilBrownd3374822009-01-09 08:31:10 +11005516 if (mddev->gendisk != bdev->bd_disk) {
5517 /* we are racing with mddev_put which is discarding this
5518 * bd_disk.
5519 */
5520 mddev_put(mddev);
5521 /* Wait until bdev->bd_disk is definitely gone */
5522 flush_scheduled_work();
5523 /* Then retry the open from the top */
5524 return -ERESTARTSYS;
5525 }
5526 BUG_ON(mddev != bdev->bd_disk->private_data);
5527
NeilBrownd63a5a72006-12-08 02:36:17 -08005528 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 goto out;
5530
5531 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005532 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 mddev_unlock(mddev);
5534
Al Viroa39907f2008-03-02 10:31:15 -05005535 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 out:
5537 return err;
5538}
5539
Al Viroa39907f2008-03-02 10:31:15 -05005540static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541{
Al Viroa39907f2008-03-02 10:31:15 -05005542 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005544 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005545 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 mddev_put(mddev);
5547
5548 return 0;
5549}
5550
Linus Torvalds44ce62942007-05-09 18:51:36 -07005551static int md_media_changed(struct gendisk *disk)
5552{
5553 mddev_t *mddev = disk->private_data;
5554
5555 return mddev->changed;
5556}
5557
5558static int md_revalidate(struct gendisk *disk)
5559{
5560 mddev_t *mddev = disk->private_data;
5561
5562 mddev->changed = 0;
5563 return 0;
5564}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565static struct block_device_operations md_fops =
5566{
5567 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005568 .open = md_open,
5569 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005570 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005571 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005572 .media_changed = md_media_changed,
5573 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574};
5575
Adrian Bunk75c96f82005-05-05 16:16:09 -07005576static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
5578 mdk_thread_t *thread = arg;
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 /*
5581 * md_thread is a 'system-thread', it's priority should be very
5582 * high. We avoid resource deadlocks individually in each
5583 * raid personality. (RAID5 does preallocation) We also use RR and
5584 * the very same RT priority as kswapd, thus we will never get
5585 * into a priority inversion deadlock.
5586 *
5587 * we definitely have to have equal or higher priority than
5588 * bdflush, otherwise bdflush will deadlock if there are too
5589 * many dirty RAID5 blocks.
5590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
NeilBrown6985c432005-10-19 21:23:47 -07005592 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005593 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
NeilBrown93588e22005-11-15 00:09:12 -08005595 /* We need to wait INTERRUPTIBLE so that
5596 * we don't add to the load-average.
5597 * That means we need to be sure no signals are
5598 * pending
5599 */
5600 if (signal_pending(current))
5601 flush_signals(current);
5602
5603 wait_event_interruptible_timeout
5604 (thread->wqueue,
5605 test_bit(THREAD_WAKEUP, &thread->flags)
5606 || kthread_should_stop(),
5607 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609 clear_bit(THREAD_WAKEUP, &thread->flags);
5610
NeilBrown787453c2005-11-08 21:39:43 -08005611 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005613
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 return 0;
5615}
5616
5617void md_wakeup_thread(mdk_thread_t *thread)
5618{
5619 if (thread) {
5620 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5621 set_bit(THREAD_WAKEUP, &thread->flags);
5622 wake_up(&thread->wqueue);
5623 }
5624}
5625
5626mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5627 const char *name)
5628{
5629 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
NeilBrown9ffae0c2006-01-06 00:20:32 -08005631 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 if (!thread)
5633 return NULL;
5634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 init_waitqueue_head(&thread->wqueue);
5636
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 thread->run = run;
5638 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005639 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005640 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005641 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 kfree(thread);
5643 return NULL;
5644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 return thread;
5646}
5647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648void md_unregister_thread(mdk_thread_t *thread)
5649{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005650 if (!thread)
5651 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005652 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005653
5654 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 kfree(thread);
5656}
5657
5658void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5659{
5660 if (!mddev) {
5661 MD_BUG();
5662 return;
5663 }
5664
NeilBrownb2d444d2005-11-08 21:39:31 -08005665 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005667
5668 if (mddev->external)
5669 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005670/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5672 mdname(mddev),
5673 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5674 __builtin_return_address(0),__builtin_return_address(1),
5675 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005676*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005677 if (!mddev->pers)
5678 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 if (!mddev->pers->error_handler)
5680 return;
5681 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005682 if (mddev->degraded)
5683 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005684 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5686 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5687 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005688 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689}
5690
5691/* seq_file implementation /proc/mdstat */
5692
5693static void status_unused(struct seq_file *seq)
5694{
5695 int i = 0;
5696 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 seq_printf(seq, "unused devices: ");
5699
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005700 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 char b[BDEVNAME_SIZE];
5702 i++;
5703 seq_printf(seq, "%s ",
5704 bdevname(rdev->bdev,b));
5705 }
5706 if (!i)
5707 seq_printf(seq, "<none>");
5708
5709 seq_printf(seq, "\n");
5710}
5711
5712
5713static void status_resync(struct seq_file *seq, mddev_t * mddev)
5714{
NeilBrowndd71cf62009-05-07 12:49:35 +10005715 sector_t max_sectors, resync, res;
5716 unsigned long dt, db;
5717 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005718 int scale;
5719 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
NeilBrowndd71cf62009-05-07 12:49:35 +10005721 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005724 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005726 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
5728 /*
5729 * Should not happen.
5730 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005731 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 MD_BUG();
5733 return;
5734 }
NeilBrown4588b422006-03-27 01:18:04 -08005735 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005736 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005737 * u32, as those are the requirements for sector_div.
5738 * Thus 'scale' must be at least 10
5739 */
5740 scale = 10;
5741 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005742 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005743 scale++;
5744 }
5745 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005746 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005747
5748 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 {
NeilBrown4588b422006-03-27 01:18:04 -08005750 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 seq_printf(seq, "[");
5752 for (i = 0; i < x; i++)
5753 seq_printf(seq, "=");
5754 seq_printf(seq, ">");
5755 for (i = 0; i < y; i++)
5756 seq_printf(seq, ".");
5757 seq_printf(seq, "] ");
5758 }
NeilBrown4588b422006-03-27 01:18:04 -08005759 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005760 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5761 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005762 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5763 "check" :
5764 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5765 "resync" : "recovery"))),
5766 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005767 (unsigned long long) resync/2,
5768 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
5770 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 * dt: time from mark until now
5772 * db: blocks written from mark until now
5773 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005774 *
5775 * rt is a sector_t, so could be 32bit or 64bit.
5776 * So we divide before multiply in case it is 32bit and close
5777 * to the limit.
5778 * We scale the divisor (db) by 32 to avoid loosing precision
5779 * near the end of resync when the number of remaining sectors
5780 * is close to 'db'.
5781 * We then divide rt by 32 after multiplying by db to compensate.
5782 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 */
5784 dt = ((jiffies - mddev->resync_mark) / HZ);
5785 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005786 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5787 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
NeilBrowndd71cf62009-05-07 12:49:35 +10005789 rt = max_sectors - resync; /* number of remaining sectors */
5790 sector_div(rt, db/32+1);
5791 rt *= dt;
5792 rt >>= 5;
5793
5794 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5795 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
NeilBrownff4e8d92006-07-10 04:44:16 -07005797 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798}
5799
5800static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5801{
5802 struct list_head *tmp;
5803 loff_t l = *pos;
5804 mddev_t *mddev;
5805
5806 if (l >= 0x10000)
5807 return NULL;
5808 if (!l--)
5809 /* header */
5810 return (void*)1;
5811
5812 spin_lock(&all_mddevs_lock);
5813 list_for_each(tmp,&all_mddevs)
5814 if (!l--) {
5815 mddev = list_entry(tmp, mddev_t, all_mddevs);
5816 mddev_get(mddev);
5817 spin_unlock(&all_mddevs_lock);
5818 return mddev;
5819 }
5820 spin_unlock(&all_mddevs_lock);
5821 if (!l--)
5822 return (void*)2;/* tail */
5823 return NULL;
5824}
5825
5826static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5827{
5828 struct list_head *tmp;
5829 mddev_t *next_mddev, *mddev = v;
5830
5831 ++*pos;
5832 if (v == (void*)2)
5833 return NULL;
5834
5835 spin_lock(&all_mddevs_lock);
5836 if (v == (void*)1)
5837 tmp = all_mddevs.next;
5838 else
5839 tmp = mddev->all_mddevs.next;
5840 if (tmp != &all_mddevs)
5841 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5842 else {
5843 next_mddev = (void*)2;
5844 *pos = 0x10000;
5845 }
5846 spin_unlock(&all_mddevs_lock);
5847
5848 if (v != (void*)1)
5849 mddev_put(mddev);
5850 return next_mddev;
5851
5852}
5853
5854static void md_seq_stop(struct seq_file *seq, void *v)
5855{
5856 mddev_t *mddev = v;
5857
5858 if (mddev && v != (void*)1 && v != (void*)2)
5859 mddev_put(mddev);
5860}
5861
NeilBrownd7603b72006-01-06 00:20:30 -08005862struct mdstat_info {
5863 int event;
5864};
5865
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866static int md_seq_show(struct seq_file *seq, void *v)
5867{
5868 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005869 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005871 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005872 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005875 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 seq_printf(seq, "Personalities : ");
5877 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005878 list_for_each_entry(pers, &pers_list, list)
5879 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
5881 spin_unlock(&pers_lock);
5882 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005883 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 return 0;
5885 }
5886 if (v == (void*)2) {
5887 status_unused(seq);
5888 return 0;
5889 }
5890
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005891 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5895 seq_printf(seq, "%s : %sactive", mdname(mddev),
5896 mddev->pers ? "" : "in");
5897 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005898 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005900 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005901 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 seq_printf(seq, " %s", mddev->pers->name);
5903 }
5904
Andre Nolldd8ac332009-03-31 14:33:13 +11005905 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005906 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 char b[BDEVNAME_SIZE];
5908 seq_printf(seq, " %s[%d]",
5909 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005910 if (test_bit(WriteMostly, &rdev->flags))
5911 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005912 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 seq_printf(seq, "(F)");
5914 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005915 } else if (rdev->raid_disk < 0)
5916 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005917 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 }
5919
5920 if (!list_empty(&mddev->disks)) {
5921 if (mddev->pers)
5922 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005923 (unsigned long long)
5924 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 else
5926 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005927 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005929 if (mddev->persistent) {
5930 if (mddev->major_version != 0 ||
5931 mddev->minor_version != 90) {
5932 seq_printf(seq," super %d.%d",
5933 mddev->major_version,
5934 mddev->minor_version);
5935 }
NeilBrowne6910632008-02-06 01:39:51 -08005936 } else if (mddev->external)
5937 seq_printf(seq, " super external:%s",
5938 mddev->metadata_type);
5939 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005940 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005943 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005945 if (mddev->pers->sync_request) {
5946 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005947 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005948 seq_printf(seq, "\n ");
5949 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5950 seq_printf(seq, "\tresync=DELAYED\n ");
5951 else if (mddev->recovery_cp < MaxSector)
5952 seq_printf(seq, "\tresync=PENDING\n ");
5953 }
NeilBrown32a76272005-06-21 17:17:14 -07005954 } else
5955 seq_printf(seq, "\n ");
5956
5957 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005958 unsigned long chunk_kb;
5959 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005960 spin_lock_irqsave(&bitmap->lock, flags);
5961 chunk_kb = bitmap->chunksize >> 10;
5962 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5963 "%lu%s chunk",
5964 bitmap->pages - bitmap->missing_pages,
5965 bitmap->pages,
5966 (bitmap->pages - bitmap->missing_pages)
5967 << (PAGE_SHIFT - 10),
5968 chunk_kb ? chunk_kb : bitmap->chunksize,
5969 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005970 if (bitmap->file) {
5971 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005972 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005973 }
NeilBrown78d742d2005-06-21 17:17:15 -07005974
NeilBrown32a76272005-06-21 17:17:14 -07005975 seq_printf(seq, "\n");
5976 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 }
5978
5979 seq_printf(seq, "\n");
5980 }
5981 mddev_unlock(mddev);
5982
5983 return 0;
5984}
5985
Jan Engelhardt110518b2009-05-07 12:49:37 +10005986static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 .start = md_seq_start,
5988 .next = md_seq_next,
5989 .stop = md_seq_stop,
5990 .show = md_seq_show,
5991};
5992
5993static int md_seq_open(struct inode *inode, struct file *file)
5994{
5995 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005996 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5997 if (mi == NULL)
5998 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
6000 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006001 if (error)
6002 kfree(mi);
6003 else {
6004 struct seq_file *p = file->private_data;
6005 p->private = mi;
6006 mi->event = atomic_read(&md_event_count);
6007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 return error;
6009}
6010
NeilBrownd7603b72006-01-06 00:20:30 -08006011static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6012{
6013 struct seq_file *m = filp->private_data;
6014 struct mdstat_info *mi = m->private;
6015 int mask;
6016
6017 poll_wait(filp, &md_event_waiters, wait);
6018
6019 /* always allow read */
6020 mask = POLLIN | POLLRDNORM;
6021
6022 if (mi->event != atomic_read(&md_event_count))
6023 mask |= POLLERR | POLLPRI;
6024 return mask;
6025}
6026
Arjan van de Venfa027c22007-02-12 00:55:33 -08006027static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006028 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 .open = md_seq_open,
6030 .read = seq_read,
6031 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006032 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006033 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034};
6035
NeilBrown2604b702006-01-06 00:20:36 -08006036int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006039 list_add_tail(&p->list, &pers_list);
6040 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 spin_unlock(&pers_lock);
6042 return 0;
6043}
6044
NeilBrown2604b702006-01-06 00:20:36 -08006045int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046{
NeilBrown2604b702006-01-06 00:20:36 -08006047 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006049 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 spin_unlock(&pers_lock);
6051 return 0;
6052}
6053
NeilBrowneea1bf32009-03-31 14:27:02 +11006054static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055{
6056 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006058 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
6060 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006061 rcu_read_lock();
6062 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006064 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6065 (int)part_stat_read(&disk->part0, sectors[1]) -
6066 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006067 /* sync IO will cause sync_io to increase before the disk_stats
6068 * as sync_io is counted when a request starts, and
6069 * disk_stats is counted when it completes.
6070 * So resync activity will cause curr_events to be smaller than
6071 * when there was no such activity.
6072 * non-sync IO will cause disk_stat to increase without
6073 * increasing sync_io so curr_events will (eventually)
6074 * be larger than it was before. Once it becomes
6075 * substantially larger, the test below will cause
6076 * the array to appear non-idle, and resync will slow
6077 * down.
6078 * If there is a lot of outstanding resync activity when
6079 * we set last_event to curr_events, then all that activity
6080 * completing might cause the array to appear non-idle
6081 * and resync will be slowed down even though there might
6082 * not have been non-resync activity. This will only
6083 * happen once though. 'last_events' will soon reflect
6084 * the state where there is little or no outstanding
6085 * resync requests, and further resync activity will
6086 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006087 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006089 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 rdev->last_events = curr_events;
6091 idle = 0;
6092 }
6093 }
NeilBrown4b809912008-07-21 17:05:25 +10006094 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 return idle;
6096}
6097
6098void md_done_sync(mddev_t *mddev, int blocks, int ok)
6099{
6100 /* another "blocks" (512byte) blocks have been synced */
6101 atomic_sub(blocks, &mddev->recovery_active);
6102 wake_up(&mddev->recovery_wait);
6103 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006104 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 md_wakeup_thread(mddev->thread);
6106 // stop recovery, signal do_sync ....
6107 }
6108}
6109
6110
NeilBrown06d91a52005-06-21 17:17:12 -07006111/* md_write_start(mddev, bi)
6112 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006113 * in superblock) before writing, schedule a superblock update
6114 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006115 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006116void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117{
Neil Brown0fd62b82008-06-28 08:31:36 +10006118 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006119 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006120 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006121
NeilBrownf91de922005-11-08 21:39:36 -08006122 BUG_ON(mddev->ro == 1);
6123 if (mddev->ro == 2) {
6124 /* need to switch to read/write */
6125 mddev->ro = 0;
6126 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6127 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006128 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006129 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006130 }
NeilBrown06d91a52005-06-21 17:17:12 -07006131 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006132 if (mddev->safemode == 1)
6133 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006134 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006135 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006136 if (mddev->in_sync) {
6137 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006138 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006139 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006140 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006141 }
NeilBrowna9701a32005-11-08 21:39:34 -08006142 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006143 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006144 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006145 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006146 wait_event(mddev->sb_wait,
6147 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6148 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149}
6150
6151void md_write_end(mddev_t *mddev)
6152{
6153 if (atomic_dec_and_test(&mddev->writes_pending)) {
6154 if (mddev->safemode == 2)
6155 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006156 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6158 }
6159}
6160
NeilBrown2a2275d2007-01-26 00:57:11 -08006161/* md_allow_write(mddev)
6162 * Calling this ensures that the array is marked 'active' so that writes
6163 * may proceed without blocking. It is important to call this before
6164 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6165 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006166 *
6167 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6168 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006169 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006170int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006171{
6172 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006173 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006174 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006175 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006176 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006177 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006178
6179 spin_lock_irq(&mddev->write_lock);
6180 if (mddev->in_sync) {
6181 mddev->in_sync = 0;
6182 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6183 if (mddev->safemode_delay &&
6184 mddev->safemode == 0)
6185 mddev->safemode = 1;
6186 spin_unlock_irq(&mddev->write_lock);
6187 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006188 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006189 } else
6190 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006191
6192 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6193 return -EAGAIN;
6194 else
6195 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006196}
6197EXPORT_SYMBOL_GPL(md_allow_write);
6198
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199#define SYNC_MARKS 10
6200#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006201void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
6203 mddev_t *mddev2;
6204 unsigned int currspeed = 0,
6205 window;
NeilBrown57afd892005-06-21 17:17:13 -07006206 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 unsigned long mark[SYNC_MARKS];
6208 sector_t mark_cnt[SYNC_MARKS];
6209 int last_mark,m;
6210 struct list_head *tmp;
6211 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006212 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006213 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006214 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
6216 /* just incase thread restarts... */
6217 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6218 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006219 if (mddev->ro) /* never try to sync a read-only array */
6220 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
NeilBrown61df9d92006-10-03 01:15:57 -07006222 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6223 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6224 desc = "data-check";
6225 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6226 desc = "requested-resync";
6227 else
6228 desc = "resync";
6229 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6230 desc = "reshape";
6231 else
6232 desc = "recovery";
6233
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 /* we overload curr_resync somewhat here.
6235 * 0 == not engaged in resync at all
6236 * 2 == checking that there is no conflict with another sync
6237 * 1 == like 2, but have yielded to allow conflicting resync to
6238 * commense
6239 * other == active in resync - this many blocks
6240 *
6241 * Before starting a resync we must have set curr_resync to
6242 * 2, and then checked that every "conflicting" array has curr_resync
6243 * less than ours. When we find one that is the same or higher
6244 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6245 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6246 * This will mean we have to start checking from the beginning again.
6247 *
6248 */
6249
6250 do {
6251 mddev->curr_resync = 2;
6252
6253 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006254 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006255 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 goto skip;
6257 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006258 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 if (mddev2 == mddev)
6260 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006261 if (!mddev->parallel_resync
6262 && mddev2->curr_resync
6263 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 DEFINE_WAIT(wq);
6265 if (mddev < mddev2 && mddev->curr_resync == 2) {
6266 /* arbitrarily yield */
6267 mddev->curr_resync = 1;
6268 wake_up(&resync_wait);
6269 }
6270 if (mddev > mddev2 && mddev->curr_resync == 1)
6271 /* no need to wait here, we can wait the next
6272 * time 'round when curr_resync == 2
6273 */
6274 continue;
NeilBrown97441972008-09-19 11:49:54 +10006275 /* We need to wait 'interruptible' so as not to
6276 * contribute to the load average, and not to
6277 * be caught by 'softlockup'
6278 */
6279 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006280 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006281 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006282 printk(KERN_INFO "md: delaying %s of %s"
6283 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006285 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006287 if (signal_pending(current))
6288 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 schedule();
6290 finish_wait(&resync_wait, &wq);
6291 goto try_again;
6292 }
6293 finish_wait(&resync_wait, &wq);
6294 }
6295 }
6296 } while (mddev->curr_resync < 2);
6297
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006298 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006299 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006301 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 */
6303 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006304 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006305 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006306 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6307 j = mddev->resync_min;
6308 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006309 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006310
NeilBrownccfcc3c2006-03-27 01:18:09 -08006311 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006312 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006313 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006315 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006316 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006317 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006318 if (rdev->raid_disk >= 0 &&
6319 !test_bit(Faulty, &rdev->flags) &&
6320 !test_bit(In_sync, &rdev->flags) &&
6321 rdev->recovery_offset < j)
6322 j = rdev->recovery_offset;
6323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
NeilBrown61df9d92006-10-03 01:15:57 -07006325 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6326 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6327 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006328 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006329 "(but not more than %d KB/sec) for %s.\n",
6330 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
NeilBrowneea1bf32009-03-31 14:27:02 +11006332 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006333
NeilBrown57afd892005-06-21 17:17:13 -07006334 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 for (m = 0; m < SYNC_MARKS; m++) {
6336 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006337 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 }
6339 last_mark = 0;
6340 mddev->resync_mark = mark[last_mark];
6341 mddev->resync_mark_cnt = mark_cnt[last_mark];
6342
6343 /*
6344 * Tune reconstruction:
6345 */
6346 window = 32*(PAGE_SIZE/512);
6347 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6348 window/2,(unsigned long long) max_sectors/2);
6349
6350 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 last_check = 0;
6352
6353 if (j>2) {
6354 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006355 "md: resuming %s of %s from checkpoint.\n",
6356 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 mddev->curr_resync = j;
6358 }
6359
6360 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006361 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
NeilBrown57afd892005-06-21 17:17:13 -07006363 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006364
NeilBrown7a91ee12009-05-26 12:57:21 +10006365 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6366 ((mddev->curr_resync > mddev->curr_resync_completed &&
6367 (mddev->curr_resync - mddev->curr_resync_completed)
6368 > (max_sectors >> 4)) ||
6369 (j - mddev->curr_resync_completed)*2
6370 >= mddev->resync_max - mddev->curr_resync_completed
6371 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006372 /* time to update curr_resync_completed */
6373 blk_unplug(mddev->queue);
6374 wait_event(mddev->recovery_wait,
6375 atomic_read(&mddev->recovery_active) == 0);
6376 mddev->curr_resync_completed =
6377 mddev->curr_resync;
6378 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006379 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006380 }
NeilBrownacb180b2009-04-14 16:28:34 +10006381
NeilBrowne62e58a2009-07-01 13:15:35 +10006382 while (j >= mddev->resync_max && !kthread_should_stop()) {
6383 /* As this condition is controlled by user-space,
6384 * we can block indefinitely, so use '_interruptible'
6385 * to avoid triggering warnings.
6386 */
6387 flush_signals(current); /* just in case */
6388 wait_event_interruptible(mddev->recovery_wait,
6389 mddev->resync_max > j
6390 || kthread_should_stop());
6391 }
NeilBrownacb180b2009-04-14 16:28:34 +10006392
6393 if (kthread_should_stop())
6394 goto interrupted;
6395
NeilBrown57afd892005-06-21 17:17:13 -07006396 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006397 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006398 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006399 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 goto out;
6401 }
NeilBrown57afd892005-06-21 17:17:13 -07006402
6403 if (!skipped) { /* actual IO requested */
6404 io_sectors += sectors;
6405 atomic_add(sectors, &mddev->recovery_active);
6406 }
6407
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 j += sectors;
6409 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006410 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006411 if (last_check == 0)
6412 /* this is the earliers that rebuilt will be
6413 * visible in /proc/mdstat
6414 */
6415 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006416
6417 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 continue;
6419
NeilBrown57afd892005-06-21 17:17:13 -07006420 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421
NeilBrowndfc70642008-05-23 13:04:39 -07006422 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 break;
6424
6425 repeat:
6426 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6427 /* step marks */
6428 int next = (last_mark+1) % SYNC_MARKS;
6429
6430 mddev->resync_mark = mark[next];
6431 mddev->resync_mark_cnt = mark_cnt[next];
6432 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006433 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 last_mark = next;
6435 }
6436
6437
NeilBrownc6207272008-02-06 01:39:52 -08006438 if (kthread_should_stop())
6439 goto interrupted;
6440
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
6442 /*
6443 * this loop exits only if either when we are slower than
6444 * the 'hard' speed limit, or the system was IO-idle for
6445 * a jiffy.
6446 * the system might be non-idle CPU-wise, but we only care
6447 * about not overloading the IO subsystem. (things like an
6448 * e2fsck being done on the RAID array should execute fast)
6449 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006450 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 cond_resched();
6452
NeilBrown57afd892005-06-21 17:17:13 -07006453 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6454 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455
NeilBrown88202a02006-01-06 00:21:36 -08006456 if (currspeed > speed_min(mddev)) {
6457 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006458 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006459 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 goto repeat;
6461 }
6462 }
6463 }
NeilBrown61df9d92006-10-03 01:15:57 -07006464 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 /*
6466 * this also signals 'finished resyncing' to md_stop
6467 */
6468 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006469 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470
6471 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6472
6473 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006474 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475
NeilBrowndfc70642008-05-23 13:04:39 -07006476 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006477 mddev->curr_resync > 2) {
6478 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6479 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6480 if (mddev->curr_resync >= mddev->recovery_cp) {
6481 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006482 "md: checkpointing %s of %s.\n",
6483 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006484 mddev->recovery_cp = mddev->curr_resync;
6485 }
6486 } else
6487 mddev->recovery_cp = MaxSector;
6488 } else {
6489 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6490 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006491 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006492 if (rdev->raid_disk >= 0 &&
6493 !test_bit(Faulty, &rdev->flags) &&
6494 !test_bit(In_sync, &rdev->flags) &&
6495 rdev->recovery_offset < mddev->curr_resync)
6496 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 }
NeilBrown17571282006-12-10 02:20:52 -08006499 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501 skip:
6502 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006503 mddev->curr_resync_completed = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006504 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006505 mddev->resync_max = MaxSector;
6506 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 wake_up(&resync_wait);
6508 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6509 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006510 return;
6511
6512 interrupted:
6513 /*
6514 * got a signal, exit.
6515 */
6516 printk(KERN_INFO
6517 "md: md_do_sync() got signal ... exiting\n");
6518 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6519 goto out;
6520
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521}
NeilBrown29269552006-03-27 01:18:10 -08006522EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
6524
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006525static int remove_and_add_spares(mddev_t *mddev)
6526{
6527 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006528 int spares = 0;
6529
NeilBrown97e4f422009-03-31 14:33:13 +11006530 mddev->curr_resync_completed = 0;
6531
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006532 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006533 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006534 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006535 (test_bit(Faulty, &rdev->flags) ||
6536 ! test_bit(In_sync, &rdev->flags)) &&
6537 atomic_read(&rdev->nr_pending)==0) {
6538 if (mddev->pers->hot_remove_disk(
6539 mddev, rdev->raid_disk)==0) {
6540 char nm[20];
6541 sprintf(nm,"rd%d", rdev->raid_disk);
6542 sysfs_remove_link(&mddev->kobj, nm);
6543 rdev->raid_disk = -1;
6544 }
6545 }
6546
NeilBrown4044ba52009-01-09 08:31:11 +11006547 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006548 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006549 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006550 !test_bit(In_sync, &rdev->flags) &&
6551 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006552 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006553 if (rdev->raid_disk < 0
6554 && !test_bit(Faulty, &rdev->flags)) {
6555 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006556 if (mddev->pers->
6557 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006558 char nm[20];
6559 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006560 if (sysfs_create_link(&mddev->kobj,
6561 &rdev->kobj, nm))
6562 printk(KERN_WARNING
6563 "md: cannot register "
6564 "%s for %s\n",
6565 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006566 spares++;
6567 md_new_event(mddev);
6568 } else
6569 break;
6570 }
NeilBrowndfc70642008-05-23 13:04:39 -07006571 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006572 }
6573 return spares;
6574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575/*
6576 * This routine is regularly called by all per-raid-array threads to
6577 * deal with generic issues like resync and super-block update.
6578 * Raid personalities that don't have a thread (linear/raid0) do not
6579 * need this as they never do any recovery or update the superblock.
6580 *
6581 * It does not do any resync itself, but rather "forks" off other threads
6582 * to do that as needed.
6583 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6584 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006585 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 * and wakeups up this thread which will reap the thread and finish up.
6587 * This thread also removes any faulty devices (with nr_pending == 0).
6588 *
6589 * The overall approach is:
6590 * 1/ if the superblock needs updating, update it.
6591 * 2/ If a recovery thread is running, don't do anything else.
6592 * 3/ If recovery has finished, clean up, possibly marking spares active.
6593 * 4/ If there are any faulty devices, remove them.
6594 * 5/ If array is degraded, try to add spares devices
6595 * 6/ If array has spares or is not in-sync, start a resync thread.
6596 */
6597void md_check_recovery(mddev_t *mddev)
6598{
6599 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
6601
NeilBrown5f404022005-06-21 17:17:16 -07006602 if (mddev->bitmap)
6603 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604
6605 if (mddev->ro)
6606 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006607
6608 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006609 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006610 printk(KERN_INFO "md: %s in immediate safe mode\n",
6611 mdname(mddev));
6612 mddev->safemode = 2;
6613 }
6614 flush_signals(current);
6615 }
6616
NeilBrownc89a8ee2008-08-05 15:54:13 +10006617 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6618 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006620 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006622 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006623 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006624 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6625 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 ))
6627 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006628
NeilBrowndf5b89b2006-03-27 01:18:20 -08006629 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006630 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006631
NeilBrownc89a8ee2008-08-05 15:54:13 +10006632 if (mddev->ro) {
6633 /* Only thing we do on a ro array is remove
6634 * failed devices.
6635 */
6636 remove_and_add_spares(mddev);
6637 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6638 goto unlock;
6639 }
6640
NeilBrown31a59e32008-04-30 00:52:30 -07006641 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006642 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006643 spin_lock_irq(&mddev->write_lock);
6644 if (mddev->safemode &&
6645 !atomic_read(&mddev->writes_pending) &&
6646 !mddev->in_sync &&
6647 mddev->recovery_cp == MaxSector) {
6648 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006649 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006650 if (mddev->persistent)
6651 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6652 }
6653 if (mddev->safemode == 1)
6654 mddev->safemode = 0;
6655 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006656 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006657 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006658 }
NeilBrownfca4d842005-06-21 17:17:11 -07006659
NeilBrown850b2b42006-10-03 01:15:46 -07006660 if (mddev->flags)
6661 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006662
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006663 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006664 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006665 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006666
NeilBrown06d91a52005-06-21 17:17:12 -07006667
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6669 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6670 /* resync/recovery still happening */
6671 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6672 goto unlock;
6673 }
6674 if (mddev->sync_thread) {
6675 /* resync has finished, collect result */
6676 md_unregister_thread(mddev->sync_thread);
6677 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006678 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6679 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 /* success...*/
6681 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006682 if (mddev->pers->spare_active(mddev))
6683 sysfs_notify(&mddev->kobj, NULL,
6684 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 }
NeilBrowncea9c222009-03-31 15:15:05 +11006686 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6687 mddev->pers->finish_reshape)
6688 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006689 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006690
6691 /* if array is no-longer degraded, then any saved_raid_disk
6692 * information must be scrapped
6693 */
6694 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006695 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006696 rdev->saved_raid_disk = -1;
6697
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 mddev->recovery = 0;
6699 /* flag recovery needed just to double check */
6700 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006701 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006702 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 goto unlock;
6704 }
Neil Brown72a23c22008-06-28 08:31:41 +10006705 /* Set RUNNING before clearing NEEDED to avoid
6706 * any transients in the value of "sync_action".
6707 */
6708 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6709 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006710 /* Clear some bits that don't mean anything, but
6711 * might be left set
6712 */
NeilBrown24dd4692005-11-08 21:39:26 -08006713 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6714 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006716 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6717 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 /* no recovery is running.
6719 * remove any failed drives, then
6720 * add spares if possible.
6721 * Spare are also removed and re-added, to allow
6722 * the personality to fail the re-add.
6723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006725 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006726 if (mddev->pers->check_reshape == NULL ||
6727 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006728 /* Cannot proceed */
6729 goto unlock;
6730 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006731 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006732 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006733 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6734 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006735 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006736 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006737 } else if (mddev->recovery_cp < MaxSector) {
6738 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006739 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006740 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6741 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006743
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006745 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6746 /* We are adding a device or devices to an array
6747 * which has the bitmap stored on all devices.
6748 * So make sure all bitmap pages get written
6749 */
6750 bitmap_write_all(mddev->bitmap);
6751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 mddev->sync_thread = md_register_thread(md_do_sync,
6753 mddev,
6754 "%s_resync");
6755 if (!mddev->sync_thread) {
6756 printk(KERN_ERR "%s: could not start resync"
6757 " thread...\n",
6758 mdname(mddev));
6759 /* leave the spares where they are, it shouldn't hurt */
6760 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006761 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006763 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006764 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 }
6766 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006767 if (!mddev->sync_thread) {
6768 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6769 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6770 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006771 if (mddev->sysfs_action)
6772 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 mddev_unlock(mddev);
6775 }
6776}
6777
Dan Williams6bfe0b42008-04-30 00:52:32 -07006778void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6779{
NeilBrown3c0ee632008-10-21 13:25:28 +11006780 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006781 wait_event_timeout(rdev->blocked_wait,
6782 !test_bit(Blocked, &rdev->flags),
6783 msecs_to_jiffies(5000));
6784 rdev_dec_pending(rdev, mddev);
6785}
6786EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6787
Adrian Bunk75c96f82005-05-05 16:16:09 -07006788static int md_notify_reboot(struct notifier_block *this,
6789 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
6791 struct list_head *tmp;
6792 mddev_t *mddev;
6793
6794 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6795
6796 printk(KERN_INFO "md: stopping all md devices.\n");
6797
NeilBrown29ac4aa2008-02-06 01:39:58 -08006798 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006799 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006800 /* Force a switch to readonly even array
6801 * appears to still be in use. Hence
6802 * the '100'.
6803 */
NeilBrownd710e132008-10-13 11:55:12 +11006804 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006805 mddev_unlock(mddev);
6806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 /*
6808 * certain more exotic SCSI devices are known to be
6809 * volatile wrt too early system reboots. While the
6810 * right place to handle this issue is the given
6811 * driver, we do want to have a safe RAID driver ...
6812 */
6813 mdelay(1000*1);
6814 }
6815 return NOTIFY_DONE;
6816}
6817
Adrian Bunk75c96f82005-05-05 16:16:09 -07006818static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 .notifier_call = md_notify_reboot,
6820 .next = NULL,
6821 .priority = INT_MAX, /* before any real devices */
6822};
6823
6824static void md_geninit(void)
6825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6827
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006828 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829}
6830
Adrian Bunk75c96f82005-05-05 16:16:09 -07006831static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006833 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 return -1;
6835 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006836 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 return -1;
6838 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006839 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006840 md_probe, NULL, NULL);
6841 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 md_probe, NULL, NULL);
6843
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006845 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
6847 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849}
6850
6851
6852#ifndef MODULE
6853
6854/*
6855 * Searches all registered partitions for autorun RAID arrays
6856 * at boot time.
6857 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006858
6859static LIST_HEAD(all_detected_devices);
6860struct detected_devices_node {
6861 struct list_head list;
6862 dev_t dev;
6863};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
6865void md_autodetect_dev(dev_t dev)
6866{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006867 struct detected_devices_node *node_detected_dev;
6868
6869 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6870 if (node_detected_dev) {
6871 node_detected_dev->dev = dev;
6872 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6873 } else {
6874 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6875 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877}
6878
6879
6880static void autostart_arrays(int part)
6881{
6882 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006883 struct detected_devices_node *node_detected_dev;
6884 dev_t dev;
6885 int i_scanned, i_passed;
6886
6887 i_scanned = 0;
6888 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889
6890 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6891
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006892 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6893 i_scanned++;
6894 node_detected_dev = list_entry(all_detected_devices.next,
6895 struct detected_devices_node, list);
6896 list_del(&node_detected_dev->list);
6897 dev = node_detected_dev->dev;
6898 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006899 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 if (IS_ERR(rdev))
6901 continue;
6902
NeilBrownb2d444d2005-11-08 21:39:31 -08006903 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904 MD_BUG();
6905 continue;
6906 }
NeilBrownd0fae182008-03-04 14:29:31 -08006907 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006909 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006911
6912 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6913 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915 autorun_devices(part);
6916}
6917
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006918#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919
6920static __exit void md_exit(void)
6921{
6922 mddev_t *mddev;
6923 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006924
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006925 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006926 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006928 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 unregister_blkdev(mdp_major, "mdp");
6930 unregister_reboot_notifier(&md_notifier);
6931 unregister_sysctl_table(raid_table_header);
6932 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006933 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006935 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 }
6937}
6938
Dan Williams685784a2007-07-09 11:56:42 -07006939subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940module_exit(md_exit)
6941
NeilBrownf91de922005-11-08 21:39:36 -08006942static int get_ro(char *buffer, struct kernel_param *kp)
6943{
6944 return sprintf(buffer, "%d", start_readonly);
6945}
6946static int set_ro(const char *val, struct kernel_param *kp)
6947{
6948 char *e;
6949 int num = simple_strtoul(val, &e, 10);
6950 if (*val && (*e == '\0' || *e == '\n')) {
6951 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006952 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006953 }
6954 return -EINVAL;
6955}
6956
NeilBrown80ca3a42006-07-10 04:44:18 -07006957module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6958module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006959
NeilBrownefeb53c2009-01-09 08:31:10 +11006960module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006961
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962EXPORT_SYMBOL(register_md_personality);
6963EXPORT_SYMBOL(unregister_md_personality);
6964EXPORT_SYMBOL(md_error);
6965EXPORT_SYMBOL(md_done_sync);
6966EXPORT_SYMBOL(md_write_start);
6967EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968EXPORT_SYMBOL(md_register_thread);
6969EXPORT_SYMBOL(md_unregister_thread);
6970EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971EXPORT_SYMBOL(md_check_recovery);
6972MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006973MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006974MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);