blob: aa79d55875f051eacbc24a0fec4ae46e9795549b [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;
NeilBrown720a3dc2005-09-09 16:23:59 -07001205 bmask = queue_hardsect_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);
NeilBrown62e1e382009-05-26 09:40:59 +10001378 sb->chunksize = cpu_to_le32(mddev->chunk_size >> 9);
1379 sb->level = cpu_to_le32(mddev->level);
1380 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001381
NeilBrowna654b9d82005-06-21 17:17:27 -07001382 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1383 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001384 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001385 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001386
1387 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001388 !test_bit(In_sync, &rdev->flags)) {
1389 if (mddev->curr_resync_completed > rdev->recovery_offset)
1390 rdev->recovery_offset = mddev->curr_resync_completed;
1391 if (rdev->recovery_offset > 0) {
1392 sb->feature_map |=
1393 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1394 sb->recovery_offset =
1395 cpu_to_le64(rdev->recovery_offset);
1396 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001397 }
1398
NeilBrownf6705572006-03-27 01:18:11 -08001399 if (mddev->reshape_position != MaxSector) {
1400 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1401 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1402 sb->new_layout = cpu_to_le32(mddev->new_layout);
1403 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1404 sb->new_level = cpu_to_le32(mddev->new_level);
1405 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1406 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001409 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (rdev2->desc_nr+1 > max_dev)
1411 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001412
1413 if (max_dev > le32_to_cpu(sb->max_dev))
1414 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 for (i=0; i<max_dev;i++)
1416 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1417
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001418 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001420 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001422 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001424 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1425 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 else
1427 sb->dev_roles[i] = cpu_to_le16(0xffff);
1428 }
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 sb->sb_csum = calc_sb_1_csum(sb);
1431}
1432
Chris Webb0cd17fe2008-06-28 08:31:46 +10001433static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001434super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001435{
1436 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001437 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001438 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001439 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001440 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001441 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001442 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1443 max_sectors -= rdev->data_offset;
1444 if (!num_sectors || num_sectors > max_sectors)
1445 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001446 } else if (rdev->mddev->bitmap_offset) {
1447 /* minor version 0 with bitmap we can't move */
1448 return 0;
1449 } else {
1450 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001451 sector_t sb_start;
1452 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1453 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001454 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001455 if (!num_sectors || num_sectors > max_sectors)
1456 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001457 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001458 }
1459 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001460 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001461 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001462 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001463 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001464 rdev->sb_page);
1465 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001466 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Adrian Bunk75c96f82005-05-05 16:16:09 -07001469static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 [0] = {
1471 .name = "0.90.0",
1472 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001473 .load_super = super_90_load,
1474 .validate_super = super_90_validate,
1475 .sync_super = super_90_sync,
1476 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 },
1478 [1] = {
1479 .name = "md-1",
1480 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001481 .load_super = super_1_load,
1482 .validate_super = super_1_validate,
1483 .sync_super = super_1_sync,
1484 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 },
1486};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1489{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001490 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
NeilBrown4b809912008-07-21 17:05:25 +10001492 rcu_read_lock();
1493 rdev_for_each_rcu(rdev, mddev1)
1494 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001495 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001496 rdev2->bdev->bd_contains) {
1497 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001498 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001499 }
1500 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return 0;
1502}
1503
1504static LIST_HEAD(pending_raid_disks);
1505
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001506static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev)
1507{
1508 struct mdk_personality *pers = mddev->pers;
1509 struct gendisk *disk = mddev->gendisk;
1510 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1511 struct blk_integrity *bi_mddev = blk_get_integrity(disk);
1512
1513 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1514 if (pers && pers->level >= 4 && pers->level <= 6)
1515 return;
1516
1517 /* If rdev is integrity capable, register profile for mddev */
1518 if (!bi_mddev && bi_rdev) {
1519 if (blk_integrity_register(disk, bi_rdev))
1520 printk(KERN_ERR "%s: %s Could not register integrity!\n",
1521 __func__, disk->disk_name);
1522 else
1523 printk(KERN_NOTICE "Enabling data integrity on %s\n",
1524 disk->disk_name);
1525 return;
1526 }
1527
1528 /* Check that mddev and rdev have matching profiles */
1529 if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) {
1530 printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__,
1531 disk->disk_name, rdev->bdev->bd_disk->disk_name);
1532 printk(KERN_NOTICE "Disabling data integrity on %s\n",
1533 disk->disk_name);
1534 blk_integrity_unregister(disk);
1535 }
1536}
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1539{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001540 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001541 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001542 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001543 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 if (rdev->mddev) {
1546 MD_BUG();
1547 return -EINVAL;
1548 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001549
1550 /* prevent duplicates */
1551 if (find_rdev(mddev, rdev->bdev->bd_dev))
1552 return -EEXIST;
1553
Andre Nolldd8ac332009-03-31 14:33:13 +11001554 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1555 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1556 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001557 if (mddev->pers) {
1558 /* Cannot change size, so fail
1559 * If mddev->level <= 0, then we don't care
1560 * about aligning sizes (e.g. linear)
1561 */
1562 if (mddev->level > 0)
1563 return -ENOSPC;
1564 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001565 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 /* Verify rdev->desc_nr is unique.
1569 * If it is -1, assign a free number, else
1570 * check number is not in use
1571 */
1572 if (rdev->desc_nr < 0) {
1573 int choice = 0;
1574 if (mddev->pers) choice = mddev->raid_disks;
1575 while (find_rdev_nr(mddev, choice))
1576 choice++;
1577 rdev->desc_nr = choice;
1578 } else {
1579 if (find_rdev_nr(mddev, rdev->desc_nr))
1580 return -EBUSY;
1581 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001582 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1583 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1584 mdname(mddev), mddev->max_disks);
1585 return -EBUSY;
1586 }
NeilBrown19133a42005-11-08 21:39:35 -08001587 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001588 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001589 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001592 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001593
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001594 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001595 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001596
Tejun Heo0762b8b2008-08-25 19:56:12 +09001597 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001598 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1599 kobject_del(&rdev->kobj);
1600 goto fail;
1601 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001602 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1603
NeilBrown4b809912008-07-21 17:05:25 +10001604 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001605 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001606
1607 /* May as well allow recovery to be retried once */
1608 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001609
1610 md_integrity_check(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001612
1613 fail:
1614 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1615 b, mdname(mddev));
1616 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
NeilBrown177a99b2008-02-06 01:39:56 -08001619static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001620{
1621 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1622 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001623 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001624}
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1627{
1628 char b[BDEVNAME_SIZE];
1629 if (!rdev->mddev) {
1630 MD_BUG();
1631 return;
1632 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001633 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001634 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1636 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001637 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001638 sysfs_put(rdev->sysfs_state);
1639 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001640 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001641 * writing to 'remove' to "dev/state". We also need
1642 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001643 */
NeilBrown4b809912008-07-21 17:05:25 +10001644 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001645 INIT_WORK(&rdev->del_work, md_delayed_delete);
1646 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001647 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650/*
1651 * prevent the device from being mounted, repartitioned or
1652 * otherwise reused by a RAID array (or any other kernel
1653 * subsystem), by bd_claiming the device.
1654 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001655static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 int err = 0;
1658 struct block_device *bdev;
1659 char b[BDEVNAME_SIZE];
1660
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001661 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (IS_ERR(bdev)) {
1663 printk(KERN_ERR "md: could not open %s.\n",
1664 __bdevname(dev, b));
1665 return PTR_ERR(bdev);
1666 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001667 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (err) {
1669 printk(KERN_ERR "md: could not bd_claim %s.\n",
1670 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001671 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return err;
1673 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001674 if (!shared)
1675 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 rdev->bdev = bdev;
1677 return err;
1678}
1679
1680static void unlock_rdev(mdk_rdev_t *rdev)
1681{
1682 struct block_device *bdev = rdev->bdev;
1683 rdev->bdev = NULL;
1684 if (!bdev)
1685 MD_BUG();
1686 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001687 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690void md_autodetect_dev(dev_t dev);
1691
1692static void export_rdev(mdk_rdev_t * rdev)
1693{
1694 char b[BDEVNAME_SIZE];
1695 printk(KERN_INFO "md: export_rdev(%s)\n",
1696 bdevname(rdev->bdev,b));
1697 if (rdev->mddev)
1698 MD_BUG();
1699 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001701 if (test_bit(AutoDetected, &rdev->flags))
1702 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703#endif
1704 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001705 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708static void kick_rdev_from_array(mdk_rdev_t * rdev)
1709{
1710 unbind_rdev_from_array(rdev);
1711 export_rdev(rdev);
1712}
1713
1714static void export_array(mddev_t *mddev)
1715{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001716 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
NeilBrownd089c6a2008-02-06 01:39:59 -08001718 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (!rdev->mddev) {
1720 MD_BUG();
1721 continue;
1722 }
1723 kick_rdev_from_array(rdev);
1724 }
1725 if (!list_empty(&mddev->disks))
1726 MD_BUG();
1727 mddev->raid_disks = 0;
1728 mddev->major_version = 0;
1729}
1730
1731static void print_desc(mdp_disk_t *desc)
1732{
1733 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1734 desc->major,desc->minor,desc->raid_disk,desc->state);
1735}
1736
Cheng Renquancd2ac932009-01-09 08:31:08 +11001737static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 int i;
1740
1741 printk(KERN_INFO
1742 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1743 sb->major_version, sb->minor_version, sb->patch_version,
1744 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1745 sb->ctime);
1746 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1747 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1748 sb->md_minor, sb->layout, sb->chunk_size);
1749 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1750 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1751 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1752 sb->failed_disks, sb->spare_disks,
1753 sb->sb_csum, (unsigned long)sb->events_lo);
1754
1755 printk(KERN_INFO);
1756 for (i = 0; i < MD_SB_DISKS; i++) {
1757 mdp_disk_t *desc;
1758
1759 desc = sb->disks + i;
1760 if (desc->number || desc->major || desc->minor ||
1761 desc->raid_disk || (desc->state && (desc->state != 4))) {
1762 printk(" D %2d: ", i);
1763 print_desc(desc);
1764 }
1765 }
1766 printk(KERN_INFO "md: THIS: ");
1767 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
Cheng Renquancd2ac932009-01-09 08:31:08 +11001770static void print_sb_1(struct mdp_superblock_1 *sb)
1771{
1772 __u8 *uuid;
1773
1774 uuid = sb->set_uuid;
1775 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1776 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1777 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1778 le32_to_cpu(sb->major_version),
1779 le32_to_cpu(sb->feature_map),
1780 uuid[0], uuid[1], uuid[2], uuid[3],
1781 uuid[4], uuid[5], uuid[6], uuid[7],
1782 uuid[8], uuid[9], uuid[10], uuid[11],
1783 uuid[12], uuid[13], uuid[14], uuid[15],
1784 sb->set_name,
1785 (unsigned long long)le64_to_cpu(sb->ctime)
1786 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1787
1788 uuid = sb->device_uuid;
1789 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1790 " RO:%llu\n"
1791 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1792 ":%02x%02x%02x%02x%02x%02x\n"
1793 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1794 KERN_INFO "md: (MaxDev:%u) \n",
1795 le32_to_cpu(sb->level),
1796 (unsigned long long)le64_to_cpu(sb->size),
1797 le32_to_cpu(sb->raid_disks),
1798 le32_to_cpu(sb->layout),
1799 le32_to_cpu(sb->chunksize),
1800 (unsigned long long)le64_to_cpu(sb->data_offset),
1801 (unsigned long long)le64_to_cpu(sb->data_size),
1802 (unsigned long long)le64_to_cpu(sb->super_offset),
1803 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1804 le32_to_cpu(sb->dev_number),
1805 uuid[0], uuid[1], uuid[2], uuid[3],
1806 uuid[4], uuid[5], uuid[6], uuid[7],
1807 uuid[8], uuid[9], uuid[10], uuid[11],
1808 uuid[12], uuid[13], uuid[14], uuid[15],
1809 sb->devflags,
1810 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1811 (unsigned long long)le64_to_cpu(sb->events),
1812 (unsigned long long)le64_to_cpu(sb->resync_offset),
1813 le32_to_cpu(sb->sb_csum),
1814 le32_to_cpu(sb->max_dev)
1815 );
1816}
1817
1818static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001821 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1822 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001823 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1824 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001826 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1827 switch (major_version) {
1828 case 0:
1829 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1830 break;
1831 case 1:
1832 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1833 break;
1834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 } else
1836 printk(KERN_INFO "md: no rdev superblock!\n");
1837}
1838
Adrian Bunk5e563412006-06-26 00:27:42 -07001839static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001841 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 mdk_rdev_t *rdev;
1843 mddev_t *mddev;
1844 char b[BDEVNAME_SIZE];
1845
1846 printk("\n");
1847 printk("md: **********************************\n");
1848 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1849 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001850 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
NeilBrown32a76272005-06-21 17:17:14 -07001852 if (mddev->bitmap)
1853 bitmap_print_sb(mddev->bitmap);
1854 else
1855 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001856 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 printk("<%s>", bdevname(rdev->bdev,b));
1858 printk("\n");
1859
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001860 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001861 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863 printk("md: **********************************\n");
1864 printk("\n");
1865}
1866
1867
NeilBrown42543762006-06-26 00:27:57 -07001868static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
NeilBrown42543762006-06-26 00:27:57 -07001870 /* Update each superblock (in-memory image), but
1871 * if we are allowed to, skip spares which already
1872 * have the right event counter, or have one earlier
1873 * (which would mean they aren't being marked as dirty
1874 * with the rest of the array)
1875 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001878 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001879 if (rdev->sb_events == mddev->events ||
1880 (nospares &&
1881 rdev->raid_disk < 0 &&
1882 (rdev->sb_events&1)==0 &&
1883 rdev->sb_events+1 == mddev->events)) {
1884 /* Don't update this superblock */
1885 rdev->sb_loaded = 2;
1886 } else {
1887 super_types[mddev->major_version].
1888 sync_super(mddev, rdev);
1889 rdev->sb_loaded = 1;
1890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892}
1893
NeilBrown850b2b42006-10-03 01:15:46 -07001894static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001897 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001898 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
NeilBrown8377bc82008-04-30 00:52:28 -07001900 if (mddev->external)
1901 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001903 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001904
NeilBrown850b2b42006-10-03 01:15:46 -07001905 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1906 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1907 force_change = 1;
1908 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1909 /* just a clean<-> dirty transition, possibly leave spares alone,
1910 * though if events isn't the right even/odd, we will have to do
1911 * spares after all
1912 */
1913 nospares = 1;
1914 if (force_change)
1915 nospares = 0;
1916 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001917 /* If the array is degraded, then skipping spares is both
1918 * dangerous and fairly pointless.
1919 * Dangerous because a device that was removed from the array
1920 * might have a event_count that still looks up-to-date,
1921 * so it can be re-added without a resync.
1922 * Pointless because if there are any spares to skip,
1923 * then a recovery will happen and soon that array won't
1924 * be degraded any more and the spare can go back to sleep then.
1925 */
NeilBrown850b2b42006-10-03 01:15:46 -07001926 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001927
NeilBrown06d91a52005-06-21 17:17:12 -07001928 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001930
1931 /* If this is just a dirty<->clean transition, and the array is clean
1932 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001933 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001934 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001935 && (mddev->events & 1)
1936 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001937 mddev->events--;
1938 else {
1939 /* otherwise we have to go forward and ... */
1940 mddev->events ++;
1941 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1942 /* .. if the array isn't clean, insist on an odd 'events' */
1943 if ((mddev->events&1)==0) {
1944 mddev->events++;
1945 nospares = 0;
1946 }
1947 } else {
1948 /* otherwise insist on an even 'events' (for clean states) */
1949 if ((mddev->events&1)) {
1950 mddev->events++;
1951 nospares = 0;
1952 }
1953 }
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 if (!mddev->events) {
1957 /*
1958 * oops, this 64-bit counter should never wrap.
1959 * Either we are in around ~1 trillion A.C., assuming
1960 * 1 reboot per second, or we have a bug:
1961 */
1962 MD_BUG();
1963 mddev->events --;
1964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 /*
1967 * do not write anything to disk if using
1968 * nonpersistent superblocks
1969 */
NeilBrown06d91a52005-06-21 17:17:12 -07001970 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001971 if (!mddev->external)
1972 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1973
NeilBrowna9701a32005-11-08 21:39:34 -08001974 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001975 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001977 }
NeilBrowne6910632008-02-06 01:39:51 -08001978 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001979 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 dprintk(KERN_INFO
1982 "md: updating %s RAID superblock on device (in sync %d)\n",
1983 mdname(mddev),mddev->in_sync);
1984
NeilBrown4ad13662007-07-17 04:06:13 -07001985 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001986 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 char b[BDEVNAME_SIZE];
1988 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001989 if (rdev->sb_loaded != 1)
1990 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001991 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 dprintk("(skipping faulty ");
1993
1994 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001995 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001996 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001997 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001998 rdev->sb_page);
1999 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2000 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002001 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002002 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 } else
2005 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002006 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /* only need to write one superblock... */
2008 break;
2009 }
NeilBrowna9701a32005-11-08 21:39:34 -08002010 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002011 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002012
NeilBrowna9701a32005-11-08 21:39:34 -08002013 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002014 if (mddev->in_sync != sync_req ||
2015 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002016 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002017 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002018 goto repeat;
2019 }
NeilBrown850b2b42006-10-03 01:15:46 -07002020 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002021 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002022 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002023 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2024 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Andre Noll7f6ce762008-03-23 18:34:54 +01002028/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002029 * We want to accept with case. For this we use cmd_match.
2030 */
2031static int cmd_match(const char *cmd, const char *str)
2032{
2033 /* See if cmd, written into a sysfs file, matches
2034 * str. They must either be the same, or cmd can
2035 * have a trailing newline
2036 */
2037 while (*cmd && *str && *cmd == *str) {
2038 cmd++;
2039 str++;
2040 }
2041 if (*cmd == '\n')
2042 cmd++;
2043 if (*str || *cmd)
2044 return 0;
2045 return 1;
2046}
2047
NeilBrown86e6ffd2005-11-08 21:39:24 -08002048struct rdev_sysfs_entry {
2049 struct attribute attr;
2050 ssize_t (*show)(mdk_rdev_t *, char *);
2051 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2052};
2053
2054static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002055state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002056{
2057 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002058 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002059
NeilBrownb2d444d2005-11-08 21:39:31 -08002060 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002061 len+= sprintf(page+len, "%sfaulty",sep);
2062 sep = ",";
2063 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002064 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002065 len += sprintf(page+len, "%sin_sync",sep);
2066 sep = ",";
2067 }
NeilBrownf6556752006-06-26 00:28:01 -07002068 if (test_bit(WriteMostly, &rdev->flags)) {
2069 len += sprintf(page+len, "%swrite_mostly",sep);
2070 sep = ",";
2071 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002072 if (test_bit(Blocked, &rdev->flags)) {
2073 len += sprintf(page+len, "%sblocked", sep);
2074 sep = ",";
2075 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002076 if (!test_bit(Faulty, &rdev->flags) &&
2077 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002078 len += sprintf(page+len, "%sspare", sep);
2079 sep = ",";
2080 }
2081 return len+sprintf(page+len, "\n");
2082}
2083
NeilBrown45dc2de2006-06-26 00:27:58 -07002084static ssize_t
2085state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2086{
2087 /* can write
2088 * faulty - simulates and error
2089 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002090 * writemostly - sets write_mostly
2091 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002092 * blocked - sets the Blocked flag
2093 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002094 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002095 */
2096 int err = -EINVAL;
2097 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2098 md_error(rdev->mddev, rdev);
2099 err = 0;
2100 } else if (cmd_match(buf, "remove")) {
2101 if (rdev->raid_disk >= 0)
2102 err = -EBUSY;
2103 else {
2104 mddev_t *mddev = rdev->mddev;
2105 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002106 if (mddev->pers)
2107 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002108 md_new_event(mddev);
2109 err = 0;
2110 }
NeilBrownf6556752006-06-26 00:28:01 -07002111 } else if (cmd_match(buf, "writemostly")) {
2112 set_bit(WriteMostly, &rdev->flags);
2113 err = 0;
2114 } else if (cmd_match(buf, "-writemostly")) {
2115 clear_bit(WriteMostly, &rdev->flags);
2116 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002117 } else if (cmd_match(buf, "blocked")) {
2118 set_bit(Blocked, &rdev->flags);
2119 err = 0;
2120 } else if (cmd_match(buf, "-blocked")) {
2121 clear_bit(Blocked, &rdev->flags);
2122 wake_up(&rdev->blocked_wait);
2123 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2124 md_wakeup_thread(rdev->mddev->thread);
2125
2126 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002127 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2128 set_bit(In_sync, &rdev->flags);
2129 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002130 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002131 if (!err && rdev->sysfs_state)
2132 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002133 return err ? err : len;
2134}
NeilBrown80ca3a42006-07-10 04:44:18 -07002135static struct rdev_sysfs_entry rdev_state =
2136__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002137
2138static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002139errors_show(mdk_rdev_t *rdev, char *page)
2140{
2141 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2142}
2143
2144static ssize_t
2145errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2146{
2147 char *e;
2148 unsigned long n = simple_strtoul(buf, &e, 10);
2149 if (*buf && (*e == 0 || *e == '\n')) {
2150 atomic_set(&rdev->corrected_errors, n);
2151 return len;
2152 }
2153 return -EINVAL;
2154}
2155static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002156__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002157
NeilBrown014236d2006-01-06 00:20:55 -08002158static ssize_t
2159slot_show(mdk_rdev_t *rdev, char *page)
2160{
2161 if (rdev->raid_disk < 0)
2162 return sprintf(page, "none\n");
2163 else
2164 return sprintf(page, "%d\n", rdev->raid_disk);
2165}
2166
2167static ssize_t
2168slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2169{
2170 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002171 int err;
2172 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002173 int slot = simple_strtoul(buf, &e, 10);
2174 if (strncmp(buf, "none", 4)==0)
2175 slot = -1;
2176 else if (e==buf || (*e && *e!= '\n'))
2177 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002178 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002179 /* Setting 'slot' on an active array requires also
2180 * updating the 'rd%d' link, and communicating
2181 * with the personality with ->hot_*_disk.
2182 * For now we only support removing
2183 * failed/spare devices. This normally happens automatically,
2184 * but not when the metadata is externally managed.
2185 */
NeilBrownc303da62008-02-06 01:39:51 -08002186 if (rdev->raid_disk == -1)
2187 return -EEXIST;
2188 /* personality does all needed checks */
2189 if (rdev->mddev->pers->hot_add_disk == NULL)
2190 return -EINVAL;
2191 err = rdev->mddev->pers->
2192 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2193 if (err)
2194 return err;
2195 sprintf(nm, "rd%d", rdev->raid_disk);
2196 sysfs_remove_link(&rdev->mddev->kobj, nm);
2197 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2198 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002199 } else if (rdev->mddev->pers) {
2200 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002201 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002202 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002203 */
2204
2205 if (rdev->raid_disk != -1)
2206 return -EBUSY;
2207
2208 if (rdev->mddev->pers->hot_add_disk == NULL)
2209 return -EINVAL;
2210
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002211 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002212 if (rdev2->raid_disk == slot)
2213 return -EEXIST;
2214
2215 rdev->raid_disk = slot;
2216 if (test_bit(In_sync, &rdev->flags))
2217 rdev->saved_raid_disk = slot;
2218 else
2219 rdev->saved_raid_disk = -1;
2220 err = rdev->mddev->pers->
2221 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002222 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002223 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002224 return err;
Neil Brown52664732008-06-28 08:31:44 +10002225 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002226 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002227 sprintf(nm, "rd%d", rdev->raid_disk);
2228 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2229 printk(KERN_WARNING
2230 "md: cannot register "
2231 "%s for %s\n",
2232 nm, mdname(rdev->mddev));
2233
2234 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002235 } else {
2236 if (slot >= rdev->mddev->raid_disks)
2237 return -ENOSPC;
2238 rdev->raid_disk = slot;
2239 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002240 clear_bit(Faulty, &rdev->flags);
2241 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002242 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002243 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002244 }
NeilBrown014236d2006-01-06 00:20:55 -08002245 return len;
2246}
2247
2248
2249static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002250__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002251
NeilBrown93c8cad2006-01-06 00:20:56 -08002252static ssize_t
2253offset_show(mdk_rdev_t *rdev, char *page)
2254{
Andrew Morton6961ece2006-01-06 00:20:59 -08002255 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002256}
2257
2258static ssize_t
2259offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2260{
2261 char *e;
2262 unsigned long long offset = simple_strtoull(buf, &e, 10);
2263 if (e==buf || (*e && *e != '\n'))
2264 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002265 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002266 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002267 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002268 /* Must set offset before size, so overlap checks
2269 * can be sane */
2270 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002271 rdev->data_offset = offset;
2272 return len;
2273}
2274
2275static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002276__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002277
NeilBrown83303b62006-01-06 00:21:06 -08002278static ssize_t
2279rdev_size_show(mdk_rdev_t *rdev, char *page)
2280{
Andre Nolldd8ac332009-03-31 14:33:13 +11002281 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002282}
2283
NeilBrownc5d79ad2008-02-06 01:39:54 -08002284static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2285{
2286 /* check if two start/length pairs overlap */
2287 if (s1+l1 <= s2)
2288 return 0;
2289 if (s2+l2 <= s1)
2290 return 0;
2291 return 1;
2292}
2293
Dan Williamsb522adc2009-03-31 15:00:31 +11002294static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2295{
2296 unsigned long long blocks;
2297 sector_t new;
2298
2299 if (strict_strtoull(buf, 10, &blocks) < 0)
2300 return -EINVAL;
2301
2302 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2303 return -EINVAL; /* sector conversion overflow */
2304
2305 new = blocks * 2;
2306 if (new != blocks * 2)
2307 return -EINVAL; /* unsigned long long to sector_t overflow */
2308
2309 *sectors = new;
2310 return 0;
2311}
2312
NeilBrown83303b62006-01-06 00:21:06 -08002313static ssize_t
2314rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2315{
NeilBrown27c529b2008-03-04 14:29:33 -08002316 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002317 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002318 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002319
Dan Williamsb522adc2009-03-31 15:00:31 +11002320 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002321 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002322 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002323 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002324 sectors = super_types[my_mddev->major_version].
2325 rdev_size_change(rdev, sectors);
2326 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002327 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002328 } else if (!sectors)
2329 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2330 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002331 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002332 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002333 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002334
Andre Nolldd8ac332009-03-31 14:33:13 +11002335 rdev->sectors = sectors;
2336 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002337 /* need to check that all other rdevs with the same ->bdev
2338 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002339 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002340 * we have to change it back, we will have the lock again.
2341 */
2342 mddev_t *mddev;
2343 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002344 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002345
NeilBrown27c529b2008-03-04 14:29:33 -08002346 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002347 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002348 mdk_rdev_t *rdev2;
2349
2350 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002351 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002352 if (test_bit(AllReserved, &rdev2->flags) ||
2353 (rdev->bdev == rdev2->bdev &&
2354 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002355 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002356 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002357 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002358 overlap = 1;
2359 break;
2360 }
2361 mddev_unlock(mddev);
2362 if (overlap) {
2363 mddev_put(mddev);
2364 break;
2365 }
2366 }
NeilBrown27c529b2008-03-04 14:29:33 -08002367 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002368 if (overlap) {
2369 /* Someone else could have slipped in a size
2370 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002371 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002372 * safe, and trust userspace not to race with
2373 * itself
2374 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002375 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002376 return -EBUSY;
2377 }
2378 }
NeilBrown83303b62006-01-06 00:21:06 -08002379 return len;
2380}
2381
2382static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002383__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002384
NeilBrown86e6ffd2005-11-08 21:39:24 -08002385static struct attribute *rdev_default_attrs[] = {
2386 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002387 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002388 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002389 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002390 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002391 NULL,
2392};
2393static ssize_t
2394rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2395{
2396 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2397 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002398 mddev_t *mddev = rdev->mddev;
2399 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002400
2401 if (!entry->show)
2402 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002403
2404 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2405 if (!rv) {
2406 if (rdev->mddev == NULL)
2407 rv = -EBUSY;
2408 else
2409 rv = entry->show(rdev, page);
2410 mddev_unlock(mddev);
2411 }
2412 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002413}
2414
2415static ssize_t
2416rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2417 const char *page, size_t length)
2418{
2419 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2420 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002421 ssize_t rv;
2422 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002423
2424 if (!entry->store)
2425 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002426 if (!capable(CAP_SYS_ADMIN))
2427 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002428 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002429 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002430 if (rdev->mddev == NULL)
2431 rv = -EBUSY;
2432 else
2433 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002434 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002435 }
2436 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002437}
2438
2439static void rdev_free(struct kobject *ko)
2440{
2441 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2442 kfree(rdev);
2443}
2444static struct sysfs_ops rdev_sysfs_ops = {
2445 .show = rdev_attr_show,
2446 .store = rdev_attr_store,
2447};
2448static struct kobj_type rdev_ktype = {
2449 .release = rdev_free,
2450 .sysfs_ops = &rdev_sysfs_ops,
2451 .default_attrs = rdev_default_attrs,
2452};
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454/*
2455 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2456 *
2457 * mark the device faulty if:
2458 *
2459 * - the device is nonexistent (zero size)
2460 * - the device has no valid superblock
2461 *
2462 * a faulty rdev _never_ has rdev->sb set.
2463 */
2464static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2465{
2466 char b[BDEVNAME_SIZE];
2467 int err;
2468 mdk_rdev_t *rdev;
2469 sector_t size;
2470
NeilBrown9ffae0c2006-01-06 00:20:32 -08002471 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (!rdev) {
2473 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2474 return ERR_PTR(-ENOMEM);
2475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 if ((err = alloc_disk_sb(rdev)))
2478 goto abort_free;
2479
NeilBrownc5d79ad2008-02-06 01:39:54 -08002480 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (err)
2482 goto abort_free;
2483
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002484 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002487 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002488 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002489 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002491 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002493 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002494 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2497 if (!size) {
2498 printk(KERN_WARNING
2499 "md: %s has zero or unknown size, marking faulty!\n",
2500 bdevname(rdev->bdev,b));
2501 err = -EINVAL;
2502 goto abort_free;
2503 }
2504
2505 if (super_format >= 0) {
2506 err = super_types[super_format].
2507 load_super(rdev, NULL, super_minor);
2508 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002509 printk(KERN_WARNING
2510 "md: %s does not have a valid v%d.%d "
2511 "superblock, not importing!\n",
2512 bdevname(rdev->bdev,b),
2513 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 goto abort_free;
2515 }
2516 if (err < 0) {
2517 printk(KERN_WARNING
2518 "md: could not read %s's sb, not importing!\n",
2519 bdevname(rdev->bdev,b));
2520 goto abort_free;
2521 }
2522 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002523
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002525 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 return rdev;
2528
2529abort_free:
2530 if (rdev->sb_page) {
2531 if (rdev->bdev)
2532 unlock_rdev(rdev);
2533 free_disk_sb(rdev);
2534 }
2535 kfree(rdev);
2536 return ERR_PTR(err);
2537}
2538
2539/*
2540 * Check a full RAID array for plausibility
2541 */
2542
2543
NeilBrowna757e642005-04-16 15:26:42 -07002544static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002547 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 char b[BDEVNAME_SIZE];
2549
2550 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002551 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 switch (super_types[mddev->major_version].
2553 load_super(rdev, freshest, mddev->minor_version)) {
2554 case 1:
2555 freshest = rdev;
2556 break;
2557 case 0:
2558 break;
2559 default:
2560 printk( KERN_ERR \
2561 "md: fatal superblock inconsistency in %s"
2562 " -- removing from array\n",
2563 bdevname(rdev->bdev,b));
2564 kick_rdev_from_array(rdev);
2565 }
2566
2567
2568 super_types[mddev->major_version].
2569 validate_super(mddev, freshest);
2570
2571 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002572 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002573 if (rdev->desc_nr >= mddev->max_disks ||
2574 i > mddev->max_disks) {
2575 printk(KERN_WARNING
2576 "md: %s: %s: only %d devices permitted\n",
2577 mdname(mddev), bdevname(rdev->bdev, b),
2578 mddev->max_disks);
2579 kick_rdev_from_array(rdev);
2580 continue;
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (rdev != freshest)
2583 if (super_types[mddev->major_version].
2584 validate_super(mddev, rdev)) {
2585 printk(KERN_WARNING "md: kicking non-fresh %s"
2586 " from array!\n",
2587 bdevname(rdev->bdev,b));
2588 kick_rdev_from_array(rdev);
2589 continue;
2590 }
2591 if (mddev->level == LEVEL_MULTIPATH) {
2592 rdev->desc_nr = i++;
2593 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002594 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002595 } else if (rdev->raid_disk >= mddev->raid_disks) {
2596 rdev->raid_disk = -1;
2597 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599 }
2600
2601
2602
2603 if (mddev->recovery_cp != MaxSector &&
2604 mddev->level >= 1)
2605 printk(KERN_ERR "md: %s: raid array is not clean"
2606 " -- starting background reconstruction\n",
2607 mdname(mddev));
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
NeilBrown19052c02008-08-05 15:54:13 +10002611static void md_safemode_timeout(unsigned long data);
2612
NeilBrowneae17012005-11-08 21:39:23 -08002613static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002614safe_delay_show(mddev_t *mddev, char *page)
2615{
2616 int msec = (mddev->safemode_delay*1000)/HZ;
2617 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2618}
2619static ssize_t
2620safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2621{
2622 int scale=1;
2623 int dot=0;
2624 int i;
2625 unsigned long msec;
2626 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002627
NeilBrown16f17b32006-06-26 00:27:37 -07002628 /* remove a period, and count digits after it */
2629 if (len >= sizeof(buf))
2630 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002631 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002632 for (i=0; i<len; i++) {
2633 if (dot) {
2634 if (isdigit(buf[i])) {
2635 buf[i-1] = buf[i];
2636 scale *= 10;
2637 }
2638 buf[i] = 0;
2639 } else if (buf[i] == '.') {
2640 dot=1;
2641 buf[i] = 0;
2642 }
2643 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002644 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002645 return -EINVAL;
2646 msec = (msec * 1000) / scale;
2647 if (msec == 0)
2648 mddev->safemode_delay = 0;
2649 else {
NeilBrown19052c02008-08-05 15:54:13 +10002650 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002651 mddev->safemode_delay = (msec*HZ)/1000;
2652 if (mddev->safemode_delay == 0)
2653 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002654 if (mddev->safemode_delay < old_delay)
2655 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002656 }
2657 return len;
2658}
2659static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002660__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002661
2662static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002663level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002664{
NeilBrown2604b702006-01-06 00:20:36 -08002665 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002666 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002667 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002668 else if (mddev->clevel[0])
2669 return sprintf(page, "%s\n", mddev->clevel);
2670 else if (mddev->level != LEVEL_NONE)
2671 return sprintf(page, "%d\n", mddev->level);
2672 else
2673 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002674}
2675
NeilBrownd9d166c2006-01-06 00:20:51 -08002676static ssize_t
2677level_store(mddev_t *mddev, const char *buf, size_t len)
2678{
NeilBrown245f46c2009-03-31 14:39:39 +11002679 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002680 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002681 struct mdk_personality *pers;
2682 void *priv;
2683
2684 if (mddev->pers == NULL) {
2685 if (len == 0)
2686 return 0;
2687 if (len >= sizeof(mddev->clevel))
2688 return -ENOSPC;
2689 strncpy(mddev->clevel, buf, len);
2690 if (mddev->clevel[len-1] == '\n')
2691 len--;
2692 mddev->clevel[len] = 0;
2693 mddev->level = LEVEL_NONE;
2694 return rv;
2695 }
2696
2697 /* request to change the personality. Need to ensure:
2698 * - array is not engaged in resync/recovery/reshape
2699 * - old personality can be suspended
2700 * - new personality will access other array.
2701 */
2702
2703 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002704 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002705
2706 if (!mddev->pers->quiesce) {
2707 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2708 mdname(mddev), mddev->pers->name);
2709 return -EINVAL;
2710 }
2711
2712 /* Now find the new personality */
2713 if (len == 0 || len >= sizeof(level))
2714 return -EINVAL;
2715 strncpy(level, buf, len);
2716 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002717 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002718 level[len] = 0;
2719
2720 request_module("md-%s", level);
2721 spin_lock(&pers_lock);
2722 pers = find_pers(LEVEL_NONE, level);
2723 if (!pers || !try_module_get(pers->owner)) {
2724 spin_unlock(&pers_lock);
2725 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2726 return -EINVAL;
2727 }
2728 spin_unlock(&pers_lock);
2729
2730 if (pers == mddev->pers) {
2731 /* Nothing to do! */
2732 module_put(pers->owner);
2733 return rv;
2734 }
2735 if (!pers->takeover) {
2736 module_put(pers->owner);
2737 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2738 mdname(mddev), level);
2739 return -EINVAL;
2740 }
2741
2742 /* ->takeover must set new_* and/or delta_disks
2743 * if it succeeds, and may set them when it fails.
2744 */
2745 priv = pers->takeover(mddev);
2746 if (IS_ERR(priv)) {
2747 mddev->new_level = mddev->level;
2748 mddev->new_layout = mddev->layout;
2749 mddev->new_chunk = mddev->chunk_size;
2750 mddev->raid_disks -= mddev->delta_disks;
2751 mddev->delta_disks = 0;
2752 module_put(pers->owner);
2753 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2754 mdname(mddev), level);
2755 return PTR_ERR(priv);
2756 }
2757
2758 /* Looks like we have a winner */
2759 mddev_suspend(mddev);
2760 mddev->pers->stop(mddev);
2761 module_put(mddev->pers->owner);
2762 mddev->pers = pers;
2763 mddev->private = priv;
2764 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2765 mddev->level = mddev->new_level;
2766 mddev->layout = mddev->new_layout;
2767 mddev->chunk_size = mddev->new_chunk;
2768 mddev->delta_disks = 0;
2769 pers->run(mddev);
2770 mddev_resume(mddev);
2771 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2772 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2773 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002774 return rv;
2775}
2776
2777static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002778__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002779
NeilBrownd4dbd022006-06-26 00:27:59 -07002780
2781static ssize_t
2782layout_show(mddev_t *mddev, char *page)
2783{
2784 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002785 if (mddev->reshape_position != MaxSector &&
2786 mddev->layout != mddev->new_layout)
2787 return sprintf(page, "%d (%d)\n",
2788 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002789 return sprintf(page, "%d\n", mddev->layout);
2790}
2791
2792static ssize_t
2793layout_store(mddev_t *mddev, const char *buf, size_t len)
2794{
2795 char *e;
2796 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002797
2798 if (!*buf || (*e && *e != '\n'))
2799 return -EINVAL;
2800
NeilBrownb3546032009-03-31 14:56:41 +11002801 if (mddev->pers) {
2802 int err;
2803 if (mddev->pers->reconfig == NULL)
2804 return -EBUSY;
2805 err = mddev->pers->reconfig(mddev, n, -1);
2806 if (err)
2807 return err;
2808 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002809 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002810 if (mddev->reshape_position == MaxSector)
2811 mddev->layout = n;
2812 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002813 return len;
2814}
2815static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002816__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002817
2818
NeilBrowneae17012005-11-08 21:39:23 -08002819static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002820raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002821{
NeilBrownbb636542005-11-08 21:39:45 -08002822 if (mddev->raid_disks == 0)
2823 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002824 if (mddev->reshape_position != MaxSector &&
2825 mddev->delta_disks != 0)
2826 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2827 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002828 return sprintf(page, "%d\n", mddev->raid_disks);
2829}
2830
NeilBrownda943b992006-01-06 00:20:54 -08002831static int update_raid_disks(mddev_t *mddev, int raid_disks);
2832
2833static ssize_t
2834raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2835{
NeilBrownda943b992006-01-06 00:20:54 -08002836 char *e;
2837 int rv = 0;
2838 unsigned long n = simple_strtoul(buf, &e, 10);
2839
2840 if (!*buf || (*e && *e != '\n'))
2841 return -EINVAL;
2842
2843 if (mddev->pers)
2844 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002845 else if (mddev->reshape_position != MaxSector) {
2846 int olddisks = mddev->raid_disks - mddev->delta_disks;
2847 mddev->delta_disks = n - olddisks;
2848 mddev->raid_disks = n;
2849 } else
NeilBrownda943b992006-01-06 00:20:54 -08002850 mddev->raid_disks = n;
2851 return rv ? rv : len;
2852}
2853static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002854__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002855
NeilBrown24dd4692005-11-08 21:39:26 -08002856static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002857chunk_size_show(mddev_t *mddev, char *page)
2858{
NeilBrown08a02ec2007-05-09 02:35:38 -07002859 if (mddev->reshape_position != MaxSector &&
2860 mddev->chunk_size != mddev->new_chunk)
2861 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2862 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002863 return sprintf(page, "%d\n", mddev->chunk_size);
2864}
2865
2866static ssize_t
2867chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2868{
NeilBrown3b343802006-01-06 00:20:47 -08002869 char *e;
2870 unsigned long n = simple_strtoul(buf, &e, 10);
2871
NeilBrown3b343802006-01-06 00:20:47 -08002872 if (!*buf || (*e && *e != '\n'))
2873 return -EINVAL;
2874
NeilBrownb3546032009-03-31 14:56:41 +11002875 if (mddev->pers) {
2876 int err;
2877 if (mddev->pers->reconfig == NULL)
2878 return -EBUSY;
2879 err = mddev->pers->reconfig(mddev, -1, n);
2880 if (err)
2881 return err;
2882 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002883 mddev->new_chunk = n;
NeilBrownb3546032009-03-31 14:56:41 +11002884 if (mddev->reshape_position == MaxSector)
2885 mddev->chunk_size = n;
2886 }
NeilBrown3b343802006-01-06 00:20:47 -08002887 return len;
2888}
2889static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002890__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002891
NeilBrowna94213b2006-06-26 00:28:00 -07002892static ssize_t
2893resync_start_show(mddev_t *mddev, char *page)
2894{
NeilBrownd1a7c502009-03-31 15:24:32 +11002895 if (mddev->recovery_cp == MaxSector)
2896 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002897 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2898}
2899
2900static ssize_t
2901resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2902{
NeilBrowna94213b2006-06-26 00:28:00 -07002903 char *e;
2904 unsigned long long n = simple_strtoull(buf, &e, 10);
2905
2906 if (mddev->pers)
2907 return -EBUSY;
2908 if (!*buf || (*e && *e != '\n'))
2909 return -EINVAL;
2910
2911 mddev->recovery_cp = n;
2912 return len;
2913}
2914static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002915__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002916
NeilBrown9e653b62006-06-26 00:27:58 -07002917/*
2918 * The array state can be:
2919 *
2920 * clear
2921 * No devices, no size, no level
2922 * Equivalent to STOP_ARRAY ioctl
2923 * inactive
2924 * May have some settings, but array is not active
2925 * all IO results in error
2926 * When written, doesn't tear down array, but just stops it
2927 * suspended (not supported yet)
2928 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002929 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002930 * readonly
2931 * no resync can happen. no superblocks get written.
2932 * write requests fail
2933 * read-auto
2934 * like readonly, but behaves like 'clean' on a write request.
2935 *
2936 * clean - no pending writes, but otherwise active.
2937 * When written to inactive array, starts without resync
2938 * If a write request arrives then
2939 * if metadata is known, mark 'dirty' and switch to 'active'.
2940 * if not known, block and switch to write-pending
2941 * If written to an active array that has pending writes, then fails.
2942 * active
2943 * fully active: IO and resync can be happening.
2944 * When written to inactive array, starts with resync
2945 *
2946 * write-pending
2947 * clean, but writes are blocked waiting for 'active' to be written.
2948 *
2949 * active-idle
2950 * like active, but no writes have been seen for a while (100msec).
2951 *
2952 */
2953enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2954 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002955static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002956 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2957 "write-pending", "active-idle", NULL };
2958
2959static int match_word(const char *word, char **list)
2960{
2961 int n;
2962 for (n=0; list[n]; n++)
2963 if (cmd_match(word, list[n]))
2964 break;
2965 return n;
2966}
2967
2968static ssize_t
2969array_state_show(mddev_t *mddev, char *page)
2970{
2971 enum array_state st = inactive;
2972
2973 if (mddev->pers)
2974 switch(mddev->ro) {
2975 case 1:
2976 st = readonly;
2977 break;
2978 case 2:
2979 st = read_auto;
2980 break;
2981 case 0:
2982 if (mddev->in_sync)
2983 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002984 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2985 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002986 else if (mddev->safemode)
2987 st = active_idle;
2988 else
2989 st = active;
2990 }
2991 else {
2992 if (list_empty(&mddev->disks) &&
2993 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11002994 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002995 st = clear;
2996 else
2997 st = inactive;
2998 }
2999 return sprintf(page, "%s\n", array_states[st]);
3000}
3001
Neil Browndf5b20c2008-07-11 22:02:22 +10003002static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003003static int do_md_run(mddev_t * mddev);
3004static int restart_array(mddev_t *mddev);
3005
3006static ssize_t
3007array_state_store(mddev_t *mddev, const char *buf, size_t len)
3008{
3009 int err = -EINVAL;
3010 enum array_state st = match_word(buf, array_states);
3011 switch(st) {
3012 case bad_word:
3013 break;
3014 case clear:
3015 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003016 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003017 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003018 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003019 break;
3020 case inactive:
3021 /* stopping an active array */
3022 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003023 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003024 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003025 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003026 } else
3027 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003028 break;
3029 case suspended:
3030 break; /* not supported yet */
3031 case readonly:
3032 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003033 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003034 else {
3035 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003036 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003037 err = do_md_run(mddev);
3038 }
3039 break;
3040 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003041 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003042 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003043 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003044 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003045 err = restart_array(mddev);
3046 if (err == 0) {
3047 mddev->ro = 2;
3048 set_disk_ro(mddev->gendisk, 0);
3049 }
NeilBrown9e653b62006-06-26 00:27:58 -07003050 } else {
3051 mddev->ro = 2;
3052 err = do_md_run(mddev);
3053 }
3054 break;
3055 case clean:
3056 if (mddev->pers) {
3057 restart_array(mddev);
3058 spin_lock_irq(&mddev->write_lock);
3059 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003060 if (mddev->in_sync == 0) {
3061 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003062 if (mddev->safemode == 1)
3063 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003064 if (mddev->persistent)
3065 set_bit(MD_CHANGE_CLEAN,
3066 &mddev->flags);
3067 }
3068 err = 0;
3069 } else
3070 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003071 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003072 } else
3073 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003074 break;
3075 case active:
3076 if (mddev->pers) {
3077 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003078 if (mddev->external)
3079 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003080 wake_up(&mddev->sb_wait);
3081 err = 0;
3082 } else {
3083 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003084 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003085 err = do_md_run(mddev);
3086 }
3087 break;
3088 case write_pending:
3089 case active_idle:
3090 /* these cannot be set */
3091 break;
3092 }
3093 if (err)
3094 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003095 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003096 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003097 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003098 }
NeilBrown9e653b62006-06-26 00:27:58 -07003099}
NeilBrown80ca3a42006-07-10 04:44:18 -07003100static struct md_sysfs_entry md_array_state =
3101__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003102
NeilBrown6d7ff732006-01-06 00:21:16 -08003103static ssize_t
3104null_show(mddev_t *mddev, char *page)
3105{
3106 return -EINVAL;
3107}
3108
3109static ssize_t
3110new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3111{
3112 /* buf must be %d:%d\n? giving major and minor numbers */
3113 /* The new device is added to the array.
3114 * If the array has a persistent superblock, we read the
3115 * superblock to initialise info and check validity.
3116 * Otherwise, only checking done is that in bind_rdev_to_array,
3117 * which mainly checks size.
3118 */
3119 char *e;
3120 int major = simple_strtoul(buf, &e, 10);
3121 int minor;
3122 dev_t dev;
3123 mdk_rdev_t *rdev;
3124 int err;
3125
3126 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3127 return -EINVAL;
3128 minor = simple_strtoul(e+1, &e, 10);
3129 if (*e && *e != '\n')
3130 return -EINVAL;
3131 dev = MKDEV(major, minor);
3132 if (major != MAJOR(dev) ||
3133 minor != MINOR(dev))
3134 return -EOVERFLOW;
3135
3136
3137 if (mddev->persistent) {
3138 rdev = md_import_device(dev, mddev->major_version,
3139 mddev->minor_version);
3140 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3141 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3142 mdk_rdev_t, same_set);
3143 err = super_types[mddev->major_version]
3144 .load_super(rdev, rdev0, mddev->minor_version);
3145 if (err < 0)
3146 goto out;
3147 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003148 } else if (mddev->external)
3149 rdev = md_import_device(dev, -2, -1);
3150 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003151 rdev = md_import_device(dev, -1, -1);
3152
3153 if (IS_ERR(rdev))
3154 return PTR_ERR(rdev);
3155 err = bind_rdev_to_array(rdev, mddev);
3156 out:
3157 if (err)
3158 export_rdev(rdev);
3159 return err ? err : len;
3160}
3161
3162static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003163__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003164
3165static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003166bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3167{
3168 char *end;
3169 unsigned long chunk, end_chunk;
3170
3171 if (!mddev->bitmap)
3172 goto out;
3173 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3174 while (*buf) {
3175 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3176 if (buf == end) break;
3177 if (*end == '-') { /* range */
3178 buf = end + 1;
3179 end_chunk = simple_strtoul(buf, &end, 0);
3180 if (buf == end) break;
3181 }
3182 if (*end && !isspace(*end)) break;
3183 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3184 buf = end;
3185 while (isspace(*buf)) buf++;
3186 }
3187 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3188out:
3189 return len;
3190}
3191
3192static struct md_sysfs_entry md_bitmap =
3193__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3194
3195static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003196size_show(mddev_t *mddev, char *page)
3197{
Andre Noll58c0fed2009-03-31 14:33:13 +11003198 return sprintf(page, "%llu\n",
3199 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003200}
3201
Andre Nolld71f9f82008-07-11 22:02:22 +10003202static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003203
3204static ssize_t
3205size_store(mddev_t *mddev, const char *buf, size_t len)
3206{
3207 /* If array is inactive, we can reduce the component size, but
3208 * not increase it (except from 0).
3209 * If array is active, we can try an on-line resize
3210 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003211 sector_t sectors;
3212 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003213
Andre Noll58c0fed2009-03-31 14:33:13 +11003214 if (err < 0)
3215 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003216 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003217 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003218 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003219 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003220 if (mddev->dev_sectors == 0 ||
3221 mddev->dev_sectors > sectors)
3222 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003223 else
3224 err = -ENOSPC;
3225 }
3226 return err ? err : len;
3227}
3228
3229static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003230__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003231
NeilBrown8bb93aa2006-01-06 00:20:50 -08003232
3233/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003234 * This is one of
3235 * 'none' for arrays with no metadata (good luck...)
3236 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003237 * or N.M for internally known formats
3238 */
3239static ssize_t
3240metadata_show(mddev_t *mddev, char *page)
3241{
3242 if (mddev->persistent)
3243 return sprintf(page, "%d.%d\n",
3244 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003245 else if (mddev->external)
3246 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003247 else
3248 return sprintf(page, "none\n");
3249}
3250
3251static ssize_t
3252metadata_store(mddev_t *mddev, const char *buf, size_t len)
3253{
3254 int major, minor;
3255 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003256 /* Changing the details of 'external' metadata is
3257 * always permitted. Otherwise there must be
3258 * no devices attached to the array.
3259 */
3260 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3261 ;
3262 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003263 return -EBUSY;
3264
3265 if (cmd_match(buf, "none")) {
3266 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003267 mddev->external = 0;
3268 mddev->major_version = 0;
3269 mddev->minor_version = 90;
3270 return len;
3271 }
3272 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003273 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003274 if (namelen >= sizeof(mddev->metadata_type))
3275 namelen = sizeof(mddev->metadata_type)-1;
3276 strncpy(mddev->metadata_type, buf+9, namelen);
3277 mddev->metadata_type[namelen] = 0;
3278 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3279 mddev->metadata_type[--namelen] = 0;
3280 mddev->persistent = 0;
3281 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003282 mddev->major_version = 0;
3283 mddev->minor_version = 90;
3284 return len;
3285 }
3286 major = simple_strtoul(buf, &e, 10);
3287 if (e==buf || *e != '.')
3288 return -EINVAL;
3289 buf = e+1;
3290 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003291 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003292 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003293 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003294 return -ENOENT;
3295 mddev->major_version = major;
3296 mddev->minor_version = minor;
3297 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003298 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003299 return len;
3300}
3301
3302static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003303__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003304
NeilBrowna35b0d62006-01-06 00:20:49 -08003305static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003306action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003307{
NeilBrown7eec3142005-11-08 21:39:44 -08003308 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08003309 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003310 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003311 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3312 type = "reshape";
3313 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003314 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3315 type = "resync";
3316 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3317 type = "check";
3318 else
3319 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003320 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003321 type = "recover";
3322 }
3323 return sprintf(page, "%s\n", type);
3324}
3325
3326static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003327action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003328{
NeilBrown7eec3142005-11-08 21:39:44 -08003329 if (!mddev->pers || !mddev->pers->sync_request)
3330 return -EINVAL;
3331
NeilBrownbce74da2006-01-06 00:20:41 -08003332 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003333 if (mddev->sync_thread) {
3334 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3335 md_unregister_thread(mddev->sync_thread);
3336 mddev->sync_thread = NULL;
3337 mddev->recovery = 0;
3338 }
NeilBrown03c902e2006-01-06 00:20:46 -08003339 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3340 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003341 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003342 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003344 else if (cmd_match(page, "recover")) {
3345 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3346 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3347 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003348 int err;
3349 if (mddev->pers->start_reshape == NULL)
3350 return -EINVAL;
3351 err = mddev->pers->start_reshape(mddev);
3352 if (err)
3353 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003354 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003355 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003356 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003357 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003358 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003359 return -EINVAL;
3360 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3361 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003362 }
NeilBrown03c902e2006-01-06 00:20:46 -08003363 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003364 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003365 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003366 return len;
3367}
3368
NeilBrown9d888832005-11-08 21:39:26 -08003369static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003370mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003371{
3372 return sprintf(page, "%llu\n",
3373 (unsigned long long) mddev->resync_mismatches);
3374}
3375
NeilBrown80ca3a42006-07-10 04:44:18 -07003376static struct md_sysfs_entry md_scan_mode =
3377__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003378
NeilBrown96de1e62005-11-08 21:39:39 -08003379
NeilBrown80ca3a42006-07-10 04:44:18 -07003380static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003381
NeilBrown88202a02006-01-06 00:21:36 -08003382static ssize_t
3383sync_min_show(mddev_t *mddev, char *page)
3384{
3385 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3386 mddev->sync_speed_min ? "local": "system");
3387}
3388
3389static ssize_t
3390sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3391{
3392 int min;
3393 char *e;
3394 if (strncmp(buf, "system", 6)==0) {
3395 mddev->sync_speed_min = 0;
3396 return len;
3397 }
3398 min = simple_strtoul(buf, &e, 10);
3399 if (buf == e || (*e && *e != '\n') || min <= 0)
3400 return -EINVAL;
3401 mddev->sync_speed_min = min;
3402 return len;
3403}
3404
3405static struct md_sysfs_entry md_sync_min =
3406__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3407
3408static ssize_t
3409sync_max_show(mddev_t *mddev, char *page)
3410{
3411 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3412 mddev->sync_speed_max ? "local": "system");
3413}
3414
3415static ssize_t
3416sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3417{
3418 int max;
3419 char *e;
3420 if (strncmp(buf, "system", 6)==0) {
3421 mddev->sync_speed_max = 0;
3422 return len;
3423 }
3424 max = simple_strtoul(buf, &e, 10);
3425 if (buf == e || (*e && *e != '\n') || max <= 0)
3426 return -EINVAL;
3427 mddev->sync_speed_max = max;
3428 return len;
3429}
3430
3431static struct md_sysfs_entry md_sync_max =
3432__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3433
Iustin Popd7f3d292007-10-16 23:30:54 -07003434static ssize_t
3435degraded_show(mddev_t *mddev, char *page)
3436{
3437 return sprintf(page, "%d\n", mddev->degraded);
3438}
3439static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003440
3441static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003442sync_force_parallel_show(mddev_t *mddev, char *page)
3443{
3444 return sprintf(page, "%d\n", mddev->parallel_resync);
3445}
3446
3447static ssize_t
3448sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3449{
3450 long n;
3451
3452 if (strict_strtol(buf, 10, &n))
3453 return -EINVAL;
3454
3455 if (n != 0 && n != 1)
3456 return -EINVAL;
3457
3458 mddev->parallel_resync = n;
3459
3460 if (mddev->sync_thread)
3461 wake_up(&resync_wait);
3462
3463 return len;
3464}
3465
3466/* force parallel resync, even with shared block devices */
3467static struct md_sysfs_entry md_sync_force_parallel =
3468__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3469 sync_force_parallel_show, sync_force_parallel_store);
3470
3471static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003472sync_speed_show(mddev_t *mddev, char *page)
3473{
3474 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003475 if (mddev->curr_resync == 0)
3476 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003477 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3478 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003479 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003480 db = resync - mddev->resync_mark_cnt;
3481 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003482}
3483
NeilBrown80ca3a42006-07-10 04:44:18 -07003484static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003485
3486static ssize_t
3487sync_completed_show(mddev_t *mddev, char *page)
3488{
Andre Noll58c0fed2009-03-31 14:33:13 +11003489 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003490
NeilBrownacb180b2009-04-14 16:28:34 +10003491 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3492 return sprintf(page, "none\n");
3493
NeilBrown88202a02006-01-06 00:21:36 -08003494 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003495 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003496 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003497 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003498
NeilBrownacb180b2009-04-14 16:28:34 +10003499 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003500 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003501}
3502
NeilBrown80ca3a42006-07-10 04:44:18 -07003503static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003504
NeilBrowne464eaf2006-03-27 01:18:14 -08003505static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003506min_sync_show(mddev_t *mddev, char *page)
3507{
3508 return sprintf(page, "%llu\n",
3509 (unsigned long long)mddev->resync_min);
3510}
3511static ssize_t
3512min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3513{
3514 unsigned long long min;
3515 if (strict_strtoull(buf, 10, &min))
3516 return -EINVAL;
3517 if (min > mddev->resync_max)
3518 return -EINVAL;
3519 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3520 return -EBUSY;
3521
3522 /* Must be a multiple of chunk_size */
3523 if (mddev->chunk_size) {
3524 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3525 return -EINVAL;
3526 }
3527 mddev->resync_min = min;
3528
3529 return len;
3530}
3531
3532static struct md_sysfs_entry md_min_sync =
3533__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3534
3535static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003536max_sync_show(mddev_t *mddev, char *page)
3537{
3538 if (mddev->resync_max == MaxSector)
3539 return sprintf(page, "max\n");
3540 else
3541 return sprintf(page, "%llu\n",
3542 (unsigned long long)mddev->resync_max);
3543}
3544static ssize_t
3545max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3546{
3547 if (strncmp(buf, "max", 3) == 0)
3548 mddev->resync_max = MaxSector;
3549 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003550 unsigned long long max;
3551 if (strict_strtoull(buf, 10, &max))
3552 return -EINVAL;
3553 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003554 return -EINVAL;
3555 if (max < mddev->resync_max &&
3556 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3557 return -EBUSY;
3558
3559 /* Must be a multiple of chunk_size */
3560 if (mddev->chunk_size) {
3561 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3562 return -EINVAL;
3563 }
3564 mddev->resync_max = max;
3565 }
3566 wake_up(&mddev->recovery_wait);
3567 return len;
3568}
3569
3570static struct md_sysfs_entry md_max_sync =
3571__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3572
3573static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003574suspend_lo_show(mddev_t *mddev, char *page)
3575{
3576 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3577}
3578
3579static ssize_t
3580suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3581{
3582 char *e;
3583 unsigned long long new = simple_strtoull(buf, &e, 10);
3584
3585 if (mddev->pers->quiesce == NULL)
3586 return -EINVAL;
3587 if (buf == e || (*e && *e != '\n'))
3588 return -EINVAL;
3589 if (new >= mddev->suspend_hi ||
3590 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3591 mddev->suspend_lo = new;
3592 mddev->pers->quiesce(mddev, 2);
3593 return len;
3594 } else
3595 return -EINVAL;
3596}
3597static struct md_sysfs_entry md_suspend_lo =
3598__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3599
3600
3601static ssize_t
3602suspend_hi_show(mddev_t *mddev, char *page)
3603{
3604 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3605}
3606
3607static ssize_t
3608suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3609{
3610 char *e;
3611 unsigned long long new = simple_strtoull(buf, &e, 10);
3612
3613 if (mddev->pers->quiesce == NULL)
3614 return -EINVAL;
3615 if (buf == e || (*e && *e != '\n'))
3616 return -EINVAL;
3617 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3618 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3619 mddev->suspend_hi = new;
3620 mddev->pers->quiesce(mddev, 1);
3621 mddev->pers->quiesce(mddev, 0);
3622 return len;
3623 } else
3624 return -EINVAL;
3625}
3626static struct md_sysfs_entry md_suspend_hi =
3627__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3628
NeilBrown08a02ec2007-05-09 02:35:38 -07003629static ssize_t
3630reshape_position_show(mddev_t *mddev, char *page)
3631{
3632 if (mddev->reshape_position != MaxSector)
3633 return sprintf(page, "%llu\n",
3634 (unsigned long long)mddev->reshape_position);
3635 strcpy(page, "none\n");
3636 return 5;
3637}
3638
3639static ssize_t
3640reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3641{
3642 char *e;
3643 unsigned long long new = simple_strtoull(buf, &e, 10);
3644 if (mddev->pers)
3645 return -EBUSY;
3646 if (buf == e || (*e && *e != '\n'))
3647 return -EINVAL;
3648 mddev->reshape_position = new;
3649 mddev->delta_disks = 0;
3650 mddev->new_level = mddev->level;
3651 mddev->new_layout = mddev->layout;
3652 mddev->new_chunk = mddev->chunk_size;
3653 return len;
3654}
3655
3656static struct md_sysfs_entry md_reshape_position =
3657__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3658 reshape_position_store);
3659
Dan Williamsb522adc2009-03-31 15:00:31 +11003660static ssize_t
3661array_size_show(mddev_t *mddev, char *page)
3662{
3663 if (mddev->external_size)
3664 return sprintf(page, "%llu\n",
3665 (unsigned long long)mddev->array_sectors/2);
3666 else
3667 return sprintf(page, "default\n");
3668}
3669
3670static ssize_t
3671array_size_store(mddev_t *mddev, const char *buf, size_t len)
3672{
3673 sector_t sectors;
3674
3675 if (strncmp(buf, "default", 7) == 0) {
3676 if (mddev->pers)
3677 sectors = mddev->pers->size(mddev, 0, 0);
3678 else
3679 sectors = mddev->array_sectors;
3680
3681 mddev->external_size = 0;
3682 } else {
3683 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3684 return -EINVAL;
3685 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
3686 return -EINVAL;
3687
3688 mddev->external_size = 1;
3689 }
3690
3691 mddev->array_sectors = sectors;
3692 set_capacity(mddev->gendisk, mddev->array_sectors);
3693 if (mddev->pers) {
3694 struct block_device *bdev = bdget_disk(mddev->gendisk, 0);
3695
3696 if (bdev) {
3697 mutex_lock(&bdev->bd_inode->i_mutex);
3698 i_size_write(bdev->bd_inode,
3699 (loff_t)mddev->array_sectors << 9);
3700 mutex_unlock(&bdev->bd_inode->i_mutex);
3701 bdput(bdev);
3702 }
3703 }
3704
3705 return len;
3706}
3707
3708static struct md_sysfs_entry md_array_size =
3709__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3710 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003711
NeilBrowneae17012005-11-08 21:39:23 -08003712static struct attribute *md_default_attrs[] = {
3713 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003714 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003715 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003716 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003717 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003718 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003719 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003720 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003721 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003722 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003723 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003724 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003725 NULL,
3726};
3727
3728static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003729 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003730 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003731 &md_sync_min.attr,
3732 &md_sync_max.attr,
3733 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003734 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003735 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003736 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003737 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003738 &md_suspend_lo.attr,
3739 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003740 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003741 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003742 NULL,
3743};
NeilBrown411036f2005-11-08 21:39:40 -08003744static struct attribute_group md_redundancy_group = {
3745 .name = NULL,
3746 .attrs = md_redundancy_attrs,
3747};
3748
NeilBrowneae17012005-11-08 21:39:23 -08003749
3750static ssize_t
3751md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3752{
3753 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3754 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003755 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003756
3757 if (!entry->show)
3758 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003759 rv = mddev_lock(mddev);
3760 if (!rv) {
3761 rv = entry->show(mddev, page);
3762 mddev_unlock(mddev);
3763 }
NeilBrown96de1e62005-11-08 21:39:39 -08003764 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003765}
3766
3767static ssize_t
3768md_attr_store(struct kobject *kobj, struct attribute *attr,
3769 const char *page, size_t length)
3770{
3771 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3772 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003773 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003774
3775 if (!entry->store)
3776 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003777 if (!capable(CAP_SYS_ADMIN))
3778 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003779 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003780 if (mddev->hold_active == UNTIL_IOCTL)
3781 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003782 if (!rv) {
3783 rv = entry->store(mddev, page, length);
3784 mddev_unlock(mddev);
3785 }
NeilBrown96de1e62005-11-08 21:39:39 -08003786 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003787}
3788
3789static void md_free(struct kobject *ko)
3790{
3791 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003792
3793 if (mddev->sysfs_state)
3794 sysfs_put(mddev->sysfs_state);
3795
3796 if (mddev->gendisk) {
3797 del_gendisk(mddev->gendisk);
3798 put_disk(mddev->gendisk);
3799 }
3800 if (mddev->queue)
3801 blk_cleanup_queue(mddev->queue);
3802
NeilBrowneae17012005-11-08 21:39:23 -08003803 kfree(mddev);
3804}
3805
3806static struct sysfs_ops md_sysfs_ops = {
3807 .show = md_attr_show,
3808 .store = md_attr_store,
3809};
3810static struct kobj_type md_ktype = {
3811 .release = md_free,
3812 .sysfs_ops = &md_sysfs_ops,
3813 .default_attrs = md_default_attrs,
3814};
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816int mdp_major = 0;
3817
Dan Williams5fd3a172009-03-04 00:57:25 -07003818static void mddev_delayed_delete(struct work_struct *ws)
3819{
3820 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3821
3822 if (mddev->private == &md_redundancy_group) {
3823 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3824 if (mddev->sysfs_action)
3825 sysfs_put(mddev->sysfs_action);
3826 mddev->sysfs_action = NULL;
3827 mddev->private = NULL;
3828 }
3829 kobject_del(&mddev->kobj);
3830 kobject_put(&mddev->kobj);
3831}
3832
NeilBrownefeb53c2009-01-09 08:31:10 +11003833static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003835 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 mddev_t *mddev = mddev_find(dev);
3837 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003838 int partitioned;
3839 int shift;
3840 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003841 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003844 return -ENODEV;
3845
3846 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3847 shift = partitioned ? MdpMinorShift : 0;
3848 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
NeilBrownd3374822009-01-09 08:31:10 +11003850 /* wait for any previous instance if this device
3851 * to be completed removed (mddev_delayed_delete).
3852 */
3853 flush_scheduled_work();
3854
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003855 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003857 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003859 return -EEXIST;
3860 }
3861
3862 if (name) {
3863 /* Need to ensure that 'name' is not a duplicate.
3864 */
3865 mddev_t *mddev2;
3866 spin_lock(&all_mddevs_lock);
3867
3868 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3869 if (mddev2->gendisk &&
3870 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3871 spin_unlock(&all_mddevs_lock);
3872 return -EEXIST;
3873 }
3874 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 }
NeilBrown8b765392009-01-09 08:31:08 +11003876
3877 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3878 if (!mddev->queue) {
3879 mutex_unlock(&disks_mutex);
3880 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003881 return -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003882 }
NeilBrown409c57f2009-03-31 14:39:39 +11003883 mddev->queue->queuedata = mddev;
3884
NeilBrown8b765392009-01-09 08:31:08 +11003885 /* Can be unlocked because the queue is new: no concurrency */
3886 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3887
NeilBrown409c57f2009-03-31 14:39:39 +11003888 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 disk = alloc_disk(1 << shift);
3891 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003892 mutex_unlock(&disks_mutex);
NeilBrown8b765392009-01-09 08:31:08 +11003893 blk_cleanup_queue(mddev->queue);
3894 mddev->queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003896 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003898 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003900 if (name)
3901 strcpy(disk->disk_name, name);
3902 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003904 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 disk->fops = &md_fops;
3907 disk->private_data = mddev;
3908 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003909 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003910 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003911 * remove it now.
3912 */
3913 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 add_disk(disk);
3915 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003916 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3917 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003918 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003919 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003920 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3921 disk->disk_name);
NeilBrownb62b7592008-10-21 13:25:21 +11003922 else {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003923 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003924 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3925 }
NeilBrownd3374822009-01-09 08:31:10 +11003926 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003927 return 0;
3928}
3929
3930static struct kobject *md_probe(dev_t dev, int *part, void *data)
3931{
3932 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return NULL;
3934}
3935
NeilBrownefeb53c2009-01-09 08:31:10 +11003936static int add_named_array(const char *val, struct kernel_param *kp)
3937{
3938 /* val must be "md_*" where * is not all digits.
3939 * We allocate an array with a large free minor number, and
3940 * set the name to val. val must not already be an active name.
3941 */
3942 int len = strlen(val);
3943 char buf[DISK_NAME_LEN];
3944
3945 while (len && val[len-1] == '\n')
3946 len--;
3947 if (len >= DISK_NAME_LEN)
3948 return -E2BIG;
3949 strlcpy(buf, val, len+1);
3950 if (strncmp(buf, "md_", 3) != 0)
3951 return -EINVAL;
3952 return md_alloc(0, buf);
3953}
3954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955static void md_safemode_timeout(unsigned long data)
3956{
3957 mddev_t *mddev = (mddev_t *) data;
3958
Neil Brown0fd62b82008-06-28 08:31:36 +10003959 if (!atomic_read(&mddev->writes_pending)) {
3960 mddev->safemode = 1;
3961 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11003962 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 md_wakeup_thread(mddev->thread);
3965}
3966
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003967static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969static int do_md_run(mddev_t * mddev)
3970{
NeilBrown2604b702006-01-06 00:20:36 -08003971 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 int chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 mdk_rdev_t *rdev;
3974 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003975 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 char b[BDEVNAME_SIZE];
3977
NeilBrowna757e642005-04-16 15:26:42 -07003978 if (list_empty(&mddev->disks))
3979 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 if (mddev->pers)
3983 return -EBUSY;
3984
3985 /*
3986 * Analyze all RAID superblock(s)
3987 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003988 if (!mddev->raid_disks) {
3989 if (!mddev->persistent)
3990 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003991 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
NeilBrown2604b702006-01-06 00:20:36 -08003996 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (chunk_size > MAX_CHUNK_SIZE) {
3998 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3999 chunk_size, MAX_CHUNK_SIZE);
4000 return -EINVAL;
4001 }
4002 /*
NeilBrown4bbf3772008-10-13 11:55:12 +11004003 * chunk-size has to be a power of 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 */
4005 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07004006 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return -EINVAL;
4008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
4010 /* devices must have minimum size of one chunk */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004011 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004012 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 continue;
Andre Nolldd8ac332009-03-31 14:33:13 +11004014 if (rdev->sectors < chunk_size / 512) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 printk(KERN_WARNING
4016 "md: Dev %s smaller than chunk_size:"
Andre Nolldd8ac332009-03-31 14:33:13 +11004017 " %llu < %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 bdevname(rdev->bdev,b),
Andre Nolldd8ac332009-03-31 14:33:13 +11004019 (unsigned long long)rdev->sectors,
4020 chunk_size / 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 return -EINVAL;
4022 }
4023 }
4024 }
4025
NeilBrownd9d166c2006-01-06 00:20:51 -08004026 if (mddev->level != LEVEL_NONE)
4027 request_module("md-level-%d", mddev->level);
4028 else if (mddev->clevel[0])
4029 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
4031 /*
4032 * Drop all container device buffers, from now on
4033 * the only valid external interface is through the md
4034 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004036 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004037 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 continue;
4039 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004040 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004041
4042 /* perform some consistency tests on the device.
4043 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004044 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004045 */
Andre Noll0f420352008-07-11 22:02:23 +10004046 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004047 if (mddev->dev_sectors &&
4048 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004049 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004050 printk("md: %s: data overlaps metadata\n",
4051 mdname(mddev));
4052 return -EINVAL;
4053 }
4054 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004055 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004056 > rdev->data_offset) {
4057 printk("md: %s: metadata overlaps data\n",
4058 mdname(mddev));
4059 return -EINVAL;
4060 }
4061 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004062 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
4064
4065 md_probe(mddev->unit, NULL, NULL);
4066 disk = mddev->gendisk;
4067 if (!disk)
4068 return -ENOMEM;
4069
4070 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004071 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004072 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004074 if (mddev->level != LEVEL_NONE)
4075 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4076 mddev->level);
4077 else
4078 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4079 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return -EINVAL;
4081 }
NeilBrown2604b702006-01-06 00:20:36 -08004082 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004084 if (mddev->level != pers->level) {
4085 mddev->level = pers->level;
4086 mddev->new_level = pers->level;
4087 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004088 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004090 if (pers->level >= 4 && pers->level <= 6)
4091 /* Cannot support integrity (yet) */
4092 blk_integrity_unregister(mddev->gendisk);
4093
NeilBrownf6705572006-03-27 01:18:11 -08004094 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004095 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004096 /* This personality cannot handle reshaping... */
4097 mddev->pers = NULL;
4098 module_put(pers->owner);
4099 return -EINVAL;
4100 }
4101
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004102 if (pers->sync_request) {
4103 /* Warn if this is a potentially silly
4104 * configuration.
4105 */
4106 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4107 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004108 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004109
4110 list_for_each_entry(rdev, &mddev->disks, same_set)
4111 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004112 if (rdev < rdev2 &&
4113 rdev->bdev->bd_contains ==
4114 rdev2->bdev->bd_contains) {
4115 printk(KERN_WARNING
4116 "%s: WARNING: %s appears to be"
4117 " on the same physical disk as"
4118 " %s.\n",
4119 mdname(mddev),
4120 bdevname(rdev->bdev,b),
4121 bdevname(rdev2->bdev,b2));
4122 warned = 1;
4123 }
4124 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004125
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004126 if (warned)
4127 printk(KERN_WARNING
4128 "True protection against single-disk"
4129 " failure might be compromised.\n");
4130 }
4131
NeilBrown657390d2005-08-26 18:34:16 -07004132 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004133 /* may be over-ridden by personality */
4134 mddev->resync_max_sectors = mddev->dev_sectors;
4135
NeilBrowna9701a32005-11-08 21:39:34 -08004136 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004137 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
NeilBrownf91de922005-11-08 21:39:36 -08004139 if (start_readonly)
4140 mddev->ro = 2; /* read-only, but switch on first write */
4141
NeilBrownb15c2e52006-01-06 00:20:16 -08004142 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004143 if (err)
4144 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004145 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4146 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4147 " but 'external_size' not in effect?\n", __func__);
4148 printk(KERN_ERR
4149 "md: invalid array_size %llu > default size %llu\n",
4150 (unsigned long long)mddev->array_sectors / 2,
4151 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4152 err = -EINVAL;
4153 mddev->pers->stop(mddev);
4154 }
4155 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004156 err = bitmap_create(mddev);
4157 if (err) {
4158 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4159 mdname(mddev), err);
4160 mddev->pers->stop(mddev);
4161 }
4162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 module_put(mddev->pers->owner);
4165 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004166 bitmap_destroy(mddev);
4167 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004169 if (mddev->pers->sync_request) {
4170 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4171 printk(KERN_WARNING
4172 "md: cannot register extra attributes for %s\n",
4173 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004174 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004175 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004176 mddev->ro = 0;
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 atomic_set(&mddev->writes_pending,0);
4179 mddev->safemode = 0;
4180 mddev->safemode_timer.function = md_safemode_timeout;
4181 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004182 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004184
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004185 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004186 if (rdev->raid_disk >= 0) {
4187 char nm[20];
4188 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004189 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4190 printk("md: cannot register %s for %s\n",
4191 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
4194 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4195
NeilBrown850b2b42006-10-03 01:15:46 -07004196 if (mddev->flags)
4197 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Andre Nollf233ea52008-07-21 17:05:22 +10004199 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004201 /* If there is a partially-recovered drive we need to
4202 * start recovery here. If we leave it to md_check_recovery,
4203 * it will remove the drives and not do the right thing
4204 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004205 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004206 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004207 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004208 if (rdev->raid_disk >= 0 &&
4209 !test_bit(In_sync, &rdev->flags) &&
4210 !test_bit(Faulty, &rdev->flags))
4211 /* complete an interrupted recovery */
4212 spares++;
4213 if (spares && mddev->pers->sync_request) {
4214 mddev->recovery = 0;
4215 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4216 mddev->sync_thread = md_register_thread(md_do_sync,
4217 mddev,
4218 "%s_resync");
4219 if (!mddev->sync_thread) {
4220 printk(KERN_ERR "%s: could not start resync"
4221 " thread...\n",
4222 mdname(mddev));
4223 /* leave the spares where they are, it shouldn't hurt */
4224 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004225 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004226 }
4227 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004228 md_wakeup_thread(mddev->thread);
4229 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004230
Linus Torvalds44ce62942007-05-09 18:51:36 -07004231 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004232 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004233 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004234 if (mddev->sysfs_action)
4235 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004236 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004237 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return 0;
4239}
4240
4241static int restart_array(mddev_t *mddev)
4242{
4243 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Andre Noll80fab1d2008-07-11 22:02:21 +10004245 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004247 return -ENXIO;
4248 if (!mddev->pers)
4249 return -EINVAL;
4250 if (!mddev->ro)
4251 return -EBUSY;
4252 mddev->safemode = 0;
4253 mddev->ro = 0;
4254 set_disk_ro(disk, 0);
4255 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4256 mdname(mddev));
4257 /* Kick recovery or resync if necessary */
4258 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4259 md_wakeup_thread(mddev->thread);
4260 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004261 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263}
4264
NeilBrownacc55e22006-06-26 00:27:47 -07004265/* similar to deny_write_access, but accounts for our holding a reference
4266 * to the file ourselves */
4267static int deny_bitmap_write_access(struct file * file)
4268{
4269 struct inode *inode = file->f_mapping->host;
4270
4271 spin_lock(&inode->i_lock);
4272 if (atomic_read(&inode->i_writecount) > 1) {
4273 spin_unlock(&inode->i_lock);
4274 return -ETXTBSY;
4275 }
4276 atomic_set(&inode->i_writecount, -1);
4277 spin_unlock(&inode->i_lock);
4278
4279 return 0;
4280}
4281
4282static void restore_bitmap_write_access(struct file *file)
4283{
4284 struct inode *inode = file->f_mapping->host;
4285
4286 spin_lock(&inode->i_lock);
4287 atomic_set(&inode->i_writecount, 1);
4288 spin_unlock(&inode->i_lock);
4289}
4290
NeilBrown9e653b62006-06-26 00:27:58 -07004291/* mode:
4292 * 0 - completely stop and dis-assemble array
4293 * 1 - switch to readonly
4294 * 2 - stop but do not disassemble array
4295 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004296static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297{
4298 int err = 0;
4299 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004300 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
NeilBrownf2ea68c2008-07-21 17:05:25 +10004302 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004303 printk("md: %s still in use.\n",mdname(mddev));
4304 return -EBUSY;
4305 }
4306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
4309 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004310 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4312 md_unregister_thread(mddev->sync_thread);
4313 mddev->sync_thread = NULL;
4314 }
4315
4316 del_timer_sync(&mddev->safemode_timer);
4317
NeilBrown9e653b62006-06-26 00:27:58 -07004318 switch(mode) {
4319 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004321 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 goto out;
4323 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004324 break;
4325 case 0: /* disassemble */
4326 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004327 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004328 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (mddev->ro)
4330 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004333 mddev->queue->merge_bvec_fn = NULL;
4334 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004335 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004337 if (mddev->pers->sync_request)
4338 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004340 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004341 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004342
NeilBrownc4647292009-05-07 12:51:06 +10004343 list_for_each_entry(rdev, &mddev->disks, same_set)
4344 if (rdev->raid_disk >= 0) {
4345 char nm[20];
4346 sprintf(nm, "rd%d", rdev->raid_disk);
4347 sysfs_remove_link(&mddev->kobj, nm);
4348 }
4349
NeilBrown0d4ca602006-12-10 02:20:44 -08004350 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004351 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (mddev->ro)
4354 mddev->ro = 0;
4355 }
NeilBrown850b2b42006-10-03 01:15:46 -07004356 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 /* mark array as shutdown cleanly */
4358 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004359 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 }
NeilBrown9e653b62006-06-26 00:27:58 -07004361 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004363 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 }
NeilBrown32a76272005-06-21 17:17:14 -07004365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 /*
4367 * Free resources if final stop
4368 */
NeilBrown9e653b62006-06-26 00:27:58 -07004369 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4372
NeilBrown978f9462006-02-02 14:28:05 -08004373 bitmap_destroy(mddev);
4374 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004375 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004376 fput(mddev->bitmap_file);
4377 mddev->bitmap_file = NULL;
4378 }
4379 mddev->bitmap_offset = 0;
4380
NeilBrown177a99b2008-02-06 01:39:56 -08004381 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004382 flush_scheduled_work();
4383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 export_array(mddev);
4385
Andre Nollf233ea52008-07-21 17:05:22 +10004386 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004387 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004388 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004389 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004390 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004391 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004392 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004393 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004394 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004395 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004396 mddev->level = LEVEL_NONE;
4397 mddev->clevel[0] = 0;
4398 mddev->flags = 0;
4399 mddev->ro = 0;
4400 mddev->metadata_type[0] = 0;
4401 mddev->chunk_size = 0;
4402 mddev->ctime = mddev->utime = 0;
4403 mddev->layout = 0;
4404 mddev->max_disks = 0;
4405 mddev->events = 0;
4406 mddev->delta_disks = 0;
4407 mddev->new_level = LEVEL_NONE;
4408 mddev->new_layout = 0;
4409 mddev->new_chunk = 0;
4410 mddev->curr_resync = 0;
4411 mddev->resync_mismatches = 0;
4412 mddev->suspend_lo = mddev->suspend_hi = 0;
4413 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4414 mddev->recovery = 0;
4415 mddev->in_sync = 0;
4416 mddev->changed = 0;
4417 mddev->degraded = 0;
4418 mddev->barriers_work = 0;
4419 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004420 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004421 if (mddev->hold_active == UNTIL_STOP)
4422 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004423
NeilBrowna8a55c32006-06-26 00:27:39 -07004424 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4426 mdname(mddev));
4427 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004428 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004429 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004430 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431out:
4432 return err;
4433}
4434
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004435#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436static void autorun_array(mddev_t *mddev)
4437{
4438 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 int err;
4440
NeilBrowna757e642005-04-16 15:26:42 -07004441 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 printk(KERN_INFO "md: running: ");
4445
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004446 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 char b[BDEVNAME_SIZE];
4448 printk("<%s>", bdevname(rdev->bdev,b));
4449 }
4450 printk("\n");
4451
NeilBrownd710e132008-10-13 11:55:12 +11004452 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 if (err) {
4454 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004455 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 }
4457}
4458
4459/*
4460 * lets try to run arrays based on all disks that have arrived
4461 * until now. (those are in pending_raid_disks)
4462 *
4463 * the method: pick the first pending disk, collect all disks with
4464 * the same UUID, remove all from the pending list and put them into
4465 * the 'same_array' list. Then order this list based on superblock
4466 * update time (freshest comes first), kick out 'old' disks and
4467 * compare superblocks. If everything's fine then run it.
4468 *
4469 * If "unit" is allocated, then bump its reference count
4470 */
4471static void autorun_devices(int part)
4472{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004473 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 mddev_t *mddev;
4475 char b[BDEVNAME_SIZE];
4476
4477 printk(KERN_INFO "md: autorun ...\n");
4478 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004479 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004481 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 rdev0 = list_entry(pending_raid_disks.next,
4483 mdk_rdev_t, same_set);
4484
4485 printk(KERN_INFO "md: considering %s ...\n",
4486 bdevname(rdev0->bdev,b));
4487 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004488 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 if (super_90_load(rdev, rdev0, 0) >= 0) {
4490 printk(KERN_INFO "md: adding %s ...\n",
4491 bdevname(rdev->bdev,b));
4492 list_move(&rdev->same_set, &candidates);
4493 }
4494 /*
4495 * now we have a set of devices, with all of them having
4496 * mostly sane superblocks. It's time to allocate the
4497 * mddev.
4498 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004499 if (part) {
4500 dev = MKDEV(mdp_major,
4501 rdev0->preferred_minor << MdpMinorShift);
4502 unit = MINOR(dev) >> MdpMinorShift;
4503 } else {
4504 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4505 unit = MINOR(dev);
4506 }
4507 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4509 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4510 break;
4511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513 md_probe(dev, NULL, NULL);
4514 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004515 if (!mddev || !mddev->gendisk) {
4516 if (mddev)
4517 mddev_put(mddev);
4518 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 "md: cannot allocate memory for md drive.\n");
4520 break;
4521 }
4522 if (mddev_lock(mddev))
4523 printk(KERN_WARNING "md: %s locked, cannot run\n",
4524 mdname(mddev));
4525 else if (mddev->raid_disks || mddev->major_version
4526 || !list_empty(&mddev->disks)) {
4527 printk(KERN_WARNING
4528 "md: %s already running, cannot run %s\n",
4529 mdname(mddev), bdevname(rdev0->bdev,b));
4530 mddev_unlock(mddev);
4531 } else {
4532 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004533 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004534 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 list_del_init(&rdev->same_set);
4536 if (bind_rdev_to_array(rdev, mddev))
4537 export_rdev(rdev);
4538 }
4539 autorun_array(mddev);
4540 mddev_unlock(mddev);
4541 }
4542 /* on success, candidates will be empty, on error
4543 * it won't...
4544 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004545 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004546 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 mddev_put(mddev);
4550 }
4551 printk(KERN_INFO "md: ... autorun DONE.\n");
4552}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004553#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555static int get_version(void __user * arg)
4556{
4557 mdu_version_t ver;
4558
4559 ver.major = MD_MAJOR_VERSION;
4560 ver.minor = MD_MINOR_VERSION;
4561 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4562
4563 if (copy_to_user(arg, &ver, sizeof(ver)))
4564 return -EFAULT;
4565
4566 return 0;
4567}
4568
4569static int get_array_info(mddev_t * mddev, void __user * arg)
4570{
4571 mdu_array_info_t info;
4572 int nr,working,active,failed,spare;
4573 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
4575 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004576 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004578 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 failed++;
4580 else {
4581 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004582 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 active++;
4584 else
4585 spare++;
4586 }
4587 }
4588
4589 info.major_version = mddev->major_version;
4590 info.minor_version = mddev->minor_version;
4591 info.patch_version = MD_PATCHLEVEL_VERSION;
4592 info.ctime = mddev->ctime;
4593 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004594 info.size = mddev->dev_sectors / 2;
4595 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004596 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 info.nr_disks = nr;
4598 info.raid_disks = mddev->raid_disks;
4599 info.md_minor = mddev->md_minor;
4600 info.not_persistent= !mddev->persistent;
4601
4602 info.utime = mddev->utime;
4603 info.state = 0;
4604 if (mddev->in_sync)
4605 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004606 if (mddev->bitmap && mddev->bitmap_offset)
4607 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 info.active_disks = active;
4609 info.working_disks = working;
4610 info.failed_disks = failed;
4611 info.spare_disks = spare;
4612
4613 info.layout = mddev->layout;
4614 info.chunk_size = mddev->chunk_size;
4615
4616 if (copy_to_user(arg, &info, sizeof(info)))
4617 return -EFAULT;
4618
4619 return 0;
4620}
4621
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004622static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004623{
4624 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4625 char *ptr, *buf = NULL;
4626 int err = -ENOMEM;
4627
Dan Williamsb5470dc2008-06-27 21:44:04 -07004628 if (md_allow_write(mddev))
4629 file = kmalloc(sizeof(*file), GFP_NOIO);
4630 else
4631 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004632
NeilBrown32a76272005-06-21 17:17:14 -07004633 if (!file)
4634 goto out;
4635
4636 /* bitmap disabled, zero the first byte and copy out */
4637 if (!mddev->bitmap || !mddev->bitmap->file) {
4638 file->pathname[0] = '\0';
4639 goto copy_out;
4640 }
4641
4642 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4643 if (!buf)
4644 goto out;
4645
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004646 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4647 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004648 goto out;
4649
4650 strcpy(file->pathname, ptr);
4651
4652copy_out:
4653 err = 0;
4654 if (copy_to_user(arg, file, sizeof(*file)))
4655 err = -EFAULT;
4656out:
4657 kfree(buf);
4658 kfree(file);
4659 return err;
4660}
4661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662static int get_disk_info(mddev_t * mddev, void __user * arg)
4663{
4664 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 mdk_rdev_t *rdev;
4666
4667 if (copy_from_user(&info, arg, sizeof(info)))
4668 return -EFAULT;
4669
Andre Noll26ef3792008-07-11 22:02:21 +10004670 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 if (rdev) {
4672 info.major = MAJOR(rdev->bdev->bd_dev);
4673 info.minor = MINOR(rdev->bdev->bd_dev);
4674 info.raid_disk = rdev->raid_disk;
4675 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004676 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004678 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 info.state |= (1<<MD_DISK_ACTIVE);
4680 info.state |= (1<<MD_DISK_SYNC);
4681 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004682 if (test_bit(WriteMostly, &rdev->flags))
4683 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 } else {
4685 info.major = info.minor = 0;
4686 info.raid_disk = -1;
4687 info.state = (1<<MD_DISK_REMOVED);
4688 }
4689
4690 if (copy_to_user(arg, &info, sizeof(info)))
4691 return -EFAULT;
4692
4693 return 0;
4694}
4695
4696static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4697{
4698 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4699 mdk_rdev_t *rdev;
4700 dev_t dev = MKDEV(info->major,info->minor);
4701
4702 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4703 return -EOVERFLOW;
4704
4705 if (!mddev->raid_disks) {
4706 int err;
4707 /* expecting a device which has a superblock */
4708 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4709 if (IS_ERR(rdev)) {
4710 printk(KERN_WARNING
4711 "md: md_import_device returned %ld\n",
4712 PTR_ERR(rdev));
4713 return PTR_ERR(rdev);
4714 }
4715 if (!list_empty(&mddev->disks)) {
4716 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4717 mdk_rdev_t, same_set);
4718 int err = super_types[mddev->major_version]
4719 .load_super(rdev, rdev0, mddev->minor_version);
4720 if (err < 0) {
4721 printk(KERN_WARNING
4722 "md: %s has different UUID to %s\n",
4723 bdevname(rdev->bdev,b),
4724 bdevname(rdev0->bdev,b2));
4725 export_rdev(rdev);
4726 return -EINVAL;
4727 }
4728 }
4729 err = bind_rdev_to_array(rdev, mddev);
4730 if (err)
4731 export_rdev(rdev);
4732 return err;
4733 }
4734
4735 /*
4736 * add_new_disk can be used once the array is assembled
4737 * to add "hot spares". They must already have a superblock
4738 * written
4739 */
4740 if (mddev->pers) {
4741 int err;
4742 if (!mddev->pers->hot_add_disk) {
4743 printk(KERN_WARNING
4744 "%s: personality does not support diskops!\n",
4745 mdname(mddev));
4746 return -EINVAL;
4747 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004748 if (mddev->persistent)
4749 rdev = md_import_device(dev, mddev->major_version,
4750 mddev->minor_version);
4751 else
4752 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 if (IS_ERR(rdev)) {
4754 printk(KERN_WARNING
4755 "md: md_import_device returned %ld\n",
4756 PTR_ERR(rdev));
4757 return PTR_ERR(rdev);
4758 }
NeilBrown41158c72005-06-21 17:17:25 -07004759 /* set save_raid_disk if appropriate */
4760 if (!mddev->persistent) {
4761 if (info->state & (1<<MD_DISK_SYNC) &&
4762 info->raid_disk < mddev->raid_disks)
4763 rdev->raid_disk = info->raid_disk;
4764 else
4765 rdev->raid_disk = -1;
4766 } else
4767 super_types[mddev->major_version].
4768 validate_super(mddev, rdev);
4769 rdev->saved_raid_disk = rdev->raid_disk;
4770
NeilBrownb2d444d2005-11-08 21:39:31 -08004771 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004772 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4773 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004774 else
4775 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004776
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 rdev->raid_disk = -1;
4778 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004779 if (!err && !mddev->pers->hot_remove_disk) {
4780 /* If there is hot_add_disk but no hot_remove_disk
4781 * then added disks for geometry changes,
4782 * and should be added immediately.
4783 */
4784 super_types[mddev->major_version].
4785 validate_super(mddev, rdev);
4786 err = mddev->pers->hot_add_disk(mddev, rdev);
4787 if (err)
4788 unbind_rdev_from_array(rdev);
4789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 if (err)
4791 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004792 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004793 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004794
NeilBrown17571282006-12-10 02:20:52 -08004795 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004796 if (mddev->degraded)
4797 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004798 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004799 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 return err;
4801 }
4802
4803 /* otherwise, add_new_disk is only allowed
4804 * for major_version==0 superblocks
4805 */
4806 if (mddev->major_version != 0) {
4807 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4808 mdname(mddev));
4809 return -EINVAL;
4810 }
4811
4812 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4813 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004814 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 if (IS_ERR(rdev)) {
4816 printk(KERN_WARNING
4817 "md: error, md_import_device() returned %ld\n",
4818 PTR_ERR(rdev));
4819 return PTR_ERR(rdev);
4820 }
4821 rdev->desc_nr = info->number;
4822 if (info->raid_disk < mddev->raid_disks)
4823 rdev->raid_disk = info->raid_disk;
4824 else
4825 rdev->raid_disk = -1;
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004828 if (info->state & (1<<MD_DISK_SYNC))
4829 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004831 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4832 set_bit(WriteMostly, &rdev->flags);
4833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 if (!mddev->persistent) {
4835 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004836 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 } else
Andre Noll0f420352008-07-11 22:02:23 +10004838 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolldd8ac332009-03-31 14:33:13 +11004839 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
NeilBrown2bf071b2006-01-06 00:20:55 -08004841 err = bind_rdev_to_array(rdev, mddev);
4842 if (err) {
4843 export_rdev(rdev);
4844 return err;
4845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 }
4847
4848 return 0;
4849}
4850
4851static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4852{
4853 char b[BDEVNAME_SIZE];
4854 mdk_rdev_t *rdev;
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 rdev = find_rdev(mddev, dev);
4857 if (!rdev)
4858 return -ENXIO;
4859
4860 if (rdev->raid_disk >= 0)
4861 goto busy;
4862
4863 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004864 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004865 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
4867 return 0;
4868busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004869 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 bdevname(rdev->bdev,b), mdname(mddev));
4871 return -EBUSY;
4872}
4873
4874static int hot_add_disk(mddev_t * mddev, dev_t dev)
4875{
4876 char b[BDEVNAME_SIZE];
4877 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 mdk_rdev_t *rdev;
4879
4880 if (!mddev->pers)
4881 return -ENODEV;
4882
4883 if (mddev->major_version != 0) {
4884 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4885 " version-0 superblocks.\n",
4886 mdname(mddev));
4887 return -EINVAL;
4888 }
4889 if (!mddev->pers->hot_add_disk) {
4890 printk(KERN_WARNING
4891 "%s: personality does not support diskops!\n",
4892 mdname(mddev));
4893 return -EINVAL;
4894 }
4895
NeilBrownd710e132008-10-13 11:55:12 +11004896 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 if (IS_ERR(rdev)) {
4898 printk(KERN_WARNING
4899 "md: error, md_import_device() returned %ld\n",
4900 PTR_ERR(rdev));
4901 return -EINVAL;
4902 }
4903
4904 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004905 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 else
Andre Noll0f420352008-07-11 22:02:23 +10004907 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Andre Nolldd8ac332009-03-31 14:33:13 +11004909 rdev->sectors = calc_num_sectors(rdev, mddev->chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
NeilBrownb2d444d2005-11-08 21:39:31 -08004911 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 printk(KERN_WARNING
4913 "md: can not hot-add faulty %s disk to %s!\n",
4914 bdevname(rdev->bdev,b), mdname(mddev));
4915 err = -EINVAL;
4916 goto abort_export;
4917 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004918 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004920 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004921 err = bind_rdev_to_array(rdev, mddev);
4922 if (err)
4923 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 /*
4926 * The rest should better be atomic, we can have disk failures
4927 * noticed in interrupt contexts ...
4928 */
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 rdev->raid_disk = -1;
4931
NeilBrown850b2b42006-10-03 01:15:46 -07004932 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
4934 /*
4935 * Kick recovery, maybe this spare has to be added to the
4936 * array immediately.
4937 */
4938 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4939 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004940 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 return 0;
4942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943abort_export:
4944 export_rdev(rdev);
4945 return err;
4946}
4947
NeilBrown32a76272005-06-21 17:17:14 -07004948static int set_bitmap_file(mddev_t *mddev, int fd)
4949{
4950 int err;
4951
NeilBrown36fa3062005-09-09 16:23:45 -07004952 if (mddev->pers) {
4953 if (!mddev->pers->quiesce)
4954 return -EBUSY;
4955 if (mddev->recovery || mddev->sync_thread)
4956 return -EBUSY;
4957 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004958 }
4959
NeilBrown36fa3062005-09-09 16:23:45 -07004960
4961 if (fd >= 0) {
4962 if (mddev->bitmap)
4963 return -EEXIST; /* cannot add when bitmap is present */
4964 mddev->bitmap_file = fget(fd);
4965
4966 if (mddev->bitmap_file == NULL) {
4967 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4968 mdname(mddev));
4969 return -EBADF;
4970 }
4971
4972 err = deny_bitmap_write_access(mddev->bitmap_file);
4973 if (err) {
4974 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4975 mdname(mddev));
4976 fput(mddev->bitmap_file);
4977 mddev->bitmap_file = NULL;
4978 return err;
4979 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004980 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004981 } else if (mddev->bitmap == NULL)
4982 return -ENOENT; /* cannot remove what isn't there */
4983 err = 0;
4984 if (mddev->pers) {
4985 mddev->pers->quiesce(mddev, 1);
4986 if (fd >= 0)
4987 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004988 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004989 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004990 fd = -1; /* make sure to put the file */
4991 }
NeilBrown36fa3062005-09-09 16:23:45 -07004992 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004993 }
4994 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004995 if (mddev->bitmap_file) {
4996 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004997 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004998 }
NeilBrown36fa3062005-09-09 16:23:45 -07004999 mddev->bitmap_file = NULL;
5000 }
5001
NeilBrown32a76272005-06-21 17:17:14 -07005002 return err;
5003}
5004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005/*
5006 * set_array_info is used two different ways
5007 * The original usage is when creating a new array.
5008 * In this usage, raid_disks is > 0 and it together with
5009 * level, size, not_persistent,layout,chunksize determine the
5010 * shape of the array.
5011 * This will always create an array with a type-0.90.0 superblock.
5012 * The newer usage is when assembling an array.
5013 * In this case raid_disks will be 0, and the major_version field is
5014 * use to determine which style super-blocks are to be found on the devices.
5015 * The minor and patch _version numbers are also kept incase the
5016 * super_block handler wishes to interpret them.
5017 */
5018static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5019{
5020
5021 if (info->raid_disks == 0) {
5022 /* just setting version number for superblock loading */
5023 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005024 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 super_types[info->major_version].name == NULL) {
5026 /* maybe try to auto-load a module? */
5027 printk(KERN_INFO
5028 "md: superblock version %d not known\n",
5029 info->major_version);
5030 return -EINVAL;
5031 }
5032 mddev->major_version = info->major_version;
5033 mddev->minor_version = info->minor_version;
5034 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005035 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 return 0;
5037 }
5038 mddev->major_version = MD_MAJOR_VERSION;
5039 mddev->minor_version = MD_MINOR_VERSION;
5040 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5041 mddev->ctime = get_seconds();
5042
5043 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005044 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005045 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 mddev->raid_disks = info->raid_disks;
5047 /* don't set md_minor, it is determined by which /dev/md* was
5048 * openned
5049 */
5050 if (info->state & (1<<MD_SB_CLEAN))
5051 mddev->recovery_cp = MaxSector;
5052 else
5053 mddev->recovery_cp = 0;
5054 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005055 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056
5057 mddev->layout = info->layout;
5058 mddev->chunk_size = info->chunk_size;
5059
5060 mddev->max_disks = MD_SB_DISKS;
5061
NeilBrowne6910632008-02-06 01:39:51 -08005062 if (mddev->persistent)
5063 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005064 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
NeilBrownb2a27032005-11-28 13:44:12 -08005066 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5067 mddev->bitmap_offset = 0;
5068
NeilBrownf6705572006-03-27 01:18:11 -08005069 mddev->reshape_position = MaxSector;
5070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 /*
5072 * Generate a 128 bit UUID
5073 */
5074 get_random_bytes(mddev->uuid, 16);
5075
NeilBrownf6705572006-03-27 01:18:11 -08005076 mddev->new_level = mddev->level;
5077 mddev->new_chunk = mddev->chunk_size;
5078 mddev->new_layout = mddev->layout;
5079 mddev->delta_disks = 0;
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 return 0;
5082}
5083
Dan Williams1f403622009-03-31 14:59:03 +11005084void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5085{
Dan Williamsb522adc2009-03-31 15:00:31 +11005086 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5087
5088 if (mddev->external_size)
5089 return;
5090
Dan Williams1f403622009-03-31 14:59:03 +11005091 mddev->array_sectors = array_sectors;
5092}
5093EXPORT_SYMBOL(md_set_array_sectors);
5094
Andre Nolld71f9f82008-07-11 22:02:22 +10005095static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005096{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005097 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005098 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005099 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005100
5101 if (mddev->pers->resize == NULL)
5102 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005103 /* The "num_sectors" is the number of sectors of each device that
5104 * is used. This can only make sense for arrays with redundancy.
5105 * linear and raid0 always use whatever space is available. We can only
5106 * consider changing this number if no resync or reconstruction is
5107 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005108 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005109 * of each device. If num_sectors is zero, we find the largest size
5110 * that fits.
5111
NeilBrowna35b0d62006-01-06 00:20:49 -08005112 */
5113 if (mddev->sync_thread)
5114 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005115 if (mddev->bitmap)
5116 /* Sorry, cannot grow a bitmap yet, just remove it,
5117 * grow, and re-add.
5118 */
5119 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005120 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005121 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005122
Andre Nolld71f9f82008-07-11 22:02:22 +10005123 if (fit && (num_sectors == 0 || num_sectors > avail))
5124 num_sectors = avail;
5125 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005126 return -ENOSPC;
5127 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005128 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08005129 if (!rv) {
5130 struct block_device *bdev;
5131
5132 bdev = bdget_disk(mddev->gendisk, 0);
5133 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005134 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10005135 i_size_write(bdev->bd_inode,
5136 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005137 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08005138 bdput(bdev);
5139 }
5140 }
5141 return rv;
5142}
5143
NeilBrownda943b992006-01-06 00:20:54 -08005144static int update_raid_disks(mddev_t *mddev, int raid_disks)
5145{
5146 int rv;
5147 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005148 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005149 return -EINVAL;
5150 if (raid_disks <= 0 ||
5151 raid_disks >= mddev->max_disks)
5152 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005153 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005154 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005155 mddev->delta_disks = raid_disks - mddev->raid_disks;
5156
5157 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005158 return rv;
5159}
5160
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162/*
5163 * update_array_info is used to change the configuration of an
5164 * on-line array.
5165 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5166 * fields in the info are checked against the array.
5167 * Any differences that cannot be handled will cause an error.
5168 * Normally, only one change can be managed at a time.
5169 */
5170static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5171{
5172 int rv = 0;
5173 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005174 int state = 0;
5175
5176 /* calculate expected state,ignoring low bits */
5177 if (mddev->bitmap && mddev->bitmap_offset)
5178 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
5180 if (mddev->major_version != info->major_version ||
5181 mddev->minor_version != info->minor_version ||
5182/* mddev->patch_version != info->patch_version || */
5183 mddev->ctime != info->ctime ||
5184 mddev->level != info->level ||
5185/* mddev->layout != info->layout || */
5186 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07005187 mddev->chunk_size != info->chunk_size ||
5188 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5189 ((state^info->state) & 0xfffffe00)
5190 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 return -EINVAL;
5192 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005193 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5194 cnt++;
5195 if (mddev->raid_disks != info->raid_disks)
5196 cnt++;
5197 if (mddev->layout != info->layout)
5198 cnt++;
5199 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5200 cnt++;
5201 if (cnt == 0)
5202 return 0;
5203 if (cnt > 1)
5204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
5206 if (mddev->layout != info->layout) {
5207 /* Change layout
5208 * we don't need to do anything at the md level, the
5209 * personality will take care of it all.
5210 */
5211 if (mddev->pers->reconfig == NULL)
5212 return -EINVAL;
5213 else
5214 return mddev->pers->reconfig(mddev, info->layout, -1);
5215 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005216 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005217 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
NeilBrownda943b992006-01-06 00:20:54 -08005219 if (mddev->raid_disks != info->raid_disks)
5220 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
NeilBrown36fa3062005-09-09 16:23:45 -07005222 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5223 if (mddev->pers->quiesce == NULL)
5224 return -EINVAL;
5225 if (mddev->recovery || mddev->sync_thread)
5226 return -EBUSY;
5227 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5228 /* add the bitmap */
5229 if (mddev->bitmap)
5230 return -EEXIST;
5231 if (mddev->default_bitmap_offset == 0)
5232 return -EINVAL;
5233 mddev->bitmap_offset = mddev->default_bitmap_offset;
5234 mddev->pers->quiesce(mddev, 1);
5235 rv = bitmap_create(mddev);
5236 if (rv)
5237 bitmap_destroy(mddev);
5238 mddev->pers->quiesce(mddev, 0);
5239 } else {
5240 /* remove the bitmap */
5241 if (!mddev->bitmap)
5242 return -ENOENT;
5243 if (mddev->bitmap->file)
5244 return -EINVAL;
5245 mddev->pers->quiesce(mddev, 1);
5246 bitmap_destroy(mddev);
5247 mddev->pers->quiesce(mddev, 0);
5248 mddev->bitmap_offset = 0;
5249 }
5250 }
NeilBrown850b2b42006-10-03 01:15:46 -07005251 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 return rv;
5253}
5254
5255static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5256{
5257 mdk_rdev_t *rdev;
5258
5259 if (mddev->pers == NULL)
5260 return -ENODEV;
5261
5262 rdev = find_rdev(mddev, dev);
5263 if (!rdev)
5264 return -ENODEV;
5265
5266 md_error(mddev, rdev);
5267 return 0;
5268}
5269
Andre Noll2f9618c2008-04-25 18:57:58 +02005270/*
5271 * We have a problem here : there is no easy way to give a CHS
5272 * virtual geometry. We currently pretend that we have a 2 heads
5273 * 4 sectors (with a BIG number of cylinders...). This drives
5274 * dosfs just mad... ;-)
5275 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005276static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5277{
5278 mddev_t *mddev = bdev->bd_disk->private_data;
5279
5280 geo->heads = 2;
5281 geo->sectors = 4;
5282 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5283 return 0;
5284}
5285
Al Viroa39907f2008-03-02 10:31:15 -05005286static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 unsigned int cmd, unsigned long arg)
5288{
5289 int err = 0;
5290 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 mddev_t *mddev = NULL;
5292
5293 if (!capable(CAP_SYS_ADMIN))
5294 return -EACCES;
5295
5296 /*
5297 * Commands dealing with the RAID driver but not any
5298 * particular array:
5299 */
5300 switch (cmd)
5301 {
5302 case RAID_VERSION:
5303 err = get_version(argp);
5304 goto done;
5305
5306 case PRINT_RAID_DEBUG:
5307 err = 0;
5308 md_print_devices();
5309 goto done;
5310
5311#ifndef MODULE
5312 case RAID_AUTORUN:
5313 err = 0;
5314 autostart_arrays(arg);
5315 goto done;
5316#endif
5317 default:;
5318 }
5319
5320 /*
5321 * Commands creating/starting a new array:
5322 */
5323
Al Viroa39907f2008-03-02 10:31:15 -05005324 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
5326 if (!mddev) {
5327 BUG();
5328 goto abort;
5329 }
5330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 err = mddev_lock(mddev);
5332 if (err) {
5333 printk(KERN_INFO
5334 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5335 err, cmd);
5336 goto abort;
5337 }
5338
5339 switch (cmd)
5340 {
5341 case SET_ARRAY_INFO:
5342 {
5343 mdu_array_info_t info;
5344 if (!arg)
5345 memset(&info, 0, sizeof(info));
5346 else if (copy_from_user(&info, argp, sizeof(info))) {
5347 err = -EFAULT;
5348 goto abort_unlock;
5349 }
5350 if (mddev->pers) {
5351 err = update_array_info(mddev, &info);
5352 if (err) {
5353 printk(KERN_WARNING "md: couldn't update"
5354 " array info. %d\n", err);
5355 goto abort_unlock;
5356 }
5357 goto done_unlock;
5358 }
5359 if (!list_empty(&mddev->disks)) {
5360 printk(KERN_WARNING
5361 "md: array %s already has disks!\n",
5362 mdname(mddev));
5363 err = -EBUSY;
5364 goto abort_unlock;
5365 }
5366 if (mddev->raid_disks) {
5367 printk(KERN_WARNING
5368 "md: array %s already initialised!\n",
5369 mdname(mddev));
5370 err = -EBUSY;
5371 goto abort_unlock;
5372 }
5373 err = set_array_info(mddev, &info);
5374 if (err) {
5375 printk(KERN_WARNING "md: couldn't set"
5376 " array info. %d\n", err);
5377 goto abort_unlock;
5378 }
5379 }
5380 goto done_unlock;
5381
5382 default:;
5383 }
5384
5385 /*
5386 * Commands querying/configuring an existing array:
5387 */
NeilBrown32a76272005-06-21 17:17:14 -07005388 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005389 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005390 if ((!mddev->raid_disks && !mddev->external)
5391 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5392 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5393 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 err = -ENODEV;
5395 goto abort_unlock;
5396 }
5397
5398 /*
5399 * Commands even a read-only array can execute:
5400 */
5401 switch (cmd)
5402 {
5403 case GET_ARRAY_INFO:
5404 err = get_array_info(mddev, argp);
5405 goto done_unlock;
5406
NeilBrown32a76272005-06-21 17:17:14 -07005407 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005408 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005409 goto done_unlock;
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 case GET_DISK_INFO:
5412 err = get_disk_info(mddev, argp);
5413 goto done_unlock;
5414
5415 case RESTART_ARRAY_RW:
5416 err = restart_array(mddev);
5417 goto done_unlock;
5418
5419 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005420 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 goto done_unlock;
5422
5423 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005424 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 goto done_unlock;
5426
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428
5429 /*
5430 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005431 * superblock, so we do not allow them on read-only arrays.
5432 * However non-MD ioctls (e.g. get-size) will still come through
5433 * here and hit the 'default' below, so only disallow
5434 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005436 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005437 if (mddev->ro == 2) {
5438 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005439 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005440 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5441 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005442 } else {
5443 err = -EROFS;
5444 goto abort_unlock;
5445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 }
5447
5448 switch (cmd)
5449 {
5450 case ADD_NEW_DISK:
5451 {
5452 mdu_disk_info_t info;
5453 if (copy_from_user(&info, argp, sizeof(info)))
5454 err = -EFAULT;
5455 else
5456 err = add_new_disk(mddev, &info);
5457 goto done_unlock;
5458 }
5459
5460 case HOT_REMOVE_DISK:
5461 err = hot_remove_disk(mddev, new_decode_dev(arg));
5462 goto done_unlock;
5463
5464 case HOT_ADD_DISK:
5465 err = hot_add_disk(mddev, new_decode_dev(arg));
5466 goto done_unlock;
5467
5468 case SET_DISK_FAULTY:
5469 err = set_disk_faulty(mddev, new_decode_dev(arg));
5470 goto done_unlock;
5471
5472 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005473 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 goto done_unlock;
5475
NeilBrown32a76272005-06-21 17:17:14 -07005476 case SET_BITMAP_FILE:
5477 err = set_bitmap_file(mddev, (int)arg);
5478 goto done_unlock;
5479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 err = -EINVAL;
5482 goto abort_unlock;
5483 }
5484
5485done_unlock:
5486abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005487 if (mddev->hold_active == UNTIL_IOCTL &&
5488 err != -EINVAL)
5489 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 mddev_unlock(mddev);
5491
5492 return err;
5493done:
5494 if (err)
5495 MD_BUG();
5496abort:
5497 return err;
5498}
5499
Al Viroa39907f2008-03-02 10:31:15 -05005500static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501{
5502 /*
5503 * Succeed if we can lock the mddev, which confirms that
5504 * it isn't being stopped right now.
5505 */
NeilBrownd3374822009-01-09 08:31:10 +11005506 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 int err;
5508
NeilBrownd3374822009-01-09 08:31:10 +11005509 if (mddev->gendisk != bdev->bd_disk) {
5510 /* we are racing with mddev_put which is discarding this
5511 * bd_disk.
5512 */
5513 mddev_put(mddev);
5514 /* Wait until bdev->bd_disk is definitely gone */
5515 flush_scheduled_work();
5516 /* Then retry the open from the top */
5517 return -ERESTARTSYS;
5518 }
5519 BUG_ON(mddev != bdev->bd_disk->private_data);
5520
NeilBrownd63a5a72006-12-08 02:36:17 -08005521 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 goto out;
5523
5524 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005525 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 mddev_unlock(mddev);
5527
Al Viroa39907f2008-03-02 10:31:15 -05005528 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 out:
5530 return err;
5531}
5532
Al Viroa39907f2008-03-02 10:31:15 -05005533static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534{
Al Viroa39907f2008-03-02 10:31:15 -05005535 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005537 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005538 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 mddev_put(mddev);
5540
5541 return 0;
5542}
5543
Linus Torvalds44ce62942007-05-09 18:51:36 -07005544static int md_media_changed(struct gendisk *disk)
5545{
5546 mddev_t *mddev = disk->private_data;
5547
5548 return mddev->changed;
5549}
5550
5551static int md_revalidate(struct gendisk *disk)
5552{
5553 mddev_t *mddev = disk->private_data;
5554
5555 mddev->changed = 0;
5556 return 0;
5557}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558static struct block_device_operations md_fops =
5559{
5560 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005561 .open = md_open,
5562 .release = md_release,
5563 .locked_ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005564 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005565 .media_changed = md_media_changed,
5566 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567};
5568
Adrian Bunk75c96f82005-05-05 16:16:09 -07005569static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570{
5571 mdk_thread_t *thread = arg;
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 /*
5574 * md_thread is a 'system-thread', it's priority should be very
5575 * high. We avoid resource deadlocks individually in each
5576 * raid personality. (RAID5 does preallocation) We also use RR and
5577 * the very same RT priority as kswapd, thus we will never get
5578 * into a priority inversion deadlock.
5579 *
5580 * we definitely have to have equal or higher priority than
5581 * bdflush, otherwise bdflush will deadlock if there are too
5582 * many dirty RAID5 blocks.
5583 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
NeilBrown6985c432005-10-19 21:23:47 -07005585 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005586 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
NeilBrown93588e22005-11-15 00:09:12 -08005588 /* We need to wait INTERRUPTIBLE so that
5589 * we don't add to the load-average.
5590 * That means we need to be sure no signals are
5591 * pending
5592 */
5593 if (signal_pending(current))
5594 flush_signals(current);
5595
5596 wait_event_interruptible_timeout
5597 (thread->wqueue,
5598 test_bit(THREAD_WAKEUP, &thread->flags)
5599 || kthread_should_stop(),
5600 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 clear_bit(THREAD_WAKEUP, &thread->flags);
5603
NeilBrown787453c2005-11-08 21:39:43 -08005604 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 return 0;
5608}
5609
5610void md_wakeup_thread(mdk_thread_t *thread)
5611{
5612 if (thread) {
5613 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5614 set_bit(THREAD_WAKEUP, &thread->flags);
5615 wake_up(&thread->wqueue);
5616 }
5617}
5618
5619mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5620 const char *name)
5621{
5622 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
NeilBrown9ffae0c2006-01-06 00:20:32 -08005624 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 if (!thread)
5626 return NULL;
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 init_waitqueue_head(&thread->wqueue);
5629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 thread->run = run;
5631 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005632 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005633 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005634 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 kfree(thread);
5636 return NULL;
5637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 return thread;
5639}
5640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641void md_unregister_thread(mdk_thread_t *thread)
5642{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005643 if (!thread)
5644 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005645 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005646
5647 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 kfree(thread);
5649}
5650
5651void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5652{
5653 if (!mddev) {
5654 MD_BUG();
5655 return;
5656 }
5657
NeilBrownb2d444d2005-11-08 21:39:31 -08005658 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005660
5661 if (mddev->external)
5662 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005663/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5665 mdname(mddev),
5666 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5667 __builtin_return_address(0),__builtin_return_address(1),
5668 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005669*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005670 if (!mddev->pers)
5671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 if (!mddev->pers->error_handler)
5673 return;
5674 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005675 if (mddev->degraded)
5676 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005677 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5679 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5680 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005681 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682}
5683
5684/* seq_file implementation /proc/mdstat */
5685
5686static void status_unused(struct seq_file *seq)
5687{
5688 int i = 0;
5689 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
5691 seq_printf(seq, "unused devices: ");
5692
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005693 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 char b[BDEVNAME_SIZE];
5695 i++;
5696 seq_printf(seq, "%s ",
5697 bdevname(rdev->bdev,b));
5698 }
5699 if (!i)
5700 seq_printf(seq, "<none>");
5701
5702 seq_printf(seq, "\n");
5703}
5704
5705
5706static void status_resync(struct seq_file *seq, mddev_t * mddev)
5707{
NeilBrowndd71cf62009-05-07 12:49:35 +10005708 sector_t max_sectors, resync, res;
5709 unsigned long dt, db;
5710 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005711 int scale;
5712 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713
NeilBrowndd71cf62009-05-07 12:49:35 +10005714 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715
5716 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005717 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005719 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 /*
5722 * Should not happen.
5723 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005724 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 MD_BUG();
5726 return;
5727 }
NeilBrown4588b422006-03-27 01:18:04 -08005728 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005729 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005730 * u32, as those are the requirements for sector_div.
5731 * Thus 'scale' must be at least 10
5732 */
5733 scale = 10;
5734 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005735 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005736 scale++;
5737 }
5738 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005739 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005740
5741 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 {
NeilBrown4588b422006-03-27 01:18:04 -08005743 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 seq_printf(seq, "[");
5745 for (i = 0; i < x; i++)
5746 seq_printf(seq, "=");
5747 seq_printf(seq, ">");
5748 for (i = 0; i < y; i++)
5749 seq_printf(seq, ".");
5750 seq_printf(seq, "] ");
5751 }
NeilBrown4588b422006-03-27 01:18:04 -08005752 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005753 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5754 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005755 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5756 "check" :
5757 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5758 "resync" : "recovery"))),
5759 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005760 (unsigned long long) resync/2,
5761 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 * dt: time from mark until now
5765 * db: blocks written from mark until now
5766 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005767 *
5768 * rt is a sector_t, so could be 32bit or 64bit.
5769 * So we divide before multiply in case it is 32bit and close
5770 * to the limit.
5771 * We scale the divisor (db) by 32 to avoid loosing precision
5772 * near the end of resync when the number of remaining sectors
5773 * is close to 'db'.
5774 * We then divide rt by 32 after multiplying by db to compensate.
5775 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 */
5777 dt = ((jiffies - mddev->resync_mark) / HZ);
5778 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005779 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5780 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
NeilBrowndd71cf62009-05-07 12:49:35 +10005782 rt = max_sectors - resync; /* number of remaining sectors */
5783 sector_div(rt, db/32+1);
5784 rt *= dt;
5785 rt >>= 5;
5786
5787 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5788 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
NeilBrownff4e8d92006-07-10 04:44:16 -07005790 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791}
5792
5793static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5794{
5795 struct list_head *tmp;
5796 loff_t l = *pos;
5797 mddev_t *mddev;
5798
5799 if (l >= 0x10000)
5800 return NULL;
5801 if (!l--)
5802 /* header */
5803 return (void*)1;
5804
5805 spin_lock(&all_mddevs_lock);
5806 list_for_each(tmp,&all_mddevs)
5807 if (!l--) {
5808 mddev = list_entry(tmp, mddev_t, all_mddevs);
5809 mddev_get(mddev);
5810 spin_unlock(&all_mddevs_lock);
5811 return mddev;
5812 }
5813 spin_unlock(&all_mddevs_lock);
5814 if (!l--)
5815 return (void*)2;/* tail */
5816 return NULL;
5817}
5818
5819static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5820{
5821 struct list_head *tmp;
5822 mddev_t *next_mddev, *mddev = v;
5823
5824 ++*pos;
5825 if (v == (void*)2)
5826 return NULL;
5827
5828 spin_lock(&all_mddevs_lock);
5829 if (v == (void*)1)
5830 tmp = all_mddevs.next;
5831 else
5832 tmp = mddev->all_mddevs.next;
5833 if (tmp != &all_mddevs)
5834 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5835 else {
5836 next_mddev = (void*)2;
5837 *pos = 0x10000;
5838 }
5839 spin_unlock(&all_mddevs_lock);
5840
5841 if (v != (void*)1)
5842 mddev_put(mddev);
5843 return next_mddev;
5844
5845}
5846
5847static void md_seq_stop(struct seq_file *seq, void *v)
5848{
5849 mddev_t *mddev = v;
5850
5851 if (mddev && v != (void*)1 && v != (void*)2)
5852 mddev_put(mddev);
5853}
5854
NeilBrownd7603b72006-01-06 00:20:30 -08005855struct mdstat_info {
5856 int event;
5857};
5858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859static int md_seq_show(struct seq_file *seq, void *v)
5860{
5861 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005862 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005864 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005865 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005868 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 seq_printf(seq, "Personalities : ");
5870 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005871 list_for_each_entry(pers, &pers_list, list)
5872 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874 spin_unlock(&pers_lock);
5875 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005876 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 return 0;
5878 }
5879 if (v == (void*)2) {
5880 status_unused(seq);
5881 return 0;
5882 }
5883
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005884 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005886
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5888 seq_printf(seq, "%s : %sactive", mdname(mddev),
5889 mddev->pers ? "" : "in");
5890 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005891 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005893 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005894 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 seq_printf(seq, " %s", mddev->pers->name);
5896 }
5897
Andre Nolldd8ac332009-03-31 14:33:13 +11005898 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005899 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 char b[BDEVNAME_SIZE];
5901 seq_printf(seq, " %s[%d]",
5902 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005903 if (test_bit(WriteMostly, &rdev->flags))
5904 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005905 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 seq_printf(seq, "(F)");
5907 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005908 } else if (rdev->raid_disk < 0)
5909 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005910 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
5912
5913 if (!list_empty(&mddev->disks)) {
5914 if (mddev->pers)
5915 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005916 (unsigned long long)
5917 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 else
5919 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005920 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005922 if (mddev->persistent) {
5923 if (mddev->major_version != 0 ||
5924 mddev->minor_version != 90) {
5925 seq_printf(seq," super %d.%d",
5926 mddev->major_version,
5927 mddev->minor_version);
5928 }
NeilBrowne6910632008-02-06 01:39:51 -08005929 } else if (mddev->external)
5930 seq_printf(seq, " super external:%s",
5931 mddev->metadata_type);
5932 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005933 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
5935 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005936 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005938 if (mddev->pers->sync_request) {
5939 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005940 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005941 seq_printf(seq, "\n ");
5942 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5943 seq_printf(seq, "\tresync=DELAYED\n ");
5944 else if (mddev->recovery_cp < MaxSector)
5945 seq_printf(seq, "\tresync=PENDING\n ");
5946 }
NeilBrown32a76272005-06-21 17:17:14 -07005947 } else
5948 seq_printf(seq, "\n ");
5949
5950 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005951 unsigned long chunk_kb;
5952 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005953 spin_lock_irqsave(&bitmap->lock, flags);
5954 chunk_kb = bitmap->chunksize >> 10;
5955 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5956 "%lu%s chunk",
5957 bitmap->pages - bitmap->missing_pages,
5958 bitmap->pages,
5959 (bitmap->pages - bitmap->missing_pages)
5960 << (PAGE_SHIFT - 10),
5961 chunk_kb ? chunk_kb : bitmap->chunksize,
5962 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005963 if (bitmap->file) {
5964 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005965 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005966 }
NeilBrown78d742d2005-06-21 17:17:15 -07005967
NeilBrown32a76272005-06-21 17:17:14 -07005968 seq_printf(seq, "\n");
5969 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 }
5971
5972 seq_printf(seq, "\n");
5973 }
5974 mddev_unlock(mddev);
5975
5976 return 0;
5977}
5978
Jan Engelhardt110518b2009-05-07 12:49:37 +10005979static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 .start = md_seq_start,
5981 .next = md_seq_next,
5982 .stop = md_seq_stop,
5983 .show = md_seq_show,
5984};
5985
5986static int md_seq_open(struct inode *inode, struct file *file)
5987{
5988 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005989 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5990 if (mi == NULL)
5991 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
5993 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005994 if (error)
5995 kfree(mi);
5996 else {
5997 struct seq_file *p = file->private_data;
5998 p->private = mi;
5999 mi->event = atomic_read(&md_event_count);
6000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 return error;
6002}
6003
NeilBrownd7603b72006-01-06 00:20:30 -08006004static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6005{
6006 struct seq_file *m = filp->private_data;
6007 struct mdstat_info *mi = m->private;
6008 int mask;
6009
6010 poll_wait(filp, &md_event_waiters, wait);
6011
6012 /* always allow read */
6013 mask = POLLIN | POLLRDNORM;
6014
6015 if (mi->event != atomic_read(&md_event_count))
6016 mask |= POLLERR | POLLPRI;
6017 return mask;
6018}
6019
Arjan van de Venfa027c22007-02-12 00:55:33 -08006020static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006021 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 .open = md_seq_open,
6023 .read = seq_read,
6024 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006025 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006026 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027};
6028
NeilBrown2604b702006-01-06 00:20:36 -08006029int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006032 list_add_tail(&p->list, &pers_list);
6033 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 spin_unlock(&pers_lock);
6035 return 0;
6036}
6037
NeilBrown2604b702006-01-06 00:20:36 -08006038int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039{
NeilBrown2604b702006-01-06 00:20:36 -08006040 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006042 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 spin_unlock(&pers_lock);
6044 return 0;
6045}
6046
NeilBrowneea1bf32009-03-31 14:27:02 +11006047static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048{
6049 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006051 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
6053 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006054 rcu_read_lock();
6055 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006057 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6058 (int)part_stat_read(&disk->part0, sectors[1]) -
6059 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006060 /* sync IO will cause sync_io to increase before the disk_stats
6061 * as sync_io is counted when a request starts, and
6062 * disk_stats is counted when it completes.
6063 * So resync activity will cause curr_events to be smaller than
6064 * when there was no such activity.
6065 * non-sync IO will cause disk_stat to increase without
6066 * increasing sync_io so curr_events will (eventually)
6067 * be larger than it was before. Once it becomes
6068 * substantially larger, the test below will cause
6069 * the array to appear non-idle, and resync will slow
6070 * down.
6071 * If there is a lot of outstanding resync activity when
6072 * we set last_event to curr_events, then all that activity
6073 * completing might cause the array to appear non-idle
6074 * and resync will be slowed down even though there might
6075 * not have been non-resync activity. This will only
6076 * happen once though. 'last_events' will soon reflect
6077 * the state where there is little or no outstanding
6078 * resync requests, and further resync activity will
6079 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006080 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006082 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 rdev->last_events = curr_events;
6084 idle = 0;
6085 }
6086 }
NeilBrown4b809912008-07-21 17:05:25 +10006087 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 return idle;
6089}
6090
6091void md_done_sync(mddev_t *mddev, int blocks, int ok)
6092{
6093 /* another "blocks" (512byte) blocks have been synced */
6094 atomic_sub(blocks, &mddev->recovery_active);
6095 wake_up(&mddev->recovery_wait);
6096 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006097 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 md_wakeup_thread(mddev->thread);
6099 // stop recovery, signal do_sync ....
6100 }
6101}
6102
6103
NeilBrown06d91a52005-06-21 17:17:12 -07006104/* md_write_start(mddev, bi)
6105 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006106 * in superblock) before writing, schedule a superblock update
6107 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006108 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006109void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110{
Neil Brown0fd62b82008-06-28 08:31:36 +10006111 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006112 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006113 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006114
NeilBrownf91de922005-11-08 21:39:36 -08006115 BUG_ON(mddev->ro == 1);
6116 if (mddev->ro == 2) {
6117 /* need to switch to read/write */
6118 mddev->ro = 0;
6119 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6120 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006121 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006122 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006123 }
NeilBrown06d91a52005-06-21 17:17:12 -07006124 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006125 if (mddev->safemode == 1)
6126 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006127 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006128 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006129 if (mddev->in_sync) {
6130 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006131 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006132 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006133 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006134 }
NeilBrowna9701a32005-11-08 21:39:34 -08006135 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006136 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006137 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006138 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006139 wait_event(mddev->sb_wait,
6140 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6141 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142}
6143
6144void md_write_end(mddev_t *mddev)
6145{
6146 if (atomic_dec_and_test(&mddev->writes_pending)) {
6147 if (mddev->safemode == 2)
6148 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006149 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6151 }
6152}
6153
NeilBrown2a2275d2007-01-26 00:57:11 -08006154/* md_allow_write(mddev)
6155 * Calling this ensures that the array is marked 'active' so that writes
6156 * may proceed without blocking. It is important to call this before
6157 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6158 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006159 *
6160 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6161 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006162 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006163int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006164{
6165 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006166 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006167 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006168 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006169 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006170 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006171
6172 spin_lock_irq(&mddev->write_lock);
6173 if (mddev->in_sync) {
6174 mddev->in_sync = 0;
6175 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6176 if (mddev->safemode_delay &&
6177 mddev->safemode == 0)
6178 mddev->safemode = 1;
6179 spin_unlock_irq(&mddev->write_lock);
6180 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006181 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006182 } else
6183 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006184
6185 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6186 return -EAGAIN;
6187 else
6188 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006189}
6190EXPORT_SYMBOL_GPL(md_allow_write);
6191
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192#define SYNC_MARKS 10
6193#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006194void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195{
6196 mddev_t *mddev2;
6197 unsigned int currspeed = 0,
6198 window;
NeilBrown57afd892005-06-21 17:17:13 -07006199 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 unsigned long mark[SYNC_MARKS];
6201 sector_t mark_cnt[SYNC_MARKS];
6202 int last_mark,m;
6203 struct list_head *tmp;
6204 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006205 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006206 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006207 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
6209 /* just incase thread restarts... */
6210 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6211 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006212 if (mddev->ro) /* never try to sync a read-only array */
6213 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
NeilBrown61df9d92006-10-03 01:15:57 -07006215 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6216 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6217 desc = "data-check";
6218 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6219 desc = "requested-resync";
6220 else
6221 desc = "resync";
6222 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6223 desc = "reshape";
6224 else
6225 desc = "recovery";
6226
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 /* we overload curr_resync somewhat here.
6228 * 0 == not engaged in resync at all
6229 * 2 == checking that there is no conflict with another sync
6230 * 1 == like 2, but have yielded to allow conflicting resync to
6231 * commense
6232 * other == active in resync - this many blocks
6233 *
6234 * Before starting a resync we must have set curr_resync to
6235 * 2, and then checked that every "conflicting" array has curr_resync
6236 * less than ours. When we find one that is the same or higher
6237 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6238 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6239 * This will mean we have to start checking from the beginning again.
6240 *
6241 */
6242
6243 do {
6244 mddev->curr_resync = 2;
6245
6246 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006247 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006248 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 goto skip;
6250 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006251 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 if (mddev2 == mddev)
6253 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006254 if (!mddev->parallel_resync
6255 && mddev2->curr_resync
6256 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 DEFINE_WAIT(wq);
6258 if (mddev < mddev2 && mddev->curr_resync == 2) {
6259 /* arbitrarily yield */
6260 mddev->curr_resync = 1;
6261 wake_up(&resync_wait);
6262 }
6263 if (mddev > mddev2 && mddev->curr_resync == 1)
6264 /* no need to wait here, we can wait the next
6265 * time 'round when curr_resync == 2
6266 */
6267 continue;
NeilBrown97441972008-09-19 11:49:54 +10006268 /* We need to wait 'interruptible' so as not to
6269 * contribute to the load average, and not to
6270 * be caught by 'softlockup'
6271 */
6272 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006273 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006274 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006275 printk(KERN_INFO "md: delaying %s of %s"
6276 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006278 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006280 if (signal_pending(current))
6281 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 schedule();
6283 finish_wait(&resync_wait, &wq);
6284 goto try_again;
6285 }
6286 finish_wait(&resync_wait, &wq);
6287 }
6288 }
6289 } while (mddev->curr_resync < 2);
6290
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006291 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006292 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006294 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 */
6296 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006297 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006298 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006299 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6300 j = mddev->resync_min;
6301 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006302 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006303
NeilBrownccfcc3c2006-03-27 01:18:09 -08006304 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006305 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006306 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006308 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006309 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006310 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006311 if (rdev->raid_disk >= 0 &&
6312 !test_bit(Faulty, &rdev->flags) &&
6313 !test_bit(In_sync, &rdev->flags) &&
6314 rdev->recovery_offset < j)
6315 j = rdev->recovery_offset;
6316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
NeilBrown61df9d92006-10-03 01:15:57 -07006318 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6319 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6320 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006321 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006322 "(but not more than %d KB/sec) for %s.\n",
6323 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
NeilBrowneea1bf32009-03-31 14:27:02 +11006325 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006326
NeilBrown57afd892005-06-21 17:17:13 -07006327 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 for (m = 0; m < SYNC_MARKS; m++) {
6329 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006330 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 }
6332 last_mark = 0;
6333 mddev->resync_mark = mark[last_mark];
6334 mddev->resync_mark_cnt = mark_cnt[last_mark];
6335
6336 /*
6337 * Tune reconstruction:
6338 */
6339 window = 32*(PAGE_SIZE/512);
6340 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6341 window/2,(unsigned long long) max_sectors/2);
6342
6343 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 last_check = 0;
6345
6346 if (j>2) {
6347 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006348 "md: resuming %s of %s from checkpoint.\n",
6349 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 mddev->curr_resync = j;
6351 }
6352
6353 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006354 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
NeilBrown57afd892005-06-21 17:17:13 -07006356 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006357
NeilBrownacb180b2009-04-14 16:28:34 +10006358 if ((mddev->curr_resync > mddev->curr_resync_completed &&
6359 (mddev->curr_resync - mddev->curr_resync_completed)
6360 > (max_sectors >> 4)) ||
NeilBrownc03f6a12009-04-17 11:06:30 +10006361 (j - mddev->curr_resync_completed)*2
6362 >= mddev->resync_max - mddev->curr_resync_completed
NeilBrownacb180b2009-04-14 16:28:34 +10006363 ) {
NeilBrown97e4f422009-03-31 14:33:13 +11006364 /* time to update curr_resync_completed */
6365 blk_unplug(mddev->queue);
6366 wait_event(mddev->recovery_wait,
6367 atomic_read(&mddev->recovery_active) == 0);
6368 mddev->curr_resync_completed =
6369 mddev->curr_resync;
6370 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006371 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006372 }
NeilBrownacb180b2009-04-14 16:28:34 +10006373
6374 if (j >= mddev->resync_max)
6375 wait_event(mddev->recovery_wait,
6376 mddev->resync_max > j
6377 || kthread_should_stop());
6378
6379 if (kthread_should_stop())
6380 goto interrupted;
6381
NeilBrown57afd892005-06-21 17:17:13 -07006382 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006383 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006384 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006385 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 goto out;
6387 }
NeilBrown57afd892005-06-21 17:17:13 -07006388
6389 if (!skipped) { /* actual IO requested */
6390 io_sectors += sectors;
6391 atomic_add(sectors, &mddev->recovery_active);
6392 }
6393
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 j += sectors;
6395 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006396 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006397 if (last_check == 0)
6398 /* this is the earliers that rebuilt will be
6399 * visible in /proc/mdstat
6400 */
6401 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006402
6403 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 continue;
6405
NeilBrown57afd892005-06-21 17:17:13 -07006406 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
NeilBrowndfc70642008-05-23 13:04:39 -07006408 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 break;
6410
6411 repeat:
6412 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6413 /* step marks */
6414 int next = (last_mark+1) % SYNC_MARKS;
6415
6416 mddev->resync_mark = mark[next];
6417 mddev->resync_mark_cnt = mark_cnt[next];
6418 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006419 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 last_mark = next;
6421 }
6422
6423
NeilBrownc6207272008-02-06 01:39:52 -08006424 if (kthread_should_stop())
6425 goto interrupted;
6426
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
6428 /*
6429 * this loop exits only if either when we are slower than
6430 * the 'hard' speed limit, or the system was IO-idle for
6431 * a jiffy.
6432 * the system might be non-idle CPU-wise, but we only care
6433 * about not overloading the IO subsystem. (things like an
6434 * e2fsck being done on the RAID array should execute fast)
6435 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006436 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 cond_resched();
6438
NeilBrown57afd892005-06-21 17:17:13 -07006439 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6440 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441
NeilBrown88202a02006-01-06 00:21:36 -08006442 if (currspeed > speed_min(mddev)) {
6443 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006444 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006445 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 goto repeat;
6447 }
6448 }
6449 }
NeilBrown61df9d92006-10-03 01:15:57 -07006450 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 /*
6452 * this also signals 'finished resyncing' to md_stop
6453 */
6454 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006455 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456
6457 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6458
6459 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006460 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461
NeilBrowndfc70642008-05-23 13:04:39 -07006462 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006463 mddev->curr_resync > 2) {
6464 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6465 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6466 if (mddev->curr_resync >= mddev->recovery_cp) {
6467 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006468 "md: checkpointing %s of %s.\n",
6469 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006470 mddev->recovery_cp = mddev->curr_resync;
6471 }
6472 } else
6473 mddev->recovery_cp = MaxSector;
6474 } else {
6475 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6476 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006477 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006478 if (rdev->raid_disk >= 0 &&
6479 !test_bit(Faulty, &rdev->flags) &&
6480 !test_bit(In_sync, &rdev->flags) &&
6481 rdev->recovery_offset < mddev->curr_resync)
6482 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 }
NeilBrown17571282006-12-10 02:20:52 -08006485 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 skip:
6488 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006489 mddev->curr_resync_completed = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006490 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006491 mddev->resync_max = MaxSector;
6492 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 wake_up(&resync_wait);
6494 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6495 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006496 return;
6497
6498 interrupted:
6499 /*
6500 * got a signal, exit.
6501 */
6502 printk(KERN_INFO
6503 "md: md_do_sync() got signal ... exiting\n");
6504 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6505 goto out;
6506
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507}
NeilBrown29269552006-03-27 01:18:10 -08006508EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
6510
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006511static int remove_and_add_spares(mddev_t *mddev)
6512{
6513 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006514 int spares = 0;
6515
NeilBrown97e4f422009-03-31 14:33:13 +11006516 mddev->curr_resync_completed = 0;
6517
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006518 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006519 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006520 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006521 (test_bit(Faulty, &rdev->flags) ||
6522 ! test_bit(In_sync, &rdev->flags)) &&
6523 atomic_read(&rdev->nr_pending)==0) {
6524 if (mddev->pers->hot_remove_disk(
6525 mddev, rdev->raid_disk)==0) {
6526 char nm[20];
6527 sprintf(nm,"rd%d", rdev->raid_disk);
6528 sysfs_remove_link(&mddev->kobj, nm);
6529 rdev->raid_disk = -1;
6530 }
6531 }
6532
NeilBrown4044ba52009-01-09 08:31:11 +11006533 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006534 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006535 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006536 !test_bit(In_sync, &rdev->flags) &&
6537 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006538 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006539 if (rdev->raid_disk < 0
6540 && !test_bit(Faulty, &rdev->flags)) {
6541 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006542 if (mddev->pers->
6543 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006544 char nm[20];
6545 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006546 if (sysfs_create_link(&mddev->kobj,
6547 &rdev->kobj, nm))
6548 printk(KERN_WARNING
6549 "md: cannot register "
6550 "%s for %s\n",
6551 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006552 spares++;
6553 md_new_event(mddev);
6554 } else
6555 break;
6556 }
NeilBrowndfc70642008-05-23 13:04:39 -07006557 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006558 }
6559 return spares;
6560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561/*
6562 * This routine is regularly called by all per-raid-array threads to
6563 * deal with generic issues like resync and super-block update.
6564 * Raid personalities that don't have a thread (linear/raid0) do not
6565 * need this as they never do any recovery or update the superblock.
6566 *
6567 * It does not do any resync itself, but rather "forks" off other threads
6568 * to do that as needed.
6569 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6570 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006571 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 * and wakeups up this thread which will reap the thread and finish up.
6573 * This thread also removes any faulty devices (with nr_pending == 0).
6574 *
6575 * The overall approach is:
6576 * 1/ if the superblock needs updating, update it.
6577 * 2/ If a recovery thread is running, don't do anything else.
6578 * 3/ If recovery has finished, clean up, possibly marking spares active.
6579 * 4/ If there are any faulty devices, remove them.
6580 * 5/ If array is degraded, try to add spares devices
6581 * 6/ If array has spares or is not in-sync, start a resync thread.
6582 */
6583void md_check_recovery(mddev_t *mddev)
6584{
6585 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
6587
NeilBrown5f404022005-06-21 17:17:16 -07006588 if (mddev->bitmap)
6589 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
6591 if (mddev->ro)
6592 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006593
6594 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006595 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006596 printk(KERN_INFO "md: %s in immediate safe mode\n",
6597 mdname(mddev));
6598 mddev->safemode = 2;
6599 }
6600 flush_signals(current);
6601 }
6602
NeilBrownc89a8ee2008-08-05 15:54:13 +10006603 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006606 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006608 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006609 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006610 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6611 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 ))
6613 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006614
NeilBrowndf5b89b2006-03-27 01:18:20 -08006615 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006616 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006617
NeilBrownc89a8ee2008-08-05 15:54:13 +10006618 if (mddev->ro) {
6619 /* Only thing we do on a ro array is remove
6620 * failed devices.
6621 */
6622 remove_and_add_spares(mddev);
6623 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6624 goto unlock;
6625 }
6626
NeilBrown31a59e32008-04-30 00:52:30 -07006627 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006628 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006629 spin_lock_irq(&mddev->write_lock);
6630 if (mddev->safemode &&
6631 !atomic_read(&mddev->writes_pending) &&
6632 !mddev->in_sync &&
6633 mddev->recovery_cp == MaxSector) {
6634 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006635 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006636 if (mddev->persistent)
6637 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6638 }
6639 if (mddev->safemode == 1)
6640 mddev->safemode = 0;
6641 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006642 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006643 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006644 }
NeilBrownfca4d842005-06-21 17:17:11 -07006645
NeilBrown850b2b42006-10-03 01:15:46 -07006646 if (mddev->flags)
6647 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006648
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006649 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006650 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006651 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006652
NeilBrown06d91a52005-06-21 17:17:12 -07006653
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6655 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6656 /* resync/recovery still happening */
6657 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6658 goto unlock;
6659 }
6660 if (mddev->sync_thread) {
6661 /* resync has finished, collect result */
6662 md_unregister_thread(mddev->sync_thread);
6663 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006664 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6665 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 /* success...*/
6667 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006668 if (mddev->pers->spare_active(mddev))
6669 sysfs_notify(&mddev->kobj, NULL,
6670 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 }
NeilBrowncea9c222009-03-31 15:15:05 +11006672 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6673 mddev->pers->finish_reshape)
6674 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006675 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006676
6677 /* if array is no-longer degraded, then any saved_raid_disk
6678 * information must be scrapped
6679 */
6680 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006681 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006682 rdev->saved_raid_disk = -1;
6683
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 mddev->recovery = 0;
6685 /* flag recovery needed just to double check */
6686 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006687 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006688 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689 goto unlock;
6690 }
Neil Brown72a23c22008-06-28 08:31:41 +10006691 /* Set RUNNING before clearing NEEDED to avoid
6692 * any transients in the value of "sync_action".
6693 */
6694 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6695 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006696 /* Clear some bits that don't mean anything, but
6697 * might be left set
6698 */
NeilBrown24dd4692005-11-08 21:39:26 -08006699 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6700 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006702 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6703 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 /* no recovery is running.
6705 * remove any failed drives, then
6706 * add spares if possible.
6707 * Spare are also removed and re-added, to allow
6708 * the personality to fail the re-add.
6709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006711 if (mddev->reshape_position != MaxSector) {
6712 if (mddev->pers->check_reshape(mddev) != 0)
6713 /* Cannot proceed */
6714 goto unlock;
6715 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006716 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006717 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006718 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6719 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006720 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006721 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006722 } else if (mddev->recovery_cp < MaxSector) {
6723 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006724 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006725 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6726 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006728
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006730 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6731 /* We are adding a device or devices to an array
6732 * which has the bitmap stored on all devices.
6733 * So make sure all bitmap pages get written
6734 */
6735 bitmap_write_all(mddev->bitmap);
6736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 mddev->sync_thread = md_register_thread(md_do_sync,
6738 mddev,
6739 "%s_resync");
6740 if (!mddev->sync_thread) {
6741 printk(KERN_ERR "%s: could not start resync"
6742 " thread...\n",
6743 mdname(mddev));
6744 /* leave the spares where they are, it shouldn't hurt */
6745 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006746 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006748 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006749 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 }
6751 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006752 if (!mddev->sync_thread) {
6753 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6754 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6755 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006756 if (mddev->sysfs_action)
6757 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 mddev_unlock(mddev);
6760 }
6761}
6762
Dan Williams6bfe0b42008-04-30 00:52:32 -07006763void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6764{
NeilBrown3c0ee632008-10-21 13:25:28 +11006765 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006766 wait_event_timeout(rdev->blocked_wait,
6767 !test_bit(Blocked, &rdev->flags),
6768 msecs_to_jiffies(5000));
6769 rdev_dec_pending(rdev, mddev);
6770}
6771EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6772
Adrian Bunk75c96f82005-05-05 16:16:09 -07006773static int md_notify_reboot(struct notifier_block *this,
6774 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775{
6776 struct list_head *tmp;
6777 mddev_t *mddev;
6778
6779 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6780
6781 printk(KERN_INFO "md: stopping all md devices.\n");
6782
NeilBrown29ac4aa2008-02-06 01:39:58 -08006783 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006784 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006785 /* Force a switch to readonly even array
6786 * appears to still be in use. Hence
6787 * the '100'.
6788 */
NeilBrownd710e132008-10-13 11:55:12 +11006789 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006790 mddev_unlock(mddev);
6791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 /*
6793 * certain more exotic SCSI devices are known to be
6794 * volatile wrt too early system reboots. While the
6795 * right place to handle this issue is the given
6796 * driver, we do want to have a safe RAID driver ...
6797 */
6798 mdelay(1000*1);
6799 }
6800 return NOTIFY_DONE;
6801}
6802
Adrian Bunk75c96f82005-05-05 16:16:09 -07006803static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 .notifier_call = md_notify_reboot,
6805 .next = NULL,
6806 .priority = INT_MAX, /* before any real devices */
6807};
6808
6809static void md_geninit(void)
6810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6812
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006813 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814}
6815
Adrian Bunk75c96f82005-05-05 16:16:09 -07006816static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006818 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 return -1;
6820 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006821 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 return -1;
6823 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006824 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006825 md_probe, NULL, NULL);
6826 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 md_probe, NULL, NULL);
6828
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006830 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
6832 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834}
6835
6836
6837#ifndef MODULE
6838
6839/*
6840 * Searches all registered partitions for autorun RAID arrays
6841 * at boot time.
6842 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006843
6844static LIST_HEAD(all_detected_devices);
6845struct detected_devices_node {
6846 struct list_head list;
6847 dev_t dev;
6848};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
6850void md_autodetect_dev(dev_t dev)
6851{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006852 struct detected_devices_node *node_detected_dev;
6853
6854 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6855 if (node_detected_dev) {
6856 node_detected_dev->dev = dev;
6857 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6858 } else {
6859 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6860 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862}
6863
6864
6865static void autostart_arrays(int part)
6866{
6867 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006868 struct detected_devices_node *node_detected_dev;
6869 dev_t dev;
6870 int i_scanned, i_passed;
6871
6872 i_scanned = 0;
6873 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
6875 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6876
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006877 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6878 i_scanned++;
6879 node_detected_dev = list_entry(all_detected_devices.next,
6880 struct detected_devices_node, list);
6881 list_del(&node_detected_dev->list);
6882 dev = node_detected_dev->dev;
6883 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006884 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 if (IS_ERR(rdev))
6886 continue;
6887
NeilBrownb2d444d2005-11-08 21:39:31 -08006888 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 MD_BUG();
6890 continue;
6891 }
NeilBrownd0fae182008-03-04 14:29:31 -08006892 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006894 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006896
6897 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6898 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
6900 autorun_devices(part);
6901}
6902
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006903#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904
6905static __exit void md_exit(void)
6906{
6907 mddev_t *mddev;
6908 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006909
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006910 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006911 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006913 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 unregister_blkdev(mdp_major, "mdp");
6915 unregister_reboot_notifier(&md_notifier);
6916 unregister_sysctl_table(raid_table_header);
6917 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006918 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006920 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 }
6922}
6923
Dan Williams685784a2007-07-09 11:56:42 -07006924subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925module_exit(md_exit)
6926
NeilBrownf91de922005-11-08 21:39:36 -08006927static int get_ro(char *buffer, struct kernel_param *kp)
6928{
6929 return sprintf(buffer, "%d", start_readonly);
6930}
6931static int set_ro(const char *val, struct kernel_param *kp)
6932{
6933 char *e;
6934 int num = simple_strtoul(val, &e, 10);
6935 if (*val && (*e == '\0' || *e == '\n')) {
6936 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006937 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006938 }
6939 return -EINVAL;
6940}
6941
NeilBrown80ca3a42006-07-10 04:44:18 -07006942module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6943module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006944
NeilBrownefeb53c2009-01-09 08:31:10 +11006945module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947EXPORT_SYMBOL(register_md_personality);
6948EXPORT_SYMBOL(unregister_md_personality);
6949EXPORT_SYMBOL(md_error);
6950EXPORT_SYMBOL(md_done_sync);
6951EXPORT_SYMBOL(md_write_start);
6952EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953EXPORT_SYMBOL(md_register_thread);
6954EXPORT_SYMBOL(md_unregister_thread);
6955EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956EXPORT_SYMBOL(md_check_recovery);
6957MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006958MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006959MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);