blob: 4cbc19f5c3047f93578d655ef426f69e04bcb1fb [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
Andre Nolle7debaa2008-07-11 22:02:23 +1000443static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Andre Noll0f420352008-07-11 22:02:23 +1000445 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000448 num_sectors &= ~((sector_t)chunk_size/512 - 1);
449 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static int alloc_disk_sb(mdk_rdev_t * rdev)
453{
454 if (rdev->sb_page)
455 MD_BUG();
456
457 rdev->sb_page = alloc_page(GFP_KERNEL);
458 if (!rdev->sb_page) {
459 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000460 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
463 return 0;
464}
465
466static void free_disk_sb(mdk_rdev_t * rdev)
467{
468 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800469 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 rdev->sb_loaded = 0;
471 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000472 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100473 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475}
476
477
NeilBrown6712ecf2007-09-27 12:47:43 +0200478static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700479{
480 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800481 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700482
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700483 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
484 printk("md: super_written gets error=%d, uptodate=%d\n",
485 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
486 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800487 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700488 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700489
NeilBrowna9701a32005-11-08 21:39:34 -0800490 if (atomic_dec_and_test(&mddev->pending_writes))
491 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700492 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700493}
494
NeilBrown6712ecf2007-09-27 12:47:43 +0200495static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800496{
497 struct bio *bio2 = bio->bi_private;
498 mdk_rdev_t *rdev = bio2->bi_private;
499 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800500
501 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
502 error == -EOPNOTSUPP) {
503 unsigned long flags;
504 /* barriers don't appear to be supported :-( */
505 set_bit(BarriersNotsupp, &rdev->flags);
506 mddev->barriers_work = 0;
507 spin_lock_irqsave(&mddev->write_lock, flags);
508 bio2->bi_next = mddev->biolist;
509 mddev->biolist = bio2;
510 spin_unlock_irqrestore(&mddev->write_lock, flags);
511 wake_up(&mddev->sb_wait);
512 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200513 } else {
514 bio_put(bio2);
515 bio->bi_private = rdev;
516 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800517 }
NeilBrowna9701a32005-11-08 21:39:34 -0800518}
519
NeilBrown7bfa19f2005-06-21 17:17:28 -0700520void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
521 sector_t sector, int size, struct page *page)
522{
523 /* write first size bytes of page to sector of rdev
524 * Increment mddev->pending_writes before returning
525 * and decrement it on completion, waking up sb_wait
526 * if zero is reached.
527 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800528 *
529 * As we might need to resubmit the request if BIO_RW_BARRIER
530 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700531 */
532 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100533 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700534
535 bio->bi_bdev = rdev->bdev;
536 bio->bi_sector = sector;
537 bio_add_page(bio, page, size, 0);
538 bio->bi_private = rdev;
539 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800540 bio->bi_rw = rw;
541
NeilBrown7bfa19f2005-06-21 17:17:28 -0700542 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800543 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
544 struct bio *rbio;
545 rw |= (1<<BIO_RW_BARRIER);
546 rbio = bio_clone(bio, GFP_NOIO);
547 rbio->bi_private = bio;
548 rbio->bi_end_io = super_written_barrier;
549 submit_bio(rw, rbio);
550 } else
551 submit_bio(rw, bio);
552}
553
554void md_super_wait(mddev_t *mddev)
555{
556 /* wait for all superblock writes that were scheduled to complete.
557 * if any had to be retried (due to BARRIER problems), retry them
558 */
559 DEFINE_WAIT(wq);
560 for(;;) {
561 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
562 if (atomic_read(&mddev->pending_writes)==0)
563 break;
564 while (mddev->biolist) {
565 struct bio *bio;
566 spin_lock_irq(&mddev->write_lock);
567 bio = mddev->biolist;
568 mddev->biolist = bio->bi_next ;
569 bio->bi_next = NULL;
570 spin_unlock_irq(&mddev->write_lock);
571 submit_bio(bio->bi_rw, bio);
572 }
573 schedule();
574 }
575 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700576}
577
NeilBrown6712ecf2007-09-27 12:47:43 +0200578static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
NeilBrowna654b9d82005-06-21 17:17:27 -0700583int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct page *page, int rw)
585{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700586 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct completion event;
588 int ret;
589
Jens Axboe93dbb392009-02-16 10:25:40 +0100590 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 bio->bi_bdev = bdev;
593 bio->bi_sector = sector;
594 bio_add_page(bio, page, size, 0);
595 init_completion(&event);
596 bio->bi_private = &event;
597 bio->bi_end_io = bi_complete;
598 submit_bio(rw, bio);
599 wait_for_completion(&event);
600
601 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
602 bio_put(bio);
603 return ret;
604}
NeilBrowna8745db2006-01-06 00:20:34 -0800605EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
NeilBrown0002b272005-09-09 16:23:53 -0700607static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 char b[BDEVNAME_SIZE];
610 if (!rdev->sb_page) {
611 MD_BUG();
612 return -EINVAL;
613 }
614 if (rdev->sb_loaded)
615 return 0;
616
617
Andre Noll0f420352008-07-11 22:02:23 +1000618 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 goto fail;
620 rdev->sb_loaded = 1;
621 return 0;
622
623fail:
624 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
625 bdevname(rdev->bdev,b));
626 return -EINVAL;
627}
628
629static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
630{
Andre Noll05710462008-07-11 22:02:20 +1000631 return sb1->set_uuid0 == sb2->set_uuid0 &&
632 sb1->set_uuid1 == sb2->set_uuid1 &&
633 sb1->set_uuid2 == sb2->set_uuid2 &&
634 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
638{
639 int ret;
640 mdp_super_t *tmp1, *tmp2;
641
642 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
643 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
644
645 if (!tmp1 || !tmp2) {
646 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100647 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto abort;
649 }
650
651 *tmp1 = *sb1;
652 *tmp2 = *sb2;
653
654 /*
655 * nr_disks is not constant
656 */
657 tmp1->nr_disks = 0;
658 tmp2->nr_disks = 0;
659
Andre Nollce0c8e02008-07-11 22:02:20 +1000660 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700662 kfree(tmp1);
663 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return ret;
665}
666
NeilBrown4d167f02007-05-09 02:35:37 -0700667
668static u32 md_csum_fold(u32 csum)
669{
670 csum = (csum & 0xffff) + (csum >> 16);
671 return (csum & 0xffff) + (csum >> 16);
672}
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static unsigned int calc_sb_csum(mdp_super_t * sb)
675{
NeilBrown4d167f02007-05-09 02:35:37 -0700676 u64 newcsum = 0;
677 u32 *sb32 = (u32*)sb;
678 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 unsigned int disk_csum, csum;
680
681 disk_csum = sb->sb_csum;
682 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700683
684 for (i = 0; i < MD_SB_BYTES/4 ; i++)
685 newcsum += sb32[i];
686 csum = (newcsum & 0xffffffff) + (newcsum>>32);
687
688
689#ifdef CONFIG_ALPHA
690 /* This used to use csum_partial, which was wrong for several
691 * reasons including that different results are returned on
692 * different architectures. It isn't critical that we get exactly
693 * the same return value as before (we always csum_fold before
694 * testing, and that removes any differences). However as we
695 * know that csum_partial always returned a 16bit value on
696 * alphas, do a fold to maximise conformity to previous behaviour.
697 */
698 sb->sb_csum = md_csum_fold(disk_csum);
699#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return csum;
703}
704
705
706/*
707 * Handle superblock details.
708 * We want to be able to handle multiple superblock formats
709 * so we have a common interface to them all, and an array of
710 * different handlers.
711 * We rely on user-space to write the initial superblock, and support
712 * reading and updating of superblocks.
713 * Interface methods are:
714 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
715 * loads and validates a superblock on dev.
716 * if refdev != NULL, compare superblocks on both devices
717 * Return:
718 * 0 - dev has a superblock that is compatible with refdev
719 * 1 - dev has a superblock that is compatible and newer than refdev
720 * so dev should be used as the refdev in future
721 * -EINVAL superblock incompatible or invalid
722 * -othererror e.g. -EIO
723 *
724 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
725 * Verify that dev is acceptable into mddev.
726 * The first time, mddev->raid_disks will be 0, and data from
727 * dev should be merged in. Subsequent calls check that dev
728 * is new enough. Return 0 or -EINVAL
729 *
730 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
731 * Update the superblock for rdev with data in mddev
732 * This does not write to disc.
733 *
734 */
735
736struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000737 char *name;
738 struct module *owner;
739 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
740 int minor_version);
741 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
742 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
743 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000744 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745};
746
747/*
748 * load_super for 0.90.0
749 */
750static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
751{
752 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
753 mdp_super_t *sb;
754 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /*
Andre Noll0f420352008-07-11 22:02:23 +1000757 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * it's at the end of the disk.
759 *
760 * It also happens to be a multiple of 4Kb.
761 */
Andre Noll0f420352008-07-11 22:02:23 +1000762 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
NeilBrown0002b272005-09-09 16:23:53 -0700764 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (ret) return ret;
766
767 ret = -EINVAL;
768
769 bdevname(rdev->bdev, b);
770 sb = (mdp_super_t*)page_address(rdev->sb_page);
771
772 if (sb->md_magic != MD_SB_MAGIC) {
773 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
774 b);
775 goto abort;
776 }
777
778 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800779 sb->minor_version < 90 ||
780 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
782 sb->major_version, sb->minor_version,
783 b);
784 goto abort;
785 }
786
787 if (sb->raid_disks <= 0)
788 goto abort;
789
NeilBrown4d167f02007-05-09 02:35:37 -0700790 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
792 b);
793 goto abort;
794 }
795
796 rdev->preferred_minor = sb->md_minor;
797 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700798 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
NeilBrowne11e93f2007-05-09 02:35:36 -0700800 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
801 if (sb->level != 1 && sb->level != 4
802 && sb->level != 5 && sb->level != 6
803 && sb->level != 10) {
804 /* FIXME use a better test */
805 printk(KERN_WARNING
806 "md: bitmaps not supported for this level.\n");
807 goto abort;
808 }
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (sb->level == LEVEL_MULTIPATH)
812 rdev->desc_nr = -1;
813 else
814 rdev->desc_nr = sb->this_disk.number;
815
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700816 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700818 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 __u64 ev1, ev2;
820 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
821 if (!uuid_equal(refsb, sb)) {
822 printk(KERN_WARNING "md: %s has different UUID to %s\n",
823 b, bdevname(refdev->bdev,b2));
824 goto abort;
825 }
826 if (!sb_equal(refsb, sb)) {
827 printk(KERN_WARNING "md: %s has same UUID"
828 " but different superblock to %s\n",
829 b, bdevname(refdev->bdev, b2));
830 goto abort;
831 }
832 ev1 = md_event(sb);
833 ev2 = md_event(refsb);
834 if (ev1 > ev2)
835 ret = 1;
836 else
837 ret = 0;
838 }
Andre Nolldd8ac332009-03-31 14:33:13 +1100839 rdev->sectors = calc_num_sectors(rdev, sb->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Andre Nolldd8ac332009-03-31 14:33:13 +1100841 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800842 /* "this cannot possibly happen" ... */
843 ret = -EINVAL;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 abort:
846 return ret;
847}
848
849/*
850 * validate_super for 0.90.0
851 */
852static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
853{
854 mdp_disk_t *desc;
855 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700856 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
NeilBrown41158c72005-06-21 17:17:25 -0700858 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800859 clear_bit(Faulty, &rdev->flags);
860 clear_bit(In_sync, &rdev->flags);
861 clear_bit(WriteMostly, &rdev->flags);
862 clear_bit(BarriersNotsupp, &rdev->flags);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (mddev->raid_disks == 0) {
865 mddev->major_version = 0;
866 mddev->minor_version = sb->minor_version;
867 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800868 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 mddev->chunk_size = sb->chunk_size;
870 mddev->ctime = sb->ctime;
871 mddev->utime = sb->utime;
872 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800873 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 mddev->layout = sb->layout;
875 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100876 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700877 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700878 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700879 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
NeilBrownf6705572006-03-27 01:18:11 -0800881 if (mddev->minor_version >= 91) {
882 mddev->reshape_position = sb->reshape_position;
883 mddev->delta_disks = sb->delta_disks;
884 mddev->new_level = sb->new_level;
885 mddev->new_layout = sb->new_layout;
886 mddev->new_chunk = sb->new_chunk;
887 } else {
888 mddev->reshape_position = MaxSector;
889 mddev->delta_disks = 0;
890 mddev->new_level = mddev->level;
891 mddev->new_layout = mddev->layout;
892 mddev->new_chunk = mddev->chunk_size;
893 }
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (sb->state & (1<<MD_SB_CLEAN))
896 mddev->recovery_cp = MaxSector;
897 else {
898 if (sb->events_hi == sb->cp_events_hi &&
899 sb->events_lo == sb->cp_events_lo) {
900 mddev->recovery_cp = sb->recovery_cp;
901 } else
902 mddev->recovery_cp = 0;
903 }
904
905 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
906 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
907 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
908 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
909
910 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700911
912 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700913 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700914 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700915
NeilBrown41158c72005-06-21 17:17:25 -0700916 } else if (mddev->pers == NULL) {
917 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 ++ev1;
919 if (ev1 < mddev->events)
920 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700921 } else if (mddev->bitmap) {
922 /* if adding to array with a bitmap, then we can accept an
923 * older device ... but not too old.
924 */
NeilBrown41158c72005-06-21 17:17:25 -0700925 if (ev1 < mddev->bitmap->events_cleared)
926 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700927 } else {
928 if (ev1 < mddev->events)
929 /* just a hot-add of a new device, leave raid_disk at -1 */
930 return 0;
931 }
NeilBrown41158c72005-06-21 17:17:25 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 desc = sb->disks + rdev->desc_nr;
935
936 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800937 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700938 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
939 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800940 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 rdev->raid_disk = desc->raid_disk;
942 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700943 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
944 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700945 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800946 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return 0;
948}
949
950/*
951 * sync_super for 0.90.0
952 */
953static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
954{
955 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 mdk_rdev_t *rdev2;
957 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /* make rdev->sb match mddev data..
961 *
962 * 1/ zero out disks
963 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
964 * 3/ any empty disks < next_spare become removed
965 *
966 * disks[0] gets initialised to REMOVED because
967 * we cannot be sure from other fields if it has
968 * been initialised or not.
969 */
970 int i;
971 int active=0, working=0,failed=0,spare=0,nr_disks=0;
972
NeilBrown61181562005-09-09 16:24:02 -0700973 rdev->sb_size = MD_SB_BYTES;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 sb = (mdp_super_t*)page_address(rdev->sb_page);
976
977 memset(sb, 0, sizeof(*sb));
978
979 sb->md_magic = MD_SB_MAGIC;
980 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 sb->patch_version = mddev->patch_version;
982 sb->gvalid_words = 0; /* ignored */
983 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
984 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
985 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
986 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
987
988 sb->ctime = mddev->ctime;
989 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +1100990 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 sb->raid_disks = mddev->raid_disks;
992 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800993 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 sb->utime = mddev->utime;
995 sb->state = 0;
996 sb->events_hi = (mddev->events>>32);
997 sb->events_lo = (u32)mddev->events;
998
NeilBrownf6705572006-03-27 01:18:11 -0800999 if (mddev->reshape_position == MaxSector)
1000 sb->minor_version = 90;
1001 else {
1002 sb->minor_version = 91;
1003 sb->reshape_position = mddev->reshape_position;
1004 sb->new_level = mddev->new_level;
1005 sb->delta_disks = mddev->delta_disks;
1006 sb->new_layout = mddev->new_layout;
1007 sb->new_chunk = mddev->new_chunk;
1008 }
1009 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (mddev->in_sync)
1011 {
1012 sb->recovery_cp = mddev->recovery_cp;
1013 sb->cp_events_hi = (mddev->events>>32);
1014 sb->cp_events_lo = (u32)mddev->events;
1015 if (mddev->recovery_cp == MaxSector)
1016 sb->state = (1<< MD_SB_CLEAN);
1017 } else
1018 sb->recovery_cp = 0;
1019
1020 sb->layout = mddev->layout;
1021 sb->chunk_size = mddev->chunk_size;
1022
NeilBrowna654b9d82005-06-21 17:17:27 -07001023 if (mddev->bitmap && mddev->bitmap_file == NULL)
1024 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001027 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001029 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001030 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1031 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -08001032 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001034 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001035 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 d = &sb->disks[rdev2->desc_nr];
1037 nr_disks++;
1038 d->number = rdev2->desc_nr;
1039 d->major = MAJOR(rdev2->bdev->bd_dev);
1040 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001041 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1042 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 d->raid_disk = rdev2->raid_disk;
1044 else
1045 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001046 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -08001048 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 d->state = (1<<MD_DISK_ACTIVE);
1050 d->state |= (1<<MD_DISK_SYNC);
1051 active++;
1052 working++;
1053 } else {
1054 d->state = 0;
1055 spare++;
1056 working++;
1057 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001058 if (test_bit(WriteMostly, &rdev2->flags))
1059 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* now set the "removed" and "faulty" bits on any missing devices */
1062 for (i=0 ; i < mddev->raid_disks ; i++) {
1063 mdp_disk_t *d = &sb->disks[i];
1064 if (d->state == 0 && d->number == 0) {
1065 d->number = i;
1066 d->raid_disk = i;
1067 d->state = (1<<MD_DISK_REMOVED);
1068 d->state |= (1<<MD_DISK_FAULTY);
1069 failed++;
1070 }
1071 }
1072 sb->nr_disks = nr_disks;
1073 sb->active_disks = active;
1074 sb->working_disks = working;
1075 sb->failed_disks = failed;
1076 sb->spare_disks = spare;
1077
1078 sb->this_disk = sb->disks[rdev->desc_nr];
1079 sb->sb_csum = calc_sb_csum(sb);
1080}
1081
1082/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001083 * rdev_size_change for 0.90.0
1084 */
1085static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001086super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001087{
Andre Noll58c0fed2009-03-31 14:33:13 +11001088 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001089 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001090 if (rdev->mddev->bitmap_offset)
1091 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001092 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001093 if (!num_sectors || num_sectors > rdev->sb_start)
1094 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001095 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001096 rdev->sb_page);
1097 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001098 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001099}
1100
1101
1102/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 * version 1 superblock
1104 */
1105
NeilBrown1c05b4b2006-10-21 10:24:08 -07001106static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001108 __le32 disk_csum;
1109 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 unsigned long long newcsum;
1111 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001112 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int i;
1114
1115 disk_csum = sb->sb_csum;
1116 sb->sb_csum = 0;
1117 newcsum = 0;
1118 for (i=0; size>=4; size -= 4 )
1119 newcsum += le32_to_cpu(*isuper++);
1120
1121 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001122 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1125 sb->sb_csum = disk_csum;
1126 return cpu_to_le32(csum);
1127}
1128
1129static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1130{
1131 struct mdp_superblock_1 *sb;
1132 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001133 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001135 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /*
Andre Noll0f420352008-07-11 22:02:23 +10001138 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 * It is always aligned to a 4K boundary and
1140 * depeding on minor_version, it can be:
1141 * 0: At least 8K, but less than 12K, from end of device
1142 * 1: At start of device
1143 * 2: 4K from start of device.
1144 */
1145 switch(minor_version) {
1146 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001147 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1148 sb_start -= 8*2;
1149 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
1151 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001152 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001155 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
1157 default:
1158 return -EINVAL;
1159 }
Andre Noll0f420352008-07-11 22:02:23 +10001160 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
NeilBrown0002b272005-09-09 16:23:53 -07001162 /* superblock is rarely larger than 1K, but it can be larger,
1163 * and it is safe to read 4k, so we do that
1164 */
1165 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (ret) return ret;
1167
1168
1169 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1170
1171 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1172 sb->major_version != cpu_to_le32(1) ||
1173 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001174 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001175 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -EINVAL;
1177
1178 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1179 printk("md: invalid superblock checksum on %s\n",
1180 bdevname(rdev->bdev,b));
1181 return -EINVAL;
1182 }
1183 if (le64_to_cpu(sb->data_size) < 10) {
1184 printk("md: data_size too small on %s\n",
1185 bdevname(rdev->bdev,b));
1186 return -EINVAL;
1187 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001188 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1189 if (sb->level != cpu_to_le32(1) &&
1190 sb->level != cpu_to_le32(4) &&
1191 sb->level != cpu_to_le32(5) &&
1192 sb->level != cpu_to_le32(6) &&
1193 sb->level != cpu_to_le32(10)) {
1194 printk(KERN_WARNING
1195 "md: bitmaps not supported for this level.\n");
1196 return -EINVAL;
1197 }
1198 }
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 rdev->preferred_minor = 0xffff;
1201 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001202 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
NeilBrown0002b272005-09-09 16:23:53 -07001204 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001205 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001206 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001207 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1208
1209 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001210 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001211 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001212
NeilBrown31b65a02006-07-10 04:44:14 -07001213 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1214 rdev->desc_nr = -1;
1215 else
1216 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1217
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001218 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001219 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001220 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 __u64 ev1, ev2;
1222 struct mdp_superblock_1 *refsb =
1223 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1224
1225 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1226 sb->level != refsb->level ||
1227 sb->layout != refsb->layout ||
1228 sb->chunksize != refsb->chunksize) {
1229 printk(KERN_WARNING "md: %s has strangely different"
1230 " superblock to %s\n",
1231 bdevname(rdev->bdev,b),
1232 bdevname(refdev->bdev,b2));
1233 return -EINVAL;
1234 }
1235 ev1 = le64_to_cpu(sb->events);
1236 ev2 = le64_to_cpu(refsb->events);
1237
1238 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001239 ret = 1;
1240 else
1241 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
NeilBrowna1801f82008-03-04 14:29:31 -08001243 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001244 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1245 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001247 rdev->sectors = rdev->sb_start;
1248 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001250 rdev->sectors = le64_to_cpu(sb->data_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (le32_to_cpu(sb->chunksize))
Andre Nolldd8ac332009-03-31 14:33:13 +11001252 rdev->sectors &= ~((sector_t)le32_to_cpu(sb->chunksize) - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001253
Andre Nolldd8ac332009-03-31 14:33:13 +11001254 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001255 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001256 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1260{
1261 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001262 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
NeilBrown41158c72005-06-21 17:17:25 -07001264 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001265 clear_bit(Faulty, &rdev->flags);
1266 clear_bit(In_sync, &rdev->flags);
1267 clear_bit(WriteMostly, &rdev->flags);
1268 clear_bit(BarriersNotsupp, &rdev->flags);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (mddev->raid_disks == 0) {
1271 mddev->major_version = 1;
1272 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001273 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1275 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1276 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1277 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001278 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 mddev->layout = le32_to_cpu(sb->layout);
1280 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001281 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001282 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001283 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001284 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1287 memcpy(mddev->uuid, sb->set_uuid, 16);
1288
1289 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001290
NeilBrown71c08052005-09-09 16:23:51 -07001291 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001292 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001293 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001294
NeilBrownf6705572006-03-27 01:18:11 -08001295 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1296 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1297 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1298 mddev->new_level = le32_to_cpu(sb->new_level);
1299 mddev->new_layout = le32_to_cpu(sb->new_layout);
1300 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1301 } else {
1302 mddev->reshape_position = MaxSector;
1303 mddev->delta_disks = 0;
1304 mddev->new_level = mddev->level;
1305 mddev->new_layout = mddev->layout;
1306 mddev->new_chunk = mddev->chunk_size;
1307 }
1308
NeilBrown41158c72005-06-21 17:17:25 -07001309 } else if (mddev->pers == NULL) {
1310 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 ++ev1;
1312 if (ev1 < mddev->events)
1313 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001314 } else if (mddev->bitmap) {
1315 /* If adding to array with a bitmap, then we can accept an
1316 * older device, but not too old.
1317 */
NeilBrown41158c72005-06-21 17:17:25 -07001318 if (ev1 < mddev->bitmap->events_cleared)
1319 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001320 } else {
1321 if (ev1 < mddev->events)
1322 /* just a hot-add of a new device, leave raid_disk at -1 */
1323 return 0;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (mddev->level != LEVEL_MULTIPATH) {
1326 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1328 switch(role) {
1329 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
1331 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001332 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 break;
1334 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001335 if ((le32_to_cpu(sb->feature_map) &
1336 MD_FEATURE_RECOVERY_OFFSET))
1337 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1338 else
1339 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 rdev->raid_disk = role;
1341 break;
1342 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001343 if (sb->devflags & WriteMostly1)
1344 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001345 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001346 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return 0;
1349}
1350
1351static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1352{
1353 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 mdk_rdev_t *rdev2;
1355 int max_dev, i;
1356 /* make rdev->sb match mddev and rdev data. */
1357
1358 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1359
1360 sb->feature_map = 0;
1361 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001362 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 memset(sb->pad1, 0, sizeof(sb->pad1));
1364 memset(sb->pad2, 0, sizeof(sb->pad2));
1365 memset(sb->pad3, 0, sizeof(sb->pad3));
1366
1367 sb->utime = cpu_to_le64((__u64)mddev->utime);
1368 sb->events = cpu_to_le64(mddev->events);
1369 if (mddev->in_sync)
1370 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1371 else
1372 sb->resync_offset = cpu_to_le64(0);
1373
NeilBrown1c05b4b2006-10-21 10:24:08 -07001374 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001375
NeilBrownf0ca3402006-02-02 14:28:04 -08001376 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001377 sb->size = cpu_to_le64(mddev->dev_sectors);
NeilBrownf0ca3402006-02-02 14:28:04 -08001378
NeilBrowna654b9d82005-06-21 17:17:27 -07001379 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1380 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001381 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001382 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001383
1384 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001385 !test_bit(In_sync, &rdev->flags)) {
1386 if (mddev->curr_resync_completed > rdev->recovery_offset)
1387 rdev->recovery_offset = mddev->curr_resync_completed;
1388 if (rdev->recovery_offset > 0) {
1389 sb->feature_map |=
1390 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1391 sb->recovery_offset =
1392 cpu_to_le64(rdev->recovery_offset);
1393 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001394 }
1395
NeilBrownf6705572006-03-27 01:18:11 -08001396 if (mddev->reshape_position != MaxSector) {
1397 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1398 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1399 sb->new_layout = cpu_to_le32(mddev->new_layout);
1400 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1401 sb->new_level = cpu_to_le32(mddev->new_level);
1402 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1403 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001406 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (rdev2->desc_nr+1 > max_dev)
1408 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001409
1410 if (max_dev > le32_to_cpu(sb->max_dev))
1411 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 for (i=0; i<max_dev;i++)
1413 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1414
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001415 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001417 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001419 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001421 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1422 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 else
1424 sb->dev_roles[i] = cpu_to_le16(0xffff);
1425 }
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 sb->sb_csum = calc_sb_1_csum(sb);
1428}
1429
Chris Webb0cd17fe2008-06-28 08:31:46 +10001430static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001431super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001432{
1433 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001434 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001435 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001436 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001437 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001438 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001439 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1440 max_sectors -= rdev->data_offset;
1441 if (!num_sectors || num_sectors > max_sectors)
1442 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001443 } else if (rdev->mddev->bitmap_offset) {
1444 /* minor version 0 with bitmap we can't move */
1445 return 0;
1446 } else {
1447 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001448 sector_t sb_start;
1449 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1450 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001451 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001452 if (!num_sectors || num_sectors > max_sectors)
1453 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001454 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001455 }
1456 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001457 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001458 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001459 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001460 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001461 rdev->sb_page);
1462 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001463 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001464}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Adrian Bunk75c96f82005-05-05 16:16:09 -07001466static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 [0] = {
1468 .name = "0.90.0",
1469 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001470 .load_super = super_90_load,
1471 .validate_super = super_90_validate,
1472 .sync_super = super_90_sync,
1473 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 },
1475 [1] = {
1476 .name = "md-1",
1477 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001478 .load_super = super_1_load,
1479 .validate_super = super_1_validate,
1480 .sync_super = super_1_sync,
1481 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 },
1483};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1486{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001487 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
NeilBrown4b809912008-07-21 17:05:25 +10001489 rcu_read_lock();
1490 rdev_for_each_rcu(rdev, mddev1)
1491 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001492 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001493 rdev2->bdev->bd_contains) {
1494 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001495 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001496 }
1497 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return 0;
1499}
1500
1501static LIST_HEAD(pending_raid_disks);
1502
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001503static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev)
1504{
1505 struct mdk_personality *pers = mddev->pers;
1506 struct gendisk *disk = mddev->gendisk;
1507 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1508 struct blk_integrity *bi_mddev = blk_get_integrity(disk);
1509
1510 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1511 if (pers && pers->level >= 4 && pers->level <= 6)
1512 return;
1513
1514 /* If rdev is integrity capable, register profile for mddev */
1515 if (!bi_mddev && bi_rdev) {
1516 if (blk_integrity_register(disk, bi_rdev))
1517 printk(KERN_ERR "%s: %s Could not register integrity!\n",
1518 __func__, disk->disk_name);
1519 else
1520 printk(KERN_NOTICE "Enabling data integrity on %s\n",
1521 disk->disk_name);
1522 return;
1523 }
1524
1525 /* Check that mddev and rdev have matching profiles */
1526 if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) {
1527 printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__,
1528 disk->disk_name, rdev->bdev->bd_disk->disk_name);
1529 printk(KERN_NOTICE "Disabling data integrity on %s\n",
1530 disk->disk_name);
1531 blk_integrity_unregister(disk);
1532 }
1533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1536{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001537 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001538 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001539 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 if (rdev->mddev) {
1543 MD_BUG();
1544 return -EINVAL;
1545 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001546
1547 /* prevent duplicates */
1548 if (find_rdev(mddev, rdev->bdev->bd_dev))
1549 return -EEXIST;
1550
Andre Nolldd8ac332009-03-31 14:33:13 +11001551 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1552 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1553 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001554 if (mddev->pers) {
1555 /* Cannot change size, so fail
1556 * If mddev->level <= 0, then we don't care
1557 * about aligning sizes (e.g. linear)
1558 */
1559 if (mddev->level > 0)
1560 return -ENOSPC;
1561 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001562 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 /* Verify rdev->desc_nr is unique.
1566 * If it is -1, assign a free number, else
1567 * check number is not in use
1568 */
1569 if (rdev->desc_nr < 0) {
1570 int choice = 0;
1571 if (mddev->pers) choice = mddev->raid_disks;
1572 while (find_rdev_nr(mddev, choice))
1573 choice++;
1574 rdev->desc_nr = choice;
1575 } else {
1576 if (find_rdev_nr(mddev, rdev->desc_nr))
1577 return -EBUSY;
1578 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001579 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1580 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1581 mdname(mddev), mddev->max_disks);
1582 return -EBUSY;
1583 }
NeilBrown19133a42005-11-08 21:39:35 -08001584 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001585 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001586 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001589 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001590
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001591 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001592 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001593
Tejun Heo0762b8b2008-08-25 19:56:12 +09001594 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001595 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1596 kobject_del(&rdev->kobj);
1597 goto fail;
1598 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001599 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1600
NeilBrown4b809912008-07-21 17:05:25 +10001601 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001602 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001603
1604 /* May as well allow recovery to be retried once */
1605 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001606
1607 md_integrity_check(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001609
1610 fail:
1611 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1612 b, mdname(mddev));
1613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
NeilBrown177a99b2008-02-06 01:39:56 -08001616static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001617{
1618 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1619 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001620 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1624{
1625 char b[BDEVNAME_SIZE];
1626 if (!rdev->mddev) {
1627 MD_BUG();
1628 return;
1629 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001630 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001631 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1633 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001634 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001635 sysfs_put(rdev->sysfs_state);
1636 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001637 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001638 * writing to 'remove' to "dev/state". We also need
1639 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001640 */
NeilBrown4b809912008-07-21 17:05:25 +10001641 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001642 INIT_WORK(&rdev->del_work, md_delayed_delete);
1643 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001644 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
1647/*
1648 * prevent the device from being mounted, repartitioned or
1649 * otherwise reused by a RAID array (or any other kernel
1650 * subsystem), by bd_claiming the device.
1651 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001652static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 int err = 0;
1655 struct block_device *bdev;
1656 char b[BDEVNAME_SIZE];
1657
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001658 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (IS_ERR(bdev)) {
1660 printk(KERN_ERR "md: could not open %s.\n",
1661 __bdevname(dev, b));
1662 return PTR_ERR(bdev);
1663 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001664 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (err) {
1666 printk(KERN_ERR "md: could not bd_claim %s.\n",
1667 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001668 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return err;
1670 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001671 if (!shared)
1672 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 rdev->bdev = bdev;
1674 return err;
1675}
1676
1677static void unlock_rdev(mdk_rdev_t *rdev)
1678{
1679 struct block_device *bdev = rdev->bdev;
1680 rdev->bdev = NULL;
1681 if (!bdev)
1682 MD_BUG();
1683 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001684 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687void md_autodetect_dev(dev_t dev);
1688
1689static void export_rdev(mdk_rdev_t * rdev)
1690{
1691 char b[BDEVNAME_SIZE];
1692 printk(KERN_INFO "md: export_rdev(%s)\n",
1693 bdevname(rdev->bdev,b));
1694 if (rdev->mddev)
1695 MD_BUG();
1696 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001698 if (test_bit(AutoDetected, &rdev->flags))
1699 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700#endif
1701 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001702 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705static void kick_rdev_from_array(mdk_rdev_t * rdev)
1706{
1707 unbind_rdev_from_array(rdev);
1708 export_rdev(rdev);
1709}
1710
1711static void export_array(mddev_t *mddev)
1712{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001713 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
NeilBrownd089c6a2008-02-06 01:39:59 -08001715 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (!rdev->mddev) {
1717 MD_BUG();
1718 continue;
1719 }
1720 kick_rdev_from_array(rdev);
1721 }
1722 if (!list_empty(&mddev->disks))
1723 MD_BUG();
1724 mddev->raid_disks = 0;
1725 mddev->major_version = 0;
1726}
1727
1728static void print_desc(mdp_disk_t *desc)
1729{
1730 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1731 desc->major,desc->minor,desc->raid_disk,desc->state);
1732}
1733
Cheng Renquancd2ac932009-01-09 08:31:08 +11001734static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 int i;
1737
1738 printk(KERN_INFO
1739 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1740 sb->major_version, sb->minor_version, sb->patch_version,
1741 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1742 sb->ctime);
1743 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1744 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1745 sb->md_minor, sb->layout, sb->chunk_size);
1746 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1747 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1748 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1749 sb->failed_disks, sb->spare_disks,
1750 sb->sb_csum, (unsigned long)sb->events_lo);
1751
1752 printk(KERN_INFO);
1753 for (i = 0; i < MD_SB_DISKS; i++) {
1754 mdp_disk_t *desc;
1755
1756 desc = sb->disks + i;
1757 if (desc->number || desc->major || desc->minor ||
1758 desc->raid_disk || (desc->state && (desc->state != 4))) {
1759 printk(" D %2d: ", i);
1760 print_desc(desc);
1761 }
1762 }
1763 printk(KERN_INFO "md: THIS: ");
1764 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Cheng Renquancd2ac932009-01-09 08:31:08 +11001767static void print_sb_1(struct mdp_superblock_1 *sb)
1768{
1769 __u8 *uuid;
1770
1771 uuid = sb->set_uuid;
1772 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1773 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1774 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1775 le32_to_cpu(sb->major_version),
1776 le32_to_cpu(sb->feature_map),
1777 uuid[0], uuid[1], uuid[2], uuid[3],
1778 uuid[4], uuid[5], uuid[6], uuid[7],
1779 uuid[8], uuid[9], uuid[10], uuid[11],
1780 uuid[12], uuid[13], uuid[14], uuid[15],
1781 sb->set_name,
1782 (unsigned long long)le64_to_cpu(sb->ctime)
1783 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1784
1785 uuid = sb->device_uuid;
1786 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1787 " RO:%llu\n"
1788 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1789 ":%02x%02x%02x%02x%02x%02x\n"
1790 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1791 KERN_INFO "md: (MaxDev:%u) \n",
1792 le32_to_cpu(sb->level),
1793 (unsigned long long)le64_to_cpu(sb->size),
1794 le32_to_cpu(sb->raid_disks),
1795 le32_to_cpu(sb->layout),
1796 le32_to_cpu(sb->chunksize),
1797 (unsigned long long)le64_to_cpu(sb->data_offset),
1798 (unsigned long long)le64_to_cpu(sb->data_size),
1799 (unsigned long long)le64_to_cpu(sb->super_offset),
1800 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1801 le32_to_cpu(sb->dev_number),
1802 uuid[0], uuid[1], uuid[2], uuid[3],
1803 uuid[4], uuid[5], uuid[6], uuid[7],
1804 uuid[8], uuid[9], uuid[10], uuid[11],
1805 uuid[12], uuid[13], uuid[14], uuid[15],
1806 sb->devflags,
1807 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1808 (unsigned long long)le64_to_cpu(sb->events),
1809 (unsigned long long)le64_to_cpu(sb->resync_offset),
1810 le32_to_cpu(sb->sb_csum),
1811 le32_to_cpu(sb->max_dev)
1812 );
1813}
1814
1815static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001818 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1819 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001820 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1821 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001823 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1824 switch (major_version) {
1825 case 0:
1826 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1827 break;
1828 case 1:
1829 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1830 break;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 } else
1833 printk(KERN_INFO "md: no rdev superblock!\n");
1834}
1835
Adrian Bunk5e563412006-06-26 00:27:42 -07001836static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001838 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 mdk_rdev_t *rdev;
1840 mddev_t *mddev;
1841 char b[BDEVNAME_SIZE];
1842
1843 printk("\n");
1844 printk("md: **********************************\n");
1845 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1846 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001847 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
NeilBrown32a76272005-06-21 17:17:14 -07001849 if (mddev->bitmap)
1850 bitmap_print_sb(mddev->bitmap);
1851 else
1852 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001853 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 printk("<%s>", bdevname(rdev->bdev,b));
1855 printk("\n");
1856
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001857 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001858 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 printk("md: **********************************\n");
1861 printk("\n");
1862}
1863
1864
NeilBrown42543762006-06-26 00:27:57 -07001865static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
NeilBrown42543762006-06-26 00:27:57 -07001867 /* Update each superblock (in-memory image), but
1868 * if we are allowed to, skip spares which already
1869 * have the right event counter, or have one earlier
1870 * (which would mean they aren't being marked as dirty
1871 * with the rest of the array)
1872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001875 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001876 if (rdev->sb_events == mddev->events ||
1877 (nospares &&
1878 rdev->raid_disk < 0 &&
1879 (rdev->sb_events&1)==0 &&
1880 rdev->sb_events+1 == mddev->events)) {
1881 /* Don't update this superblock */
1882 rdev->sb_loaded = 2;
1883 } else {
1884 super_types[mddev->major_version].
1885 sync_super(mddev, rdev);
1886 rdev->sb_loaded = 1;
1887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889}
1890
NeilBrown850b2b42006-10-03 01:15:46 -07001891static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001894 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001895 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
NeilBrown8377bc82008-04-30 00:52:28 -07001897 if (mddev->external)
1898 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001900 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001901
NeilBrown850b2b42006-10-03 01:15:46 -07001902 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1903 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1904 force_change = 1;
1905 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1906 /* just a clean<-> dirty transition, possibly leave spares alone,
1907 * though if events isn't the right even/odd, we will have to do
1908 * spares after all
1909 */
1910 nospares = 1;
1911 if (force_change)
1912 nospares = 0;
1913 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001914 /* If the array is degraded, then skipping spares is both
1915 * dangerous and fairly pointless.
1916 * Dangerous because a device that was removed from the array
1917 * might have a event_count that still looks up-to-date,
1918 * so it can be re-added without a resync.
1919 * Pointless because if there are any spares to skip,
1920 * then a recovery will happen and soon that array won't
1921 * be degraded any more and the spare can go back to sleep then.
1922 */
NeilBrown850b2b42006-10-03 01:15:46 -07001923 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001924
NeilBrown06d91a52005-06-21 17:17:12 -07001925 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001927
1928 /* If this is just a dirty<->clean transition, and the array is clean
1929 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001930 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001931 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001932 && (mddev->events & 1)
1933 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001934 mddev->events--;
1935 else {
1936 /* otherwise we have to go forward and ... */
1937 mddev->events ++;
1938 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1939 /* .. if the array isn't clean, insist on an odd 'events' */
1940 if ((mddev->events&1)==0) {
1941 mddev->events++;
1942 nospares = 0;
1943 }
1944 } else {
1945 /* otherwise insist on an even 'events' (for clean states) */
1946 if ((mddev->events&1)) {
1947 mddev->events++;
1948 nospares = 0;
1949 }
1950 }
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 if (!mddev->events) {
1954 /*
1955 * oops, this 64-bit counter should never wrap.
1956 * Either we are in around ~1 trillion A.C., assuming
1957 * 1 reboot per second, or we have a bug:
1958 */
1959 MD_BUG();
1960 mddev->events --;
1961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 /*
1964 * do not write anything to disk if using
1965 * nonpersistent superblocks
1966 */
NeilBrown06d91a52005-06-21 17:17:12 -07001967 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001968 if (!mddev->external)
1969 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1970
NeilBrowna9701a32005-11-08 21:39:34 -08001971 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001972 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001974 }
NeilBrowne6910632008-02-06 01:39:51 -08001975 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001976 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 dprintk(KERN_INFO
1979 "md: updating %s RAID superblock on device (in sync %d)\n",
1980 mdname(mddev),mddev->in_sync);
1981
NeilBrown4ad13662007-07-17 04:06:13 -07001982 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001983 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 char b[BDEVNAME_SIZE];
1985 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001986 if (rdev->sb_loaded != 1)
1987 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001988 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 dprintk("(skipping faulty ");
1990
1991 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001992 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001993 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001994 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001995 rdev->sb_page);
1996 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1997 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001998 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001999 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 } else
2002 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002003 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 /* only need to write one superblock... */
2005 break;
2006 }
NeilBrowna9701a32005-11-08 21:39:34 -08002007 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002008 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002009
NeilBrowna9701a32005-11-08 21:39:34 -08002010 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002011 if (mddev->in_sync != sync_req ||
2012 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002013 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002014 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002015 goto repeat;
2016 }
NeilBrown850b2b42006-10-03 01:15:46 -07002017 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002018 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002019 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002020 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2021 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
2024
Andre Noll7f6ce762008-03-23 18:34:54 +01002025/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002026 * We want to accept with case. For this we use cmd_match.
2027 */
2028static int cmd_match(const char *cmd, const char *str)
2029{
2030 /* See if cmd, written into a sysfs file, matches
2031 * str. They must either be the same, or cmd can
2032 * have a trailing newline
2033 */
2034 while (*cmd && *str && *cmd == *str) {
2035 cmd++;
2036 str++;
2037 }
2038 if (*cmd == '\n')
2039 cmd++;
2040 if (*str || *cmd)
2041 return 0;
2042 return 1;
2043}
2044
NeilBrown86e6ffd2005-11-08 21:39:24 -08002045struct rdev_sysfs_entry {
2046 struct attribute attr;
2047 ssize_t (*show)(mdk_rdev_t *, char *);
2048 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2049};
2050
2051static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002052state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002053{
2054 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002055 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002056
NeilBrownb2d444d2005-11-08 21:39:31 -08002057 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002058 len+= sprintf(page+len, "%sfaulty",sep);
2059 sep = ",";
2060 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002061 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002062 len += sprintf(page+len, "%sin_sync",sep);
2063 sep = ",";
2064 }
NeilBrownf6556752006-06-26 00:28:01 -07002065 if (test_bit(WriteMostly, &rdev->flags)) {
2066 len += sprintf(page+len, "%swrite_mostly",sep);
2067 sep = ",";
2068 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002069 if (test_bit(Blocked, &rdev->flags)) {
2070 len += sprintf(page+len, "%sblocked", sep);
2071 sep = ",";
2072 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002073 if (!test_bit(Faulty, &rdev->flags) &&
2074 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002075 len += sprintf(page+len, "%sspare", sep);
2076 sep = ",";
2077 }
2078 return len+sprintf(page+len, "\n");
2079}
2080
NeilBrown45dc2de2006-06-26 00:27:58 -07002081static ssize_t
2082state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2083{
2084 /* can write
2085 * faulty - simulates and error
2086 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002087 * writemostly - sets write_mostly
2088 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002089 * blocked - sets the Blocked flag
2090 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002091 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002092 */
2093 int err = -EINVAL;
2094 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2095 md_error(rdev->mddev, rdev);
2096 err = 0;
2097 } else if (cmd_match(buf, "remove")) {
2098 if (rdev->raid_disk >= 0)
2099 err = -EBUSY;
2100 else {
2101 mddev_t *mddev = rdev->mddev;
2102 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002103 if (mddev->pers)
2104 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002105 md_new_event(mddev);
2106 err = 0;
2107 }
NeilBrownf6556752006-06-26 00:28:01 -07002108 } else if (cmd_match(buf, "writemostly")) {
2109 set_bit(WriteMostly, &rdev->flags);
2110 err = 0;
2111 } else if (cmd_match(buf, "-writemostly")) {
2112 clear_bit(WriteMostly, &rdev->flags);
2113 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002114 } else if (cmd_match(buf, "blocked")) {
2115 set_bit(Blocked, &rdev->flags);
2116 err = 0;
2117 } else if (cmd_match(buf, "-blocked")) {
2118 clear_bit(Blocked, &rdev->flags);
2119 wake_up(&rdev->blocked_wait);
2120 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2121 md_wakeup_thread(rdev->mddev->thread);
2122
2123 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002124 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2125 set_bit(In_sync, &rdev->flags);
2126 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002127 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002128 if (!err && rdev->sysfs_state)
2129 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002130 return err ? err : len;
2131}
NeilBrown80ca3a42006-07-10 04:44:18 -07002132static struct rdev_sysfs_entry rdev_state =
2133__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002134
2135static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002136errors_show(mdk_rdev_t *rdev, char *page)
2137{
2138 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2139}
2140
2141static ssize_t
2142errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2143{
2144 char *e;
2145 unsigned long n = simple_strtoul(buf, &e, 10);
2146 if (*buf && (*e == 0 || *e == '\n')) {
2147 atomic_set(&rdev->corrected_errors, n);
2148 return len;
2149 }
2150 return -EINVAL;
2151}
2152static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002153__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002154
NeilBrown014236d2006-01-06 00:20:55 -08002155static ssize_t
2156slot_show(mdk_rdev_t *rdev, char *page)
2157{
2158 if (rdev->raid_disk < 0)
2159 return sprintf(page, "none\n");
2160 else
2161 return sprintf(page, "%d\n", rdev->raid_disk);
2162}
2163
2164static ssize_t
2165slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2166{
2167 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002168 int err;
2169 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002170 int slot = simple_strtoul(buf, &e, 10);
2171 if (strncmp(buf, "none", 4)==0)
2172 slot = -1;
2173 else if (e==buf || (*e && *e!= '\n'))
2174 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002175 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002176 /* Setting 'slot' on an active array requires also
2177 * updating the 'rd%d' link, and communicating
2178 * with the personality with ->hot_*_disk.
2179 * For now we only support removing
2180 * failed/spare devices. This normally happens automatically,
2181 * but not when the metadata is externally managed.
2182 */
NeilBrownc303da62008-02-06 01:39:51 -08002183 if (rdev->raid_disk == -1)
2184 return -EEXIST;
2185 /* personality does all needed checks */
2186 if (rdev->mddev->pers->hot_add_disk == NULL)
2187 return -EINVAL;
2188 err = rdev->mddev->pers->
2189 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2190 if (err)
2191 return err;
2192 sprintf(nm, "rd%d", rdev->raid_disk);
2193 sysfs_remove_link(&rdev->mddev->kobj, nm);
2194 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2195 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002196 } else if (rdev->mddev->pers) {
2197 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002198 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002199 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002200 */
2201
2202 if (rdev->raid_disk != -1)
2203 return -EBUSY;
2204
2205 if (rdev->mddev->pers->hot_add_disk == NULL)
2206 return -EINVAL;
2207
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002208 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002209 if (rdev2->raid_disk == slot)
2210 return -EEXIST;
2211
2212 rdev->raid_disk = slot;
2213 if (test_bit(In_sync, &rdev->flags))
2214 rdev->saved_raid_disk = slot;
2215 else
2216 rdev->saved_raid_disk = -1;
2217 err = rdev->mddev->pers->
2218 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002219 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002220 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002221 return err;
Neil Brown52664732008-06-28 08:31:44 +10002222 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002223 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002224 sprintf(nm, "rd%d", rdev->raid_disk);
2225 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2226 printk(KERN_WARNING
2227 "md: cannot register "
2228 "%s for %s\n",
2229 nm, mdname(rdev->mddev));
2230
2231 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002232 } else {
2233 if (slot >= rdev->mddev->raid_disks)
2234 return -ENOSPC;
2235 rdev->raid_disk = slot;
2236 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002237 clear_bit(Faulty, &rdev->flags);
2238 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002239 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002240 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002241 }
NeilBrown014236d2006-01-06 00:20:55 -08002242 return len;
2243}
2244
2245
2246static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002247__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002248
NeilBrown93c8cad2006-01-06 00:20:56 -08002249static ssize_t
2250offset_show(mdk_rdev_t *rdev, char *page)
2251{
Andrew Morton6961ece2006-01-06 00:20:59 -08002252 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002253}
2254
2255static ssize_t
2256offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2257{
2258 char *e;
2259 unsigned long long offset = simple_strtoull(buf, &e, 10);
2260 if (e==buf || (*e && *e != '\n'))
2261 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002262 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002263 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002264 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002265 /* Must set offset before size, so overlap checks
2266 * can be sane */
2267 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002268 rdev->data_offset = offset;
2269 return len;
2270}
2271
2272static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002273__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002274
NeilBrown83303b62006-01-06 00:21:06 -08002275static ssize_t
2276rdev_size_show(mdk_rdev_t *rdev, char *page)
2277{
Andre Nolldd8ac332009-03-31 14:33:13 +11002278 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002279}
2280
NeilBrownc5d79ad2008-02-06 01:39:54 -08002281static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2282{
2283 /* check if two start/length pairs overlap */
2284 if (s1+l1 <= s2)
2285 return 0;
2286 if (s2+l2 <= s1)
2287 return 0;
2288 return 1;
2289}
2290
Dan Williamsb522adc2009-03-31 15:00:31 +11002291static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2292{
2293 unsigned long long blocks;
2294 sector_t new;
2295
2296 if (strict_strtoull(buf, 10, &blocks) < 0)
2297 return -EINVAL;
2298
2299 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2300 return -EINVAL; /* sector conversion overflow */
2301
2302 new = blocks * 2;
2303 if (new != blocks * 2)
2304 return -EINVAL; /* unsigned long long to sector_t overflow */
2305
2306 *sectors = new;
2307 return 0;
2308}
2309
NeilBrown83303b62006-01-06 00:21:06 -08002310static ssize_t
2311rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2312{
NeilBrown27c529b2008-03-04 14:29:33 -08002313 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002314 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002315 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002316
Dan Williamsb522adc2009-03-31 15:00:31 +11002317 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002318 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002319 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002320 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002321 sectors = super_types[my_mddev->major_version].
2322 rdev_size_change(rdev, sectors);
2323 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002324 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002325 } else if (!sectors)
2326 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2327 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002328 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002329 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002330 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002331
Andre Nolldd8ac332009-03-31 14:33:13 +11002332 rdev->sectors = sectors;
2333 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002334 /* need to check that all other rdevs with the same ->bdev
2335 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002336 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002337 * we have to change it back, we will have the lock again.
2338 */
2339 mddev_t *mddev;
2340 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002341 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002342
NeilBrown27c529b2008-03-04 14:29:33 -08002343 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002344 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002345 mdk_rdev_t *rdev2;
2346
2347 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002348 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002349 if (test_bit(AllReserved, &rdev2->flags) ||
2350 (rdev->bdev == rdev2->bdev &&
2351 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002352 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002353 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002354 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002355 overlap = 1;
2356 break;
2357 }
2358 mddev_unlock(mddev);
2359 if (overlap) {
2360 mddev_put(mddev);
2361 break;
2362 }
2363 }
NeilBrown27c529b2008-03-04 14:29:33 -08002364 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002365 if (overlap) {
2366 /* Someone else could have slipped in a size
2367 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002368 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002369 * safe, and trust userspace not to race with
2370 * itself
2371 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002372 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002373 return -EBUSY;
2374 }
2375 }
NeilBrown83303b62006-01-06 00:21:06 -08002376 return len;
2377}
2378
2379static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002380__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002381
NeilBrown86e6ffd2005-11-08 21:39:24 -08002382static struct attribute *rdev_default_attrs[] = {
2383 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002384 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002385 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002386 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002387 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002388 NULL,
2389};
2390static ssize_t
2391rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2392{
2393 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2394 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002395 mddev_t *mddev = rdev->mddev;
2396 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002397
2398 if (!entry->show)
2399 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002400
2401 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2402 if (!rv) {
2403 if (rdev->mddev == NULL)
2404 rv = -EBUSY;
2405 else
2406 rv = entry->show(rdev, page);
2407 mddev_unlock(mddev);
2408 }
2409 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002410}
2411
2412static ssize_t
2413rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2414 const char *page, size_t length)
2415{
2416 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2417 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002418 ssize_t rv;
2419 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002420
2421 if (!entry->store)
2422 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002423 if (!capable(CAP_SYS_ADMIN))
2424 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002425 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002426 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002427 if (rdev->mddev == NULL)
2428 rv = -EBUSY;
2429 else
2430 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002431 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002432 }
2433 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002434}
2435
2436static void rdev_free(struct kobject *ko)
2437{
2438 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2439 kfree(rdev);
2440}
2441static struct sysfs_ops rdev_sysfs_ops = {
2442 .show = rdev_attr_show,
2443 .store = rdev_attr_store,
2444};
2445static struct kobj_type rdev_ktype = {
2446 .release = rdev_free,
2447 .sysfs_ops = &rdev_sysfs_ops,
2448 .default_attrs = rdev_default_attrs,
2449};
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/*
2452 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2453 *
2454 * mark the device faulty if:
2455 *
2456 * - the device is nonexistent (zero size)
2457 * - the device has no valid superblock
2458 *
2459 * a faulty rdev _never_ has rdev->sb set.
2460 */
2461static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2462{
2463 char b[BDEVNAME_SIZE];
2464 int err;
2465 mdk_rdev_t *rdev;
2466 sector_t size;
2467
NeilBrown9ffae0c2006-01-06 00:20:32 -08002468 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (!rdev) {
2470 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2471 return ERR_PTR(-ENOMEM);
2472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 if ((err = alloc_disk_sb(rdev)))
2475 goto abort_free;
2476
NeilBrownc5d79ad2008-02-06 01:39:54 -08002477 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (err)
2479 goto abort_free;
2480
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002481 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002484 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002485 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002486 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002488 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002490 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002491 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2494 if (!size) {
2495 printk(KERN_WARNING
2496 "md: %s has zero or unknown size, marking faulty!\n",
2497 bdevname(rdev->bdev,b));
2498 err = -EINVAL;
2499 goto abort_free;
2500 }
2501
2502 if (super_format >= 0) {
2503 err = super_types[super_format].
2504 load_super(rdev, NULL, super_minor);
2505 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002506 printk(KERN_WARNING
2507 "md: %s does not have a valid v%d.%d "
2508 "superblock, not importing!\n",
2509 bdevname(rdev->bdev,b),
2510 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 goto abort_free;
2512 }
2513 if (err < 0) {
2514 printk(KERN_WARNING
2515 "md: could not read %s's sb, not importing!\n",
2516 bdevname(rdev->bdev,b));
2517 goto abort_free;
2518 }
2519 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002522 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 return rdev;
2525
2526abort_free:
2527 if (rdev->sb_page) {
2528 if (rdev->bdev)
2529 unlock_rdev(rdev);
2530 free_disk_sb(rdev);
2531 }
2532 kfree(rdev);
2533 return ERR_PTR(err);
2534}
2535
2536/*
2537 * Check a full RAID array for plausibility
2538 */
2539
2540
NeilBrowna757e642005-04-16 15:26:42 -07002541static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
2543 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002544 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 char b[BDEVNAME_SIZE];
2546
2547 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002548 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 switch (super_types[mddev->major_version].
2550 load_super(rdev, freshest, mddev->minor_version)) {
2551 case 1:
2552 freshest = rdev;
2553 break;
2554 case 0:
2555 break;
2556 default:
2557 printk( KERN_ERR \
2558 "md: fatal superblock inconsistency in %s"
2559 " -- removing from array\n",
2560 bdevname(rdev->bdev,b));
2561 kick_rdev_from_array(rdev);
2562 }
2563
2564
2565 super_types[mddev->major_version].
2566 validate_super(mddev, freshest);
2567
2568 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002569 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002570 if (rdev->desc_nr >= mddev->max_disks ||
2571 i > mddev->max_disks) {
2572 printk(KERN_WARNING
2573 "md: %s: %s: only %d devices permitted\n",
2574 mdname(mddev), bdevname(rdev->bdev, b),
2575 mddev->max_disks);
2576 kick_rdev_from_array(rdev);
2577 continue;
2578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (rdev != freshest)
2580 if (super_types[mddev->major_version].
2581 validate_super(mddev, rdev)) {
2582 printk(KERN_WARNING "md: kicking non-fresh %s"
2583 " from array!\n",
2584 bdevname(rdev->bdev,b));
2585 kick_rdev_from_array(rdev);
2586 continue;
2587 }
2588 if (mddev->level == LEVEL_MULTIPATH) {
2589 rdev->desc_nr = i++;
2590 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002591 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002592 } else if (rdev->raid_disk >= mddev->raid_disks) {
2593 rdev->raid_disk = -1;
2594 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 }
2597
2598
2599
2600 if (mddev->recovery_cp != MaxSector &&
2601 mddev->level >= 1)
2602 printk(KERN_ERR "md: %s: raid array is not clean"
2603 " -- starting background reconstruction\n",
2604 mdname(mddev));
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
NeilBrown19052c02008-08-05 15:54:13 +10002608static void md_safemode_timeout(unsigned long data);
2609
NeilBrowneae17012005-11-08 21:39:23 -08002610static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002611safe_delay_show(mddev_t *mddev, char *page)
2612{
2613 int msec = (mddev->safemode_delay*1000)/HZ;
2614 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2615}
2616static ssize_t
2617safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2618{
2619 int scale=1;
2620 int dot=0;
2621 int i;
2622 unsigned long msec;
2623 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002624
NeilBrown16f17b32006-06-26 00:27:37 -07002625 /* remove a period, and count digits after it */
2626 if (len >= sizeof(buf))
2627 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002628 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002629 for (i=0; i<len; i++) {
2630 if (dot) {
2631 if (isdigit(buf[i])) {
2632 buf[i-1] = buf[i];
2633 scale *= 10;
2634 }
2635 buf[i] = 0;
2636 } else if (buf[i] == '.') {
2637 dot=1;
2638 buf[i] = 0;
2639 }
2640 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002641 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002642 return -EINVAL;
2643 msec = (msec * 1000) / scale;
2644 if (msec == 0)
2645 mddev->safemode_delay = 0;
2646 else {
NeilBrown19052c02008-08-05 15:54:13 +10002647 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002648 mddev->safemode_delay = (msec*HZ)/1000;
2649 if (mddev->safemode_delay == 0)
2650 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002651 if (mddev->safemode_delay < old_delay)
2652 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002653 }
2654 return len;
2655}
2656static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002657__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002658
2659static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002660level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002661{
NeilBrown2604b702006-01-06 00:20:36 -08002662 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002663 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002664 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002665 else if (mddev->clevel[0])
2666 return sprintf(page, "%s\n", mddev->clevel);
2667 else if (mddev->level != LEVEL_NONE)
2668 return sprintf(page, "%d\n", mddev->level);
2669 else
2670 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002671}
2672
NeilBrownd9d166c2006-01-06 00:20:51 -08002673static ssize_t
2674level_store(mddev_t *mddev, const char *buf, size_t len)
2675{
NeilBrown245f46c2009-03-31 14:39:39 +11002676 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002677 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002678 struct mdk_personality *pers;
2679 void *priv;
2680
2681 if (mddev->pers == NULL) {
2682 if (len == 0)
2683 return 0;
2684 if (len >= sizeof(mddev->clevel))
2685 return -ENOSPC;
2686 strncpy(mddev->clevel, buf, len);
2687 if (mddev->clevel[len-1] == '\n')
2688 len--;
2689 mddev->clevel[len] = 0;
2690 mddev->level = LEVEL_NONE;
2691 return rv;
2692 }
2693
2694 /* request to change the personality. Need to ensure:
2695 * - array is not engaged in resync/recovery/reshape
2696 * - old personality can be suspended
2697 * - new personality will access other array.
2698 */
2699
2700 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002701 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002702
2703 if (!mddev->pers->quiesce) {
2704 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2705 mdname(mddev), mddev->pers->name);
2706 return -EINVAL;
2707 }
2708
2709 /* Now find the new personality */
2710 if (len == 0 || len >= sizeof(level))
2711 return -EINVAL;
2712 strncpy(level, buf, len);
2713 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002714 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002715 level[len] = 0;
2716
2717 request_module("md-%s", level);
2718 spin_lock(&pers_lock);
2719 pers = find_pers(LEVEL_NONE, level);
2720 if (!pers || !try_module_get(pers->owner)) {
2721 spin_unlock(&pers_lock);
2722 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2723 return -EINVAL;
2724 }
2725 spin_unlock(&pers_lock);
2726
2727 if (pers == mddev->pers) {
2728 /* Nothing to do! */
2729 module_put(pers->owner);
2730 return rv;
2731 }
2732 if (!pers->takeover) {
2733 module_put(pers->owner);
2734 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2735 mdname(mddev), level);
2736 return -EINVAL;
2737 }
2738
2739 /* ->takeover must set new_* and/or delta_disks
2740 * if it succeeds, and may set them when it fails.
2741 */
2742 priv = pers->takeover(mddev);
2743 if (IS_ERR(priv)) {
2744 mddev->new_level = mddev->level;
2745 mddev->new_layout = mddev->layout;
2746 mddev->new_chunk = mddev->chunk_size;
2747 mddev->raid_disks -= mddev->delta_disks;
2748 mddev->delta_disks = 0;
2749 module_put(pers->owner);
2750 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2751 mdname(mddev), level);
2752 return PTR_ERR(priv);
2753 }
2754
2755 /* Looks like we have a winner */
2756 mddev_suspend(mddev);
2757 mddev->pers->stop(mddev);
2758 module_put(mddev->pers->owner);
2759 mddev->pers = pers;
2760 mddev->private = priv;
2761 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2762 mddev->level = mddev->new_level;
2763 mddev->layout = mddev->new_layout;
2764 mddev->chunk_size = mddev->new_chunk;
2765 mddev->delta_disks = 0;
2766 pers->run(mddev);
2767 mddev_resume(mddev);
2768 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2769 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2770 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002771 return rv;
2772}
2773
2774static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002775__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002776
NeilBrownd4dbd022006-06-26 00:27:59 -07002777
2778static ssize_t
2779layout_show(mddev_t *mddev, char *page)
2780{
2781 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002782 if (mddev->reshape_position != MaxSector &&
2783 mddev->layout != mddev->new_layout)
2784 return sprintf(page, "%d (%d)\n",
2785 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002786 return sprintf(page, "%d\n", mddev->layout);
2787}
2788
2789static ssize_t
2790layout_store(mddev_t *mddev, const char *buf, size_t len)
2791{
2792 char *e;
2793 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002794
2795 if (!*buf || (*e && *e != '\n'))
2796 return -EINVAL;
2797
NeilBrownb3546032009-03-31 14:56:41 +11002798 if (mddev->pers) {
2799 int err;
2800 if (mddev->pers->reconfig == NULL)
2801 return -EBUSY;
2802 err = mddev->pers->reconfig(mddev, n, -1);
2803 if (err)
2804 return err;
2805 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002806 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002807 if (mddev->reshape_position == MaxSector)
2808 mddev->layout = n;
2809 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002810 return len;
2811}
2812static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002813__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002814
2815
NeilBrowneae17012005-11-08 21:39:23 -08002816static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002817raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002818{
NeilBrownbb636542005-11-08 21:39:45 -08002819 if (mddev->raid_disks == 0)
2820 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002821 if (mddev->reshape_position != MaxSector &&
2822 mddev->delta_disks != 0)
2823 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2824 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002825 return sprintf(page, "%d\n", mddev->raid_disks);
2826}
2827
NeilBrownda943b92006-01-06 00:20:54 -08002828static int update_raid_disks(mddev_t *mddev, int raid_disks);
2829
2830static ssize_t
2831raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2832{
NeilBrownda943b92006-01-06 00:20:54 -08002833 char *e;
2834 int rv = 0;
2835 unsigned long n = simple_strtoul(buf, &e, 10);
2836
2837 if (!*buf || (*e && *e != '\n'))
2838 return -EINVAL;
2839
2840 if (mddev->pers)
2841 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002842 else if (mddev->reshape_position != MaxSector) {
2843 int olddisks = mddev->raid_disks - mddev->delta_disks;
2844 mddev->delta_disks = n - olddisks;
2845 mddev->raid_disks = n;
2846 } else
NeilBrownda943b92006-01-06 00:20:54 -08002847 mddev->raid_disks = n;
2848 return rv ? rv : len;
2849}
2850static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002851__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002852
NeilBrown24dd4692005-11-08 21:39:26 -08002853static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002854chunk_size_show(mddev_t *mddev, char *page)
2855{
NeilBrown08a02ec2007-05-09 02:35:38 -07002856 if (mddev->reshape_position != MaxSector &&
2857 mddev->chunk_size != mddev->new_chunk)
2858 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2859 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002860 return sprintf(page, "%d\n", mddev->chunk_size);
2861}
2862
2863static ssize_t
2864chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2865{
NeilBrown3b343802006-01-06 00:20:47 -08002866 char *e;
2867 unsigned long n = simple_strtoul(buf, &e, 10);
2868
NeilBrown3b343802006-01-06 00:20:47 -08002869 if (!*buf || (*e && *e != '\n'))
2870 return -EINVAL;
2871
NeilBrownb3546032009-03-31 14:56:41 +11002872 if (mddev->pers) {
2873 int err;
2874 if (mddev->pers->reconfig == NULL)
2875 return -EBUSY;
2876 err = mddev->pers->reconfig(mddev, -1, n);
2877 if (err)
2878 return err;
2879 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002880 mddev->new_chunk = n;
NeilBrownb3546032009-03-31 14:56:41 +11002881 if (mddev->reshape_position == MaxSector)
2882 mddev->chunk_size = n;
2883 }
NeilBrown3b343802006-01-06 00:20:47 -08002884 return len;
2885}
2886static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002887__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002888
NeilBrowna94213b2006-06-26 00:28:00 -07002889static ssize_t
2890resync_start_show(mddev_t *mddev, char *page)
2891{
NeilBrownd1a7c502009-03-31 15:24:32 +11002892 if (mddev->recovery_cp == MaxSector)
2893 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002894 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2895}
2896
2897static ssize_t
2898resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2899{
NeilBrowna94213b2006-06-26 00:28:00 -07002900 char *e;
2901 unsigned long long n = simple_strtoull(buf, &e, 10);
2902
2903 if (mddev->pers)
2904 return -EBUSY;
2905 if (!*buf || (*e && *e != '\n'))
2906 return -EINVAL;
2907
2908 mddev->recovery_cp = n;
2909 return len;
2910}
2911static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002912__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002913
NeilBrown9e653b62006-06-26 00:27:58 -07002914/*
2915 * The array state can be:
2916 *
2917 * clear
2918 * No devices, no size, no level
2919 * Equivalent to STOP_ARRAY ioctl
2920 * inactive
2921 * May have some settings, but array is not active
2922 * all IO results in error
2923 * When written, doesn't tear down array, but just stops it
2924 * suspended (not supported yet)
2925 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002926 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002927 * readonly
2928 * no resync can happen. no superblocks get written.
2929 * write requests fail
2930 * read-auto
2931 * like readonly, but behaves like 'clean' on a write request.
2932 *
2933 * clean - no pending writes, but otherwise active.
2934 * When written to inactive array, starts without resync
2935 * If a write request arrives then
2936 * if metadata is known, mark 'dirty' and switch to 'active'.
2937 * if not known, block and switch to write-pending
2938 * If written to an active array that has pending writes, then fails.
2939 * active
2940 * fully active: IO and resync can be happening.
2941 * When written to inactive array, starts with resync
2942 *
2943 * write-pending
2944 * clean, but writes are blocked waiting for 'active' to be written.
2945 *
2946 * active-idle
2947 * like active, but no writes have been seen for a while (100msec).
2948 *
2949 */
2950enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2951 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002952static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002953 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2954 "write-pending", "active-idle", NULL };
2955
2956static int match_word(const char *word, char **list)
2957{
2958 int n;
2959 for (n=0; list[n]; n++)
2960 if (cmd_match(word, list[n]))
2961 break;
2962 return n;
2963}
2964
2965static ssize_t
2966array_state_show(mddev_t *mddev, char *page)
2967{
2968 enum array_state st = inactive;
2969
2970 if (mddev->pers)
2971 switch(mddev->ro) {
2972 case 1:
2973 st = readonly;
2974 break;
2975 case 2:
2976 st = read_auto;
2977 break;
2978 case 0:
2979 if (mddev->in_sync)
2980 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002981 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2982 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002983 else if (mddev->safemode)
2984 st = active_idle;
2985 else
2986 st = active;
2987 }
2988 else {
2989 if (list_empty(&mddev->disks) &&
2990 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11002991 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002992 st = clear;
2993 else
2994 st = inactive;
2995 }
2996 return sprintf(page, "%s\n", array_states[st]);
2997}
2998
Neil Browndf5b20c2008-07-11 22:02:22 +10002999static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003000static int do_md_run(mddev_t * mddev);
3001static int restart_array(mddev_t *mddev);
3002
3003static ssize_t
3004array_state_store(mddev_t *mddev, const char *buf, size_t len)
3005{
3006 int err = -EINVAL;
3007 enum array_state st = match_word(buf, array_states);
3008 switch(st) {
3009 case bad_word:
3010 break;
3011 case clear:
3012 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003013 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003014 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003015 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003016 break;
3017 case inactive:
3018 /* stopping an active array */
3019 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003020 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003021 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003022 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003023 } else
3024 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003025 break;
3026 case suspended:
3027 break; /* not supported yet */
3028 case readonly:
3029 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003030 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003031 else {
3032 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003033 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003034 err = do_md_run(mddev);
3035 }
3036 break;
3037 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003038 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003039 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003040 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003041 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003042 err = restart_array(mddev);
3043 if (err == 0) {
3044 mddev->ro = 2;
3045 set_disk_ro(mddev->gendisk, 0);
3046 }
NeilBrown9e653b62006-06-26 00:27:58 -07003047 } else {
3048 mddev->ro = 2;
3049 err = do_md_run(mddev);
3050 }
3051 break;
3052 case clean:
3053 if (mddev->pers) {
3054 restart_array(mddev);
3055 spin_lock_irq(&mddev->write_lock);
3056 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003057 if (mddev->in_sync == 0) {
3058 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003059 if (mddev->safemode == 1)
3060 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003061 if (mddev->persistent)
3062 set_bit(MD_CHANGE_CLEAN,
3063 &mddev->flags);
3064 }
3065 err = 0;
3066 } else
3067 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003068 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003069 } else
3070 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003071 break;
3072 case active:
3073 if (mddev->pers) {
3074 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003075 if (mddev->external)
3076 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003077 wake_up(&mddev->sb_wait);
3078 err = 0;
3079 } else {
3080 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003081 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003082 err = do_md_run(mddev);
3083 }
3084 break;
3085 case write_pending:
3086 case active_idle:
3087 /* these cannot be set */
3088 break;
3089 }
3090 if (err)
3091 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003092 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003093 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003094 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003095 }
NeilBrown9e653b62006-06-26 00:27:58 -07003096}
NeilBrown80ca3a42006-07-10 04:44:18 -07003097static struct md_sysfs_entry md_array_state =
3098__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003099
NeilBrown6d7ff732006-01-06 00:21:16 -08003100static ssize_t
3101null_show(mddev_t *mddev, char *page)
3102{
3103 return -EINVAL;
3104}
3105
3106static ssize_t
3107new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3108{
3109 /* buf must be %d:%d\n? giving major and minor numbers */
3110 /* The new device is added to the array.
3111 * If the array has a persistent superblock, we read the
3112 * superblock to initialise info and check validity.
3113 * Otherwise, only checking done is that in bind_rdev_to_array,
3114 * which mainly checks size.
3115 */
3116 char *e;
3117 int major = simple_strtoul(buf, &e, 10);
3118 int minor;
3119 dev_t dev;
3120 mdk_rdev_t *rdev;
3121 int err;
3122
3123 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3124 return -EINVAL;
3125 minor = simple_strtoul(e+1, &e, 10);
3126 if (*e && *e != '\n')
3127 return -EINVAL;
3128 dev = MKDEV(major, minor);
3129 if (major != MAJOR(dev) ||
3130 minor != MINOR(dev))
3131 return -EOVERFLOW;
3132
3133
3134 if (mddev->persistent) {
3135 rdev = md_import_device(dev, mddev->major_version,
3136 mddev->minor_version);
3137 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3138 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3139 mdk_rdev_t, same_set);
3140 err = super_types[mddev->major_version]
3141 .load_super(rdev, rdev0, mddev->minor_version);
3142 if (err < 0)
3143 goto out;
3144 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003145 } else if (mddev->external)
3146 rdev = md_import_device(dev, -2, -1);
3147 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003148 rdev = md_import_device(dev, -1, -1);
3149
3150 if (IS_ERR(rdev))
3151 return PTR_ERR(rdev);
3152 err = bind_rdev_to_array(rdev, mddev);
3153 out:
3154 if (err)
3155 export_rdev(rdev);
3156 return err ? err : len;
3157}
3158
3159static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003160__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003161
3162static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003163bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3164{
3165 char *end;
3166 unsigned long chunk, end_chunk;
3167
3168 if (!mddev->bitmap)
3169 goto out;
3170 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3171 while (*buf) {
3172 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3173 if (buf == end) break;
3174 if (*end == '-') { /* range */
3175 buf = end + 1;
3176 end_chunk = simple_strtoul(buf, &end, 0);
3177 if (buf == end) break;
3178 }
3179 if (*end && !isspace(*end)) break;
3180 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3181 buf = end;
3182 while (isspace(*buf)) buf++;
3183 }
3184 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3185out:
3186 return len;
3187}
3188
3189static struct md_sysfs_entry md_bitmap =
3190__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3191
3192static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003193size_show(mddev_t *mddev, char *page)
3194{
Andre Noll58c0fed2009-03-31 14:33:13 +11003195 return sprintf(page, "%llu\n",
3196 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003197}
3198
Andre Nolld71f9f82008-07-11 22:02:22 +10003199static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003200
3201static ssize_t
3202size_store(mddev_t *mddev, const char *buf, size_t len)
3203{
3204 /* If array is inactive, we can reduce the component size, but
3205 * not increase it (except from 0).
3206 * If array is active, we can try an on-line resize
3207 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003208 sector_t sectors;
3209 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003210
Andre Noll58c0fed2009-03-31 14:33:13 +11003211 if (err < 0)
3212 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003213 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003214 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003215 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003216 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003217 if (mddev->dev_sectors == 0 ||
3218 mddev->dev_sectors > sectors)
3219 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003220 else
3221 err = -ENOSPC;
3222 }
3223 return err ? err : len;
3224}
3225
3226static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003227__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003228
NeilBrown8bb93aa2006-01-06 00:20:50 -08003229
3230/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003231 * This is one of
3232 * 'none' for arrays with no metadata (good luck...)
3233 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003234 * or N.M for internally known formats
3235 */
3236static ssize_t
3237metadata_show(mddev_t *mddev, char *page)
3238{
3239 if (mddev->persistent)
3240 return sprintf(page, "%d.%d\n",
3241 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003242 else if (mddev->external)
3243 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003244 else
3245 return sprintf(page, "none\n");
3246}
3247
3248static ssize_t
3249metadata_store(mddev_t *mddev, const char *buf, size_t len)
3250{
3251 int major, minor;
3252 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003253 /* Changing the details of 'external' metadata is
3254 * always permitted. Otherwise there must be
3255 * no devices attached to the array.
3256 */
3257 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3258 ;
3259 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003260 return -EBUSY;
3261
3262 if (cmd_match(buf, "none")) {
3263 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003264 mddev->external = 0;
3265 mddev->major_version = 0;
3266 mddev->minor_version = 90;
3267 return len;
3268 }
3269 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003270 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003271 if (namelen >= sizeof(mddev->metadata_type))
3272 namelen = sizeof(mddev->metadata_type)-1;
3273 strncpy(mddev->metadata_type, buf+9, namelen);
3274 mddev->metadata_type[namelen] = 0;
3275 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3276 mddev->metadata_type[--namelen] = 0;
3277 mddev->persistent = 0;
3278 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003279 mddev->major_version = 0;
3280 mddev->minor_version = 90;
3281 return len;
3282 }
3283 major = simple_strtoul(buf, &e, 10);
3284 if (e==buf || *e != '.')
3285 return -EINVAL;
3286 buf = e+1;
3287 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003288 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003289 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003290 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003291 return -ENOENT;
3292 mddev->major_version = major;
3293 mddev->minor_version = minor;
3294 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003295 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003296 return len;
3297}
3298
3299static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003300__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003301
NeilBrowna35b0d62006-01-06 00:20:49 -08003302static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003303action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003304{
NeilBrown7eec3142005-11-08 21:39:44 -08003305 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08003306 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003307 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003308 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3309 type = "reshape";
3310 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003311 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3312 type = "resync";
3313 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3314 type = "check";
3315 else
3316 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003317 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003318 type = "recover";
3319 }
3320 return sprintf(page, "%s\n", type);
3321}
3322
3323static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003324action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003325{
NeilBrown7eec3142005-11-08 21:39:44 -08003326 if (!mddev->pers || !mddev->pers->sync_request)
3327 return -EINVAL;
3328
NeilBrownbce74da2006-01-06 00:20:41 -08003329 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003330 if (mddev->sync_thread) {
3331 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3332 md_unregister_thread(mddev->sync_thread);
3333 mddev->sync_thread = NULL;
3334 mddev->recovery = 0;
3335 }
NeilBrown03c902e2006-01-06 00:20:46 -08003336 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3337 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003338 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003339 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003340 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003341 else if (cmd_match(page, "recover")) {
3342 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3344 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003345 int err;
3346 if (mddev->pers->start_reshape == NULL)
3347 return -EINVAL;
3348 err = mddev->pers->start_reshape(mddev);
3349 if (err)
3350 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003351 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003352 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003353 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003354 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003355 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003356 return -EINVAL;
3357 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3358 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003359 }
NeilBrown03c902e2006-01-06 00:20:46 -08003360 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003361 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003362 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003363 return len;
3364}
3365
NeilBrown9d888832005-11-08 21:39:26 -08003366static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003367mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003368{
3369 return sprintf(page, "%llu\n",
3370 (unsigned long long) mddev->resync_mismatches);
3371}
3372
NeilBrown80ca3a42006-07-10 04:44:18 -07003373static struct md_sysfs_entry md_scan_mode =
3374__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003375
NeilBrown96de1e62005-11-08 21:39:39 -08003376
NeilBrown80ca3a42006-07-10 04:44:18 -07003377static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003378
NeilBrown88202a02006-01-06 00:21:36 -08003379static ssize_t
3380sync_min_show(mddev_t *mddev, char *page)
3381{
3382 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3383 mddev->sync_speed_min ? "local": "system");
3384}
3385
3386static ssize_t
3387sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3388{
3389 int min;
3390 char *e;
3391 if (strncmp(buf, "system", 6)==0) {
3392 mddev->sync_speed_min = 0;
3393 return len;
3394 }
3395 min = simple_strtoul(buf, &e, 10);
3396 if (buf == e || (*e && *e != '\n') || min <= 0)
3397 return -EINVAL;
3398 mddev->sync_speed_min = min;
3399 return len;
3400}
3401
3402static struct md_sysfs_entry md_sync_min =
3403__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3404
3405static ssize_t
3406sync_max_show(mddev_t *mddev, char *page)
3407{
3408 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3409 mddev->sync_speed_max ? "local": "system");
3410}
3411
3412static ssize_t
3413sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3414{
3415 int max;
3416 char *e;
3417 if (strncmp(buf, "system", 6)==0) {
3418 mddev->sync_speed_max = 0;
3419 return len;
3420 }
3421 max = simple_strtoul(buf, &e, 10);
3422 if (buf == e || (*e && *e != '\n') || max <= 0)
3423 return -EINVAL;
3424 mddev->sync_speed_max = max;
3425 return len;
3426}
3427
3428static struct md_sysfs_entry md_sync_max =
3429__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3430
Iustin Popd7f3d292007-10-16 23:30:54 -07003431static ssize_t
3432degraded_show(mddev_t *mddev, char *page)
3433{
3434 return sprintf(page, "%d\n", mddev->degraded);
3435}
3436static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003437
3438static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003439sync_force_parallel_show(mddev_t *mddev, char *page)
3440{
3441 return sprintf(page, "%d\n", mddev->parallel_resync);
3442}
3443
3444static ssize_t
3445sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3446{
3447 long n;
3448
3449 if (strict_strtol(buf, 10, &n))
3450 return -EINVAL;
3451
3452 if (n != 0 && n != 1)
3453 return -EINVAL;
3454
3455 mddev->parallel_resync = n;
3456
3457 if (mddev->sync_thread)
3458 wake_up(&resync_wait);
3459
3460 return len;
3461}
3462
3463/* force parallel resync, even with shared block devices */
3464static struct md_sysfs_entry md_sync_force_parallel =
3465__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3466 sync_force_parallel_show, sync_force_parallel_store);
3467
3468static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003469sync_speed_show(mddev_t *mddev, char *page)
3470{
3471 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003472 if (mddev->curr_resync == 0)
3473 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003474 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3475 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003476 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003477 db = resync - mddev->resync_mark_cnt;
3478 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003479}
3480
NeilBrown80ca3a42006-07-10 04:44:18 -07003481static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003482
3483static ssize_t
3484sync_completed_show(mddev_t *mddev, char *page)
3485{
Andre Noll58c0fed2009-03-31 14:33:13 +11003486 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003487
NeilBrownacb180b2009-04-14 16:28:34 +10003488 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3489 return sprintf(page, "none\n");
3490
NeilBrown88202a02006-01-06 00:21:36 -08003491 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003492 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003493 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003494 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003495
NeilBrownacb180b2009-04-14 16:28:34 +10003496 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003497 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003498}
3499
NeilBrown80ca3a42006-07-10 04:44:18 -07003500static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003501
NeilBrowne464eaf2006-03-27 01:18:14 -08003502static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003503min_sync_show(mddev_t *mddev, char *page)
3504{
3505 return sprintf(page, "%llu\n",
3506 (unsigned long long)mddev->resync_min);
3507}
3508static ssize_t
3509min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3510{
3511 unsigned long long min;
3512 if (strict_strtoull(buf, 10, &min))
3513 return -EINVAL;
3514 if (min > mddev->resync_max)
3515 return -EINVAL;
3516 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3517 return -EBUSY;
3518
3519 /* Must be a multiple of chunk_size */
3520 if (mddev->chunk_size) {
3521 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3522 return -EINVAL;
3523 }
3524 mddev->resync_min = min;
3525
3526 return len;
3527}
3528
3529static struct md_sysfs_entry md_min_sync =
3530__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3531
3532static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003533max_sync_show(mddev_t *mddev, char *page)
3534{
3535 if (mddev->resync_max == MaxSector)
3536 return sprintf(page, "max\n");
3537 else
3538 return sprintf(page, "%llu\n",
3539 (unsigned long long)mddev->resync_max);
3540}
3541static ssize_t
3542max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3543{
3544 if (strncmp(buf, "max", 3) == 0)
3545 mddev->resync_max = MaxSector;
3546 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003547 unsigned long long max;
3548 if (strict_strtoull(buf, 10, &max))
3549 return -EINVAL;
3550 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003551 return -EINVAL;
3552 if (max < mddev->resync_max &&
3553 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3554 return -EBUSY;
3555
3556 /* Must be a multiple of chunk_size */
3557 if (mddev->chunk_size) {
3558 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3559 return -EINVAL;
3560 }
3561 mddev->resync_max = max;
3562 }
3563 wake_up(&mddev->recovery_wait);
3564 return len;
3565}
3566
3567static struct md_sysfs_entry md_max_sync =
3568__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3569
3570static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003571suspend_lo_show(mddev_t *mddev, char *page)
3572{
3573 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3574}
3575
3576static ssize_t
3577suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3578{
3579 char *e;
3580 unsigned long long new = simple_strtoull(buf, &e, 10);
3581
3582 if (mddev->pers->quiesce == NULL)
3583 return -EINVAL;
3584 if (buf == e || (*e && *e != '\n'))
3585 return -EINVAL;
3586 if (new >= mddev->suspend_hi ||
3587 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3588 mddev->suspend_lo = new;
3589 mddev->pers->quiesce(mddev, 2);
3590 return len;
3591 } else
3592 return -EINVAL;
3593}
3594static struct md_sysfs_entry md_suspend_lo =
3595__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3596
3597
3598static ssize_t
3599suspend_hi_show(mddev_t *mddev, char *page)
3600{
3601 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3602}
3603
3604static ssize_t
3605suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3606{
3607 char *e;
3608 unsigned long long new = simple_strtoull(buf, &e, 10);
3609
3610 if (mddev->pers->quiesce == NULL)
3611 return -EINVAL;
3612 if (buf == e || (*e && *e != '\n'))
3613 return -EINVAL;
3614 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3615 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3616 mddev->suspend_hi = new;
3617 mddev->pers->quiesce(mddev, 1);
3618 mddev->pers->quiesce(mddev, 0);
3619 return len;
3620 } else
3621 return -EINVAL;
3622}
3623static struct md_sysfs_entry md_suspend_hi =
3624__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3625
NeilBrown08a02ec2007-05-09 02:35:38 -07003626static ssize_t
3627reshape_position_show(mddev_t *mddev, char *page)
3628{
3629 if (mddev->reshape_position != MaxSector)
3630 return sprintf(page, "%llu\n",
3631 (unsigned long long)mddev->reshape_position);
3632 strcpy(page, "none\n");
3633 return 5;
3634}
3635
3636static ssize_t
3637reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3638{
3639 char *e;
3640 unsigned long long new = simple_strtoull(buf, &e, 10);
3641 if (mddev->pers)
3642 return -EBUSY;
3643 if (buf == e || (*e && *e != '\n'))
3644 return -EINVAL;
3645 mddev->reshape_position = new;
3646 mddev->delta_disks = 0;
3647 mddev->new_level = mddev->level;
3648 mddev->new_layout = mddev->layout;
3649 mddev->new_chunk = mddev->chunk_size;
3650 return len;
3651}
3652
3653static struct md_sysfs_entry md_reshape_position =
3654__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3655 reshape_position_store);
3656
Dan Williamsb522adc2009-03-31 15:00:31 +11003657static ssize_t
3658array_size_show(mddev_t *mddev, char *page)
3659{
3660 if (mddev->external_size)
3661 return sprintf(page, "%llu\n",
3662 (unsigned long long)mddev->array_sectors/2);
3663 else
3664 return sprintf(page, "default\n");
3665}
3666
3667static ssize_t
3668array_size_store(mddev_t *mddev, const char *buf, size_t len)
3669{
3670 sector_t sectors;
3671
3672 if (strncmp(buf, "default", 7) == 0) {
3673 if (mddev->pers)
3674 sectors = mddev->pers->size(mddev, 0, 0);
3675 else
3676 sectors = mddev->array_sectors;
3677
3678 mddev->external_size = 0;
3679 } else {
3680 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3681 return -EINVAL;
3682 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3683 return -EINVAL;
3684
3685 mddev->external_size = 1;
3686 }
3687
3688 mddev->array_sectors = sectors;
3689 set_capacity(mddev->gendisk, mddev->array_sectors);
3690 if (mddev->pers) {
3691 struct block_device *bdev = bdget_disk(mddev->gendisk, 0);
3692
3693 if (bdev) {
3694 mutex_lock(&bdev->bd_inode->i_mutex);
3695 i_size_write(bdev->bd_inode,
3696 (loff_t)mddev->array_sectors << 9);
3697 mutex_unlock(&bdev->bd_inode->i_mutex);
3698 bdput(bdev);
3699 }
3700 }
3701
3702 return len;
3703}
3704
3705static struct md_sysfs_entry md_array_size =
3706__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3707 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003708
NeilBrowneae17012005-11-08 21:39:23 -08003709static struct attribute *md_default_attrs[] = {
3710 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003711 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003712 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003713 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003714 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003715 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003716 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003717 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003718 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003719 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003720 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003721 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003722 NULL,
3723};
3724
3725static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003726 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003727 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003728 &md_sync_min.attr,
3729 &md_sync_max.attr,
3730 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003731 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003732 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003733 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003734 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003735 &md_suspend_lo.attr,
3736 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003737 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003738 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003739 NULL,
3740};
NeilBrown411036f2005-11-08 21:39:40 -08003741static struct attribute_group md_redundancy_group = {
3742 .name = NULL,
3743 .attrs = md_redundancy_attrs,
3744};
3745
NeilBrowneae17012005-11-08 21:39:23 -08003746
3747static ssize_t
3748md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3749{
3750 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3751 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003752 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003753
3754 if (!entry->show)
3755 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003756 rv = mddev_lock(mddev);
3757 if (!rv) {
3758 rv = entry->show(mddev, page);
3759 mddev_unlock(mddev);
3760 }
NeilBrown96de1e62005-11-08 21:39:39 -08003761 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003762}
3763
3764static ssize_t
3765md_attr_store(struct kobject *kobj, struct attribute *attr,
3766 const char *page, size_t length)
3767{
3768 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3769 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003770 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003771
3772 if (!entry->store)
3773 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003774 if (!capable(CAP_SYS_ADMIN))
3775 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003776 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003777 if (mddev->hold_active == UNTIL_IOCTL)
3778 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003779 if (!rv) {
3780 rv = entry->store(mddev, page, length);
3781 mddev_unlock(mddev);
3782 }
NeilBrown96de1e62005-11-08 21:39:39 -08003783 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003784}
3785
3786static void md_free(struct kobject *ko)
3787{
3788 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003789
3790 if (mddev->sysfs_state)
3791 sysfs_put(mddev->sysfs_state);
3792
3793 if (mddev->gendisk) {
3794 del_gendisk(mddev->gendisk);
3795 put_disk(mddev->gendisk);
3796 }
3797 if (mddev->queue)
3798 blk_cleanup_queue(mddev->queue);
3799
NeilBrowneae17012005-11-08 21:39:23 -08003800 kfree(mddev);
3801}
3802
3803static struct sysfs_ops md_sysfs_ops = {
3804 .show = md_attr_show,
3805 .store = md_attr_store,
3806};
3807static struct kobj_type md_ktype = {
3808 .release = md_free,
3809 .sysfs_ops = &md_sysfs_ops,
3810 .default_attrs = md_default_attrs,
3811};
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813int mdp_major = 0;
3814
Dan Williams5fd3a172009-03-04 00:57:25 -07003815static void mddev_delayed_delete(struct work_struct *ws)
3816{
3817 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3818
3819 if (mddev->private == &md_redundancy_group) {
3820 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3821 if (mddev->sysfs_action)
3822 sysfs_put(mddev->sysfs_action);
3823 mddev->sysfs_action = NULL;
3824 mddev->private = NULL;
3825 }
3826 kobject_del(&mddev->kobj);
3827 kobject_put(&mddev->kobj);
3828}
3829
NeilBrownefeb53c2009-01-09 08:31:10 +11003830static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003832 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 mddev_t *mddev = mddev_find(dev);
3834 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003835 int partitioned;
3836 int shift;
3837 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003838 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003841 return -ENODEV;
3842
3843 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3844 shift = partitioned ? MdpMinorShift : 0;
3845 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
NeilBrownd3374822009-01-09 08:31:10 +11003847 /* wait for any previous instance if this device
3848 * to be completed removed (mddev_delayed_delete).
3849 */
3850 flush_scheduled_work();
3851
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003852 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003854 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003856 return -EEXIST;
3857 }
3858
3859 if (name) {
3860 /* Need to ensure that 'name' is not a duplicate.
3861 */
3862 mddev_t *mddev2;
3863 spin_lock(&all_mddevs_lock);
3864
3865 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3866 if (mddev2->gendisk &&
3867 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3868 spin_unlock(&all_mddevs_lock);
3869 return -EEXIST;
3870 }
3871 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 }
NeilBrown8b765392009-01-09 08:31:08 +11003873
3874 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3875 if (!mddev->queue) {
3876 mutex_unlock(&disks_mutex);
3877 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003878 return -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003879 }
NeilBrown409c57f2009-03-31 14:39:39 +11003880 mddev->queue->queuedata = mddev;
3881
NeilBrown8b765392009-01-09 08:31:08 +11003882 /* Can be unlocked because the queue is new: no concurrency */
3883 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3884
NeilBrown409c57f2009-03-31 14:39:39 +11003885 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 disk = alloc_disk(1 << shift);
3888 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003889 mutex_unlock(&disks_mutex);
NeilBrown8b765392009-01-09 08:31:08 +11003890 blk_cleanup_queue(mddev->queue);
3891 mddev->queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003893 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003895 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003897 if (name)
3898 strcpy(disk->disk_name, name);
3899 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003901 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 disk->fops = &md_fops;
3904 disk->private_data = mddev;
3905 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003906 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003907 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003908 * remove it now.
3909 */
3910 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 add_disk(disk);
3912 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003913 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3914 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003915 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003916 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003917 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3918 disk->disk_name);
NeilBrownb62b7592008-10-21 13:25:21 +11003919 else {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003920 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003921 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3922 }
NeilBrownd3374822009-01-09 08:31:10 +11003923 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003924 return 0;
3925}
3926
3927static struct kobject *md_probe(dev_t dev, int *part, void *data)
3928{
3929 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return NULL;
3931}
3932
NeilBrownefeb53c2009-01-09 08:31:10 +11003933static int add_named_array(const char *val, struct kernel_param *kp)
3934{
3935 /* val must be "md_*" where * is not all digits.
3936 * We allocate an array with a large free minor number, and
3937 * set the name to val. val must not already be an active name.
3938 */
3939 int len = strlen(val);
3940 char buf[DISK_NAME_LEN];
3941
3942 while (len && val[len-1] == '\n')
3943 len--;
3944 if (len >= DISK_NAME_LEN)
3945 return -E2BIG;
3946 strlcpy(buf, val, len+1);
3947 if (strncmp(buf, "md_", 3) != 0)
3948 return -EINVAL;
3949 return md_alloc(0, buf);
3950}
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952static void md_safemode_timeout(unsigned long data)
3953{
3954 mddev_t *mddev = (mddev_t *) data;
3955
Neil Brown0fd62b82008-06-28 08:31:36 +10003956 if (!atomic_read(&mddev->writes_pending)) {
3957 mddev->safemode = 1;
3958 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11003959 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 md_wakeup_thread(mddev->thread);
3962}
3963
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003964static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966static int do_md_run(mddev_t * mddev)
3967{
NeilBrown2604b702006-01-06 00:20:36 -08003968 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 int chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 mdk_rdev_t *rdev;
3971 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003972 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 char b[BDEVNAME_SIZE];
3974
NeilBrowna757e642005-04-16 15:26:42 -07003975 if (list_empty(&mddev->disks))
3976 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979 if (mddev->pers)
3980 return -EBUSY;
3981
3982 /*
3983 * Analyze all RAID superblock(s)
3984 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003985 if (!mddev->raid_disks) {
3986 if (!mddev->persistent)
3987 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003988 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
3991 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
NeilBrown2604b702006-01-06 00:20:36 -08003993 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 if (chunk_size > MAX_CHUNK_SIZE) {
3995 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3996 chunk_size, MAX_CHUNK_SIZE);
3997 return -EINVAL;
3998 }
3999 /*
NeilBrown4bbf3772008-10-13 11:55:12 +11004000 * chunk-size has to be a power of 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 */
4002 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07004003 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 return -EINVAL;
4005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
4007 /* devices must have minimum size of one chunk */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004008 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004009 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 continue;
Andre Nolldd8ac332009-03-31 14:33:13 +11004011 if (rdev->sectors < chunk_size / 512) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 printk(KERN_WARNING
4013 "md: Dev %s smaller than chunk_size:"
Andre Nolldd8ac332009-03-31 14:33:13 +11004014 " %llu < %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 bdevname(rdev->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +11004016 (unsigned long long)rdev->sectors,
4017 chunk_size / 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return -EINVAL;
4019 }
4020 }
4021 }
4022
NeilBrownd9d166c2006-01-06 00:20:51 -08004023 if (mddev->level != LEVEL_NONE)
4024 request_module("md-level-%d", mddev->level);
4025 else if (mddev->clevel[0])
4026 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 /*
4029 * Drop all container device buffers, from now on
4030 * the only valid external interface is through the md
4031 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004033 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004034 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 continue;
4036 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004037 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004038
4039 /* perform some consistency tests on the device.
4040 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004041 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004042 */
Andre Noll0f420352008-07-11 22:02:23 +10004043 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004044 if (mddev->dev_sectors &&
4045 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004046 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004047 printk("md: %s: data overlaps metadata\n",
4048 mdname(mddev));
4049 return -EINVAL;
4050 }
4051 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004052 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004053 > rdev->data_offset) {
4054 printk("md: %s: metadata overlaps data\n",
4055 mdname(mddev));
4056 return -EINVAL;
4057 }
4058 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004059 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
4061
4062 md_probe(mddev->unit, NULL, NULL);
4063 disk = mddev->gendisk;
4064 if (!disk)
4065 return -ENOMEM;
4066
4067 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004068 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004069 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004071 if (mddev->level != LEVEL_NONE)
4072 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4073 mddev->level);
4074 else
4075 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4076 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return -EINVAL;
4078 }
NeilBrown2604b702006-01-06 00:20:36 -08004079 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004081 if (mddev->level != pers->level) {
4082 mddev->level = pers->level;
4083 mddev->new_level = pers->level;
4084 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004085 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004087 if (pers->level >= 4 && pers->level <= 6)
4088 /* Cannot support integrity (yet) */
4089 blk_integrity_unregister(mddev->gendisk);
4090
NeilBrownf6705572006-03-27 01:18:11 -08004091 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004092 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004093 /* This personality cannot handle reshaping... */
4094 mddev->pers = NULL;
4095 module_put(pers->owner);
4096 return -EINVAL;
4097 }
4098
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004099 if (pers->sync_request) {
4100 /* Warn if this is a potentially silly
4101 * configuration.
4102 */
4103 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4104 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004105 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004106
4107 list_for_each_entry(rdev, &mddev->disks, same_set)
4108 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004109 if (rdev < rdev2 &&
4110 rdev->bdev->bd_contains ==
4111 rdev2->bdev->bd_contains) {
4112 printk(KERN_WARNING
4113 "%s: WARNING: %s appears to be"
4114 " on the same physical disk as"
4115 " %s.\n",
4116 mdname(mddev),
4117 bdevname(rdev->bdev,b),
4118 bdevname(rdev2->bdev,b2));
4119 warned = 1;
4120 }
4121 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004122
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004123 if (warned)
4124 printk(KERN_WARNING
4125 "True protection against single-disk"
4126 " failure might be compromised.\n");
4127 }
4128
NeilBrown657390d2005-08-26 18:34:16 -07004129 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004130 /* may be over-ridden by personality */
4131 mddev->resync_max_sectors = mddev->dev_sectors;
4132
NeilBrowna9701a32005-11-08 21:39:34 -08004133 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004134 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
NeilBrownf91de922005-11-08 21:39:36 -08004136 if (start_readonly)
4137 mddev->ro = 2; /* read-only, but switch on first write */
4138
NeilBrownb15c2e52006-01-06 00:20:16 -08004139 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004140 if (err)
4141 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004142 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4143 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4144 " but 'external_size' not in effect?\n", __func__);
4145 printk(KERN_ERR
4146 "md: invalid array_size %llu > default size %llu\n",
4147 (unsigned long long)mddev->array_sectors / 2,
4148 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4149 err = -EINVAL;
4150 mddev->pers->stop(mddev);
4151 }
4152 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004153 err = bitmap_create(mddev);
4154 if (err) {
4155 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4156 mdname(mddev), err);
4157 mddev->pers->stop(mddev);
4158 }
4159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 module_put(mddev->pers->owner);
4162 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004163 bitmap_destroy(mddev);
4164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004166 if (mddev->pers->sync_request) {
4167 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4168 printk(KERN_WARNING
4169 "md: cannot register extra attributes for %s\n",
4170 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004171 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004172 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004173 mddev->ro = 0;
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 atomic_set(&mddev->writes_pending,0);
4176 mddev->safemode = 0;
4177 mddev->safemode_timer.function = md_safemode_timeout;
4178 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004179 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004181
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004182 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004183 if (rdev->raid_disk >= 0) {
4184 char nm[20];
4185 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004186 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4187 printk("md: cannot register %s for %s\n",
4188 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4192
NeilBrown850b2b42006-10-03 01:15:46 -07004193 if (mddev->flags)
4194 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Andre Nollf233ea52008-07-21 17:05:22 +10004196 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004198 /* If there is a partially-recovered drive we need to
4199 * start recovery here. If we leave it to md_check_recovery,
4200 * it will remove the drives and not do the right thing
4201 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004202 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004203 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004204 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004205 if (rdev->raid_disk >= 0 &&
4206 !test_bit(In_sync, &rdev->flags) &&
4207 !test_bit(Faulty, &rdev->flags))
4208 /* complete an interrupted recovery */
4209 spares++;
4210 if (spares && mddev->pers->sync_request) {
4211 mddev->recovery = 0;
4212 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4213 mddev->sync_thread = md_register_thread(md_do_sync,
4214 mddev,
4215 "%s_resync");
4216 if (!mddev->sync_thread) {
4217 printk(KERN_ERR "%s: could not start resync"
4218 " thread...\n",
4219 mdname(mddev));
4220 /* leave the spares where they are, it shouldn't hurt */
4221 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004222 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004223 }
4224 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004225 md_wakeup_thread(mddev->thread);
4226 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004227
Linus Torvalds44ce6292007-05-09 18:51:36 -07004228 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004229 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004230 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004231 if (mddev->sysfs_action)
4232 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004233 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004234 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return 0;
4236}
4237
4238static int restart_array(mddev_t *mddev)
4239{
4240 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Andre Noll80fab1d2008-07-11 22:02:21 +10004242 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004244 return -ENXIO;
4245 if (!mddev->pers)
4246 return -EINVAL;
4247 if (!mddev->ro)
4248 return -EBUSY;
4249 mddev->safemode = 0;
4250 mddev->ro = 0;
4251 set_disk_ro(disk, 0);
4252 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4253 mdname(mddev));
4254 /* Kick recovery or resync if necessary */
4255 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4256 md_wakeup_thread(mddev->thread);
4257 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004258 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
NeilBrownacc55e22006-06-26 00:27:47 -07004262/* similar to deny_write_access, but accounts for our holding a reference
4263 * to the file ourselves */
4264static int deny_bitmap_write_access(struct file * file)
4265{
4266 struct inode *inode = file->f_mapping->host;
4267
4268 spin_lock(&inode->i_lock);
4269 if (atomic_read(&inode->i_writecount) > 1) {
4270 spin_unlock(&inode->i_lock);
4271 return -ETXTBSY;
4272 }
4273 atomic_set(&inode->i_writecount, -1);
4274 spin_unlock(&inode->i_lock);
4275
4276 return 0;
4277}
4278
4279static void restore_bitmap_write_access(struct file *file)
4280{
4281 struct inode *inode = file->f_mapping->host;
4282
4283 spin_lock(&inode->i_lock);
4284 atomic_set(&inode->i_writecount, 1);
4285 spin_unlock(&inode->i_lock);
4286}
4287
NeilBrown9e653b62006-06-26 00:27:58 -07004288/* mode:
4289 * 0 - completely stop and dis-assemble array
4290 * 1 - switch to readonly
4291 * 2 - stop but do not disassemble array
4292 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004293static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
4295 int err = 0;
4296 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004297 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
NeilBrownf2ea68c2008-07-21 17:05:25 +10004299 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004300 printk("md: %s still in use.\n",mdname(mddev));
4301 return -EBUSY;
4302 }
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004307 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4309 md_unregister_thread(mddev->sync_thread);
4310 mddev->sync_thread = NULL;
4311 }
4312
4313 del_timer_sync(&mddev->safemode_timer);
4314
NeilBrown9e653b62006-06-26 00:27:58 -07004315 switch(mode) {
4316 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004318 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 goto out;
4320 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004321 break;
4322 case 0: /* disassemble */
4323 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004324 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004325 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 if (mddev->ro)
4327 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004330 mddev->queue->merge_bvec_fn = NULL;
4331 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004332 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004334 if (mddev->pers->sync_request)
4335 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004337 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004338 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004339
NeilBrownc4647292009-05-07 12:51:06 +10004340 list_for_each_entry(rdev, &mddev->disks, same_set)
4341 if (rdev->raid_disk >= 0) {
4342 char nm[20];
4343 sprintf(nm, "rd%d", rdev->raid_disk);
4344 sysfs_remove_link(&mddev->kobj, nm);
4345 }
4346
NeilBrown0d4ca602006-12-10 02:20:44 -08004347 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004348 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (mddev->ro)
4351 mddev->ro = 0;
4352 }
NeilBrown850b2b42006-10-03 01:15:46 -07004353 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 /* mark array as shutdown cleanly */
4355 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004356 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
NeilBrown9e653b62006-06-26 00:27:58 -07004358 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004360 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
NeilBrown32a76272005-06-21 17:17:14 -07004362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 /*
4364 * Free resources if final stop
4365 */
NeilBrown9e653b62006-06-26 00:27:58 -07004366 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4369
NeilBrown978f9462006-02-02 14:28:05 -08004370 bitmap_destroy(mddev);
4371 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004372 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004373 fput(mddev->bitmap_file);
4374 mddev->bitmap_file = NULL;
4375 }
4376 mddev->bitmap_offset = 0;
4377
NeilBrown177a99b2008-02-06 01:39:56 -08004378 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004379 flush_scheduled_work();
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 export_array(mddev);
4382
Andre Nollf233ea52008-07-21 17:05:22 +10004383 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004384 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004385 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004386 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004387 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004388 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004389 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004390 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004391 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004392 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004393 mddev->level = LEVEL_NONE;
4394 mddev->clevel[0] = 0;
4395 mddev->flags = 0;
4396 mddev->ro = 0;
4397 mddev->metadata_type[0] = 0;
4398 mddev->chunk_size = 0;
4399 mddev->ctime = mddev->utime = 0;
4400 mddev->layout = 0;
4401 mddev->max_disks = 0;
4402 mddev->events = 0;
4403 mddev->delta_disks = 0;
4404 mddev->new_level = LEVEL_NONE;
4405 mddev->new_layout = 0;
4406 mddev->new_chunk = 0;
4407 mddev->curr_resync = 0;
4408 mddev->resync_mismatches = 0;
4409 mddev->suspend_lo = mddev->suspend_hi = 0;
4410 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4411 mddev->recovery = 0;
4412 mddev->in_sync = 0;
4413 mddev->changed = 0;
4414 mddev->degraded = 0;
4415 mddev->barriers_work = 0;
4416 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004417 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004418 if (mddev->hold_active == UNTIL_STOP)
4419 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004420
NeilBrowna8a55c32006-06-26 00:27:39 -07004421 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4423 mdname(mddev));
4424 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004425 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004426 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004427 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428out:
4429 return err;
4430}
4431
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004432#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433static void autorun_array(mddev_t *mddev)
4434{
4435 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 int err;
4437
NeilBrowna757e642005-04-16 15:26:42 -07004438 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441 printk(KERN_INFO "md: running: ");
4442
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004443 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 char b[BDEVNAME_SIZE];
4445 printk("<%s>", bdevname(rdev->bdev,b));
4446 }
4447 printk("\n");
4448
NeilBrownd710e132008-10-13 11:55:12 +11004449 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (err) {
4451 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004452 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 }
4454}
4455
4456/*
4457 * lets try to run arrays based on all disks that have arrived
4458 * until now. (those are in pending_raid_disks)
4459 *
4460 * the method: pick the first pending disk, collect all disks with
4461 * the same UUID, remove all from the pending list and put them into
4462 * the 'same_array' list. Then order this list based on superblock
4463 * update time (freshest comes first), kick out 'old' disks and
4464 * compare superblocks. If everything's fine then run it.
4465 *
4466 * If "unit" is allocated, then bump its reference count
4467 */
4468static void autorun_devices(int part)
4469{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004470 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 mddev_t *mddev;
4472 char b[BDEVNAME_SIZE];
4473
4474 printk(KERN_INFO "md: autorun ...\n");
4475 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004476 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004478 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 rdev0 = list_entry(pending_raid_disks.next,
4480 mdk_rdev_t, same_set);
4481
4482 printk(KERN_INFO "md: considering %s ...\n",
4483 bdevname(rdev0->bdev,b));
4484 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004485 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (super_90_load(rdev, rdev0, 0) >= 0) {
4487 printk(KERN_INFO "md: adding %s ...\n",
4488 bdevname(rdev->bdev,b));
4489 list_move(&rdev->same_set, &candidates);
4490 }
4491 /*
4492 * now we have a set of devices, with all of them having
4493 * mostly sane superblocks. It's time to allocate the
4494 * mddev.
4495 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004496 if (part) {
4497 dev = MKDEV(mdp_major,
4498 rdev0->preferred_minor << MdpMinorShift);
4499 unit = MINOR(dev) >> MdpMinorShift;
4500 } else {
4501 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4502 unit = MINOR(dev);
4503 }
4504 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4506 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4507 break;
4508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510 md_probe(dev, NULL, NULL);
4511 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004512 if (!mddev || !mddev->gendisk) {
4513 if (mddev)
4514 mddev_put(mddev);
4515 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 "md: cannot allocate memory for md drive.\n");
4517 break;
4518 }
4519 if (mddev_lock(mddev))
4520 printk(KERN_WARNING "md: %s locked, cannot run\n",
4521 mdname(mddev));
4522 else if (mddev->raid_disks || mddev->major_version
4523 || !list_empty(&mddev->disks)) {
4524 printk(KERN_WARNING
4525 "md: %s already running, cannot run %s\n",
4526 mdname(mddev), bdevname(rdev0->bdev,b));
4527 mddev_unlock(mddev);
4528 } else {
4529 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004530 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004531 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 list_del_init(&rdev->same_set);
4533 if (bind_rdev_to_array(rdev, mddev))
4534 export_rdev(rdev);
4535 }
4536 autorun_array(mddev);
4537 mddev_unlock(mddev);
4538 }
4539 /* on success, candidates will be empty, on error
4540 * it won't...
4541 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004542 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004543 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 mddev_put(mddev);
4547 }
4548 printk(KERN_INFO "md: ... autorun DONE.\n");
4549}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004550#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552static int get_version(void __user * arg)
4553{
4554 mdu_version_t ver;
4555
4556 ver.major = MD_MAJOR_VERSION;
4557 ver.minor = MD_MINOR_VERSION;
4558 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4559
4560 if (copy_to_user(arg, &ver, sizeof(ver)))
4561 return -EFAULT;
4562
4563 return 0;
4564}
4565
4566static int get_array_info(mddev_t * mddev, void __user * arg)
4567{
4568 mdu_array_info_t info;
4569 int nr,working,active,failed,spare;
4570 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
4572 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004573 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004575 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 failed++;
4577 else {
4578 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004579 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 active++;
4581 else
4582 spare++;
4583 }
4584 }
4585
4586 info.major_version = mddev->major_version;
4587 info.minor_version = mddev->minor_version;
4588 info.patch_version = MD_PATCHLEVEL_VERSION;
4589 info.ctime = mddev->ctime;
4590 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004591 info.size = mddev->dev_sectors / 2;
4592 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004593 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 info.nr_disks = nr;
4595 info.raid_disks = mddev->raid_disks;
4596 info.md_minor = mddev->md_minor;
4597 info.not_persistent= !mddev->persistent;
4598
4599 info.utime = mddev->utime;
4600 info.state = 0;
4601 if (mddev->in_sync)
4602 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004603 if (mddev->bitmap && mddev->bitmap_offset)
4604 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 info.active_disks = active;
4606 info.working_disks = working;
4607 info.failed_disks = failed;
4608 info.spare_disks = spare;
4609
4610 info.layout = mddev->layout;
4611 info.chunk_size = mddev->chunk_size;
4612
4613 if (copy_to_user(arg, &info, sizeof(info)))
4614 return -EFAULT;
4615
4616 return 0;
4617}
4618
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004619static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004620{
4621 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4622 char *ptr, *buf = NULL;
4623 int err = -ENOMEM;
4624
Dan Williamsb5470dc2008-06-27 21:44:04 -07004625 if (md_allow_write(mddev))
4626 file = kmalloc(sizeof(*file), GFP_NOIO);
4627 else
4628 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004629
NeilBrown32a76272005-06-21 17:17:14 -07004630 if (!file)
4631 goto out;
4632
4633 /* bitmap disabled, zero the first byte and copy out */
4634 if (!mddev->bitmap || !mddev->bitmap->file) {
4635 file->pathname[0] = '\0';
4636 goto copy_out;
4637 }
4638
4639 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4640 if (!buf)
4641 goto out;
4642
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004643 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4644 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004645 goto out;
4646
4647 strcpy(file->pathname, ptr);
4648
4649copy_out:
4650 err = 0;
4651 if (copy_to_user(arg, file, sizeof(*file)))
4652 err = -EFAULT;
4653out:
4654 kfree(buf);
4655 kfree(file);
4656 return err;
4657}
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659static int get_disk_info(mddev_t * mddev, void __user * arg)
4660{
4661 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 mdk_rdev_t *rdev;
4663
4664 if (copy_from_user(&info, arg, sizeof(info)))
4665 return -EFAULT;
4666
Andre Noll26ef3792008-07-11 22:02:21 +10004667 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 if (rdev) {
4669 info.major = MAJOR(rdev->bdev->bd_dev);
4670 info.minor = MINOR(rdev->bdev->bd_dev);
4671 info.raid_disk = rdev->raid_disk;
4672 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004673 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004675 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 info.state |= (1<<MD_DISK_ACTIVE);
4677 info.state |= (1<<MD_DISK_SYNC);
4678 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004679 if (test_bit(WriteMostly, &rdev->flags))
4680 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 } else {
4682 info.major = info.minor = 0;
4683 info.raid_disk = -1;
4684 info.state = (1<<MD_DISK_REMOVED);
4685 }
4686
4687 if (copy_to_user(arg, &info, sizeof(info)))
4688 return -EFAULT;
4689
4690 return 0;
4691}
4692
4693static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4694{
4695 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4696 mdk_rdev_t *rdev;
4697 dev_t dev = MKDEV(info->major,info->minor);
4698
4699 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4700 return -EOVERFLOW;
4701
4702 if (!mddev->raid_disks) {
4703 int err;
4704 /* expecting a device which has a superblock */
4705 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4706 if (IS_ERR(rdev)) {
4707 printk(KERN_WARNING
4708 "md: md_import_device returned %ld\n",
4709 PTR_ERR(rdev));
4710 return PTR_ERR(rdev);
4711 }
4712 if (!list_empty(&mddev->disks)) {
4713 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4714 mdk_rdev_t, same_set);
4715 int err = super_types[mddev->major_version]
4716 .load_super(rdev, rdev0, mddev->minor_version);
4717 if (err < 0) {
4718 printk(KERN_WARNING
4719 "md: %s has different UUID to %s\n",
4720 bdevname(rdev->bdev,b),
4721 bdevname(rdev0->bdev,b2));
4722 export_rdev(rdev);
4723 return -EINVAL;
4724 }
4725 }
4726 err = bind_rdev_to_array(rdev, mddev);
4727 if (err)
4728 export_rdev(rdev);
4729 return err;
4730 }
4731
4732 /*
4733 * add_new_disk can be used once the array is assembled
4734 * to add "hot spares". They must already have a superblock
4735 * written
4736 */
4737 if (mddev->pers) {
4738 int err;
4739 if (!mddev->pers->hot_add_disk) {
4740 printk(KERN_WARNING
4741 "%s: personality does not support diskops!\n",
4742 mdname(mddev));
4743 return -EINVAL;
4744 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004745 if (mddev->persistent)
4746 rdev = md_import_device(dev, mddev->major_version,
4747 mddev->minor_version);
4748 else
4749 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 if (IS_ERR(rdev)) {
4751 printk(KERN_WARNING
4752 "md: md_import_device returned %ld\n",
4753 PTR_ERR(rdev));
4754 return PTR_ERR(rdev);
4755 }
NeilBrown41158c72005-06-21 17:17:25 -07004756 /* set save_raid_disk if appropriate */
4757 if (!mddev->persistent) {
4758 if (info->state & (1<<MD_DISK_SYNC) &&
4759 info->raid_disk < mddev->raid_disks)
4760 rdev->raid_disk = info->raid_disk;
4761 else
4762 rdev->raid_disk = -1;
4763 } else
4764 super_types[mddev->major_version].
4765 validate_super(mddev, rdev);
4766 rdev->saved_raid_disk = rdev->raid_disk;
4767
NeilBrownb2d444d2005-11-08 21:39:31 -08004768 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004769 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4770 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004771 else
4772 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 rdev->raid_disk = -1;
4775 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004776 if (!err && !mddev->pers->hot_remove_disk) {
4777 /* If there is hot_add_disk but no hot_remove_disk
4778 * then added disks for geometry changes,
4779 * and should be added immediately.
4780 */
4781 super_types[mddev->major_version].
4782 validate_super(mddev, rdev);
4783 err = mddev->pers->hot_add_disk(mddev, rdev);
4784 if (err)
4785 unbind_rdev_from_array(rdev);
4786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 if (err)
4788 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004789 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004790 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004791
NeilBrown17571282006-12-10 02:20:52 -08004792 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004793 if (mddev->degraded)
4794 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004795 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004796 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 return err;
4798 }
4799
4800 /* otherwise, add_new_disk is only allowed
4801 * for major_version==0 superblocks
4802 */
4803 if (mddev->major_version != 0) {
4804 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4805 mdname(mddev));
4806 return -EINVAL;
4807 }
4808
4809 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4810 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004811 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 if (IS_ERR(rdev)) {
4813 printk(KERN_WARNING
4814 "md: error, md_import_device() returned %ld\n",
4815 PTR_ERR(rdev));
4816 return PTR_ERR(rdev);
4817 }
4818 rdev->desc_nr = info->number;
4819 if (info->raid_disk < mddev->raid_disks)
4820 rdev->raid_disk = info->raid_disk;
4821 else
4822 rdev->raid_disk = -1;
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004825 if (info->state & (1<<MD_DISK_SYNC))
4826 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004828 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4829 set_bit(WriteMostly, &rdev->flags);
4830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (!mddev->persistent) {
4832 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004833 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 } else
Andre Noll0f420352008-07-11 22:02:23 +10004835 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolldd8ac332009-03-31 14:33:13 +11004836 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
NeilBrown2bf071b2006-01-06 00:20:55 -08004838 err = bind_rdev_to_array(rdev, mddev);
4839 if (err) {
4840 export_rdev(rdev);
4841 return err;
4842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 }
4844
4845 return 0;
4846}
4847
4848static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4849{
4850 char b[BDEVNAME_SIZE];
4851 mdk_rdev_t *rdev;
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 rdev = find_rdev(mddev, dev);
4854 if (!rdev)
4855 return -ENXIO;
4856
4857 if (rdev->raid_disk >= 0)
4858 goto busy;
4859
4860 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004861 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004862 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864 return 0;
4865busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004866 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 bdevname(rdev->bdev,b), mdname(mddev));
4868 return -EBUSY;
4869}
4870
4871static int hot_add_disk(mddev_t * mddev, dev_t dev)
4872{
4873 char b[BDEVNAME_SIZE];
4874 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 mdk_rdev_t *rdev;
4876
4877 if (!mddev->pers)
4878 return -ENODEV;
4879
4880 if (mddev->major_version != 0) {
4881 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4882 " version-0 superblocks.\n",
4883 mdname(mddev));
4884 return -EINVAL;
4885 }
4886 if (!mddev->pers->hot_add_disk) {
4887 printk(KERN_WARNING
4888 "%s: personality does not support diskops!\n",
4889 mdname(mddev));
4890 return -EINVAL;
4891 }
4892
NeilBrownd710e132008-10-13 11:55:12 +11004893 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 if (IS_ERR(rdev)) {
4895 printk(KERN_WARNING
4896 "md: error, md_import_device() returned %ld\n",
4897 PTR_ERR(rdev));
4898 return -EINVAL;
4899 }
4900
4901 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004902 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 else
Andre Noll0f420352008-07-11 22:02:23 +10004904 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
Andre Nolldd8ac332009-03-31 14:33:13 +11004906 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
NeilBrownb2d444d2005-11-08 21:39:31 -08004908 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 printk(KERN_WARNING
4910 "md: can not hot-add faulty %s disk to %s!\n",
4911 bdevname(rdev->bdev,b), mdname(mddev));
4912 err = -EINVAL;
4913 goto abort_export;
4914 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004915 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004917 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004918 err = bind_rdev_to_array(rdev, mddev);
4919 if (err)
4920 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
4922 /*
4923 * The rest should better be atomic, we can have disk failures
4924 * noticed in interrupt contexts ...
4925 */
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 rdev->raid_disk = -1;
4928
NeilBrown850b2b42006-10-03 01:15:46 -07004929 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
4931 /*
4932 * Kick recovery, maybe this spare has to be added to the
4933 * array immediately.
4934 */
4935 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4936 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004937 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 return 0;
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940abort_export:
4941 export_rdev(rdev);
4942 return err;
4943}
4944
NeilBrown32a76272005-06-21 17:17:14 -07004945static int set_bitmap_file(mddev_t *mddev, int fd)
4946{
4947 int err;
4948
NeilBrown36fa3062005-09-09 16:23:45 -07004949 if (mddev->pers) {
4950 if (!mddev->pers->quiesce)
4951 return -EBUSY;
4952 if (mddev->recovery || mddev->sync_thread)
4953 return -EBUSY;
4954 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004955 }
4956
NeilBrown36fa3062005-09-09 16:23:45 -07004957
4958 if (fd >= 0) {
4959 if (mddev->bitmap)
4960 return -EEXIST; /* cannot add when bitmap is present */
4961 mddev->bitmap_file = fget(fd);
4962
4963 if (mddev->bitmap_file == NULL) {
4964 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4965 mdname(mddev));
4966 return -EBADF;
4967 }
4968
4969 err = deny_bitmap_write_access(mddev->bitmap_file);
4970 if (err) {
4971 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4972 mdname(mddev));
4973 fput(mddev->bitmap_file);
4974 mddev->bitmap_file = NULL;
4975 return err;
4976 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004977 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004978 } else if (mddev->bitmap == NULL)
4979 return -ENOENT; /* cannot remove what isn't there */
4980 err = 0;
4981 if (mddev->pers) {
4982 mddev->pers->quiesce(mddev, 1);
4983 if (fd >= 0)
4984 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004985 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004986 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004987 fd = -1; /* make sure to put the file */
4988 }
NeilBrown36fa3062005-09-09 16:23:45 -07004989 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004990 }
4991 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004992 if (mddev->bitmap_file) {
4993 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004994 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004995 }
NeilBrown36fa3062005-09-09 16:23:45 -07004996 mddev->bitmap_file = NULL;
4997 }
4998
NeilBrown32a76272005-06-21 17:17:14 -07004999 return err;
5000}
5001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002/*
5003 * set_array_info is used two different ways
5004 * The original usage is when creating a new array.
5005 * In this usage, raid_disks is > 0 and it together with
5006 * level, size, not_persistent,layout,chunksize determine the
5007 * shape of the array.
5008 * This will always create an array with a type-0.90.0 superblock.
5009 * The newer usage is when assembling an array.
5010 * In this case raid_disks will be 0, and the major_version field is
5011 * use to determine which style super-blocks are to be found on the devices.
5012 * The minor and patch _version numbers are also kept incase the
5013 * super_block handler wishes to interpret them.
5014 */
5015static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5016{
5017
5018 if (info->raid_disks == 0) {
5019 /* just setting version number for superblock loading */
5020 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005021 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 super_types[info->major_version].name == NULL) {
5023 /* maybe try to auto-load a module? */
5024 printk(KERN_INFO
5025 "md: superblock version %d not known\n",
5026 info->major_version);
5027 return -EINVAL;
5028 }
5029 mddev->major_version = info->major_version;
5030 mddev->minor_version = info->minor_version;
5031 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005032 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 return 0;
5034 }
5035 mddev->major_version = MD_MAJOR_VERSION;
5036 mddev->minor_version = MD_MINOR_VERSION;
5037 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5038 mddev->ctime = get_seconds();
5039
5040 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005041 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005042 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 mddev->raid_disks = info->raid_disks;
5044 /* don't set md_minor, it is determined by which /dev/md* was
5045 * openned
5046 */
5047 if (info->state & (1<<MD_SB_CLEAN))
5048 mddev->recovery_cp = MaxSector;
5049 else
5050 mddev->recovery_cp = 0;
5051 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005052 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
5054 mddev->layout = info->layout;
5055 mddev->chunk_size = info->chunk_size;
5056
5057 mddev->max_disks = MD_SB_DISKS;
5058
NeilBrowne6910632008-02-06 01:39:51 -08005059 if (mddev->persistent)
5060 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005061 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
NeilBrownb2a27032005-11-28 13:44:12 -08005063 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5064 mddev->bitmap_offset = 0;
5065
NeilBrownf6705572006-03-27 01:18:11 -08005066 mddev->reshape_position = MaxSector;
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 /*
5069 * Generate a 128 bit UUID
5070 */
5071 get_random_bytes(mddev->uuid, 16);
5072
NeilBrownf6705572006-03-27 01:18:11 -08005073 mddev->new_level = mddev->level;
5074 mddev->new_chunk = mddev->chunk_size;
5075 mddev->new_layout = mddev->layout;
5076 mddev->delta_disks = 0;
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return 0;
5079}
5080
Dan Williams1f403622009-03-31 14:59:03 +11005081void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5082{
Dan Williamsb522adc2009-03-31 15:00:31 +11005083 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5084
5085 if (mddev->external_size)
5086 return;
5087
Dan Williams1f403622009-03-31 14:59:03 +11005088 mddev->array_sectors = array_sectors;
5089}
5090EXPORT_SYMBOL(md_set_array_sectors);
5091
Andre Nolld71f9f82008-07-11 22:02:22 +10005092static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005093{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005094 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005095 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005096 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005097
5098 if (mddev->pers->resize == NULL)
5099 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005100 /* The "num_sectors" is the number of sectors of each device that
5101 * is used. This can only make sense for arrays with redundancy.
5102 * linear and raid0 always use whatever space is available. We can only
5103 * consider changing this number if no resync or reconstruction is
5104 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005105 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005106 * of each device. If num_sectors is zero, we find the largest size
5107 * that fits.
5108
NeilBrowna35b0d62006-01-06 00:20:49 -08005109 */
5110 if (mddev->sync_thread)
5111 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005112 if (mddev->bitmap)
5113 /* Sorry, cannot grow a bitmap yet, just remove it,
5114 * grow, and re-add.
5115 */
5116 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005117 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005118 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005119
Andre Nolld71f9f82008-07-11 22:02:22 +10005120 if (fit && (num_sectors == 0 || num_sectors > avail))
5121 num_sectors = avail;
5122 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005123 return -ENOSPC;
5124 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005125 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08005126 if (!rv) {
5127 struct block_device *bdev;
5128
5129 bdev = bdget_disk(mddev->gendisk, 0);
5130 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005131 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10005132 i_size_write(bdev->bd_inode,
5133 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005134 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08005135 bdput(bdev);
5136 }
5137 }
5138 return rv;
5139}
5140
NeilBrownda943b92006-01-06 00:20:54 -08005141static int update_raid_disks(mddev_t *mddev, int raid_disks)
5142{
5143 int rv;
5144 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005145 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005146 return -EINVAL;
5147 if (raid_disks <= 0 ||
5148 raid_disks >= mddev->max_disks)
5149 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005150 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005151 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005152 mddev->delta_disks = raid_disks - mddev->raid_disks;
5153
5154 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08005155 return rv;
5156}
5157
5158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159/*
5160 * update_array_info is used to change the configuration of an
5161 * on-line array.
5162 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5163 * fields in the info are checked against the array.
5164 * Any differences that cannot be handled will cause an error.
5165 * Normally, only one change can be managed at a time.
5166 */
5167static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5168{
5169 int rv = 0;
5170 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005171 int state = 0;
5172
5173 /* calculate expected state,ignoring low bits */
5174 if (mddev->bitmap && mddev->bitmap_offset)
5175 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
5177 if (mddev->major_version != info->major_version ||
5178 mddev->minor_version != info->minor_version ||
5179/* mddev->patch_version != info->patch_version || */
5180 mddev->ctime != info->ctime ||
5181 mddev->level != info->level ||
5182/* mddev->layout != info->layout || */
5183 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07005184 mddev->chunk_size != info->chunk_size ||
5185 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5186 ((state^info->state) & 0xfffffe00)
5187 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 return -EINVAL;
5189 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005190 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5191 cnt++;
5192 if (mddev->raid_disks != info->raid_disks)
5193 cnt++;
5194 if (mddev->layout != info->layout)
5195 cnt++;
5196 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5197 cnt++;
5198 if (cnt == 0)
5199 return 0;
5200 if (cnt > 1)
5201 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
5203 if (mddev->layout != info->layout) {
5204 /* Change layout
5205 * we don't need to do anything at the md level, the
5206 * personality will take care of it all.
5207 */
5208 if (mddev->pers->reconfig == NULL)
5209 return -EINVAL;
5210 else
5211 return mddev->pers->reconfig(mddev, info->layout, -1);
5212 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005213 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005214 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
NeilBrownda943b92006-01-06 00:20:54 -08005216 if (mddev->raid_disks != info->raid_disks)
5217 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
NeilBrown36fa3062005-09-09 16:23:45 -07005219 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5220 if (mddev->pers->quiesce == NULL)
5221 return -EINVAL;
5222 if (mddev->recovery || mddev->sync_thread)
5223 return -EBUSY;
5224 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5225 /* add the bitmap */
5226 if (mddev->bitmap)
5227 return -EEXIST;
5228 if (mddev->default_bitmap_offset == 0)
5229 return -EINVAL;
5230 mddev->bitmap_offset = mddev->default_bitmap_offset;
5231 mddev->pers->quiesce(mddev, 1);
5232 rv = bitmap_create(mddev);
5233 if (rv)
5234 bitmap_destroy(mddev);
5235 mddev->pers->quiesce(mddev, 0);
5236 } else {
5237 /* remove the bitmap */
5238 if (!mddev->bitmap)
5239 return -ENOENT;
5240 if (mddev->bitmap->file)
5241 return -EINVAL;
5242 mddev->pers->quiesce(mddev, 1);
5243 bitmap_destroy(mddev);
5244 mddev->pers->quiesce(mddev, 0);
5245 mddev->bitmap_offset = 0;
5246 }
5247 }
NeilBrown850b2b42006-10-03 01:15:46 -07005248 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 return rv;
5250}
5251
5252static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5253{
5254 mdk_rdev_t *rdev;
5255
5256 if (mddev->pers == NULL)
5257 return -ENODEV;
5258
5259 rdev = find_rdev(mddev, dev);
5260 if (!rdev)
5261 return -ENODEV;
5262
5263 md_error(mddev, rdev);
5264 return 0;
5265}
5266
Andre Noll2f9618c2008-04-25 18:57:58 +02005267/*
5268 * We have a problem here : there is no easy way to give a CHS
5269 * virtual geometry. We currently pretend that we have a 2 heads
5270 * 4 sectors (with a BIG number of cylinders...). This drives
5271 * dosfs just mad... ;-)
5272 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005273static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5274{
5275 mddev_t *mddev = bdev->bd_disk->private_data;
5276
5277 geo->heads = 2;
5278 geo->sectors = 4;
5279 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5280 return 0;
5281}
5282
Al Viroa39907f2008-03-02 10:31:15 -05005283static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 unsigned int cmd, unsigned long arg)
5285{
5286 int err = 0;
5287 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 mddev_t *mddev = NULL;
5289
5290 if (!capable(CAP_SYS_ADMIN))
5291 return -EACCES;
5292
5293 /*
5294 * Commands dealing with the RAID driver but not any
5295 * particular array:
5296 */
5297 switch (cmd)
5298 {
5299 case RAID_VERSION:
5300 err = get_version(argp);
5301 goto done;
5302
5303 case PRINT_RAID_DEBUG:
5304 err = 0;
5305 md_print_devices();
5306 goto done;
5307
5308#ifndef MODULE
5309 case RAID_AUTORUN:
5310 err = 0;
5311 autostart_arrays(arg);
5312 goto done;
5313#endif
5314 default:;
5315 }
5316
5317 /*
5318 * Commands creating/starting a new array:
5319 */
5320
Al Viroa39907f2008-03-02 10:31:15 -05005321 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
5323 if (!mddev) {
5324 BUG();
5325 goto abort;
5326 }
5327
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 err = mddev_lock(mddev);
5329 if (err) {
5330 printk(KERN_INFO
5331 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5332 err, cmd);
5333 goto abort;
5334 }
5335
5336 switch (cmd)
5337 {
5338 case SET_ARRAY_INFO:
5339 {
5340 mdu_array_info_t info;
5341 if (!arg)
5342 memset(&info, 0, sizeof(info));
5343 else if (copy_from_user(&info, argp, sizeof(info))) {
5344 err = -EFAULT;
5345 goto abort_unlock;
5346 }
5347 if (mddev->pers) {
5348 err = update_array_info(mddev, &info);
5349 if (err) {
5350 printk(KERN_WARNING "md: couldn't update"
5351 " array info. %d\n", err);
5352 goto abort_unlock;
5353 }
5354 goto done_unlock;
5355 }
5356 if (!list_empty(&mddev->disks)) {
5357 printk(KERN_WARNING
5358 "md: array %s already has disks!\n",
5359 mdname(mddev));
5360 err = -EBUSY;
5361 goto abort_unlock;
5362 }
5363 if (mddev->raid_disks) {
5364 printk(KERN_WARNING
5365 "md: array %s already initialised!\n",
5366 mdname(mddev));
5367 err = -EBUSY;
5368 goto abort_unlock;
5369 }
5370 err = set_array_info(mddev, &info);
5371 if (err) {
5372 printk(KERN_WARNING "md: couldn't set"
5373 " array info. %d\n", err);
5374 goto abort_unlock;
5375 }
5376 }
5377 goto done_unlock;
5378
5379 default:;
5380 }
5381
5382 /*
5383 * Commands querying/configuring an existing array:
5384 */
NeilBrown32a76272005-06-21 17:17:14 -07005385 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005386 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005387 if ((!mddev->raid_disks && !mddev->external)
5388 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5389 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5390 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 err = -ENODEV;
5392 goto abort_unlock;
5393 }
5394
5395 /*
5396 * Commands even a read-only array can execute:
5397 */
5398 switch (cmd)
5399 {
5400 case GET_ARRAY_INFO:
5401 err = get_array_info(mddev, argp);
5402 goto done_unlock;
5403
NeilBrown32a76272005-06-21 17:17:14 -07005404 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005405 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005406 goto done_unlock;
5407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 case GET_DISK_INFO:
5409 err = get_disk_info(mddev, argp);
5410 goto done_unlock;
5411
5412 case RESTART_ARRAY_RW:
5413 err = restart_array(mddev);
5414 goto done_unlock;
5415
5416 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005417 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 goto done_unlock;
5419
5420 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005421 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 goto done_unlock;
5423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 }
5425
5426 /*
5427 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005428 * superblock, so we do not allow them on read-only arrays.
5429 * However non-MD ioctls (e.g. get-size) will still come through
5430 * here and hit the 'default' below, so only disallow
5431 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005433 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005434 if (mddev->ro == 2) {
5435 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005436 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005437 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5438 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005439 } else {
5440 err = -EROFS;
5441 goto abort_unlock;
5442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 }
5444
5445 switch (cmd)
5446 {
5447 case ADD_NEW_DISK:
5448 {
5449 mdu_disk_info_t info;
5450 if (copy_from_user(&info, argp, sizeof(info)))
5451 err = -EFAULT;
5452 else
5453 err = add_new_disk(mddev, &info);
5454 goto done_unlock;
5455 }
5456
5457 case HOT_REMOVE_DISK:
5458 err = hot_remove_disk(mddev, new_decode_dev(arg));
5459 goto done_unlock;
5460
5461 case HOT_ADD_DISK:
5462 err = hot_add_disk(mddev, new_decode_dev(arg));
5463 goto done_unlock;
5464
5465 case SET_DISK_FAULTY:
5466 err = set_disk_faulty(mddev, new_decode_dev(arg));
5467 goto done_unlock;
5468
5469 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005470 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 goto done_unlock;
5472
NeilBrown32a76272005-06-21 17:17:14 -07005473 case SET_BITMAP_FILE:
5474 err = set_bitmap_file(mddev, (int)arg);
5475 goto done_unlock;
5476
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 err = -EINVAL;
5479 goto abort_unlock;
5480 }
5481
5482done_unlock:
5483abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005484 if (mddev->hold_active == UNTIL_IOCTL &&
5485 err != -EINVAL)
5486 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 mddev_unlock(mddev);
5488
5489 return err;
5490done:
5491 if (err)
5492 MD_BUG();
5493abort:
5494 return err;
5495}
5496
Al Viroa39907f2008-03-02 10:31:15 -05005497static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498{
5499 /*
5500 * Succeed if we can lock the mddev, which confirms that
5501 * it isn't being stopped right now.
5502 */
NeilBrownd3374822009-01-09 08:31:10 +11005503 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 int err;
5505
NeilBrownd3374822009-01-09 08:31:10 +11005506 if (mddev->gendisk != bdev->bd_disk) {
5507 /* we are racing with mddev_put which is discarding this
5508 * bd_disk.
5509 */
5510 mddev_put(mddev);
5511 /* Wait until bdev->bd_disk is definitely gone */
5512 flush_scheduled_work();
5513 /* Then retry the open from the top */
5514 return -ERESTARTSYS;
5515 }
5516 BUG_ON(mddev != bdev->bd_disk->private_data);
5517
NeilBrownd63a5a72006-12-08 02:36:17 -08005518 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 goto out;
5520
5521 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005522 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 mddev_unlock(mddev);
5524
Al Viroa39907f2008-03-02 10:31:15 -05005525 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 out:
5527 return err;
5528}
5529
Al Viroa39907f2008-03-02 10:31:15 -05005530static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
Al Viroa39907f2008-03-02 10:31:15 -05005532 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005534 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005535 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 mddev_put(mddev);
5537
5538 return 0;
5539}
5540
Linus Torvalds44ce6292007-05-09 18:51:36 -07005541static int md_media_changed(struct gendisk *disk)
5542{
5543 mddev_t *mddev = disk->private_data;
5544
5545 return mddev->changed;
5546}
5547
5548static int md_revalidate(struct gendisk *disk)
5549{
5550 mddev_t *mddev = disk->private_data;
5551
5552 mddev->changed = 0;
5553 return 0;
5554}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555static struct block_device_operations md_fops =
5556{
5557 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005558 .open = md_open,
5559 .release = md_release,
5560 .locked_ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005561 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005562 .media_changed = md_media_changed,
5563 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564};
5565
Adrian Bunk75c96f82005-05-05 16:16:09 -07005566static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567{
5568 mdk_thread_t *thread = arg;
5569
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 /*
5571 * md_thread is a 'system-thread', it's priority should be very
5572 * high. We avoid resource deadlocks individually in each
5573 * raid personality. (RAID5 does preallocation) We also use RR and
5574 * the very same RT priority as kswapd, thus we will never get
5575 * into a priority inversion deadlock.
5576 *
5577 * we definitely have to have equal or higher priority than
5578 * bdflush, otherwise bdflush will deadlock if there are too
5579 * many dirty RAID5 blocks.
5580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
NeilBrown6985c432005-10-19 21:23:47 -07005582 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005583 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
NeilBrown93588e22005-11-15 00:09:12 -08005585 /* We need to wait INTERRUPTIBLE so that
5586 * we don't add to the load-average.
5587 * That means we need to be sure no signals are
5588 * pending
5589 */
5590 if (signal_pending(current))
5591 flush_signals(current);
5592
5593 wait_event_interruptible_timeout
5594 (thread->wqueue,
5595 test_bit(THREAD_WAKEUP, &thread->flags)
5596 || kthread_should_stop(),
5597 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
5599 clear_bit(THREAD_WAKEUP, &thread->flags);
5600
NeilBrown787453c2005-11-08 21:39:43 -08005601 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return 0;
5605}
5606
5607void md_wakeup_thread(mdk_thread_t *thread)
5608{
5609 if (thread) {
5610 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5611 set_bit(THREAD_WAKEUP, &thread->flags);
5612 wake_up(&thread->wqueue);
5613 }
5614}
5615
5616mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5617 const char *name)
5618{
5619 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
NeilBrown9ffae0c2006-01-06 00:20:32 -08005621 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 if (!thread)
5623 return NULL;
5624
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 init_waitqueue_head(&thread->wqueue);
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 thread->run = run;
5628 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005629 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005630 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005631 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 kfree(thread);
5633 return NULL;
5634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 return thread;
5636}
5637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638void md_unregister_thread(mdk_thread_t *thread)
5639{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005640 if (!thread)
5641 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005642 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005643
5644 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 kfree(thread);
5646}
5647
5648void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5649{
5650 if (!mddev) {
5651 MD_BUG();
5652 return;
5653 }
5654
NeilBrownb2d444d2005-11-08 21:39:31 -08005655 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005657
5658 if (mddev->external)
5659 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005660/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5662 mdname(mddev),
5663 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5664 __builtin_return_address(0),__builtin_return_address(1),
5665 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005666*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005667 if (!mddev->pers)
5668 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 if (!mddev->pers->error_handler)
5670 return;
5671 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005672 if (mddev->degraded)
5673 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005674 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5676 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5677 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005678 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679}
5680
5681/* seq_file implementation /proc/mdstat */
5682
5683static void status_unused(struct seq_file *seq)
5684{
5685 int i = 0;
5686 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
5688 seq_printf(seq, "unused devices: ");
5689
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005690 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 char b[BDEVNAME_SIZE];
5692 i++;
5693 seq_printf(seq, "%s ",
5694 bdevname(rdev->bdev,b));
5695 }
5696 if (!i)
5697 seq_printf(seq, "<none>");
5698
5699 seq_printf(seq, "\n");
5700}
5701
5702
5703static void status_resync(struct seq_file *seq, mddev_t * mddev)
5704{
NeilBrowndd71cf62009-05-07 12:49:35 +10005705 sector_t max_sectors, resync, res;
5706 unsigned long dt, db;
5707 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005708 int scale;
5709 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
NeilBrowndd71cf62009-05-07 12:49:35 +10005711 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
5713 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005714 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005716 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
5718 /*
5719 * Should not happen.
5720 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005721 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 MD_BUG();
5723 return;
5724 }
NeilBrown4588b422006-03-27 01:18:04 -08005725 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005726 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005727 * u32, as those are the requirements for sector_div.
5728 * Thus 'scale' must be at least 10
5729 */
5730 scale = 10;
5731 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005732 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005733 scale++;
5734 }
5735 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005736 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005737
5738 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 {
NeilBrown4588b422006-03-27 01:18:04 -08005740 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 seq_printf(seq, "[");
5742 for (i = 0; i < x; i++)
5743 seq_printf(seq, "=");
5744 seq_printf(seq, ">");
5745 for (i = 0; i < y; i++)
5746 seq_printf(seq, ".");
5747 seq_printf(seq, "] ");
5748 }
NeilBrown4588b422006-03-27 01:18:04 -08005749 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005750 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5751 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005752 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5753 "check" :
5754 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5755 "resync" : "recovery"))),
5756 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005757 (unsigned long long) resync/2,
5758 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
5760 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 * dt: time from mark until now
5762 * db: blocks written from mark until now
5763 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005764 *
5765 * rt is a sector_t, so could be 32bit or 64bit.
5766 * So we divide before multiply in case it is 32bit and close
5767 * to the limit.
5768 * We scale the divisor (db) by 32 to avoid loosing precision
5769 * near the end of resync when the number of remaining sectors
5770 * is close to 'db'.
5771 * We then divide rt by 32 after multiplying by db to compensate.
5772 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 */
5774 dt = ((jiffies - mddev->resync_mark) / HZ);
5775 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005776 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5777 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
NeilBrowndd71cf62009-05-07 12:49:35 +10005779 rt = max_sectors - resync; /* number of remaining sectors */
5780 sector_div(rt, db/32+1);
5781 rt *= dt;
5782 rt >>= 5;
5783
5784 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5785 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
NeilBrownff4e8d92006-07-10 04:44:16 -07005787 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788}
5789
5790static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5791{
5792 struct list_head *tmp;
5793 loff_t l = *pos;
5794 mddev_t *mddev;
5795
5796 if (l >= 0x10000)
5797 return NULL;
5798 if (!l--)
5799 /* header */
5800 return (void*)1;
5801
5802 spin_lock(&all_mddevs_lock);
5803 list_for_each(tmp,&all_mddevs)
5804 if (!l--) {
5805 mddev = list_entry(tmp, mddev_t, all_mddevs);
5806 mddev_get(mddev);
5807 spin_unlock(&all_mddevs_lock);
5808 return mddev;
5809 }
5810 spin_unlock(&all_mddevs_lock);
5811 if (!l--)
5812 return (void*)2;/* tail */
5813 return NULL;
5814}
5815
5816static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5817{
5818 struct list_head *tmp;
5819 mddev_t *next_mddev, *mddev = v;
5820
5821 ++*pos;
5822 if (v == (void*)2)
5823 return NULL;
5824
5825 spin_lock(&all_mddevs_lock);
5826 if (v == (void*)1)
5827 tmp = all_mddevs.next;
5828 else
5829 tmp = mddev->all_mddevs.next;
5830 if (tmp != &all_mddevs)
5831 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5832 else {
5833 next_mddev = (void*)2;
5834 *pos = 0x10000;
5835 }
5836 spin_unlock(&all_mddevs_lock);
5837
5838 if (v != (void*)1)
5839 mddev_put(mddev);
5840 return next_mddev;
5841
5842}
5843
5844static void md_seq_stop(struct seq_file *seq, void *v)
5845{
5846 mddev_t *mddev = v;
5847
5848 if (mddev && v != (void*)1 && v != (void*)2)
5849 mddev_put(mddev);
5850}
5851
NeilBrownd7603b72006-01-06 00:20:30 -08005852struct mdstat_info {
5853 int event;
5854};
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856static int md_seq_show(struct seq_file *seq, void *v)
5857{
5858 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005859 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005861 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005862 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
5864 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005865 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 seq_printf(seq, "Personalities : ");
5867 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005868 list_for_each_entry(pers, &pers_list, list)
5869 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 spin_unlock(&pers_lock);
5872 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005873 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 return 0;
5875 }
5876 if (v == (void*)2) {
5877 status_unused(seq);
5878 return 0;
5879 }
5880
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005881 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005883
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5885 seq_printf(seq, "%s : %sactive", mdname(mddev),
5886 mddev->pers ? "" : "in");
5887 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005888 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005890 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005891 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 seq_printf(seq, " %s", mddev->pers->name);
5893 }
5894
Andre Nolldd8ac332009-03-31 14:33:13 +11005895 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005896 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 char b[BDEVNAME_SIZE];
5898 seq_printf(seq, " %s[%d]",
5899 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005900 if (test_bit(WriteMostly, &rdev->flags))
5901 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005902 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 seq_printf(seq, "(F)");
5904 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005905 } else if (rdev->raid_disk < 0)
5906 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005907 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 }
5909
5910 if (!list_empty(&mddev->disks)) {
5911 if (mddev->pers)
5912 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005913 (unsigned long long)
5914 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 else
5916 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005917 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005919 if (mddev->persistent) {
5920 if (mddev->major_version != 0 ||
5921 mddev->minor_version != 90) {
5922 seq_printf(seq," super %d.%d",
5923 mddev->major_version,
5924 mddev->minor_version);
5925 }
NeilBrowne6910632008-02-06 01:39:51 -08005926 } else if (mddev->external)
5927 seq_printf(seq, " super external:%s",
5928 mddev->metadata_type);
5929 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005930 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005933 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005935 if (mddev->pers->sync_request) {
5936 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005937 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005938 seq_printf(seq, "\n ");
5939 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5940 seq_printf(seq, "\tresync=DELAYED\n ");
5941 else if (mddev->recovery_cp < MaxSector)
5942 seq_printf(seq, "\tresync=PENDING\n ");
5943 }
NeilBrown32a76272005-06-21 17:17:14 -07005944 } else
5945 seq_printf(seq, "\n ");
5946
5947 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005948 unsigned long chunk_kb;
5949 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005950 spin_lock_irqsave(&bitmap->lock, flags);
5951 chunk_kb = bitmap->chunksize >> 10;
5952 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5953 "%lu%s chunk",
5954 bitmap->pages - bitmap->missing_pages,
5955 bitmap->pages,
5956 (bitmap->pages - bitmap->missing_pages)
5957 << (PAGE_SHIFT - 10),
5958 chunk_kb ? chunk_kb : bitmap->chunksize,
5959 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005960 if (bitmap->file) {
5961 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005962 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005963 }
NeilBrown78d742d2005-06-21 17:17:15 -07005964
NeilBrown32a76272005-06-21 17:17:14 -07005965 seq_printf(seq, "\n");
5966 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 }
5968
5969 seq_printf(seq, "\n");
5970 }
5971 mddev_unlock(mddev);
5972
5973 return 0;
5974}
5975
Jan Engelhardt110518b2009-05-07 12:49:37 +10005976static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 .start = md_seq_start,
5978 .next = md_seq_next,
5979 .stop = md_seq_stop,
5980 .show = md_seq_show,
5981};
5982
5983static int md_seq_open(struct inode *inode, struct file *file)
5984{
5985 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005986 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5987 if (mi == NULL)
5988 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
5990 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005991 if (error)
5992 kfree(mi);
5993 else {
5994 struct seq_file *p = file->private_data;
5995 p->private = mi;
5996 mi->event = atomic_read(&md_event_count);
5997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 return error;
5999}
6000
NeilBrownd7603b72006-01-06 00:20:30 -08006001static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6002{
6003 struct seq_file *m = filp->private_data;
6004 struct mdstat_info *mi = m->private;
6005 int mask;
6006
6007 poll_wait(filp, &md_event_waiters, wait);
6008
6009 /* always allow read */
6010 mask = POLLIN | POLLRDNORM;
6011
6012 if (mi->event != atomic_read(&md_event_count))
6013 mask |= POLLERR | POLLPRI;
6014 return mask;
6015}
6016
Arjan van de Venfa027c22007-02-12 00:55:33 -08006017static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006018 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 .open = md_seq_open,
6020 .read = seq_read,
6021 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006022 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006023 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024};
6025
NeilBrown2604b702006-01-06 00:20:36 -08006026int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006029 list_add_tail(&p->list, &pers_list);
6030 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 spin_unlock(&pers_lock);
6032 return 0;
6033}
6034
NeilBrown2604b702006-01-06 00:20:36 -08006035int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036{
NeilBrown2604b702006-01-06 00:20:36 -08006037 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006039 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 spin_unlock(&pers_lock);
6041 return 0;
6042}
6043
NeilBrowneea1bf32009-03-31 14:27:02 +11006044static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045{
6046 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006048 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
6050 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006051 rcu_read_lock();
6052 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006054 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6055 (int)part_stat_read(&disk->part0, sectors[1]) -
6056 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006057 /* sync IO will cause sync_io to increase before the disk_stats
6058 * as sync_io is counted when a request starts, and
6059 * disk_stats is counted when it completes.
6060 * So resync activity will cause curr_events to be smaller than
6061 * when there was no such activity.
6062 * non-sync IO will cause disk_stat to increase without
6063 * increasing sync_io so curr_events will (eventually)
6064 * be larger than it was before. Once it becomes
6065 * substantially larger, the test below will cause
6066 * the array to appear non-idle, and resync will slow
6067 * down.
6068 * If there is a lot of outstanding resync activity when
6069 * we set last_event to curr_events, then all that activity
6070 * completing might cause the array to appear non-idle
6071 * and resync will be slowed down even though there might
6072 * not have been non-resync activity. This will only
6073 * happen once though. 'last_events' will soon reflect
6074 * the state where there is little or no outstanding
6075 * resync requests, and further resync activity will
6076 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006077 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006079 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 rdev->last_events = curr_events;
6081 idle = 0;
6082 }
6083 }
NeilBrown4b809912008-07-21 17:05:25 +10006084 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 return idle;
6086}
6087
6088void md_done_sync(mddev_t *mddev, int blocks, int ok)
6089{
6090 /* another "blocks" (512byte) blocks have been synced */
6091 atomic_sub(blocks, &mddev->recovery_active);
6092 wake_up(&mddev->recovery_wait);
6093 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006094 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 md_wakeup_thread(mddev->thread);
6096 // stop recovery, signal do_sync ....
6097 }
6098}
6099
6100
NeilBrown06d91a52005-06-21 17:17:12 -07006101/* md_write_start(mddev, bi)
6102 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006103 * in superblock) before writing, schedule a superblock update
6104 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006105 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006106void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107{
Neil Brown0fd62b82008-06-28 08:31:36 +10006108 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006109 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006110 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006111
NeilBrownf91de922005-11-08 21:39:36 -08006112 BUG_ON(mddev->ro == 1);
6113 if (mddev->ro == 2) {
6114 /* need to switch to read/write */
6115 mddev->ro = 0;
6116 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6117 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006118 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006119 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006120 }
NeilBrown06d91a52005-06-21 17:17:12 -07006121 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006122 if (mddev->safemode == 1)
6123 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006124 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006125 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006126 if (mddev->in_sync) {
6127 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006128 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006129 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006130 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006131 }
NeilBrowna9701a32005-11-08 21:39:34 -08006132 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006133 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006134 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006135 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006136 wait_event(mddev->sb_wait,
6137 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6138 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139}
6140
6141void md_write_end(mddev_t *mddev)
6142{
6143 if (atomic_dec_and_test(&mddev->writes_pending)) {
6144 if (mddev->safemode == 2)
6145 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006146 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6148 }
6149}
6150
NeilBrown2a2275d2007-01-26 00:57:11 -08006151/* md_allow_write(mddev)
6152 * Calling this ensures that the array is marked 'active' so that writes
6153 * may proceed without blocking. It is important to call this before
6154 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6155 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006156 *
6157 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6158 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006159 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006160int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006161{
6162 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006163 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006164 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006165 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006166 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006167 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006168
6169 spin_lock_irq(&mddev->write_lock);
6170 if (mddev->in_sync) {
6171 mddev->in_sync = 0;
6172 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6173 if (mddev->safemode_delay &&
6174 mddev->safemode == 0)
6175 mddev->safemode = 1;
6176 spin_unlock_irq(&mddev->write_lock);
6177 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006178 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006179 } else
6180 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006181
6182 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6183 return -EAGAIN;
6184 else
6185 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006186}
6187EXPORT_SYMBOL_GPL(md_allow_write);
6188
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189#define SYNC_MARKS 10
6190#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006191void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192{
6193 mddev_t *mddev2;
6194 unsigned int currspeed = 0,
6195 window;
NeilBrown57afd892005-06-21 17:17:13 -07006196 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 unsigned long mark[SYNC_MARKS];
6198 sector_t mark_cnt[SYNC_MARKS];
6199 int last_mark,m;
6200 struct list_head *tmp;
6201 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006202 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006203 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006204 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
6206 /* just incase thread restarts... */
6207 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6208 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006209 if (mddev->ro) /* never try to sync a read-only array */
6210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
NeilBrown61df9d92006-10-03 01:15:57 -07006212 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6213 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6214 desc = "data-check";
6215 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6216 desc = "requested-resync";
6217 else
6218 desc = "resync";
6219 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6220 desc = "reshape";
6221 else
6222 desc = "recovery";
6223
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 /* we overload curr_resync somewhat here.
6225 * 0 == not engaged in resync at all
6226 * 2 == checking that there is no conflict with another sync
6227 * 1 == like 2, but have yielded to allow conflicting resync to
6228 * commense
6229 * other == active in resync - this many blocks
6230 *
6231 * Before starting a resync we must have set curr_resync to
6232 * 2, and then checked that every "conflicting" array has curr_resync
6233 * less than ours. When we find one that is the same or higher
6234 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6235 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6236 * This will mean we have to start checking from the beginning again.
6237 *
6238 */
6239
6240 do {
6241 mddev->curr_resync = 2;
6242
6243 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006244 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006245 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 goto skip;
6247 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006248 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 if (mddev2 == mddev)
6250 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006251 if (!mddev->parallel_resync
6252 && mddev2->curr_resync
6253 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 DEFINE_WAIT(wq);
6255 if (mddev < mddev2 && mddev->curr_resync == 2) {
6256 /* arbitrarily yield */
6257 mddev->curr_resync = 1;
6258 wake_up(&resync_wait);
6259 }
6260 if (mddev > mddev2 && mddev->curr_resync == 1)
6261 /* no need to wait here, we can wait the next
6262 * time 'round when curr_resync == 2
6263 */
6264 continue;
NeilBrown97441972008-09-19 11:49:54 +10006265 /* We need to wait 'interruptible' so as not to
6266 * contribute to the load average, and not to
6267 * be caught by 'softlockup'
6268 */
6269 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006270 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006271 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006272 printk(KERN_INFO "md: delaying %s of %s"
6273 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006275 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006277 if (signal_pending(current))
6278 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 schedule();
6280 finish_wait(&resync_wait, &wq);
6281 goto try_again;
6282 }
6283 finish_wait(&resync_wait, &wq);
6284 }
6285 }
6286 } while (mddev->curr_resync < 2);
6287
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006288 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006289 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006291 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 */
6293 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006294 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006295 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006296 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6297 j = mddev->resync_min;
6298 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006299 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006300
NeilBrownccfcc3c2006-03-27 01:18:09 -08006301 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006302 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006303 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006305 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006306 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006307 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006308 if (rdev->raid_disk >= 0 &&
6309 !test_bit(Faulty, &rdev->flags) &&
6310 !test_bit(In_sync, &rdev->flags) &&
6311 rdev->recovery_offset < j)
6312 j = rdev->recovery_offset;
6313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
NeilBrown61df9d92006-10-03 01:15:57 -07006315 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6316 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6317 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006318 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006319 "(but not more than %d KB/sec) for %s.\n",
6320 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
NeilBrowneea1bf32009-03-31 14:27:02 +11006322 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006323
NeilBrown57afd892005-06-21 17:17:13 -07006324 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 for (m = 0; m < SYNC_MARKS; m++) {
6326 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006327 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 }
6329 last_mark = 0;
6330 mddev->resync_mark = mark[last_mark];
6331 mddev->resync_mark_cnt = mark_cnt[last_mark];
6332
6333 /*
6334 * Tune reconstruction:
6335 */
6336 window = 32*(PAGE_SIZE/512);
6337 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6338 window/2,(unsigned long long) max_sectors/2);
6339
6340 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 last_check = 0;
6342
6343 if (j>2) {
6344 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006345 "md: resuming %s of %s from checkpoint.\n",
6346 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 mddev->curr_resync = j;
6348 }
6349
6350 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006351 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352
NeilBrown57afd892005-06-21 17:17:13 -07006353 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006354
NeilBrownacb180b2009-04-14 16:28:34 +10006355 if ((mddev->curr_resync > mddev->curr_resync_completed &&
6356 (mddev->curr_resync - mddev->curr_resync_completed)
6357 > (max_sectors >> 4)) ||
NeilBrownc03f6a12009-04-17 11:06:30 +10006358 (j - mddev->curr_resync_completed)*2
6359 >= mddev->resync_max - mddev->curr_resync_completed
NeilBrownacb180b2009-04-14 16:28:34 +10006360 ) {
NeilBrown97e4f422009-03-31 14:33:13 +11006361 /* time to update curr_resync_completed */
6362 blk_unplug(mddev->queue);
6363 wait_event(mddev->recovery_wait,
6364 atomic_read(&mddev->recovery_active) == 0);
6365 mddev->curr_resync_completed =
6366 mddev->curr_resync;
6367 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006368 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006369 }
NeilBrownacb180b2009-04-14 16:28:34 +10006370
6371 if (j >= mddev->resync_max)
6372 wait_event(mddev->recovery_wait,
6373 mddev->resync_max > j
6374 || kthread_should_stop());
6375
6376 if (kthread_should_stop())
6377 goto interrupted;
6378
NeilBrown57afd892005-06-21 17:17:13 -07006379 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006380 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006381 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006382 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 goto out;
6384 }
NeilBrown57afd892005-06-21 17:17:13 -07006385
6386 if (!skipped) { /* actual IO requested */
6387 io_sectors += sectors;
6388 atomic_add(sectors, &mddev->recovery_active);
6389 }
6390
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 j += sectors;
6392 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006393 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006394 if (last_check == 0)
6395 /* this is the earliers that rebuilt will be
6396 * visible in /proc/mdstat
6397 */
6398 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006399
6400 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 continue;
6402
NeilBrown57afd892005-06-21 17:17:13 -07006403 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404
NeilBrowndfc70642008-05-23 13:04:39 -07006405 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 break;
6407
6408 repeat:
6409 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6410 /* step marks */
6411 int next = (last_mark+1) % SYNC_MARKS;
6412
6413 mddev->resync_mark = mark[next];
6414 mddev->resync_mark_cnt = mark_cnt[next];
6415 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006416 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 last_mark = next;
6418 }
6419
6420
NeilBrownc6207272008-02-06 01:39:52 -08006421 if (kthread_should_stop())
6422 goto interrupted;
6423
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424
6425 /*
6426 * this loop exits only if either when we are slower than
6427 * the 'hard' speed limit, or the system was IO-idle for
6428 * a jiffy.
6429 * the system might be non-idle CPU-wise, but we only care
6430 * about not overloading the IO subsystem. (things like an
6431 * e2fsck being done on the RAID array should execute fast)
6432 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006433 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 cond_resched();
6435
NeilBrown57afd892005-06-21 17:17:13 -07006436 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6437 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
NeilBrown88202a02006-01-06 00:21:36 -08006439 if (currspeed > speed_min(mddev)) {
6440 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006441 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006442 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 goto repeat;
6444 }
6445 }
6446 }
NeilBrown61df9d92006-10-03 01:15:57 -07006447 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 /*
6449 * this also signals 'finished resyncing' to md_stop
6450 */
6451 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006452 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
6454 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6455
6456 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006457 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458
NeilBrowndfc70642008-05-23 13:04:39 -07006459 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006460 mddev->curr_resync > 2) {
6461 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6462 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6463 if (mddev->curr_resync >= mddev->recovery_cp) {
6464 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006465 "md: checkpointing %s of %s.\n",
6466 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006467 mddev->recovery_cp = mddev->curr_resync;
6468 }
6469 } else
6470 mddev->recovery_cp = MaxSector;
6471 } else {
6472 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6473 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006474 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006475 if (rdev->raid_disk >= 0 &&
6476 !test_bit(Faulty, &rdev->flags) &&
6477 !test_bit(In_sync, &rdev->flags) &&
6478 rdev->recovery_offset < mddev->curr_resync)
6479 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 }
NeilBrown17571282006-12-10 02:20:52 -08006482 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 skip:
6485 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006486 mddev->curr_resync_completed = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006487 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006488 mddev->resync_max = MaxSector;
6489 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 wake_up(&resync_wait);
6491 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6492 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006493 return;
6494
6495 interrupted:
6496 /*
6497 * got a signal, exit.
6498 */
6499 printk(KERN_INFO
6500 "md: md_do_sync() got signal ... exiting\n");
6501 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6502 goto out;
6503
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504}
NeilBrown29269552006-03-27 01:18:10 -08006505EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
6507
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006508static int remove_and_add_spares(mddev_t *mddev)
6509{
6510 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006511 int spares = 0;
6512
NeilBrown97e4f422009-03-31 14:33:13 +11006513 mddev->curr_resync_completed = 0;
6514
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006515 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006516 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006517 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006518 (test_bit(Faulty, &rdev->flags) ||
6519 ! test_bit(In_sync, &rdev->flags)) &&
6520 atomic_read(&rdev->nr_pending)==0) {
6521 if (mddev->pers->hot_remove_disk(
6522 mddev, rdev->raid_disk)==0) {
6523 char nm[20];
6524 sprintf(nm,"rd%d", rdev->raid_disk);
6525 sysfs_remove_link(&mddev->kobj, nm);
6526 rdev->raid_disk = -1;
6527 }
6528 }
6529
NeilBrown4044ba52009-01-09 08:31:11 +11006530 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006531 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006532 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006533 !test_bit(In_sync, &rdev->flags) &&
6534 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006535 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006536 if (rdev->raid_disk < 0
6537 && !test_bit(Faulty, &rdev->flags)) {
6538 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006539 if (mddev->pers->
6540 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006541 char nm[20];
6542 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006543 if (sysfs_create_link(&mddev->kobj,
6544 &rdev->kobj, nm))
6545 printk(KERN_WARNING
6546 "md: cannot register "
6547 "%s for %s\n",
6548 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006549 spares++;
6550 md_new_event(mddev);
6551 } else
6552 break;
6553 }
NeilBrowndfc70642008-05-23 13:04:39 -07006554 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006555 }
6556 return spares;
6557}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558/*
6559 * This routine is regularly called by all per-raid-array threads to
6560 * deal with generic issues like resync and super-block update.
6561 * Raid personalities that don't have a thread (linear/raid0) do not
6562 * need this as they never do any recovery or update the superblock.
6563 *
6564 * It does not do any resync itself, but rather "forks" off other threads
6565 * to do that as needed.
6566 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6567 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006568 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 * and wakeups up this thread which will reap the thread and finish up.
6570 * This thread also removes any faulty devices (with nr_pending == 0).
6571 *
6572 * The overall approach is:
6573 * 1/ if the superblock needs updating, update it.
6574 * 2/ If a recovery thread is running, don't do anything else.
6575 * 3/ If recovery has finished, clean up, possibly marking spares active.
6576 * 4/ If there are any faulty devices, remove them.
6577 * 5/ If array is degraded, try to add spares devices
6578 * 6/ If array has spares or is not in-sync, start a resync thread.
6579 */
6580void md_check_recovery(mddev_t *mddev)
6581{
6582 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
6584
NeilBrown5f404022005-06-21 17:17:16 -07006585 if (mddev->bitmap)
6586 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587
6588 if (mddev->ro)
6589 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006590
6591 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006592 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006593 printk(KERN_INFO "md: %s in immediate safe mode\n",
6594 mdname(mddev));
6595 mddev->safemode = 2;
6596 }
6597 flush_signals(current);
6598 }
6599
NeilBrownc89a8ee2008-08-05 15:54:13 +10006600 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006603 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006605 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006606 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006607 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6608 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 ))
6610 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006611
NeilBrowndf5b89b2006-03-27 01:18:20 -08006612 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006613 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006614
NeilBrownc89a8ee2008-08-05 15:54:13 +10006615 if (mddev->ro) {
6616 /* Only thing we do on a ro array is remove
6617 * failed devices.
6618 */
6619 remove_and_add_spares(mddev);
6620 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6621 goto unlock;
6622 }
6623
NeilBrown31a59e32008-04-30 00:52:30 -07006624 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006625 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006626 spin_lock_irq(&mddev->write_lock);
6627 if (mddev->safemode &&
6628 !atomic_read(&mddev->writes_pending) &&
6629 !mddev->in_sync &&
6630 mddev->recovery_cp == MaxSector) {
6631 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006632 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006633 if (mddev->persistent)
6634 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6635 }
6636 if (mddev->safemode == 1)
6637 mddev->safemode = 0;
6638 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006639 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006640 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006641 }
NeilBrownfca4d842005-06-21 17:17:11 -07006642
NeilBrown850b2b42006-10-03 01:15:46 -07006643 if (mddev->flags)
6644 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006645
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006646 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006647 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006648 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006649
NeilBrown06d91a52005-06-21 17:17:12 -07006650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6652 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6653 /* resync/recovery still happening */
6654 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6655 goto unlock;
6656 }
6657 if (mddev->sync_thread) {
6658 /* resync has finished, collect result */
6659 md_unregister_thread(mddev->sync_thread);
6660 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006661 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6662 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 /* success...*/
6664 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006665 if (mddev->pers->spare_active(mddev))
6666 sysfs_notify(&mddev->kobj, NULL,
6667 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 }
NeilBrowncea9c222009-03-31 15:15:05 +11006669 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6670 mddev->pers->finish_reshape)
6671 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006672 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006673
6674 /* if array is no-longer degraded, then any saved_raid_disk
6675 * information must be scrapped
6676 */
6677 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006678 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006679 rdev->saved_raid_disk = -1;
6680
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 mddev->recovery = 0;
6682 /* flag recovery needed just to double check */
6683 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006684 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006685 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 goto unlock;
6687 }
Neil Brown72a23c22008-06-28 08:31:41 +10006688 /* Set RUNNING before clearing NEEDED to avoid
6689 * any transients in the value of "sync_action".
6690 */
6691 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6692 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006693 /* Clear some bits that don't mean anything, but
6694 * might be left set
6695 */
NeilBrown24dd4692005-11-08 21:39:26 -08006696 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6697 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006699 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6700 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 /* no recovery is running.
6702 * remove any failed drives, then
6703 * add spares if possible.
6704 * Spare are also removed and re-added, to allow
6705 * the personality to fail the re-add.
6706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006708 if (mddev->reshape_position != MaxSector) {
6709 if (mddev->pers->check_reshape(mddev) != 0)
6710 /* Cannot proceed */
6711 goto unlock;
6712 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006713 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006714 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006715 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6716 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006717 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006718 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006719 } else if (mddev->recovery_cp < MaxSector) {
6720 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006721 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006722 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6723 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006725
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006727 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6728 /* We are adding a device or devices to an array
6729 * which has the bitmap stored on all devices.
6730 * So make sure all bitmap pages get written
6731 */
6732 bitmap_write_all(mddev->bitmap);
6733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 mddev->sync_thread = md_register_thread(md_do_sync,
6735 mddev,
6736 "%s_resync");
6737 if (!mddev->sync_thread) {
6738 printk(KERN_ERR "%s: could not start resync"
6739 " thread...\n",
6740 mdname(mddev));
6741 /* leave the spares where they are, it shouldn't hurt */
6742 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006743 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006745 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006746 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 }
6748 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006749 if (!mddev->sync_thread) {
6750 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6751 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6752 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006753 if (mddev->sysfs_action)
6754 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 mddev_unlock(mddev);
6757 }
6758}
6759
Dan Williams6bfe0b42008-04-30 00:52:32 -07006760void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6761{
NeilBrown3c0ee632008-10-21 13:25:28 +11006762 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006763 wait_event_timeout(rdev->blocked_wait,
6764 !test_bit(Blocked, &rdev->flags),
6765 msecs_to_jiffies(5000));
6766 rdev_dec_pending(rdev, mddev);
6767}
6768EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6769
Adrian Bunk75c96f82005-05-05 16:16:09 -07006770static int md_notify_reboot(struct notifier_block *this,
6771 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772{
6773 struct list_head *tmp;
6774 mddev_t *mddev;
6775
6776 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6777
6778 printk(KERN_INFO "md: stopping all md devices.\n");
6779
NeilBrown29ac4aa2008-02-06 01:39:58 -08006780 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006781 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006782 /* Force a switch to readonly even array
6783 * appears to still be in use. Hence
6784 * the '100'.
6785 */
NeilBrownd710e132008-10-13 11:55:12 +11006786 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006787 mddev_unlock(mddev);
6788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 /*
6790 * certain more exotic SCSI devices are known to be
6791 * volatile wrt too early system reboots. While the
6792 * right place to handle this issue is the given
6793 * driver, we do want to have a safe RAID driver ...
6794 */
6795 mdelay(1000*1);
6796 }
6797 return NOTIFY_DONE;
6798}
6799
Adrian Bunk75c96f82005-05-05 16:16:09 -07006800static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 .notifier_call = md_notify_reboot,
6802 .next = NULL,
6803 .priority = INT_MAX, /* before any real devices */
6804};
6805
6806static void md_geninit(void)
6807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6809
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006810 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811}
6812
Adrian Bunk75c96f82005-05-05 16:16:09 -07006813static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006815 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 return -1;
6817 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006818 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 return -1;
6820 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006821 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006822 md_probe, NULL, NULL);
6823 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 md_probe, NULL, NULL);
6825
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006827 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828
6829 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831}
6832
6833
6834#ifndef MODULE
6835
6836/*
6837 * Searches all registered partitions for autorun RAID arrays
6838 * at boot time.
6839 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006840
6841static LIST_HEAD(all_detected_devices);
6842struct detected_devices_node {
6843 struct list_head list;
6844 dev_t dev;
6845};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
6847void md_autodetect_dev(dev_t dev)
6848{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006849 struct detected_devices_node *node_detected_dev;
6850
6851 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6852 if (node_detected_dev) {
6853 node_detected_dev->dev = dev;
6854 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6855 } else {
6856 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6857 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859}
6860
6861
6862static void autostart_arrays(int part)
6863{
6864 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006865 struct detected_devices_node *node_detected_dev;
6866 dev_t dev;
6867 int i_scanned, i_passed;
6868
6869 i_scanned = 0;
6870 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
6872 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6873
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006874 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6875 i_scanned++;
6876 node_detected_dev = list_entry(all_detected_devices.next,
6877 struct detected_devices_node, list);
6878 list_del(&node_detected_dev->list);
6879 dev = node_detected_dev->dev;
6880 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006881 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 if (IS_ERR(rdev))
6883 continue;
6884
NeilBrownb2d444d2005-11-08 21:39:31 -08006885 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 MD_BUG();
6887 continue;
6888 }
NeilBrownd0fae182008-03-04 14:29:31 -08006889 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006891 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006893
6894 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6895 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896
6897 autorun_devices(part);
6898}
6899
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006900#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901
6902static __exit void md_exit(void)
6903{
6904 mddev_t *mddev;
6905 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006906
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006907 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006908 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006910 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 unregister_blkdev(mdp_major, "mdp");
6912 unregister_reboot_notifier(&md_notifier);
6913 unregister_sysctl_table(raid_table_header);
6914 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006915 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006917 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 }
6919}
6920
Dan Williams685784a2007-07-09 11:56:42 -07006921subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922module_exit(md_exit)
6923
NeilBrownf91de922005-11-08 21:39:36 -08006924static int get_ro(char *buffer, struct kernel_param *kp)
6925{
6926 return sprintf(buffer, "%d", start_readonly);
6927}
6928static int set_ro(const char *val, struct kernel_param *kp)
6929{
6930 char *e;
6931 int num = simple_strtoul(val, &e, 10);
6932 if (*val && (*e == '\0' || *e == '\n')) {
6933 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006934 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006935 }
6936 return -EINVAL;
6937}
6938
NeilBrown80ca3a42006-07-10 04:44:18 -07006939module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6940module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08006941
NeilBrownefeb53c2009-01-09 08:31:10 +11006942module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006943
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944EXPORT_SYMBOL(register_md_personality);
6945EXPORT_SYMBOL(unregister_md_personality);
6946EXPORT_SYMBOL(md_error);
6947EXPORT_SYMBOL(md_done_sync);
6948EXPORT_SYMBOL(md_write_start);
6949EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950EXPORT_SYMBOL(md_register_thread);
6951EXPORT_SYMBOL(md_unregister_thread);
6952EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953EXPORT_SYMBOL(md_check_recovery);
6954MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006955MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006956MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);