blob: 0f11fd1417abba31227184a52bd9d50853637231 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700105 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .proc_handler = &proc_dointvec,
107 },
108 {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .proc_handler = &proc_dointvec,
115 },
116 { .ctl_name = 0 }
117};
118
119static ctl_table raid_dir_table[] = {
120 {
121 .ctl_name = DEV_RAID,
122 .procname = "raid",
123 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .child = raid_table,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_root_table[] = {
131 {
132 .ctl_name = CTL_DEV,
133 .procname = "dev",
134 .maxlen = 0,
135 .mode = 0555,
136 .child = raid_dir_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static struct block_device_operations md_fops;
142
NeilBrownf91de922005-11-08 21:39:36 -0800143static int start_readonly;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
NeilBrownd7603b72006-01-06 00:20:30 -0800146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
149 * count increases.
150 *
151 * Events are:
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
154 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800155static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800156static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800157void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800158{
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
161}
NeilBrown29269552006-03-27 01:18:10 -0800162EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800163
NeilBrownc331eb02006-05-30 21:27:13 -0700164/* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
166 */
Adrian Bunk05381952006-06-26 00:28:01 -0700167static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700168{
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171}
172
NeilBrownd7603b72006-01-06 00:20:30 -0800173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
176 */
177static LIST_HEAD(all_mddevs);
178static DEFINE_SPINLOCK(all_mddevs_lock);
179
180
181/*
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
187 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800188#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
192 mddev = NULL;}); \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
200 tmp = tmp->next;}) \
201 )
202
203
NeilBrown409c57f2009-03-31 14:39:39 +1100204/* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
210 */
211static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
NeilBrown409c57f2009-03-31 14:39:39 +1100213 mddev_t *mddev = q->queuedata;
214 int rv;
215 if (mddev == NULL || mddev->pers == NULL) {
216 bio_io_error(bio);
217 return 0;
218 }
219 rcu_read_lock();
220 if (mddev->suspended) {
221 DEFINE_WAIT(__wait);
222 for (;;) {
223 prepare_to_wait(&mddev->sb_wait, &__wait,
224 TASK_UNINTERRUPTIBLE);
225 if (!mddev->suspended)
226 break;
227 rcu_read_unlock();
228 schedule();
229 rcu_read_lock();
230 }
231 finish_wait(&mddev->sb_wait, &__wait);
232 }
233 atomic_inc(&mddev->active_io);
234 rcu_read_unlock();
235 rv = mddev->pers->make_request(q, bio);
236 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
237 wake_up(&mddev->sb_wait);
238
239 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
NeilBrown409c57f2009-03-31 14:39:39 +1100242static void mddev_suspend(mddev_t *mddev)
243{
244 BUG_ON(mddev->suspended);
245 mddev->suspended = 1;
246 synchronize_rcu();
247 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
248 mddev->pers->quiesce(mddev, 1);
249 md_unregister_thread(mddev->thread);
250 mddev->thread = NULL;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
254 * unloaded
255 */
256}
257
258static void mddev_resume(mddev_t *mddev)
259{
260 mddev->suspended = 0;
261 wake_up(&mddev->sb_wait);
262 mddev->pers->quiesce(mddev, 0);
263}
264
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static inline mddev_t *mddev_get(mddev_t *mddev)
267{
268 atomic_inc(&mddev->active);
269 return mddev;
270}
271
Dan Williams5fd3a172009-03-04 00:57:25 -0700272static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static void mddev_put(mddev_t *mddev)
275{
276 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
277 return;
NeilBrownd3374822009-01-09 08:31:10 +1100278 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
279 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100281 if (mddev->gendisk) {
282 /* we did a probe so need to clean up.
283 * Call schedule_work inside the spinlock
284 * so that flush_scheduled_work() after
285 * mddev_find will succeed in waiting for the
286 * work to be done.
287 */
288 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
289 schedule_work(&mddev->del_work);
290 } else
291 kfree(mddev);
292 }
293 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static mddev_t * mddev_find(dev_t unit)
297{
298 mddev_t *mddev, *new = NULL;
299
300 retry:
301 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
NeilBrownefeb53c2009-01-09 08:31:10 +1100303 if (unit) {
304 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
305 if (mddev->unit == unit) {
306 mddev_get(mddev);
307 spin_unlock(&all_mddevs_lock);
308 kfree(new);
309 return mddev;
310 }
311
312 if (new) {
313 list_add(&new->all_mddevs, &all_mddevs);
314 spin_unlock(&all_mddevs_lock);
315 new->hold_active = UNTIL_IOCTL;
316 return new;
317 }
318 } else if (new) {
319 /* find an unused unit number */
320 static int next_minor = 512;
321 int start = next_minor;
322 int is_free = 0;
323 int dev = 0;
324 while (!is_free) {
325 dev = MKDEV(MD_MAJOR, next_minor);
326 next_minor++;
327 if (next_minor > MINORMASK)
328 next_minor = 0;
329 if (next_minor == start) {
330 /* Oh dear, all in use. */
331 spin_unlock(&all_mddevs_lock);
332 kfree(new);
333 return NULL;
334 }
335
336 is_free = 1;
337 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
338 if (mddev->unit == dev) {
339 is_free = 0;
340 break;
341 }
342 }
343 new->unit = dev;
344 new->md_minor = MINOR(dev);
345 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 list_add(&new->all_mddevs, &all_mddevs);
347 spin_unlock(&all_mddevs_lock);
348 return new;
349 }
350 spin_unlock(&all_mddevs_lock);
351
NeilBrown9ffae0c2006-01-06 00:20:32 -0800352 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!new)
354 return NULL;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 new->unit = unit;
357 if (MAJOR(unit) == MD_MAJOR)
358 new->md_minor = MINOR(unit);
359 else
360 new->md_minor = MINOR(unit) >> MdpMinorShift;
361
NeilBrowndf5b89b2006-03-27 01:18:20 -0800362 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 INIT_LIST_HEAD(&new->disks);
364 INIT_LIST_HEAD(&new->all_mddevs);
365 init_timer(&new->safemode_timer);
366 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000367 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100368 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700369 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700370 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700371 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700372 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000373 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800374 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700375 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto retry;
378}
379
380static inline int mddev_lock(mddev_t * mddev)
381{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800382 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Dan Williamsb522adc2009-03-31 15:00:31 +1100385static inline int mddev_is_locked(mddev_t *mddev)
386{
387 return mutex_is_locked(&mddev->reconfig_mutex);
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static inline int mddev_trylock(mddev_t * mddev)
391{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800392 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static inline void mddev_unlock(mddev_t * mddev)
396{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800397 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
NeilBrown005eca52005-08-22 13:11:08 -0700399 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
NeilBrown2989ddb2006-01-06 00:20:43 -0800402static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100404 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100406 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (rdev->desc_nr == nr)
408 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return NULL;
411}
412
413static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
414{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 mdk_rdev_t *rdev;
416
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100417 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (rdev->bdev->bd_dev == dev)
419 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return NULL;
422}
423
NeilBrownd9d166c2006-01-06 00:20:51 -0800424static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800425{
426 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800427 list_for_each_entry(pers, &pers_list, list) {
428 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800429 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800430 if (strcmp(pers->name, clevel)==0)
431 return pers;
432 }
NeilBrown2604b702006-01-06 00:20:36 -0800433 return NULL;
434}
435
Andre Nollb73df2d2008-07-11 22:02:23 +1000436/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700437static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Andre Nollb73df2d2008-07-11 22:02:23 +1000439 sector_t num_sectors = bdev->bd_inode->i_size / 512;
440 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static int alloc_disk_sb(mdk_rdev_t * rdev)
444{
445 if (rdev->sb_page)
446 MD_BUG();
447
448 rdev->sb_page = alloc_page(GFP_KERNEL);
449 if (!rdev->sb_page) {
450 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000451 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
454 return 0;
455}
456
457static void free_disk_sb(mdk_rdev_t * rdev)
458{
459 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800460 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 rdev->sb_loaded = 0;
462 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000463 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100464 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466}
467
468
NeilBrown6712ecf2007-09-27 12:47:43 +0200469static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700470{
471 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800472 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700473
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700474 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
475 printk("md: super_written gets error=%d, uptodate=%d\n",
476 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
477 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800478 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700479 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700480
NeilBrowna9701a32005-11-08 21:39:34 -0800481 if (atomic_dec_and_test(&mddev->pending_writes))
482 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700483 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700484}
485
NeilBrown6712ecf2007-09-27 12:47:43 +0200486static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800487{
488 struct bio *bio2 = bio->bi_private;
489 mdk_rdev_t *rdev = bio2->bi_private;
490 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800491
492 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
493 error == -EOPNOTSUPP) {
494 unsigned long flags;
495 /* barriers don't appear to be supported :-( */
496 set_bit(BarriersNotsupp, &rdev->flags);
497 mddev->barriers_work = 0;
498 spin_lock_irqsave(&mddev->write_lock, flags);
499 bio2->bi_next = mddev->biolist;
500 mddev->biolist = bio2;
501 spin_unlock_irqrestore(&mddev->write_lock, flags);
502 wake_up(&mddev->sb_wait);
503 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200504 } else {
505 bio_put(bio2);
506 bio->bi_private = rdev;
507 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800508 }
NeilBrowna9701a32005-11-08 21:39:34 -0800509}
510
NeilBrown7bfa19f2005-06-21 17:17:28 -0700511void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
512 sector_t sector, int size, struct page *page)
513{
514 /* write first size bytes of page to sector of rdev
515 * Increment mddev->pending_writes before returning
516 * and decrement it on completion, waking up sb_wait
517 * if zero is reached.
518 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800519 *
520 * As we might need to resubmit the request if BIO_RW_BARRIER
521 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700522 */
523 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100524 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700525
526 bio->bi_bdev = rdev->bdev;
527 bio->bi_sector = sector;
528 bio_add_page(bio, page, size, 0);
529 bio->bi_private = rdev;
530 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800531 bio->bi_rw = rw;
532
NeilBrown7bfa19f2005-06-21 17:17:28 -0700533 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800534 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
535 struct bio *rbio;
536 rw |= (1<<BIO_RW_BARRIER);
537 rbio = bio_clone(bio, GFP_NOIO);
538 rbio->bi_private = bio;
539 rbio->bi_end_io = super_written_barrier;
540 submit_bio(rw, rbio);
541 } else
542 submit_bio(rw, bio);
543}
544
545void md_super_wait(mddev_t *mddev)
546{
547 /* wait for all superblock writes that were scheduled to complete.
548 * if any had to be retried (due to BARRIER problems), retry them
549 */
550 DEFINE_WAIT(wq);
551 for(;;) {
552 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
553 if (atomic_read(&mddev->pending_writes)==0)
554 break;
555 while (mddev->biolist) {
556 struct bio *bio;
557 spin_lock_irq(&mddev->write_lock);
558 bio = mddev->biolist;
559 mddev->biolist = bio->bi_next ;
560 bio->bi_next = NULL;
561 spin_unlock_irq(&mddev->write_lock);
562 submit_bio(bio->bi_rw, bio);
563 }
564 schedule();
565 }
566 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700567}
568
NeilBrown6712ecf2007-09-27 12:47:43 +0200569static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
NeilBrowna654b9d82005-06-21 17:17:27 -0700574int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct page *page, int rw)
576{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700577 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct completion event;
579 int ret;
580
Jens Axboe93dbb392009-02-16 10:25:40 +0100581 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 bio->bi_bdev = bdev;
584 bio->bi_sector = sector;
585 bio_add_page(bio, page, size, 0);
586 init_completion(&event);
587 bio->bi_private = &event;
588 bio->bi_end_io = bi_complete;
589 submit_bio(rw, bio);
590 wait_for_completion(&event);
591
592 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
593 bio_put(bio);
594 return ret;
595}
NeilBrowna8745db2006-01-06 00:20:34 -0800596EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
NeilBrown0002b272005-09-09 16:23:53 -0700598static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 char b[BDEVNAME_SIZE];
601 if (!rdev->sb_page) {
602 MD_BUG();
603 return -EINVAL;
604 }
605 if (rdev->sb_loaded)
606 return 0;
607
608
Andre Noll0f420352008-07-11 22:02:23 +1000609 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto fail;
611 rdev->sb_loaded = 1;
612 return 0;
613
614fail:
615 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
616 bdevname(rdev->bdev,b));
617 return -EINVAL;
618}
619
620static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
621{
Andre Noll05710462008-07-11 22:02:20 +1000622 return sb1->set_uuid0 == sb2->set_uuid0 &&
623 sb1->set_uuid1 == sb2->set_uuid1 &&
624 sb1->set_uuid2 == sb2->set_uuid2 &&
625 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
629{
630 int ret;
631 mdp_super_t *tmp1, *tmp2;
632
633 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
634 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
635
636 if (!tmp1 || !tmp2) {
637 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100638 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto abort;
640 }
641
642 *tmp1 = *sb1;
643 *tmp2 = *sb2;
644
645 /*
646 * nr_disks is not constant
647 */
648 tmp1->nr_disks = 0;
649 tmp2->nr_disks = 0;
650
Andre Nollce0c8e02008-07-11 22:02:20 +1000651 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700653 kfree(tmp1);
654 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return ret;
656}
657
NeilBrown4d167f02007-05-09 02:35:37 -0700658
659static u32 md_csum_fold(u32 csum)
660{
661 csum = (csum & 0xffff) + (csum >> 16);
662 return (csum & 0xffff) + (csum >> 16);
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static unsigned int calc_sb_csum(mdp_super_t * sb)
666{
NeilBrown4d167f02007-05-09 02:35:37 -0700667 u64 newcsum = 0;
668 u32 *sb32 = (u32*)sb;
669 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned int disk_csum, csum;
671
672 disk_csum = sb->sb_csum;
673 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700674
675 for (i = 0; i < MD_SB_BYTES/4 ; i++)
676 newcsum += sb32[i];
677 csum = (newcsum & 0xffffffff) + (newcsum>>32);
678
679
680#ifdef CONFIG_ALPHA
681 /* This used to use csum_partial, which was wrong for several
682 * reasons including that different results are returned on
683 * different architectures. It isn't critical that we get exactly
684 * the same return value as before (we always csum_fold before
685 * testing, and that removes any differences). However as we
686 * know that csum_partial always returned a 16bit value on
687 * alphas, do a fold to maximise conformity to previous behaviour.
688 */
689 sb->sb_csum = md_csum_fold(disk_csum);
690#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return csum;
694}
695
696
697/*
698 * Handle superblock details.
699 * We want to be able to handle multiple superblock formats
700 * so we have a common interface to them all, and an array of
701 * different handlers.
702 * We rely on user-space to write the initial superblock, and support
703 * reading and updating of superblocks.
704 * Interface methods are:
705 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
706 * loads and validates a superblock on dev.
707 * if refdev != NULL, compare superblocks on both devices
708 * Return:
709 * 0 - dev has a superblock that is compatible with refdev
710 * 1 - dev has a superblock that is compatible and newer than refdev
711 * so dev should be used as the refdev in future
712 * -EINVAL superblock incompatible or invalid
713 * -othererror e.g. -EIO
714 *
715 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
716 * Verify that dev is acceptable into mddev.
717 * The first time, mddev->raid_disks will be 0, and data from
718 * dev should be merged in. Subsequent calls check that dev
719 * is new enough. Return 0 or -EINVAL
720 *
721 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
722 * Update the superblock for rdev with data in mddev
723 * This does not write to disc.
724 *
725 */
726
727struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000728 char *name;
729 struct module *owner;
730 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
731 int minor_version);
732 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
733 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
734 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000735 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736};
737
738/*
739 * load_super for 0.90.0
740 */
741static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
742{
743 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
744 mdp_super_t *sb;
745 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /*
Andre Noll0f420352008-07-11 22:02:23 +1000748 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * it's at the end of the disk.
750 *
751 * It also happens to be a multiple of 4Kb.
752 */
Andre Noll0f420352008-07-11 22:02:23 +1000753 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrown0002b272005-09-09 16:23:53 -0700755 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (ret) return ret;
757
758 ret = -EINVAL;
759
760 bdevname(rdev->bdev, b);
761 sb = (mdp_super_t*)page_address(rdev->sb_page);
762
763 if (sb->md_magic != MD_SB_MAGIC) {
764 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
765 b);
766 goto abort;
767 }
768
769 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800770 sb->minor_version < 90 ||
771 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
773 sb->major_version, sb->minor_version,
774 b);
775 goto abort;
776 }
777
778 if (sb->raid_disks <= 0)
779 goto abort;
780
NeilBrown4d167f02007-05-09 02:35:37 -0700781 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
783 b);
784 goto abort;
785 }
786
787 rdev->preferred_minor = sb->md_minor;
788 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700789 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
NeilBrowne11e93f2007-05-09 02:35:36 -0700791 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
792 if (sb->level != 1 && sb->level != 4
793 && sb->level != 5 && sb->level != 6
794 && sb->level != 10) {
795 /* FIXME use a better test */
796 printk(KERN_WARNING
797 "md: bitmaps not supported for this level.\n");
798 goto abort;
799 }
800 }
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (sb->level == LEVEL_MULTIPATH)
803 rdev->desc_nr = -1;
804 else
805 rdev->desc_nr = sb->this_disk.number;
806
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700807 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700809 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 __u64 ev1, ev2;
811 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
812 if (!uuid_equal(refsb, sb)) {
813 printk(KERN_WARNING "md: %s has different UUID to %s\n",
814 b, bdevname(refdev->bdev,b2));
815 goto abort;
816 }
817 if (!sb_equal(refsb, sb)) {
818 printk(KERN_WARNING "md: %s has same UUID"
819 " but different superblock to %s\n",
820 b, bdevname(refdev->bdev, b2));
821 goto abort;
822 }
823 ev1 = md_event(sb);
824 ev2 = md_event(refsb);
825 if (ev1 > ev2)
826 ret = 1;
827 else
828 ret = 0;
829 }
NeilBrown8190e752009-06-18 08:48:58 +1000830 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Andre Nolldd8ac332009-03-31 14:33:13 +1100832 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800833 /* "this cannot possibly happen" ... */
834 ret = -EINVAL;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 abort:
837 return ret;
838}
839
840/*
841 * validate_super for 0.90.0
842 */
843static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
844{
845 mdp_disk_t *desc;
846 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700847 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
NeilBrown41158c72005-06-21 17:17:25 -0700849 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800850 clear_bit(Faulty, &rdev->flags);
851 clear_bit(In_sync, &rdev->flags);
852 clear_bit(WriteMostly, &rdev->flags);
853 clear_bit(BarriersNotsupp, &rdev->flags);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (mddev->raid_disks == 0) {
856 mddev->major_version = 0;
857 mddev->minor_version = sb->minor_version;
858 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800859 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000860 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 mddev->ctime = sb->ctime;
862 mddev->utime = sb->utime;
863 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800864 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 mddev->layout = sb->layout;
866 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100867 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700868 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700869 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700870 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
NeilBrownf6705572006-03-27 01:18:11 -0800872 if (mddev->minor_version >= 91) {
873 mddev->reshape_position = sb->reshape_position;
874 mddev->delta_disks = sb->delta_disks;
875 mddev->new_level = sb->new_level;
876 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000877 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800878 } else {
879 mddev->reshape_position = MaxSector;
880 mddev->delta_disks = 0;
881 mddev->new_level = mddev->level;
882 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000883 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (sb->state & (1<<MD_SB_CLEAN))
887 mddev->recovery_cp = MaxSector;
888 else {
889 if (sb->events_hi == sb->cp_events_hi &&
890 sb->events_lo == sb->cp_events_lo) {
891 mddev->recovery_cp = sb->recovery_cp;
892 } else
893 mddev->recovery_cp = 0;
894 }
895
896 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
897 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
898 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
899 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
900
901 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700902
903 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700904 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700905 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700906
NeilBrown41158c72005-06-21 17:17:25 -0700907 } else if (mddev->pers == NULL) {
908 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ++ev1;
910 if (ev1 < mddev->events)
911 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700912 } else if (mddev->bitmap) {
913 /* if adding to array with a bitmap, then we can accept an
914 * older device ... but not too old.
915 */
NeilBrown41158c72005-06-21 17:17:25 -0700916 if (ev1 < mddev->bitmap->events_cleared)
917 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700918 } else {
919 if (ev1 < mddev->events)
920 /* just a hot-add of a new device, leave raid_disk at -1 */
921 return 0;
922 }
NeilBrown41158c72005-06-21 17:17:25 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 desc = sb->disks + rdev->desc_nr;
926
927 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800928 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700929 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
930 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800931 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 rdev->raid_disk = desc->raid_disk;
933 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700934 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
935 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700936 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800937 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
939}
940
941/*
942 * sync_super for 0.90.0
943 */
944static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
945{
946 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 mdk_rdev_t *rdev2;
948 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* make rdev->sb match mddev data..
952 *
953 * 1/ zero out disks
954 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
955 * 3/ any empty disks < next_spare become removed
956 *
957 * disks[0] gets initialised to REMOVED because
958 * we cannot be sure from other fields if it has
959 * been initialised or not.
960 */
961 int i;
962 int active=0, working=0,failed=0,spare=0,nr_disks=0;
963
NeilBrown61181562005-09-09 16:24:02 -0700964 rdev->sb_size = MD_SB_BYTES;
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 sb = (mdp_super_t*)page_address(rdev->sb_page);
967
968 memset(sb, 0, sizeof(*sb));
969
970 sb->md_magic = MD_SB_MAGIC;
971 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 sb->patch_version = mddev->patch_version;
973 sb->gvalid_words = 0; /* ignored */
974 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
975 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
976 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
977 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
978
979 sb->ctime = mddev->ctime;
980 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +1100981 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 sb->raid_disks = mddev->raid_disks;
983 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800984 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sb->utime = mddev->utime;
986 sb->state = 0;
987 sb->events_hi = (mddev->events>>32);
988 sb->events_lo = (u32)mddev->events;
989
NeilBrownf6705572006-03-27 01:18:11 -0800990 if (mddev->reshape_position == MaxSector)
991 sb->minor_version = 90;
992 else {
993 sb->minor_version = 91;
994 sb->reshape_position = mddev->reshape_position;
995 sb->new_level = mddev->new_level;
996 sb->delta_disks = mddev->delta_disks;
997 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000998 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -0800999 }
1000 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (mddev->in_sync)
1002 {
1003 sb->recovery_cp = mddev->recovery_cp;
1004 sb->cp_events_hi = (mddev->events>>32);
1005 sb->cp_events_lo = (u32)mddev->events;
1006 if (mddev->recovery_cp == MaxSector)
1007 sb->state = (1<< MD_SB_CLEAN);
1008 } else
1009 sb->recovery_cp = 0;
1010
1011 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001012 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
NeilBrowna654b9d82005-06-21 17:17:27 -07001014 if (mddev->bitmap && mddev->bitmap_file == NULL)
1015 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001018 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001020 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001021 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1022 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -08001023 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001025 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001026 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 d = &sb->disks[rdev2->desc_nr];
1028 nr_disks++;
1029 d->number = rdev2->desc_nr;
1030 d->major = MAJOR(rdev2->bdev->bd_dev);
1031 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001032 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1033 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 d->raid_disk = rdev2->raid_disk;
1035 else
1036 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001037 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -08001039 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 d->state = (1<<MD_DISK_ACTIVE);
1041 d->state |= (1<<MD_DISK_SYNC);
1042 active++;
1043 working++;
1044 } else {
1045 d->state = 0;
1046 spare++;
1047 working++;
1048 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001049 if (test_bit(WriteMostly, &rdev2->flags))
1050 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /* now set the "removed" and "faulty" bits on any missing devices */
1053 for (i=0 ; i < mddev->raid_disks ; i++) {
1054 mdp_disk_t *d = &sb->disks[i];
1055 if (d->state == 0 && d->number == 0) {
1056 d->number = i;
1057 d->raid_disk = i;
1058 d->state = (1<<MD_DISK_REMOVED);
1059 d->state |= (1<<MD_DISK_FAULTY);
1060 failed++;
1061 }
1062 }
1063 sb->nr_disks = nr_disks;
1064 sb->active_disks = active;
1065 sb->working_disks = working;
1066 sb->failed_disks = failed;
1067 sb->spare_disks = spare;
1068
1069 sb->this_disk = sb->disks[rdev->desc_nr];
1070 sb->sb_csum = calc_sb_csum(sb);
1071}
1072
1073/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001074 * rdev_size_change for 0.90.0
1075 */
1076static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001077super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001078{
Andre Noll58c0fed2009-03-31 14:33:13 +11001079 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001080 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001081 if (rdev->mddev->bitmap_offset)
1082 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001083 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001084 if (!num_sectors || num_sectors > rdev->sb_start)
1085 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001086 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001087 rdev->sb_page);
1088 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001089 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001090}
1091
1092
1093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 * version 1 superblock
1095 */
1096
NeilBrown1c05b4b2006-10-21 10:24:08 -07001097static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001099 __le32 disk_csum;
1100 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 unsigned long long newcsum;
1102 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001103 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int i;
1105
1106 disk_csum = sb->sb_csum;
1107 sb->sb_csum = 0;
1108 newcsum = 0;
1109 for (i=0; size>=4; size -= 4 )
1110 newcsum += le32_to_cpu(*isuper++);
1111
1112 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001113 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1116 sb->sb_csum = disk_csum;
1117 return cpu_to_le32(csum);
1118}
1119
1120static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1121{
1122 struct mdp_superblock_1 *sb;
1123 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001124 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001126 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
Andre Noll0f420352008-07-11 22:02:23 +10001129 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * It is always aligned to a 4K boundary and
1131 * depeding on minor_version, it can be:
1132 * 0: At least 8K, but less than 12K, from end of device
1133 * 1: At start of device
1134 * 2: 4K from start of device.
1135 */
1136 switch(minor_version) {
1137 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001138 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1139 sb_start -= 8*2;
1140 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
1142 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001143 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 break;
1145 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001146 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
1148 default:
1149 return -EINVAL;
1150 }
Andre Noll0f420352008-07-11 22:02:23 +10001151 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
NeilBrown0002b272005-09-09 16:23:53 -07001153 /* superblock is rarely larger than 1K, but it can be larger,
1154 * and it is safe to read 4k, so we do that
1155 */
1156 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (ret) return ret;
1158
1159
1160 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1161
1162 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1163 sb->major_version != cpu_to_le32(1) ||
1164 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001165 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001166 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return -EINVAL;
1168
1169 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1170 printk("md: invalid superblock checksum on %s\n",
1171 bdevname(rdev->bdev,b));
1172 return -EINVAL;
1173 }
1174 if (le64_to_cpu(sb->data_size) < 10) {
1175 printk("md: data_size too small on %s\n",
1176 bdevname(rdev->bdev,b));
1177 return -EINVAL;
1178 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001179 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1180 if (sb->level != cpu_to_le32(1) &&
1181 sb->level != cpu_to_le32(4) &&
1182 sb->level != cpu_to_le32(5) &&
1183 sb->level != cpu_to_le32(6) &&
1184 sb->level != cpu_to_le32(10)) {
1185 printk(KERN_WARNING
1186 "md: bitmaps not supported for this level.\n");
1187 return -EINVAL;
1188 }
1189 }
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 rdev->preferred_minor = 0xffff;
1192 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001193 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
NeilBrown0002b272005-09-09 16:23:53 -07001195 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001196 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001197 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001198 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1199
1200 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001201 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001202 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001203
NeilBrown31b65a02006-07-10 04:44:14 -07001204 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1205 rdev->desc_nr = -1;
1206 else
1207 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1208
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001209 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001210 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001211 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 __u64 ev1, ev2;
1213 struct mdp_superblock_1 *refsb =
1214 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1215
1216 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1217 sb->level != refsb->level ||
1218 sb->layout != refsb->layout ||
1219 sb->chunksize != refsb->chunksize) {
1220 printk(KERN_WARNING "md: %s has strangely different"
1221 " superblock to %s\n",
1222 bdevname(rdev->bdev,b),
1223 bdevname(refdev->bdev,b2));
1224 return -EINVAL;
1225 }
1226 ev1 = le64_to_cpu(sb->events);
1227 ev2 = le64_to_cpu(refsb->events);
1228
1229 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001230 ret = 1;
1231 else
1232 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
NeilBrowna1801f82008-03-04 14:29:31 -08001234 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001235 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1236 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001238 rdev->sectors = rdev->sb_start;
1239 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001241 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001242 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001243 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001244 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
1247static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1248{
1249 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001250 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
NeilBrown41158c72005-06-21 17:17:25 -07001252 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001253 clear_bit(Faulty, &rdev->flags);
1254 clear_bit(In_sync, &rdev->flags);
1255 clear_bit(WriteMostly, &rdev->flags);
1256 clear_bit(BarriersNotsupp, &rdev->flags);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (mddev->raid_disks == 0) {
1259 mddev->major_version = 1;
1260 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001261 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001262 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1264 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1265 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001266 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 mddev->layout = le32_to_cpu(sb->layout);
1268 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001269 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001270 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001271 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001272 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1275 memcpy(mddev->uuid, sb->set_uuid, 16);
1276
1277 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001278
NeilBrown71c08052005-09-09 16:23:51 -07001279 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001280 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001281 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001282
NeilBrownf6705572006-03-27 01:18:11 -08001283 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1284 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1285 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1286 mddev->new_level = le32_to_cpu(sb->new_level);
1287 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001288 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001289 } else {
1290 mddev->reshape_position = MaxSector;
1291 mddev->delta_disks = 0;
1292 mddev->new_level = mddev->level;
1293 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001294 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001295 }
1296
NeilBrown41158c72005-06-21 17:17:25 -07001297 } else if (mddev->pers == NULL) {
1298 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ++ev1;
1300 if (ev1 < mddev->events)
1301 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001302 } else if (mddev->bitmap) {
1303 /* If adding to array with a bitmap, then we can accept an
1304 * older device, but not too old.
1305 */
NeilBrown41158c72005-06-21 17:17:25 -07001306 if (ev1 < mddev->bitmap->events_cleared)
1307 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001308 } else {
1309 if (ev1 < mddev->events)
1310 /* just a hot-add of a new device, leave raid_disk at -1 */
1311 return 0;
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (mddev->level != LEVEL_MULTIPATH) {
1314 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1316 switch(role) {
1317 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001320 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001323 if ((le32_to_cpu(sb->feature_map) &
1324 MD_FEATURE_RECOVERY_OFFSET))
1325 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1326 else
1327 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 rdev->raid_disk = role;
1329 break;
1330 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001331 if (sb->devflags & WriteMostly1)
1332 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001333 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001334 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return 0;
1337}
1338
1339static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1340{
1341 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 mdk_rdev_t *rdev2;
1343 int max_dev, i;
1344 /* make rdev->sb match mddev and rdev data. */
1345
1346 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1347
1348 sb->feature_map = 0;
1349 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001350 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 memset(sb->pad1, 0, sizeof(sb->pad1));
1352 memset(sb->pad2, 0, sizeof(sb->pad2));
1353 memset(sb->pad3, 0, sizeof(sb->pad3));
1354
1355 sb->utime = cpu_to_le64((__u64)mddev->utime);
1356 sb->events = cpu_to_le64(mddev->events);
1357 if (mddev->in_sync)
1358 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1359 else
1360 sb->resync_offset = cpu_to_le64(0);
1361
NeilBrown1c05b4b2006-10-21 10:24:08 -07001362 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001363
NeilBrownf0ca3402006-02-02 14:28:04 -08001364 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001365 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001366 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001367 sb->level = cpu_to_le32(mddev->level);
1368 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001369
NeilBrowna654b9d82005-06-21 17:17:27 -07001370 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1371 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001372 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001373 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001374
1375 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001376 !test_bit(In_sync, &rdev->flags)) {
1377 if (mddev->curr_resync_completed > rdev->recovery_offset)
1378 rdev->recovery_offset = mddev->curr_resync_completed;
1379 if (rdev->recovery_offset > 0) {
1380 sb->feature_map |=
1381 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1382 sb->recovery_offset =
1383 cpu_to_le64(rdev->recovery_offset);
1384 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001385 }
1386
NeilBrownf6705572006-03-27 01:18:11 -08001387 if (mddev->reshape_position != MaxSector) {
1388 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1389 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1390 sb->new_layout = cpu_to_le32(mddev->new_layout);
1391 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1392 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001393 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001394 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001397 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (rdev2->desc_nr+1 > max_dev)
1399 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001400
1401 if (max_dev > le32_to_cpu(sb->max_dev))
1402 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 for (i=0; i<max_dev;i++)
1404 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1405
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001406 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001408 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001410 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001412 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1413 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 else
1415 sb->dev_roles[i] = cpu_to_le16(0xffff);
1416 }
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 sb->sb_csum = calc_sb_1_csum(sb);
1419}
1420
Chris Webb0cd17fe2008-06-28 08:31:46 +10001421static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001422super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001423{
1424 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001425 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001426 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001427 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001428 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001429 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001430 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1431 max_sectors -= rdev->data_offset;
1432 if (!num_sectors || num_sectors > max_sectors)
1433 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001434 } else if (rdev->mddev->bitmap_offset) {
1435 /* minor version 0 with bitmap we can't move */
1436 return 0;
1437 } else {
1438 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001439 sector_t sb_start;
1440 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1441 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001442 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001443 if (!num_sectors || num_sectors > max_sectors)
1444 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001445 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001446 }
1447 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001448 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001449 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001450 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001451 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001452 rdev->sb_page);
1453 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001454 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Adrian Bunk75c96f82005-05-05 16:16:09 -07001457static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 [0] = {
1459 .name = "0.90.0",
1460 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001461 .load_super = super_90_load,
1462 .validate_super = super_90_validate,
1463 .sync_super = super_90_sync,
1464 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 },
1466 [1] = {
1467 .name = "md-1",
1468 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001469 .load_super = super_1_load,
1470 .validate_super = super_1_validate,
1471 .sync_super = super_1_sync,
1472 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 },
1474};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1477{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001478 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
NeilBrown4b809912008-07-21 17:05:25 +10001480 rcu_read_lock();
1481 rdev_for_each_rcu(rdev, mddev1)
1482 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001483 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001484 rdev2->bdev->bd_contains) {
1485 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001486 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001487 }
1488 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return 0;
1490}
1491
1492static LIST_HEAD(pending_raid_disks);
1493
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001494static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev)
1495{
1496 struct mdk_personality *pers = mddev->pers;
1497 struct gendisk *disk = mddev->gendisk;
1498 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1499 struct blk_integrity *bi_mddev = blk_get_integrity(disk);
1500
1501 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1502 if (pers && pers->level >= 4 && pers->level <= 6)
1503 return;
1504
1505 /* If rdev is integrity capable, register profile for mddev */
1506 if (!bi_mddev && bi_rdev) {
1507 if (blk_integrity_register(disk, bi_rdev))
1508 printk(KERN_ERR "%s: %s Could not register integrity!\n",
1509 __func__, disk->disk_name);
1510 else
1511 printk(KERN_NOTICE "Enabling data integrity on %s\n",
1512 disk->disk_name);
1513 return;
1514 }
1515
1516 /* Check that mddev and rdev have matching profiles */
1517 if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) {
1518 printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__,
1519 disk->disk_name, rdev->bdev->bd_disk->disk_name);
1520 printk(KERN_NOTICE "Disabling data integrity on %s\n",
1521 disk->disk_name);
1522 blk_integrity_unregister(disk);
1523 }
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1527{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001528 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001529 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001530 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001531 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (rdev->mddev) {
1534 MD_BUG();
1535 return -EINVAL;
1536 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001537
1538 /* prevent duplicates */
1539 if (find_rdev(mddev, rdev->bdev->bd_dev))
1540 return -EEXIST;
1541
Andre Nolldd8ac332009-03-31 14:33:13 +11001542 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1543 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1544 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001545 if (mddev->pers) {
1546 /* Cannot change size, so fail
1547 * If mddev->level <= 0, then we don't care
1548 * about aligning sizes (e.g. linear)
1549 */
1550 if (mddev->level > 0)
1551 return -ENOSPC;
1552 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001553 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 /* Verify rdev->desc_nr is unique.
1557 * If it is -1, assign a free number, else
1558 * check number is not in use
1559 */
1560 if (rdev->desc_nr < 0) {
1561 int choice = 0;
1562 if (mddev->pers) choice = mddev->raid_disks;
1563 while (find_rdev_nr(mddev, choice))
1564 choice++;
1565 rdev->desc_nr = choice;
1566 } else {
1567 if (find_rdev_nr(mddev, rdev->desc_nr))
1568 return -EBUSY;
1569 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001570 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1571 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1572 mdname(mddev), mddev->max_disks);
1573 return -EBUSY;
1574 }
NeilBrown19133a42005-11-08 21:39:35 -08001575 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001576 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001577 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001580 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001581
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001582 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001583 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001584
Tejun Heo0762b8b2008-08-25 19:56:12 +09001585 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001586 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1587 kobject_del(&rdev->kobj);
1588 goto fail;
1589 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001590 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1591
NeilBrown4b809912008-07-21 17:05:25 +10001592 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001593 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001594
1595 /* May as well allow recovery to be retried once */
1596 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001597
1598 md_integrity_check(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001600
1601 fail:
1602 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1603 b, mdname(mddev));
1604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
NeilBrown177a99b2008-02-06 01:39:56 -08001607static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001608{
1609 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1610 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001611 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001612}
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1615{
1616 char b[BDEVNAME_SIZE];
1617 if (!rdev->mddev) {
1618 MD_BUG();
1619 return;
1620 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001621 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001622 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1624 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001625 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001626 sysfs_put(rdev->sysfs_state);
1627 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001628 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001629 * writing to 'remove' to "dev/state". We also need
1630 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001631 */
NeilBrown4b809912008-07-21 17:05:25 +10001632 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001633 INIT_WORK(&rdev->del_work, md_delayed_delete);
1634 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001635 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638/*
1639 * prevent the device from being mounted, repartitioned or
1640 * otherwise reused by a RAID array (or any other kernel
1641 * subsystem), by bd_claiming the device.
1642 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001643static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 int err = 0;
1646 struct block_device *bdev;
1647 char b[BDEVNAME_SIZE];
1648
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001649 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (IS_ERR(bdev)) {
1651 printk(KERN_ERR "md: could not open %s.\n",
1652 __bdevname(dev, b));
1653 return PTR_ERR(bdev);
1654 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001655 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (err) {
1657 printk(KERN_ERR "md: could not bd_claim %s.\n",
1658 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001659 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return err;
1661 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001662 if (!shared)
1663 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 rdev->bdev = bdev;
1665 return err;
1666}
1667
1668static void unlock_rdev(mdk_rdev_t *rdev)
1669{
1670 struct block_device *bdev = rdev->bdev;
1671 rdev->bdev = NULL;
1672 if (!bdev)
1673 MD_BUG();
1674 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001675 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
1678void md_autodetect_dev(dev_t dev);
1679
1680static void export_rdev(mdk_rdev_t * rdev)
1681{
1682 char b[BDEVNAME_SIZE];
1683 printk(KERN_INFO "md: export_rdev(%s)\n",
1684 bdevname(rdev->bdev,b));
1685 if (rdev->mddev)
1686 MD_BUG();
1687 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001689 if (test_bit(AutoDetected, &rdev->flags))
1690 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691#endif
1692 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001693 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696static void kick_rdev_from_array(mdk_rdev_t * rdev)
1697{
1698 unbind_rdev_from_array(rdev);
1699 export_rdev(rdev);
1700}
1701
1702static void export_array(mddev_t *mddev)
1703{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001704 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
NeilBrownd089c6a2008-02-06 01:39:59 -08001706 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (!rdev->mddev) {
1708 MD_BUG();
1709 continue;
1710 }
1711 kick_rdev_from_array(rdev);
1712 }
1713 if (!list_empty(&mddev->disks))
1714 MD_BUG();
1715 mddev->raid_disks = 0;
1716 mddev->major_version = 0;
1717}
1718
1719static void print_desc(mdp_disk_t *desc)
1720{
1721 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1722 desc->major,desc->minor,desc->raid_disk,desc->state);
1723}
1724
Cheng Renquancd2ac932009-01-09 08:31:08 +11001725static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 int i;
1728
1729 printk(KERN_INFO
1730 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1731 sb->major_version, sb->minor_version, sb->patch_version,
1732 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1733 sb->ctime);
1734 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1735 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1736 sb->md_minor, sb->layout, sb->chunk_size);
1737 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1738 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1739 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1740 sb->failed_disks, sb->spare_disks,
1741 sb->sb_csum, (unsigned long)sb->events_lo);
1742
1743 printk(KERN_INFO);
1744 for (i = 0; i < MD_SB_DISKS; i++) {
1745 mdp_disk_t *desc;
1746
1747 desc = sb->disks + i;
1748 if (desc->number || desc->major || desc->minor ||
1749 desc->raid_disk || (desc->state && (desc->state != 4))) {
1750 printk(" D %2d: ", i);
1751 print_desc(desc);
1752 }
1753 }
1754 printk(KERN_INFO "md: THIS: ");
1755 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Cheng Renquancd2ac932009-01-09 08:31:08 +11001758static void print_sb_1(struct mdp_superblock_1 *sb)
1759{
1760 __u8 *uuid;
1761
1762 uuid = sb->set_uuid;
1763 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1764 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1765 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1766 le32_to_cpu(sb->major_version),
1767 le32_to_cpu(sb->feature_map),
1768 uuid[0], uuid[1], uuid[2], uuid[3],
1769 uuid[4], uuid[5], uuid[6], uuid[7],
1770 uuid[8], uuid[9], uuid[10], uuid[11],
1771 uuid[12], uuid[13], uuid[14], uuid[15],
1772 sb->set_name,
1773 (unsigned long long)le64_to_cpu(sb->ctime)
1774 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1775
1776 uuid = sb->device_uuid;
1777 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1778 " RO:%llu\n"
1779 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1780 ":%02x%02x%02x%02x%02x%02x\n"
1781 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1782 KERN_INFO "md: (MaxDev:%u) \n",
1783 le32_to_cpu(sb->level),
1784 (unsigned long long)le64_to_cpu(sb->size),
1785 le32_to_cpu(sb->raid_disks),
1786 le32_to_cpu(sb->layout),
1787 le32_to_cpu(sb->chunksize),
1788 (unsigned long long)le64_to_cpu(sb->data_offset),
1789 (unsigned long long)le64_to_cpu(sb->data_size),
1790 (unsigned long long)le64_to_cpu(sb->super_offset),
1791 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1792 le32_to_cpu(sb->dev_number),
1793 uuid[0], uuid[1], uuid[2], uuid[3],
1794 uuid[4], uuid[5], uuid[6], uuid[7],
1795 uuid[8], uuid[9], uuid[10], uuid[11],
1796 uuid[12], uuid[13], uuid[14], uuid[15],
1797 sb->devflags,
1798 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1799 (unsigned long long)le64_to_cpu(sb->events),
1800 (unsigned long long)le64_to_cpu(sb->resync_offset),
1801 le32_to_cpu(sb->sb_csum),
1802 le32_to_cpu(sb->max_dev)
1803 );
1804}
1805
1806static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001809 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1810 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001811 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1812 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001814 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1815 switch (major_version) {
1816 case 0:
1817 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1818 break;
1819 case 1:
1820 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1821 break;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 } else
1824 printk(KERN_INFO "md: no rdev superblock!\n");
1825}
1826
Adrian Bunk5e563412006-06-26 00:27:42 -07001827static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001829 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 mdk_rdev_t *rdev;
1831 mddev_t *mddev;
1832 char b[BDEVNAME_SIZE];
1833
1834 printk("\n");
1835 printk("md: **********************************\n");
1836 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1837 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001838 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
NeilBrown32a76272005-06-21 17:17:14 -07001840 if (mddev->bitmap)
1841 bitmap_print_sb(mddev->bitmap);
1842 else
1843 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001844 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 printk("<%s>", bdevname(rdev->bdev,b));
1846 printk("\n");
1847
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001848 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001849 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851 printk("md: **********************************\n");
1852 printk("\n");
1853}
1854
1855
NeilBrown42543762006-06-26 00:27:57 -07001856static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
NeilBrown42543762006-06-26 00:27:57 -07001858 /* Update each superblock (in-memory image), but
1859 * if we are allowed to, skip spares which already
1860 * have the right event counter, or have one earlier
1861 * (which would mean they aren't being marked as dirty
1862 * with the rest of the array)
1863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001866 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001867 if (rdev->sb_events == mddev->events ||
1868 (nospares &&
1869 rdev->raid_disk < 0 &&
1870 (rdev->sb_events&1)==0 &&
1871 rdev->sb_events+1 == mddev->events)) {
1872 /* Don't update this superblock */
1873 rdev->sb_loaded = 2;
1874 } else {
1875 super_types[mddev->major_version].
1876 sync_super(mddev, rdev);
1877 rdev->sb_loaded = 1;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880}
1881
NeilBrown850b2b42006-10-03 01:15:46 -07001882static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001885 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001886 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
NeilBrown1b57f132009-06-18 08:48:19 +10001888 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07001889 if (mddev->external)
1890 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001892 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001893
NeilBrown850b2b42006-10-03 01:15:46 -07001894 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1895 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1896 force_change = 1;
1897 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1898 /* just a clean<-> dirty transition, possibly leave spares alone,
1899 * though if events isn't the right even/odd, we will have to do
1900 * spares after all
1901 */
1902 nospares = 1;
1903 if (force_change)
1904 nospares = 0;
1905 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001906 /* If the array is degraded, then skipping spares is both
1907 * dangerous and fairly pointless.
1908 * Dangerous because a device that was removed from the array
1909 * might have a event_count that still looks up-to-date,
1910 * so it can be re-added without a resync.
1911 * Pointless because if there are any spares to skip,
1912 * then a recovery will happen and soon that array won't
1913 * be degraded any more and the spare can go back to sleep then.
1914 */
NeilBrown850b2b42006-10-03 01:15:46 -07001915 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001916
NeilBrown06d91a52005-06-21 17:17:12 -07001917 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07001918
1919 /* If this is just a dirty<->clean transition, and the array is clean
1920 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001921 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001922 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001923 && (mddev->events & 1)
1924 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001925 mddev->events--;
1926 else {
1927 /* otherwise we have to go forward and ... */
1928 mddev->events ++;
1929 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1930 /* .. if the array isn't clean, insist on an odd 'events' */
1931 if ((mddev->events&1)==0) {
1932 mddev->events++;
1933 nospares = 0;
1934 }
1935 } else {
1936 /* otherwise insist on an even 'events' (for clean states) */
1937 if ((mddev->events&1)) {
1938 mddev->events++;
1939 nospares = 0;
1940 }
1941 }
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 if (!mddev->events) {
1945 /*
1946 * oops, this 64-bit counter should never wrap.
1947 * Either we are in around ~1 trillion A.C., assuming
1948 * 1 reboot per second, or we have a bug:
1949 */
1950 MD_BUG();
1951 mddev->events --;
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 /*
1955 * do not write anything to disk if using
1956 * nonpersistent superblocks
1957 */
NeilBrown06d91a52005-06-21 17:17:12 -07001958 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001959 if (!mddev->external)
1960 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1961
NeilBrowna9701a32005-11-08 21:39:34 -08001962 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001963 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001965 }
NeilBrowne6910632008-02-06 01:39:51 -08001966 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001967 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 dprintk(KERN_INFO
1970 "md: updating %s RAID superblock on device (in sync %d)\n",
1971 mdname(mddev),mddev->in_sync);
1972
NeilBrown4ad13662007-07-17 04:06:13 -07001973 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001974 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 char b[BDEVNAME_SIZE];
1976 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001977 if (rdev->sb_loaded != 1)
1978 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001979 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 dprintk("(skipping faulty ");
1981
1982 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001983 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001984 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001985 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001986 rdev->sb_page);
1987 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1988 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001989 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001990 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else
1993 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001994 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* only need to write one superblock... */
1996 break;
1997 }
NeilBrowna9701a32005-11-08 21:39:34 -08001998 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001999 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002000
NeilBrowna9701a32005-11-08 21:39:34 -08002001 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002002 if (mddev->in_sync != sync_req ||
2003 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002004 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002005 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002006 goto repeat;
2007 }
NeilBrown850b2b42006-10-03 01:15:46 -07002008 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002009 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002010 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002011 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2012 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Andre Noll7f6ce762008-03-23 18:34:54 +01002016/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002017 * We want to accept with case. For this we use cmd_match.
2018 */
2019static int cmd_match(const char *cmd, const char *str)
2020{
2021 /* See if cmd, written into a sysfs file, matches
2022 * str. They must either be the same, or cmd can
2023 * have a trailing newline
2024 */
2025 while (*cmd && *str && *cmd == *str) {
2026 cmd++;
2027 str++;
2028 }
2029 if (*cmd == '\n')
2030 cmd++;
2031 if (*str || *cmd)
2032 return 0;
2033 return 1;
2034}
2035
NeilBrown86e6ffd2005-11-08 21:39:24 -08002036struct rdev_sysfs_entry {
2037 struct attribute attr;
2038 ssize_t (*show)(mdk_rdev_t *, char *);
2039 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2040};
2041
2042static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002043state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002044{
2045 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002046 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002047
NeilBrownb2d444d2005-11-08 21:39:31 -08002048 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002049 len+= sprintf(page+len, "%sfaulty",sep);
2050 sep = ",";
2051 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002052 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002053 len += sprintf(page+len, "%sin_sync",sep);
2054 sep = ",";
2055 }
NeilBrownf6556752006-06-26 00:28:01 -07002056 if (test_bit(WriteMostly, &rdev->flags)) {
2057 len += sprintf(page+len, "%swrite_mostly",sep);
2058 sep = ",";
2059 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002060 if (test_bit(Blocked, &rdev->flags)) {
2061 len += sprintf(page+len, "%sblocked", sep);
2062 sep = ",";
2063 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002064 if (!test_bit(Faulty, &rdev->flags) &&
2065 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002066 len += sprintf(page+len, "%sspare", sep);
2067 sep = ",";
2068 }
2069 return len+sprintf(page+len, "\n");
2070}
2071
NeilBrown45dc2de2006-06-26 00:27:58 -07002072static ssize_t
2073state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2074{
2075 /* can write
2076 * faulty - simulates and error
2077 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002078 * writemostly - sets write_mostly
2079 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002080 * blocked - sets the Blocked flag
2081 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002082 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002083 */
2084 int err = -EINVAL;
2085 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2086 md_error(rdev->mddev, rdev);
2087 err = 0;
2088 } else if (cmd_match(buf, "remove")) {
2089 if (rdev->raid_disk >= 0)
2090 err = -EBUSY;
2091 else {
2092 mddev_t *mddev = rdev->mddev;
2093 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002094 if (mddev->pers)
2095 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002096 md_new_event(mddev);
2097 err = 0;
2098 }
NeilBrownf6556752006-06-26 00:28:01 -07002099 } else if (cmd_match(buf, "writemostly")) {
2100 set_bit(WriteMostly, &rdev->flags);
2101 err = 0;
2102 } else if (cmd_match(buf, "-writemostly")) {
2103 clear_bit(WriteMostly, &rdev->flags);
2104 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002105 } else if (cmd_match(buf, "blocked")) {
2106 set_bit(Blocked, &rdev->flags);
2107 err = 0;
2108 } else if (cmd_match(buf, "-blocked")) {
2109 clear_bit(Blocked, &rdev->flags);
2110 wake_up(&rdev->blocked_wait);
2111 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2112 md_wakeup_thread(rdev->mddev->thread);
2113
2114 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002115 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2116 set_bit(In_sync, &rdev->flags);
2117 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002118 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002119 if (!err && rdev->sysfs_state)
2120 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002121 return err ? err : len;
2122}
NeilBrown80ca3a42006-07-10 04:44:18 -07002123static struct rdev_sysfs_entry rdev_state =
2124__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002125
2126static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002127errors_show(mdk_rdev_t *rdev, char *page)
2128{
2129 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2130}
2131
2132static ssize_t
2133errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2134{
2135 char *e;
2136 unsigned long n = simple_strtoul(buf, &e, 10);
2137 if (*buf && (*e == 0 || *e == '\n')) {
2138 atomic_set(&rdev->corrected_errors, n);
2139 return len;
2140 }
2141 return -EINVAL;
2142}
2143static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002144__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002145
NeilBrown014236d2006-01-06 00:20:55 -08002146static ssize_t
2147slot_show(mdk_rdev_t *rdev, char *page)
2148{
2149 if (rdev->raid_disk < 0)
2150 return sprintf(page, "none\n");
2151 else
2152 return sprintf(page, "%d\n", rdev->raid_disk);
2153}
2154
2155static ssize_t
2156slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2157{
2158 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002159 int err;
2160 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002161 int slot = simple_strtoul(buf, &e, 10);
2162 if (strncmp(buf, "none", 4)==0)
2163 slot = -1;
2164 else if (e==buf || (*e && *e!= '\n'))
2165 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002166 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002167 /* Setting 'slot' on an active array requires also
2168 * updating the 'rd%d' link, and communicating
2169 * with the personality with ->hot_*_disk.
2170 * For now we only support removing
2171 * failed/spare devices. This normally happens automatically,
2172 * but not when the metadata is externally managed.
2173 */
NeilBrownc303da62008-02-06 01:39:51 -08002174 if (rdev->raid_disk == -1)
2175 return -EEXIST;
2176 /* personality does all needed checks */
2177 if (rdev->mddev->pers->hot_add_disk == NULL)
2178 return -EINVAL;
2179 err = rdev->mddev->pers->
2180 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2181 if (err)
2182 return err;
2183 sprintf(nm, "rd%d", rdev->raid_disk);
2184 sysfs_remove_link(&rdev->mddev->kobj, nm);
2185 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2186 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002187 } else if (rdev->mddev->pers) {
2188 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002189 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002190 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002191 */
2192
2193 if (rdev->raid_disk != -1)
2194 return -EBUSY;
2195
2196 if (rdev->mddev->pers->hot_add_disk == NULL)
2197 return -EINVAL;
2198
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002199 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002200 if (rdev2->raid_disk == slot)
2201 return -EEXIST;
2202
2203 rdev->raid_disk = slot;
2204 if (test_bit(In_sync, &rdev->flags))
2205 rdev->saved_raid_disk = slot;
2206 else
2207 rdev->saved_raid_disk = -1;
2208 err = rdev->mddev->pers->
2209 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002210 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002211 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002212 return err;
Neil Brown52664732008-06-28 08:31:44 +10002213 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002214 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002215 sprintf(nm, "rd%d", rdev->raid_disk);
2216 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2217 printk(KERN_WARNING
2218 "md: cannot register "
2219 "%s for %s\n",
2220 nm, mdname(rdev->mddev));
2221
2222 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002223 } else {
2224 if (slot >= rdev->mddev->raid_disks)
2225 return -ENOSPC;
2226 rdev->raid_disk = slot;
2227 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002228 clear_bit(Faulty, &rdev->flags);
2229 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002230 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002231 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002232 }
NeilBrown014236d2006-01-06 00:20:55 -08002233 return len;
2234}
2235
2236
2237static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002238__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002239
NeilBrown93c8cad2006-01-06 00:20:56 -08002240static ssize_t
2241offset_show(mdk_rdev_t *rdev, char *page)
2242{
Andrew Morton6961ece2006-01-06 00:20:59 -08002243 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002244}
2245
2246static ssize_t
2247offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2248{
2249 char *e;
2250 unsigned long long offset = simple_strtoull(buf, &e, 10);
2251 if (e==buf || (*e && *e != '\n'))
2252 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002253 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002254 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002255 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002256 /* Must set offset before size, so overlap checks
2257 * can be sane */
2258 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002259 rdev->data_offset = offset;
2260 return len;
2261}
2262
2263static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002264__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002265
NeilBrown83303b62006-01-06 00:21:06 -08002266static ssize_t
2267rdev_size_show(mdk_rdev_t *rdev, char *page)
2268{
Andre Nolldd8ac332009-03-31 14:33:13 +11002269 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002270}
2271
NeilBrownc5d79ad2008-02-06 01:39:54 -08002272static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2273{
2274 /* check if two start/length pairs overlap */
2275 if (s1+l1 <= s2)
2276 return 0;
2277 if (s2+l2 <= s1)
2278 return 0;
2279 return 1;
2280}
2281
Dan Williamsb522adc2009-03-31 15:00:31 +11002282static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2283{
2284 unsigned long long blocks;
2285 sector_t new;
2286
2287 if (strict_strtoull(buf, 10, &blocks) < 0)
2288 return -EINVAL;
2289
2290 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2291 return -EINVAL; /* sector conversion overflow */
2292
2293 new = blocks * 2;
2294 if (new != blocks * 2)
2295 return -EINVAL; /* unsigned long long to sector_t overflow */
2296
2297 *sectors = new;
2298 return 0;
2299}
2300
NeilBrown83303b62006-01-06 00:21:06 -08002301static ssize_t
2302rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2303{
NeilBrown27c529b2008-03-04 14:29:33 -08002304 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002305 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002306 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002307
Dan Williamsb522adc2009-03-31 15:00:31 +11002308 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002309 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002310 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002311 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002312 sectors = super_types[my_mddev->major_version].
2313 rdev_size_change(rdev, sectors);
2314 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002315 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002316 } else if (!sectors)
2317 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2318 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002319 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002320 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002321 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002322
Andre Nolldd8ac332009-03-31 14:33:13 +11002323 rdev->sectors = sectors;
2324 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002325 /* need to check that all other rdevs with the same ->bdev
2326 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002327 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002328 * we have to change it back, we will have the lock again.
2329 */
2330 mddev_t *mddev;
2331 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002332 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002333
NeilBrown27c529b2008-03-04 14:29:33 -08002334 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002335 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002336 mdk_rdev_t *rdev2;
2337
2338 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002339 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002340 if (test_bit(AllReserved, &rdev2->flags) ||
2341 (rdev->bdev == rdev2->bdev &&
2342 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002343 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002344 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002345 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002346 overlap = 1;
2347 break;
2348 }
2349 mddev_unlock(mddev);
2350 if (overlap) {
2351 mddev_put(mddev);
2352 break;
2353 }
2354 }
NeilBrown27c529b2008-03-04 14:29:33 -08002355 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002356 if (overlap) {
2357 /* Someone else could have slipped in a size
2358 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002359 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002360 * safe, and trust userspace not to race with
2361 * itself
2362 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002363 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002364 return -EBUSY;
2365 }
2366 }
NeilBrown83303b62006-01-06 00:21:06 -08002367 return len;
2368}
2369
2370static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002371__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002372
NeilBrown86e6ffd2005-11-08 21:39:24 -08002373static struct attribute *rdev_default_attrs[] = {
2374 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002375 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002376 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002377 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002378 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002379 NULL,
2380};
2381static ssize_t
2382rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2383{
2384 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2385 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002386 mddev_t *mddev = rdev->mddev;
2387 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002388
2389 if (!entry->show)
2390 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002391
2392 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2393 if (!rv) {
2394 if (rdev->mddev == NULL)
2395 rv = -EBUSY;
2396 else
2397 rv = entry->show(rdev, page);
2398 mddev_unlock(mddev);
2399 }
2400 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002401}
2402
2403static ssize_t
2404rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2405 const char *page, size_t length)
2406{
2407 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2408 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002409 ssize_t rv;
2410 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002411
2412 if (!entry->store)
2413 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002414 if (!capable(CAP_SYS_ADMIN))
2415 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002416 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002417 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002418 if (rdev->mddev == NULL)
2419 rv = -EBUSY;
2420 else
2421 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002422 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002423 }
2424 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002425}
2426
2427static void rdev_free(struct kobject *ko)
2428{
2429 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2430 kfree(rdev);
2431}
2432static struct sysfs_ops rdev_sysfs_ops = {
2433 .show = rdev_attr_show,
2434 .store = rdev_attr_store,
2435};
2436static struct kobj_type rdev_ktype = {
2437 .release = rdev_free,
2438 .sysfs_ops = &rdev_sysfs_ops,
2439 .default_attrs = rdev_default_attrs,
2440};
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442/*
2443 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2444 *
2445 * mark the device faulty if:
2446 *
2447 * - the device is nonexistent (zero size)
2448 * - the device has no valid superblock
2449 *
2450 * a faulty rdev _never_ has rdev->sb set.
2451 */
2452static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2453{
2454 char b[BDEVNAME_SIZE];
2455 int err;
2456 mdk_rdev_t *rdev;
2457 sector_t size;
2458
NeilBrown9ffae0c2006-01-06 00:20:32 -08002459 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (!rdev) {
2461 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2462 return ERR_PTR(-ENOMEM);
2463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 if ((err = alloc_disk_sb(rdev)))
2466 goto abort_free;
2467
NeilBrownc5d79ad2008-02-06 01:39:54 -08002468 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (err)
2470 goto abort_free;
2471
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002472 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002475 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002476 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002477 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002479 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002481 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002482 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2485 if (!size) {
2486 printk(KERN_WARNING
2487 "md: %s has zero or unknown size, marking faulty!\n",
2488 bdevname(rdev->bdev,b));
2489 err = -EINVAL;
2490 goto abort_free;
2491 }
2492
2493 if (super_format >= 0) {
2494 err = super_types[super_format].
2495 load_super(rdev, NULL, super_minor);
2496 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002497 printk(KERN_WARNING
2498 "md: %s does not have a valid v%d.%d "
2499 "superblock, not importing!\n",
2500 bdevname(rdev->bdev,b),
2501 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 goto abort_free;
2503 }
2504 if (err < 0) {
2505 printk(KERN_WARNING
2506 "md: could not read %s's sb, not importing!\n",
2507 bdevname(rdev->bdev,b));
2508 goto abort_free;
2509 }
2510 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002513 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 return rdev;
2516
2517abort_free:
2518 if (rdev->sb_page) {
2519 if (rdev->bdev)
2520 unlock_rdev(rdev);
2521 free_disk_sb(rdev);
2522 }
2523 kfree(rdev);
2524 return ERR_PTR(err);
2525}
2526
2527/*
2528 * Check a full RAID array for plausibility
2529 */
2530
2531
NeilBrowna757e642005-04-16 15:26:42 -07002532static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002535 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 char b[BDEVNAME_SIZE];
2537
2538 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002539 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 switch (super_types[mddev->major_version].
2541 load_super(rdev, freshest, mddev->minor_version)) {
2542 case 1:
2543 freshest = rdev;
2544 break;
2545 case 0:
2546 break;
2547 default:
2548 printk( KERN_ERR \
2549 "md: fatal superblock inconsistency in %s"
2550 " -- removing from array\n",
2551 bdevname(rdev->bdev,b));
2552 kick_rdev_from_array(rdev);
2553 }
2554
2555
2556 super_types[mddev->major_version].
2557 validate_super(mddev, freshest);
2558
2559 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002560 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002561 if (rdev->desc_nr >= mddev->max_disks ||
2562 i > mddev->max_disks) {
2563 printk(KERN_WARNING
2564 "md: %s: %s: only %d devices permitted\n",
2565 mdname(mddev), bdevname(rdev->bdev, b),
2566 mddev->max_disks);
2567 kick_rdev_from_array(rdev);
2568 continue;
2569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 if (rdev != freshest)
2571 if (super_types[mddev->major_version].
2572 validate_super(mddev, rdev)) {
2573 printk(KERN_WARNING "md: kicking non-fresh %s"
2574 " from array!\n",
2575 bdevname(rdev->bdev,b));
2576 kick_rdev_from_array(rdev);
2577 continue;
2578 }
2579 if (mddev->level == LEVEL_MULTIPATH) {
2580 rdev->desc_nr = i++;
2581 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002582 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002583 } else if (rdev->raid_disk >= mddev->raid_disks) {
2584 rdev->raid_disk = -1;
2585 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 }
2587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
NeilBrown19052c02008-08-05 15:54:13 +10002590static void md_safemode_timeout(unsigned long data);
2591
NeilBrowneae17012005-11-08 21:39:23 -08002592static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002593safe_delay_show(mddev_t *mddev, char *page)
2594{
2595 int msec = (mddev->safemode_delay*1000)/HZ;
2596 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2597}
2598static ssize_t
2599safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2600{
2601 int scale=1;
2602 int dot=0;
2603 int i;
2604 unsigned long msec;
2605 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002606
NeilBrown16f17b32006-06-26 00:27:37 -07002607 /* remove a period, and count digits after it */
2608 if (len >= sizeof(buf))
2609 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002610 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002611 for (i=0; i<len; i++) {
2612 if (dot) {
2613 if (isdigit(buf[i])) {
2614 buf[i-1] = buf[i];
2615 scale *= 10;
2616 }
2617 buf[i] = 0;
2618 } else if (buf[i] == '.') {
2619 dot=1;
2620 buf[i] = 0;
2621 }
2622 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002623 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002624 return -EINVAL;
2625 msec = (msec * 1000) / scale;
2626 if (msec == 0)
2627 mddev->safemode_delay = 0;
2628 else {
NeilBrown19052c02008-08-05 15:54:13 +10002629 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002630 mddev->safemode_delay = (msec*HZ)/1000;
2631 if (mddev->safemode_delay == 0)
2632 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002633 if (mddev->safemode_delay < old_delay)
2634 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002635 }
2636 return len;
2637}
2638static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002639__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002640
2641static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002642level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002643{
NeilBrown2604b702006-01-06 00:20:36 -08002644 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002645 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002646 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002647 else if (mddev->clevel[0])
2648 return sprintf(page, "%s\n", mddev->clevel);
2649 else if (mddev->level != LEVEL_NONE)
2650 return sprintf(page, "%d\n", mddev->level);
2651 else
2652 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002653}
2654
NeilBrownd9d166c2006-01-06 00:20:51 -08002655static ssize_t
2656level_store(mddev_t *mddev, const char *buf, size_t len)
2657{
NeilBrown245f46c2009-03-31 14:39:39 +11002658 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002659 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002660 struct mdk_personality *pers;
2661 void *priv;
2662
2663 if (mddev->pers == NULL) {
2664 if (len == 0)
2665 return 0;
2666 if (len >= sizeof(mddev->clevel))
2667 return -ENOSPC;
2668 strncpy(mddev->clevel, buf, len);
2669 if (mddev->clevel[len-1] == '\n')
2670 len--;
2671 mddev->clevel[len] = 0;
2672 mddev->level = LEVEL_NONE;
2673 return rv;
2674 }
2675
2676 /* request to change the personality. Need to ensure:
2677 * - array is not engaged in resync/recovery/reshape
2678 * - old personality can be suspended
2679 * - new personality will access other array.
2680 */
2681
2682 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002683 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002684
2685 if (!mddev->pers->quiesce) {
2686 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2687 mdname(mddev), mddev->pers->name);
2688 return -EINVAL;
2689 }
2690
2691 /* Now find the new personality */
2692 if (len == 0 || len >= sizeof(level))
2693 return -EINVAL;
2694 strncpy(level, buf, len);
2695 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002696 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002697 level[len] = 0;
2698
2699 request_module("md-%s", level);
2700 spin_lock(&pers_lock);
2701 pers = find_pers(LEVEL_NONE, level);
2702 if (!pers || !try_module_get(pers->owner)) {
2703 spin_unlock(&pers_lock);
2704 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2705 return -EINVAL;
2706 }
2707 spin_unlock(&pers_lock);
2708
2709 if (pers == mddev->pers) {
2710 /* Nothing to do! */
2711 module_put(pers->owner);
2712 return rv;
2713 }
2714 if (!pers->takeover) {
2715 module_put(pers->owner);
2716 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2717 mdname(mddev), level);
2718 return -EINVAL;
2719 }
2720
2721 /* ->takeover must set new_* and/or delta_disks
2722 * if it succeeds, and may set them when it fails.
2723 */
2724 priv = pers->takeover(mddev);
2725 if (IS_ERR(priv)) {
2726 mddev->new_level = mddev->level;
2727 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002728 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002729 mddev->raid_disks -= mddev->delta_disks;
2730 mddev->delta_disks = 0;
2731 module_put(pers->owner);
2732 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2733 mdname(mddev), level);
2734 return PTR_ERR(priv);
2735 }
2736
2737 /* Looks like we have a winner */
2738 mddev_suspend(mddev);
2739 mddev->pers->stop(mddev);
2740 module_put(mddev->pers->owner);
2741 mddev->pers = pers;
2742 mddev->private = priv;
2743 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2744 mddev->level = mddev->new_level;
2745 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002746 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002747 mddev->delta_disks = 0;
2748 pers->run(mddev);
2749 mddev_resume(mddev);
2750 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2751 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2752 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002753 return rv;
2754}
2755
2756static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002757__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002758
NeilBrownd4dbd022006-06-26 00:27:59 -07002759
2760static ssize_t
2761layout_show(mddev_t *mddev, char *page)
2762{
2763 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002764 if (mddev->reshape_position != MaxSector &&
2765 mddev->layout != mddev->new_layout)
2766 return sprintf(page, "%d (%d)\n",
2767 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002768 return sprintf(page, "%d\n", mddev->layout);
2769}
2770
2771static ssize_t
2772layout_store(mddev_t *mddev, const char *buf, size_t len)
2773{
2774 char *e;
2775 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002776
2777 if (!*buf || (*e && *e != '\n'))
2778 return -EINVAL;
2779
NeilBrownb3546032009-03-31 14:56:41 +11002780 if (mddev->pers) {
2781 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002782 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002783 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002784 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002785 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002786 if (err) {
2787 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002788 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002789 }
NeilBrownb3546032009-03-31 14:56:41 +11002790 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002791 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002792 if (mddev->reshape_position == MaxSector)
2793 mddev->layout = n;
2794 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002795 return len;
2796}
2797static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002798__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002799
2800
NeilBrowneae17012005-11-08 21:39:23 -08002801static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002802raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002803{
NeilBrownbb636542005-11-08 21:39:45 -08002804 if (mddev->raid_disks == 0)
2805 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002806 if (mddev->reshape_position != MaxSector &&
2807 mddev->delta_disks != 0)
2808 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2809 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002810 return sprintf(page, "%d\n", mddev->raid_disks);
2811}
2812
NeilBrownda943b992006-01-06 00:20:54 -08002813static int update_raid_disks(mddev_t *mddev, int raid_disks);
2814
2815static ssize_t
2816raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2817{
NeilBrownda943b992006-01-06 00:20:54 -08002818 char *e;
2819 int rv = 0;
2820 unsigned long n = simple_strtoul(buf, &e, 10);
2821
2822 if (!*buf || (*e && *e != '\n'))
2823 return -EINVAL;
2824
2825 if (mddev->pers)
2826 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002827 else if (mddev->reshape_position != MaxSector) {
2828 int olddisks = mddev->raid_disks - mddev->delta_disks;
2829 mddev->delta_disks = n - olddisks;
2830 mddev->raid_disks = n;
2831 } else
NeilBrownda943b992006-01-06 00:20:54 -08002832 mddev->raid_disks = n;
2833 return rv ? rv : len;
2834}
2835static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002836__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002837
NeilBrown24dd4692005-11-08 21:39:26 -08002838static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002839chunk_size_show(mddev_t *mddev, char *page)
2840{
NeilBrown08a02ec2007-05-09 02:35:38 -07002841 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10002842 mddev->chunk_sectors != mddev->new_chunk_sectors)
2843 return sprintf(page, "%d (%d)\n",
2844 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10002845 mddev->chunk_sectors << 9);
2846 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08002847}
2848
2849static ssize_t
2850chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2851{
NeilBrown3b343802006-01-06 00:20:47 -08002852 char *e;
2853 unsigned long n = simple_strtoul(buf, &e, 10);
2854
NeilBrown3b343802006-01-06 00:20:47 -08002855 if (!*buf || (*e && *e != '\n'))
2856 return -EINVAL;
2857
NeilBrownb3546032009-03-31 14:56:41 +11002858 if (mddev->pers) {
2859 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002860 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002861 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002862 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10002863 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002864 if (err) {
2865 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11002866 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002867 }
NeilBrownb3546032009-03-31 14:56:41 +11002868 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10002869 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002870 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10002871 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002872 }
NeilBrown3b343802006-01-06 00:20:47 -08002873 return len;
2874}
2875static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002876__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002877
NeilBrowna94213b2006-06-26 00:28:00 -07002878static ssize_t
2879resync_start_show(mddev_t *mddev, char *page)
2880{
NeilBrownd1a7c502009-03-31 15:24:32 +11002881 if (mddev->recovery_cp == MaxSector)
2882 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002883 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2884}
2885
2886static ssize_t
2887resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2888{
NeilBrowna94213b2006-06-26 00:28:00 -07002889 char *e;
2890 unsigned long long n = simple_strtoull(buf, &e, 10);
2891
2892 if (mddev->pers)
2893 return -EBUSY;
2894 if (!*buf || (*e && *e != '\n'))
2895 return -EINVAL;
2896
2897 mddev->recovery_cp = n;
2898 return len;
2899}
2900static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002901__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002902
NeilBrown9e653b62006-06-26 00:27:58 -07002903/*
2904 * The array state can be:
2905 *
2906 * clear
2907 * No devices, no size, no level
2908 * Equivalent to STOP_ARRAY ioctl
2909 * inactive
2910 * May have some settings, but array is not active
2911 * all IO results in error
2912 * When written, doesn't tear down array, but just stops it
2913 * suspended (not supported yet)
2914 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002915 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002916 * readonly
2917 * no resync can happen. no superblocks get written.
2918 * write requests fail
2919 * read-auto
2920 * like readonly, but behaves like 'clean' on a write request.
2921 *
2922 * clean - no pending writes, but otherwise active.
2923 * When written to inactive array, starts without resync
2924 * If a write request arrives then
2925 * if metadata is known, mark 'dirty' and switch to 'active'.
2926 * if not known, block and switch to write-pending
2927 * If written to an active array that has pending writes, then fails.
2928 * active
2929 * fully active: IO and resync can be happening.
2930 * When written to inactive array, starts with resync
2931 *
2932 * write-pending
2933 * clean, but writes are blocked waiting for 'active' to be written.
2934 *
2935 * active-idle
2936 * like active, but no writes have been seen for a while (100msec).
2937 *
2938 */
2939enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2940 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002941static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002942 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2943 "write-pending", "active-idle", NULL };
2944
2945static int match_word(const char *word, char **list)
2946{
2947 int n;
2948 for (n=0; list[n]; n++)
2949 if (cmd_match(word, list[n]))
2950 break;
2951 return n;
2952}
2953
2954static ssize_t
2955array_state_show(mddev_t *mddev, char *page)
2956{
2957 enum array_state st = inactive;
2958
2959 if (mddev->pers)
2960 switch(mddev->ro) {
2961 case 1:
2962 st = readonly;
2963 break;
2964 case 2:
2965 st = read_auto;
2966 break;
2967 case 0:
2968 if (mddev->in_sync)
2969 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002970 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2971 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002972 else if (mddev->safemode)
2973 st = active_idle;
2974 else
2975 st = active;
2976 }
2977 else {
2978 if (list_empty(&mddev->disks) &&
2979 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11002980 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002981 st = clear;
2982 else
2983 st = inactive;
2984 }
2985 return sprintf(page, "%s\n", array_states[st]);
2986}
2987
Neil Browndf5b20c2008-07-11 22:02:22 +10002988static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002989static int do_md_run(mddev_t * mddev);
2990static int restart_array(mddev_t *mddev);
2991
2992static ssize_t
2993array_state_store(mddev_t *mddev, const char *buf, size_t len)
2994{
2995 int err = -EINVAL;
2996 enum array_state st = match_word(buf, array_states);
2997 switch(st) {
2998 case bad_word:
2999 break;
3000 case clear:
3001 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003002 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003003 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003004 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003005 break;
3006 case inactive:
3007 /* stopping an active array */
3008 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003009 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003010 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003011 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003012 } else
3013 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003014 break;
3015 case suspended:
3016 break; /* not supported yet */
3017 case readonly:
3018 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003019 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003020 else {
3021 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003022 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003023 err = do_md_run(mddev);
3024 }
3025 break;
3026 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003027 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003028 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003029 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003030 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003031 err = restart_array(mddev);
3032 if (err == 0) {
3033 mddev->ro = 2;
3034 set_disk_ro(mddev->gendisk, 0);
3035 }
NeilBrown9e653b62006-06-26 00:27:58 -07003036 } else {
3037 mddev->ro = 2;
3038 err = do_md_run(mddev);
3039 }
3040 break;
3041 case clean:
3042 if (mddev->pers) {
3043 restart_array(mddev);
3044 spin_lock_irq(&mddev->write_lock);
3045 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003046 if (mddev->in_sync == 0) {
3047 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003048 if (mddev->safemode == 1)
3049 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003050 if (mddev->persistent)
3051 set_bit(MD_CHANGE_CLEAN,
3052 &mddev->flags);
3053 }
3054 err = 0;
3055 } else
3056 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003057 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003058 } else
3059 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003060 break;
3061 case active:
3062 if (mddev->pers) {
3063 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003064 if (mddev->external)
3065 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003066 wake_up(&mddev->sb_wait);
3067 err = 0;
3068 } else {
3069 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003070 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003071 err = do_md_run(mddev);
3072 }
3073 break;
3074 case write_pending:
3075 case active_idle:
3076 /* these cannot be set */
3077 break;
3078 }
3079 if (err)
3080 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003081 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003082 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003083 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003084 }
NeilBrown9e653b62006-06-26 00:27:58 -07003085}
NeilBrown80ca3a42006-07-10 04:44:18 -07003086static struct md_sysfs_entry md_array_state =
3087__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003088
NeilBrown6d7ff7382006-01-06 00:21:16 -08003089static ssize_t
3090null_show(mddev_t *mddev, char *page)
3091{
3092 return -EINVAL;
3093}
3094
3095static ssize_t
3096new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3097{
3098 /* buf must be %d:%d\n? giving major and minor numbers */
3099 /* The new device is added to the array.
3100 * If the array has a persistent superblock, we read the
3101 * superblock to initialise info and check validity.
3102 * Otherwise, only checking done is that in bind_rdev_to_array,
3103 * which mainly checks size.
3104 */
3105 char *e;
3106 int major = simple_strtoul(buf, &e, 10);
3107 int minor;
3108 dev_t dev;
3109 mdk_rdev_t *rdev;
3110 int err;
3111
3112 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3113 return -EINVAL;
3114 minor = simple_strtoul(e+1, &e, 10);
3115 if (*e && *e != '\n')
3116 return -EINVAL;
3117 dev = MKDEV(major, minor);
3118 if (major != MAJOR(dev) ||
3119 minor != MINOR(dev))
3120 return -EOVERFLOW;
3121
3122
3123 if (mddev->persistent) {
3124 rdev = md_import_device(dev, mddev->major_version,
3125 mddev->minor_version);
3126 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3127 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3128 mdk_rdev_t, same_set);
3129 err = super_types[mddev->major_version]
3130 .load_super(rdev, rdev0, mddev->minor_version);
3131 if (err < 0)
3132 goto out;
3133 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003134 } else if (mddev->external)
3135 rdev = md_import_device(dev, -2, -1);
3136 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003137 rdev = md_import_device(dev, -1, -1);
3138
3139 if (IS_ERR(rdev))
3140 return PTR_ERR(rdev);
3141 err = bind_rdev_to_array(rdev, mddev);
3142 out:
3143 if (err)
3144 export_rdev(rdev);
3145 return err ? err : len;
3146}
3147
3148static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003149__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003150
3151static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003152bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3153{
3154 char *end;
3155 unsigned long chunk, end_chunk;
3156
3157 if (!mddev->bitmap)
3158 goto out;
3159 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3160 while (*buf) {
3161 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3162 if (buf == end) break;
3163 if (*end == '-') { /* range */
3164 buf = end + 1;
3165 end_chunk = simple_strtoul(buf, &end, 0);
3166 if (buf == end) break;
3167 }
3168 if (*end && !isspace(*end)) break;
3169 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3170 buf = end;
3171 while (isspace(*buf)) buf++;
3172 }
3173 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3174out:
3175 return len;
3176}
3177
3178static struct md_sysfs_entry md_bitmap =
3179__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3180
3181static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003182size_show(mddev_t *mddev, char *page)
3183{
Andre Noll58c0fed2009-03-31 14:33:13 +11003184 return sprintf(page, "%llu\n",
3185 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003186}
3187
Andre Nolld71f9f82008-07-11 22:02:22 +10003188static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003189
3190static ssize_t
3191size_store(mddev_t *mddev, const char *buf, size_t len)
3192{
3193 /* If array is inactive, we can reduce the component size, but
3194 * not increase it (except from 0).
3195 * If array is active, we can try an on-line resize
3196 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003197 sector_t sectors;
3198 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003199
Andre Noll58c0fed2009-03-31 14:33:13 +11003200 if (err < 0)
3201 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003202 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003203 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003204 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003205 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003206 if (mddev->dev_sectors == 0 ||
3207 mddev->dev_sectors > sectors)
3208 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003209 else
3210 err = -ENOSPC;
3211 }
3212 return err ? err : len;
3213}
3214
3215static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003216__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003217
NeilBrown8bb93aa2006-01-06 00:20:50 -08003218
3219/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003220 * This is one of
3221 * 'none' for arrays with no metadata (good luck...)
3222 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003223 * or N.M for internally known formats
3224 */
3225static ssize_t
3226metadata_show(mddev_t *mddev, char *page)
3227{
3228 if (mddev->persistent)
3229 return sprintf(page, "%d.%d\n",
3230 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003231 else if (mddev->external)
3232 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003233 else
3234 return sprintf(page, "none\n");
3235}
3236
3237static ssize_t
3238metadata_store(mddev_t *mddev, const char *buf, size_t len)
3239{
3240 int major, minor;
3241 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003242 /* Changing the details of 'external' metadata is
3243 * always permitted. Otherwise there must be
3244 * no devices attached to the array.
3245 */
3246 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3247 ;
3248 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003249 return -EBUSY;
3250
3251 if (cmd_match(buf, "none")) {
3252 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003253 mddev->external = 0;
3254 mddev->major_version = 0;
3255 mddev->minor_version = 90;
3256 return len;
3257 }
3258 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003259 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003260 if (namelen >= sizeof(mddev->metadata_type))
3261 namelen = sizeof(mddev->metadata_type)-1;
3262 strncpy(mddev->metadata_type, buf+9, namelen);
3263 mddev->metadata_type[namelen] = 0;
3264 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3265 mddev->metadata_type[--namelen] = 0;
3266 mddev->persistent = 0;
3267 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003268 mddev->major_version = 0;
3269 mddev->minor_version = 90;
3270 return len;
3271 }
3272 major = simple_strtoul(buf, &e, 10);
3273 if (e==buf || *e != '.')
3274 return -EINVAL;
3275 buf = e+1;
3276 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003277 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003278 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003279 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003280 return -ENOENT;
3281 mddev->major_version = major;
3282 mddev->minor_version = minor;
3283 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003284 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003285 return len;
3286}
3287
3288static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003289__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003290
NeilBrowna35b0d62006-01-06 00:20:49 -08003291static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003292action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003293{
NeilBrown7eec3142005-11-08 21:39:44 -08003294 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003295 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3296 type = "frozen";
3297 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003298 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003299 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3300 type = "reshape";
3301 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003302 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3303 type = "resync";
3304 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3305 type = "check";
3306 else
3307 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003308 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003309 type = "recover";
3310 }
3311 return sprintf(page, "%s\n", type);
3312}
3313
3314static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003315action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003316{
NeilBrown7eec3142005-11-08 21:39:44 -08003317 if (!mddev->pers || !mddev->pers->sync_request)
3318 return -EINVAL;
3319
NeilBrownb6a9ce62009-05-26 09:41:17 +10003320 if (cmd_match(page, "frozen"))
3321 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3322 else
3323 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3324
3325 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003326 if (mddev->sync_thread) {
3327 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3328 md_unregister_thread(mddev->sync_thread);
3329 mddev->sync_thread = NULL;
3330 mddev->recovery = 0;
3331 }
NeilBrown03c902e2006-01-06 00:20:46 -08003332 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3333 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003334 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003335 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003336 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003337 else if (cmd_match(page, "recover")) {
3338 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3339 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3340 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003341 int err;
3342 if (mddev->pers->start_reshape == NULL)
3343 return -EINVAL;
3344 err = mddev->pers->start_reshape(mddev);
3345 if (err)
3346 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003347 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003348 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003349 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003350 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003351 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003352 return -EINVAL;
3353 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3354 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003355 }
NeilBrown03c902e2006-01-06 00:20:46 -08003356 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003357 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003358 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003359 return len;
3360}
3361
NeilBrown9d888832005-11-08 21:39:26 -08003362static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003363mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003364{
3365 return sprintf(page, "%llu\n",
3366 (unsigned long long) mddev->resync_mismatches);
3367}
3368
NeilBrown80ca3a42006-07-10 04:44:18 -07003369static struct md_sysfs_entry md_scan_mode =
3370__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003371
NeilBrown96de1e62005-11-08 21:39:39 -08003372
NeilBrown80ca3a42006-07-10 04:44:18 -07003373static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003374
NeilBrown88202a02006-01-06 00:21:36 -08003375static ssize_t
3376sync_min_show(mddev_t *mddev, char *page)
3377{
3378 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3379 mddev->sync_speed_min ? "local": "system");
3380}
3381
3382static ssize_t
3383sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3384{
3385 int min;
3386 char *e;
3387 if (strncmp(buf, "system", 6)==0) {
3388 mddev->sync_speed_min = 0;
3389 return len;
3390 }
3391 min = simple_strtoul(buf, &e, 10);
3392 if (buf == e || (*e && *e != '\n') || min <= 0)
3393 return -EINVAL;
3394 mddev->sync_speed_min = min;
3395 return len;
3396}
3397
3398static struct md_sysfs_entry md_sync_min =
3399__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3400
3401static ssize_t
3402sync_max_show(mddev_t *mddev, char *page)
3403{
3404 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3405 mddev->sync_speed_max ? "local": "system");
3406}
3407
3408static ssize_t
3409sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3410{
3411 int max;
3412 char *e;
3413 if (strncmp(buf, "system", 6)==0) {
3414 mddev->sync_speed_max = 0;
3415 return len;
3416 }
3417 max = simple_strtoul(buf, &e, 10);
3418 if (buf == e || (*e && *e != '\n') || max <= 0)
3419 return -EINVAL;
3420 mddev->sync_speed_max = max;
3421 return len;
3422}
3423
3424static struct md_sysfs_entry md_sync_max =
3425__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3426
Iustin Popd7f3d292007-10-16 23:30:54 -07003427static ssize_t
3428degraded_show(mddev_t *mddev, char *page)
3429{
3430 return sprintf(page, "%d\n", mddev->degraded);
3431}
3432static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003433
3434static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003435sync_force_parallel_show(mddev_t *mddev, char *page)
3436{
3437 return sprintf(page, "%d\n", mddev->parallel_resync);
3438}
3439
3440static ssize_t
3441sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3442{
3443 long n;
3444
3445 if (strict_strtol(buf, 10, &n))
3446 return -EINVAL;
3447
3448 if (n != 0 && n != 1)
3449 return -EINVAL;
3450
3451 mddev->parallel_resync = n;
3452
3453 if (mddev->sync_thread)
3454 wake_up(&resync_wait);
3455
3456 return len;
3457}
3458
3459/* force parallel resync, even with shared block devices */
3460static struct md_sysfs_entry md_sync_force_parallel =
3461__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3462 sync_force_parallel_show, sync_force_parallel_store);
3463
3464static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003465sync_speed_show(mddev_t *mddev, char *page)
3466{
3467 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003468 if (mddev->curr_resync == 0)
3469 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003470 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3471 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003472 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003473 db = resync - mddev->resync_mark_cnt;
3474 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003475}
3476
NeilBrown80ca3a42006-07-10 04:44:18 -07003477static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003478
3479static ssize_t
3480sync_completed_show(mddev_t *mddev, char *page)
3481{
Andre Noll58c0fed2009-03-31 14:33:13 +11003482 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003483
NeilBrownacb180b2009-04-14 16:28:34 +10003484 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3485 return sprintf(page, "none\n");
3486
NeilBrown88202a02006-01-06 00:21:36 -08003487 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003488 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003489 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003490 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003491
NeilBrownacb180b2009-04-14 16:28:34 +10003492 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003493 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003494}
3495
NeilBrown80ca3a42006-07-10 04:44:18 -07003496static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003497
NeilBrowne464eaf2006-03-27 01:18:14 -08003498static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003499min_sync_show(mddev_t *mddev, char *page)
3500{
3501 return sprintf(page, "%llu\n",
3502 (unsigned long long)mddev->resync_min);
3503}
3504static ssize_t
3505min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3506{
3507 unsigned long long min;
3508 if (strict_strtoull(buf, 10, &min))
3509 return -EINVAL;
3510 if (min > mddev->resync_max)
3511 return -EINVAL;
3512 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3513 return -EBUSY;
3514
3515 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003516 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003517 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003518 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003519 return -EINVAL;
3520 }
3521 mddev->resync_min = min;
3522
3523 return len;
3524}
3525
3526static struct md_sysfs_entry md_min_sync =
3527__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3528
3529static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003530max_sync_show(mddev_t *mddev, char *page)
3531{
3532 if (mddev->resync_max == MaxSector)
3533 return sprintf(page, "max\n");
3534 else
3535 return sprintf(page, "%llu\n",
3536 (unsigned long long)mddev->resync_max);
3537}
3538static ssize_t
3539max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3540{
3541 if (strncmp(buf, "max", 3) == 0)
3542 mddev->resync_max = MaxSector;
3543 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003544 unsigned long long max;
3545 if (strict_strtoull(buf, 10, &max))
3546 return -EINVAL;
3547 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003548 return -EINVAL;
3549 if (max < mddev->resync_max &&
3550 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3551 return -EBUSY;
3552
3553 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003554 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003555 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003556 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003557 return -EINVAL;
3558 }
3559 mddev->resync_max = max;
3560 }
3561 wake_up(&mddev->recovery_wait);
3562 return len;
3563}
3564
3565static struct md_sysfs_entry md_max_sync =
3566__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3567
3568static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003569suspend_lo_show(mddev_t *mddev, char *page)
3570{
3571 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3572}
3573
3574static ssize_t
3575suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3576{
3577 char *e;
3578 unsigned long long new = simple_strtoull(buf, &e, 10);
3579
3580 if (mddev->pers->quiesce == NULL)
3581 return -EINVAL;
3582 if (buf == e || (*e && *e != '\n'))
3583 return -EINVAL;
3584 if (new >= mddev->suspend_hi ||
3585 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3586 mddev->suspend_lo = new;
3587 mddev->pers->quiesce(mddev, 2);
3588 return len;
3589 } else
3590 return -EINVAL;
3591}
3592static struct md_sysfs_entry md_suspend_lo =
3593__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3594
3595
3596static ssize_t
3597suspend_hi_show(mddev_t *mddev, char *page)
3598{
3599 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3600}
3601
3602static ssize_t
3603suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3604{
3605 char *e;
3606 unsigned long long new = simple_strtoull(buf, &e, 10);
3607
3608 if (mddev->pers->quiesce == NULL)
3609 return -EINVAL;
3610 if (buf == e || (*e && *e != '\n'))
3611 return -EINVAL;
3612 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3613 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3614 mddev->suspend_hi = new;
3615 mddev->pers->quiesce(mddev, 1);
3616 mddev->pers->quiesce(mddev, 0);
3617 return len;
3618 } else
3619 return -EINVAL;
3620}
3621static struct md_sysfs_entry md_suspend_hi =
3622__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3623
NeilBrown08a02ec2007-05-09 02:35:38 -07003624static ssize_t
3625reshape_position_show(mddev_t *mddev, char *page)
3626{
3627 if (mddev->reshape_position != MaxSector)
3628 return sprintf(page, "%llu\n",
3629 (unsigned long long)mddev->reshape_position);
3630 strcpy(page, "none\n");
3631 return 5;
3632}
3633
3634static ssize_t
3635reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3636{
3637 char *e;
3638 unsigned long long new = simple_strtoull(buf, &e, 10);
3639 if (mddev->pers)
3640 return -EBUSY;
3641 if (buf == e || (*e && *e != '\n'))
3642 return -EINVAL;
3643 mddev->reshape_position = new;
3644 mddev->delta_disks = 0;
3645 mddev->new_level = mddev->level;
3646 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003647 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003648 return len;
3649}
3650
3651static struct md_sysfs_entry md_reshape_position =
3652__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3653 reshape_position_store);
3654
Dan Williamsb522adc2009-03-31 15:00:31 +11003655static ssize_t
3656array_size_show(mddev_t *mddev, char *page)
3657{
3658 if (mddev->external_size)
3659 return sprintf(page, "%llu\n",
3660 (unsigned long long)mddev->array_sectors/2);
3661 else
3662 return sprintf(page, "default\n");
3663}
3664
3665static ssize_t
3666array_size_store(mddev_t *mddev, const char *buf, size_t len)
3667{
3668 sector_t sectors;
3669
3670 if (strncmp(buf, "default", 7) == 0) {
3671 if (mddev->pers)
3672 sectors = mddev->pers->size(mddev, 0, 0);
3673 else
3674 sectors = mddev->array_sectors;
3675
3676 mddev->external_size = 0;
3677 } else {
3678 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3679 return -EINVAL;
3680 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003681 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003682
3683 mddev->external_size = 1;
3684 }
3685
3686 mddev->array_sectors = sectors;
3687 set_capacity(mddev->gendisk, mddev->array_sectors);
3688 if (mddev->pers) {
3689 struct block_device *bdev = bdget_disk(mddev->gendisk, 0);
3690
3691 if (bdev) {
3692 mutex_lock(&bdev->bd_inode->i_mutex);
3693 i_size_write(bdev->bd_inode,
3694 (loff_t)mddev->array_sectors << 9);
3695 mutex_unlock(&bdev->bd_inode->i_mutex);
3696 bdput(bdev);
3697 }
3698 }
3699
3700 return len;
3701}
3702
3703static struct md_sysfs_entry md_array_size =
3704__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3705 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003706
NeilBrowneae17012005-11-08 21:39:23 -08003707static struct attribute *md_default_attrs[] = {
3708 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003709 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003710 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003711 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003712 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003713 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003714 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003715 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003716 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003717 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003718 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003719 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003720 NULL,
3721};
3722
3723static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003724 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003725 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003726 &md_sync_min.attr,
3727 &md_sync_max.attr,
3728 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003729 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003730 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003731 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003732 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003733 &md_suspend_lo.attr,
3734 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003735 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003736 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003737 NULL,
3738};
NeilBrown411036f2005-11-08 21:39:40 -08003739static struct attribute_group md_redundancy_group = {
3740 .name = NULL,
3741 .attrs = md_redundancy_attrs,
3742};
3743
NeilBrowneae17012005-11-08 21:39:23 -08003744
3745static ssize_t
3746md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3747{
3748 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3749 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003750 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003751
3752 if (!entry->show)
3753 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003754 rv = mddev_lock(mddev);
3755 if (!rv) {
3756 rv = entry->show(mddev, page);
3757 mddev_unlock(mddev);
3758 }
NeilBrown96de1e62005-11-08 21:39:39 -08003759 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003760}
3761
3762static ssize_t
3763md_attr_store(struct kobject *kobj, struct attribute *attr,
3764 const char *page, size_t length)
3765{
3766 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3767 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003768 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003769
3770 if (!entry->store)
3771 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003772 if (!capable(CAP_SYS_ADMIN))
3773 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003774 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003775 if (mddev->hold_active == UNTIL_IOCTL)
3776 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003777 if (!rv) {
3778 rv = entry->store(mddev, page, length);
3779 mddev_unlock(mddev);
3780 }
NeilBrown96de1e62005-11-08 21:39:39 -08003781 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003782}
3783
3784static void md_free(struct kobject *ko)
3785{
3786 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003787
3788 if (mddev->sysfs_state)
3789 sysfs_put(mddev->sysfs_state);
3790
3791 if (mddev->gendisk) {
3792 del_gendisk(mddev->gendisk);
3793 put_disk(mddev->gendisk);
3794 }
3795 if (mddev->queue)
3796 blk_cleanup_queue(mddev->queue);
3797
NeilBrowneae17012005-11-08 21:39:23 -08003798 kfree(mddev);
3799}
3800
3801static struct sysfs_ops md_sysfs_ops = {
3802 .show = md_attr_show,
3803 .store = md_attr_store,
3804};
3805static struct kobj_type md_ktype = {
3806 .release = md_free,
3807 .sysfs_ops = &md_sysfs_ops,
3808 .default_attrs = md_default_attrs,
3809};
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811int mdp_major = 0;
3812
Dan Williams5fd3a172009-03-04 00:57:25 -07003813static void mddev_delayed_delete(struct work_struct *ws)
3814{
3815 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3816
3817 if (mddev->private == &md_redundancy_group) {
3818 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3819 if (mddev->sysfs_action)
3820 sysfs_put(mddev->sysfs_action);
3821 mddev->sysfs_action = NULL;
3822 mddev->private = NULL;
3823 }
3824 kobject_del(&mddev->kobj);
3825 kobject_put(&mddev->kobj);
3826}
3827
NeilBrownefeb53c2009-01-09 08:31:10 +11003828static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003830 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 mddev_t *mddev = mddev_find(dev);
3832 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003833 int partitioned;
3834 int shift;
3835 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003836 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003839 return -ENODEV;
3840
3841 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3842 shift = partitioned ? MdpMinorShift : 0;
3843 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
NeilBrownd3374822009-01-09 08:31:10 +11003845 /* wait for any previous instance if this device
3846 * to be completed removed (mddev_delayed_delete).
3847 */
3848 flush_scheduled_work();
3849
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003850 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003852 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003854 return -EEXIST;
3855 }
3856
3857 if (name) {
3858 /* Need to ensure that 'name' is not a duplicate.
3859 */
3860 mddev_t *mddev2;
3861 spin_lock(&all_mddevs_lock);
3862
3863 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3864 if (mddev2->gendisk &&
3865 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3866 spin_unlock(&all_mddevs_lock);
3867 return -EEXIST;
3868 }
3869 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
NeilBrown8b765392009-01-09 08:31:08 +11003871
3872 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3873 if (!mddev->queue) {
3874 mutex_unlock(&disks_mutex);
3875 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003876 return -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003877 }
NeilBrown409c57f2009-03-31 14:39:39 +11003878 mddev->queue->queuedata = mddev;
3879
NeilBrown8b765392009-01-09 08:31:08 +11003880 /* Can be unlocked because the queue is new: no concurrency */
3881 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3882
NeilBrown409c57f2009-03-31 14:39:39 +11003883 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 disk = alloc_disk(1 << shift);
3886 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003887 mutex_unlock(&disks_mutex);
NeilBrown8b765392009-01-09 08:31:08 +11003888 blk_cleanup_queue(mddev->queue);
3889 mddev->queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003891 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003893 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003895 if (name)
3896 strcpy(disk->disk_name, name);
3897 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003899 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 disk->fops = &md_fops;
3902 disk->private_data = mddev;
3903 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003904 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003905 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003906 * remove it now.
3907 */
3908 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 add_disk(disk);
3910 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003911 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3912 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003913 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003914 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003915 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3916 disk->disk_name);
NeilBrownb62b7592008-10-21 13:25:21 +11003917 else {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003918 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003919 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3920 }
NeilBrownd3374822009-01-09 08:31:10 +11003921 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003922 return 0;
3923}
3924
3925static struct kobject *md_probe(dev_t dev, int *part, void *data)
3926{
3927 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 return NULL;
3929}
3930
NeilBrownefeb53c2009-01-09 08:31:10 +11003931static int add_named_array(const char *val, struct kernel_param *kp)
3932{
3933 /* val must be "md_*" where * is not all digits.
3934 * We allocate an array with a large free minor number, and
3935 * set the name to val. val must not already be an active name.
3936 */
3937 int len = strlen(val);
3938 char buf[DISK_NAME_LEN];
3939
3940 while (len && val[len-1] == '\n')
3941 len--;
3942 if (len >= DISK_NAME_LEN)
3943 return -E2BIG;
3944 strlcpy(buf, val, len+1);
3945 if (strncmp(buf, "md_", 3) != 0)
3946 return -EINVAL;
3947 return md_alloc(0, buf);
3948}
3949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950static void md_safemode_timeout(unsigned long data)
3951{
3952 mddev_t *mddev = (mddev_t *) data;
3953
Neil Brown0fd62b82008-06-28 08:31:36 +10003954 if (!atomic_read(&mddev->writes_pending)) {
3955 mddev->safemode = 1;
3956 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11003957 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 md_wakeup_thread(mddev->thread);
3960}
3961
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003962static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964static int do_md_run(mddev_t * mddev)
3965{
NeilBrown2604b702006-01-06 00:20:36 -08003966 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 mdk_rdev_t *rdev;
3968 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003969 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
NeilBrowna757e642005-04-16 15:26:42 -07003971 if (list_empty(&mddev->disks))
3972 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
3975 if (mddev->pers)
3976 return -EBUSY;
3977
3978 /*
3979 * Analyze all RAID superblock(s)
3980 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003981 if (!mddev->raid_disks) {
3982 if (!mddev->persistent)
3983 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003984 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
NeilBrownd9d166c2006-01-06 00:20:51 -08003987 if (mddev->level != LEVEL_NONE)
3988 request_module("md-level-%d", mddev->level);
3989 else if (mddev->clevel[0])
3990 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 /*
3993 * Drop all container device buffers, from now on
3994 * the only valid external interface is through the md
3995 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003997 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003998 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 continue;
4000 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004001 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004002
4003 /* perform some consistency tests on the device.
4004 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004005 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004006 */
Andre Noll0f420352008-07-11 22:02:23 +10004007 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004008 if (mddev->dev_sectors &&
4009 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004010 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004011 printk("md: %s: data overlaps metadata\n",
4012 mdname(mddev));
4013 return -EINVAL;
4014 }
4015 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004016 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004017 > rdev->data_offset) {
4018 printk("md: %s: metadata overlaps data\n",
4019 mdname(mddev));
4020 return -EINVAL;
4021 }
4022 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004023 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 }
4025
4026 md_probe(mddev->unit, NULL, NULL);
4027 disk = mddev->gendisk;
4028 if (!disk)
4029 return -ENOMEM;
4030
4031 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004032 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004033 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004035 if (mddev->level != LEVEL_NONE)
4036 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4037 mddev->level);
4038 else
4039 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4040 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 return -EINVAL;
4042 }
NeilBrown2604b702006-01-06 00:20:36 -08004043 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004045 if (mddev->level != pers->level) {
4046 mddev->level = pers->level;
4047 mddev->new_level = pers->level;
4048 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004049 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004051 if (pers->level >= 4 && pers->level <= 6)
4052 /* Cannot support integrity (yet) */
4053 blk_integrity_unregister(mddev->gendisk);
4054
NeilBrownf6705572006-03-27 01:18:11 -08004055 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004056 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004057 /* This personality cannot handle reshaping... */
4058 mddev->pers = NULL;
4059 module_put(pers->owner);
4060 return -EINVAL;
4061 }
4062
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004063 if (pers->sync_request) {
4064 /* Warn if this is a potentially silly
4065 * configuration.
4066 */
4067 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4068 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004069 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004070
4071 list_for_each_entry(rdev, &mddev->disks, same_set)
4072 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004073 if (rdev < rdev2 &&
4074 rdev->bdev->bd_contains ==
4075 rdev2->bdev->bd_contains) {
4076 printk(KERN_WARNING
4077 "%s: WARNING: %s appears to be"
4078 " on the same physical disk as"
4079 " %s.\n",
4080 mdname(mddev),
4081 bdevname(rdev->bdev,b),
4082 bdevname(rdev2->bdev,b2));
4083 warned = 1;
4084 }
4085 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004086
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004087 if (warned)
4088 printk(KERN_WARNING
4089 "True protection against single-disk"
4090 " failure might be compromised.\n");
4091 }
4092
NeilBrown657390d2005-08-26 18:34:16 -07004093 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004094 /* may be over-ridden by personality */
4095 mddev->resync_max_sectors = mddev->dev_sectors;
4096
NeilBrowna9701a32005-11-08 21:39:34 -08004097 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004098 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
NeilBrownf91de922005-11-08 21:39:36 -08004100 if (start_readonly)
4101 mddev->ro = 2; /* read-only, but switch on first write */
4102
NeilBrownb15c2e52006-01-06 00:20:16 -08004103 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004104 if (err)
4105 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004106 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4107 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4108 " but 'external_size' not in effect?\n", __func__);
4109 printk(KERN_ERR
4110 "md: invalid array_size %llu > default size %llu\n",
4111 (unsigned long long)mddev->array_sectors / 2,
4112 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4113 err = -EINVAL;
4114 mddev->pers->stop(mddev);
4115 }
4116 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004117 err = bitmap_create(mddev);
4118 if (err) {
4119 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4120 mdname(mddev), err);
4121 mddev->pers->stop(mddev);
4122 }
4123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 module_put(mddev->pers->owner);
4126 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004127 bitmap_destroy(mddev);
4128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004130 if (mddev->pers->sync_request) {
4131 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4132 printk(KERN_WARNING
4133 "md: cannot register extra attributes for %s\n",
4134 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004135 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004136 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004137 mddev->ro = 0;
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 atomic_set(&mddev->writes_pending,0);
4140 mddev->safemode = 0;
4141 mddev->safemode_timer.function = md_safemode_timeout;
4142 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004143 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004145
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004146 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004147 if (rdev->raid_disk >= 0) {
4148 char nm[20];
4149 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004150 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4151 printk("md: cannot register %s for %s\n",
4152 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
4155 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4156
NeilBrown850b2b42006-10-03 01:15:46 -07004157 if (mddev->flags)
4158 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Andre Nollf233ea52008-07-21 17:05:22 +10004160 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004162 /* If there is a partially-recovered drive we need to
4163 * start recovery here. If we leave it to md_check_recovery,
4164 * it will remove the drives and not do the right thing
4165 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004166 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004167 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004168 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004169 if (rdev->raid_disk >= 0 &&
4170 !test_bit(In_sync, &rdev->flags) &&
4171 !test_bit(Faulty, &rdev->flags))
4172 /* complete an interrupted recovery */
4173 spares++;
4174 if (spares && mddev->pers->sync_request) {
4175 mddev->recovery = 0;
4176 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4177 mddev->sync_thread = md_register_thread(md_do_sync,
4178 mddev,
4179 "%s_resync");
4180 if (!mddev->sync_thread) {
4181 printk(KERN_ERR "%s: could not start resync"
4182 " thread...\n",
4183 mdname(mddev));
4184 /* leave the spares where they are, it shouldn't hurt */
4185 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004186 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004187 }
4188 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004189 md_wakeup_thread(mddev->thread);
4190 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004191
Linus Torvalds44ce62942007-05-09 18:51:36 -07004192 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004193 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004194 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004195 if (mddev->sysfs_action)
4196 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004197 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004198 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return 0;
4200}
4201
4202static int restart_array(mddev_t *mddev)
4203{
4204 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Andre Noll80fab1d2008-07-11 22:02:21 +10004206 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004208 return -ENXIO;
4209 if (!mddev->pers)
4210 return -EINVAL;
4211 if (!mddev->ro)
4212 return -EBUSY;
4213 mddev->safemode = 0;
4214 mddev->ro = 0;
4215 set_disk_ro(disk, 0);
4216 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4217 mdname(mddev));
4218 /* Kick recovery or resync if necessary */
4219 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4220 md_wakeup_thread(mddev->thread);
4221 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004222 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
NeilBrownacc55e22006-06-26 00:27:47 -07004226/* similar to deny_write_access, but accounts for our holding a reference
4227 * to the file ourselves */
4228static int deny_bitmap_write_access(struct file * file)
4229{
4230 struct inode *inode = file->f_mapping->host;
4231
4232 spin_lock(&inode->i_lock);
4233 if (atomic_read(&inode->i_writecount) > 1) {
4234 spin_unlock(&inode->i_lock);
4235 return -ETXTBSY;
4236 }
4237 atomic_set(&inode->i_writecount, -1);
4238 spin_unlock(&inode->i_lock);
4239
4240 return 0;
4241}
4242
4243static void restore_bitmap_write_access(struct file *file)
4244{
4245 struct inode *inode = file->f_mapping->host;
4246
4247 spin_lock(&inode->i_lock);
4248 atomic_set(&inode->i_writecount, 1);
4249 spin_unlock(&inode->i_lock);
4250}
4251
NeilBrown9e653b62006-06-26 00:27:58 -07004252/* mode:
4253 * 0 - completely stop and dis-assemble array
4254 * 1 - switch to readonly
4255 * 2 - stop but do not disassemble array
4256 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004257static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259 int err = 0;
4260 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004261 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
NeilBrownf2ea68c2008-07-21 17:05:25 +10004263 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004264 printk("md: %s still in use.\n",mdname(mddev));
4265 return -EBUSY;
4266 }
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
4270 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004271 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4273 md_unregister_thread(mddev->sync_thread);
4274 mddev->sync_thread = NULL;
4275 }
4276
4277 del_timer_sync(&mddev->safemode_timer);
4278
NeilBrown9e653b62006-06-26 00:27:58 -07004279 switch(mode) {
4280 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004282 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 goto out;
4284 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004285 break;
4286 case 0: /* disassemble */
4287 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004288 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004289 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (mddev->ro)
4291 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004294 mddev->queue->merge_bvec_fn = NULL;
4295 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004296 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004298 if (mddev->pers->sync_request)
4299 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004301 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004302 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004303
NeilBrownc4647292009-05-07 12:51:06 +10004304 list_for_each_entry(rdev, &mddev->disks, same_set)
4305 if (rdev->raid_disk >= 0) {
4306 char nm[20];
4307 sprintf(nm, "rd%d", rdev->raid_disk);
4308 sysfs_remove_link(&mddev->kobj, nm);
4309 }
4310
NeilBrown0d4ca602006-12-10 02:20:44 -08004311 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004312 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 if (mddev->ro)
4315 mddev->ro = 0;
4316 }
NeilBrown850b2b42006-10-03 01:15:46 -07004317 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 /* mark array as shutdown cleanly */
4319 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004320 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 }
NeilBrown9e653b62006-06-26 00:27:58 -07004322 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004324 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 }
NeilBrown32a76272005-06-21 17:17:14 -07004326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 /*
4328 * Free resources if final stop
4329 */
NeilBrown9e653b62006-06-26 00:27:58 -07004330 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4333
NeilBrown978f9462006-02-02 14:28:05 -08004334 bitmap_destroy(mddev);
4335 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004336 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004337 fput(mddev->bitmap_file);
4338 mddev->bitmap_file = NULL;
4339 }
4340 mddev->bitmap_offset = 0;
4341
NeilBrown177a99b2008-02-06 01:39:56 -08004342 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004343 flush_scheduled_work();
4344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 export_array(mddev);
4346
Andre Nollf233ea52008-07-21 17:05:22 +10004347 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004348 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004349 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004350 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004351 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004352 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004353 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004354 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004355 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004356 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004357 mddev->level = LEVEL_NONE;
4358 mddev->clevel[0] = 0;
4359 mddev->flags = 0;
4360 mddev->ro = 0;
4361 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004362 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004363 mddev->ctime = mddev->utime = 0;
4364 mddev->layout = 0;
4365 mddev->max_disks = 0;
4366 mddev->events = 0;
4367 mddev->delta_disks = 0;
4368 mddev->new_level = LEVEL_NONE;
4369 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004370 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004371 mddev->curr_resync = 0;
4372 mddev->resync_mismatches = 0;
4373 mddev->suspend_lo = mddev->suspend_hi = 0;
4374 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4375 mddev->recovery = 0;
4376 mddev->in_sync = 0;
4377 mddev->changed = 0;
4378 mddev->degraded = 0;
4379 mddev->barriers_work = 0;
4380 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004381 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004382 if (mddev->hold_active == UNTIL_STOP)
4383 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004384
NeilBrowna8a55c32006-06-26 00:27:39 -07004385 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4387 mdname(mddev));
4388 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004389 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004390 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004391 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392out:
4393 return err;
4394}
4395
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004396#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397static void autorun_array(mddev_t *mddev)
4398{
4399 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 int err;
4401
NeilBrowna757e642005-04-16 15:26:42 -07004402 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
4405 printk(KERN_INFO "md: running: ");
4406
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004407 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 char b[BDEVNAME_SIZE];
4409 printk("<%s>", bdevname(rdev->bdev,b));
4410 }
4411 printk("\n");
4412
NeilBrownd710e132008-10-13 11:55:12 +11004413 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 if (err) {
4415 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004416 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
4418}
4419
4420/*
4421 * lets try to run arrays based on all disks that have arrived
4422 * until now. (those are in pending_raid_disks)
4423 *
4424 * the method: pick the first pending disk, collect all disks with
4425 * the same UUID, remove all from the pending list and put them into
4426 * the 'same_array' list. Then order this list based on superblock
4427 * update time (freshest comes first), kick out 'old' disks and
4428 * compare superblocks. If everything's fine then run it.
4429 *
4430 * If "unit" is allocated, then bump its reference count
4431 */
4432static void autorun_devices(int part)
4433{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004434 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 mddev_t *mddev;
4436 char b[BDEVNAME_SIZE];
4437
4438 printk(KERN_INFO "md: autorun ...\n");
4439 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004440 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004442 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 rdev0 = list_entry(pending_raid_disks.next,
4444 mdk_rdev_t, same_set);
4445
4446 printk(KERN_INFO "md: considering %s ...\n",
4447 bdevname(rdev0->bdev,b));
4448 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004449 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (super_90_load(rdev, rdev0, 0) >= 0) {
4451 printk(KERN_INFO "md: adding %s ...\n",
4452 bdevname(rdev->bdev,b));
4453 list_move(&rdev->same_set, &candidates);
4454 }
4455 /*
4456 * now we have a set of devices, with all of them having
4457 * mostly sane superblocks. It's time to allocate the
4458 * mddev.
4459 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004460 if (part) {
4461 dev = MKDEV(mdp_major,
4462 rdev0->preferred_minor << MdpMinorShift);
4463 unit = MINOR(dev) >> MdpMinorShift;
4464 } else {
4465 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4466 unit = MINOR(dev);
4467 }
4468 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4470 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4471 break;
4472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 md_probe(dev, NULL, NULL);
4475 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004476 if (!mddev || !mddev->gendisk) {
4477 if (mddev)
4478 mddev_put(mddev);
4479 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 "md: cannot allocate memory for md drive.\n");
4481 break;
4482 }
4483 if (mddev_lock(mddev))
4484 printk(KERN_WARNING "md: %s locked, cannot run\n",
4485 mdname(mddev));
4486 else if (mddev->raid_disks || mddev->major_version
4487 || !list_empty(&mddev->disks)) {
4488 printk(KERN_WARNING
4489 "md: %s already running, cannot run %s\n",
4490 mdname(mddev), bdevname(rdev0->bdev,b));
4491 mddev_unlock(mddev);
4492 } else {
4493 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004494 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004495 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 list_del_init(&rdev->same_set);
4497 if (bind_rdev_to_array(rdev, mddev))
4498 export_rdev(rdev);
4499 }
4500 autorun_array(mddev);
4501 mddev_unlock(mddev);
4502 }
4503 /* on success, candidates will be empty, on error
4504 * it won't...
4505 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004506 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004507 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 mddev_put(mddev);
4511 }
4512 printk(KERN_INFO "md: ... autorun DONE.\n");
4513}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004514#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516static int get_version(void __user * arg)
4517{
4518 mdu_version_t ver;
4519
4520 ver.major = MD_MAJOR_VERSION;
4521 ver.minor = MD_MINOR_VERSION;
4522 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4523
4524 if (copy_to_user(arg, &ver, sizeof(ver)))
4525 return -EFAULT;
4526
4527 return 0;
4528}
4529
4530static int get_array_info(mddev_t * mddev, void __user * arg)
4531{
4532 mdu_array_info_t info;
4533 int nr,working,active,failed,spare;
4534 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004537 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004539 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 failed++;
4541 else {
4542 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004543 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 active++;
4545 else
4546 spare++;
4547 }
4548 }
4549
4550 info.major_version = mddev->major_version;
4551 info.minor_version = mddev->minor_version;
4552 info.patch_version = MD_PATCHLEVEL_VERSION;
4553 info.ctime = mddev->ctime;
4554 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004555 info.size = mddev->dev_sectors / 2;
4556 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004557 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 info.nr_disks = nr;
4559 info.raid_disks = mddev->raid_disks;
4560 info.md_minor = mddev->md_minor;
4561 info.not_persistent= !mddev->persistent;
4562
4563 info.utime = mddev->utime;
4564 info.state = 0;
4565 if (mddev->in_sync)
4566 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004567 if (mddev->bitmap && mddev->bitmap_offset)
4568 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 info.active_disks = active;
4570 info.working_disks = working;
4571 info.failed_disks = failed;
4572 info.spare_disks = spare;
4573
4574 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004575 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576
4577 if (copy_to_user(arg, &info, sizeof(info)))
4578 return -EFAULT;
4579
4580 return 0;
4581}
4582
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004583static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004584{
4585 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4586 char *ptr, *buf = NULL;
4587 int err = -ENOMEM;
4588
Dan Williamsb5470dc2008-06-27 21:44:04 -07004589 if (md_allow_write(mddev))
4590 file = kmalloc(sizeof(*file), GFP_NOIO);
4591 else
4592 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004593
NeilBrown32a76272005-06-21 17:17:14 -07004594 if (!file)
4595 goto out;
4596
4597 /* bitmap disabled, zero the first byte and copy out */
4598 if (!mddev->bitmap || !mddev->bitmap->file) {
4599 file->pathname[0] = '\0';
4600 goto copy_out;
4601 }
4602
4603 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4604 if (!buf)
4605 goto out;
4606
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004607 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4608 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004609 goto out;
4610
4611 strcpy(file->pathname, ptr);
4612
4613copy_out:
4614 err = 0;
4615 if (copy_to_user(arg, file, sizeof(*file)))
4616 err = -EFAULT;
4617out:
4618 kfree(buf);
4619 kfree(file);
4620 return err;
4621}
4622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623static int get_disk_info(mddev_t * mddev, void __user * arg)
4624{
4625 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 mdk_rdev_t *rdev;
4627
4628 if (copy_from_user(&info, arg, sizeof(info)))
4629 return -EFAULT;
4630
Andre Noll26ef3792008-07-11 22:02:21 +10004631 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 if (rdev) {
4633 info.major = MAJOR(rdev->bdev->bd_dev);
4634 info.minor = MINOR(rdev->bdev->bd_dev);
4635 info.raid_disk = rdev->raid_disk;
4636 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004637 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004639 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 info.state |= (1<<MD_DISK_ACTIVE);
4641 info.state |= (1<<MD_DISK_SYNC);
4642 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004643 if (test_bit(WriteMostly, &rdev->flags))
4644 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 } else {
4646 info.major = info.minor = 0;
4647 info.raid_disk = -1;
4648 info.state = (1<<MD_DISK_REMOVED);
4649 }
4650
4651 if (copy_to_user(arg, &info, sizeof(info)))
4652 return -EFAULT;
4653
4654 return 0;
4655}
4656
4657static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4658{
4659 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4660 mdk_rdev_t *rdev;
4661 dev_t dev = MKDEV(info->major,info->minor);
4662
4663 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4664 return -EOVERFLOW;
4665
4666 if (!mddev->raid_disks) {
4667 int err;
4668 /* expecting a device which has a superblock */
4669 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4670 if (IS_ERR(rdev)) {
4671 printk(KERN_WARNING
4672 "md: md_import_device returned %ld\n",
4673 PTR_ERR(rdev));
4674 return PTR_ERR(rdev);
4675 }
4676 if (!list_empty(&mddev->disks)) {
4677 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4678 mdk_rdev_t, same_set);
4679 int err = super_types[mddev->major_version]
4680 .load_super(rdev, rdev0, mddev->minor_version);
4681 if (err < 0) {
4682 printk(KERN_WARNING
4683 "md: %s has different UUID to %s\n",
4684 bdevname(rdev->bdev,b),
4685 bdevname(rdev0->bdev,b2));
4686 export_rdev(rdev);
4687 return -EINVAL;
4688 }
4689 }
4690 err = bind_rdev_to_array(rdev, mddev);
4691 if (err)
4692 export_rdev(rdev);
4693 return err;
4694 }
4695
4696 /*
4697 * add_new_disk can be used once the array is assembled
4698 * to add "hot spares". They must already have a superblock
4699 * written
4700 */
4701 if (mddev->pers) {
4702 int err;
4703 if (!mddev->pers->hot_add_disk) {
4704 printk(KERN_WARNING
4705 "%s: personality does not support diskops!\n",
4706 mdname(mddev));
4707 return -EINVAL;
4708 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004709 if (mddev->persistent)
4710 rdev = md_import_device(dev, mddev->major_version,
4711 mddev->minor_version);
4712 else
4713 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (IS_ERR(rdev)) {
4715 printk(KERN_WARNING
4716 "md: md_import_device returned %ld\n",
4717 PTR_ERR(rdev));
4718 return PTR_ERR(rdev);
4719 }
NeilBrown41158c72005-06-21 17:17:25 -07004720 /* set save_raid_disk if appropriate */
4721 if (!mddev->persistent) {
4722 if (info->state & (1<<MD_DISK_SYNC) &&
4723 info->raid_disk < mddev->raid_disks)
4724 rdev->raid_disk = info->raid_disk;
4725 else
4726 rdev->raid_disk = -1;
4727 } else
4728 super_types[mddev->major_version].
4729 validate_super(mddev, rdev);
4730 rdev->saved_raid_disk = rdev->raid_disk;
4731
NeilBrownb2d444d2005-11-08 21:39:31 -08004732 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004733 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4734 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004735 else
4736 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 rdev->raid_disk = -1;
4739 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004740 if (!err && !mddev->pers->hot_remove_disk) {
4741 /* If there is hot_add_disk but no hot_remove_disk
4742 * then added disks for geometry changes,
4743 * and should be added immediately.
4744 */
4745 super_types[mddev->major_version].
4746 validate_super(mddev, rdev);
4747 err = mddev->pers->hot_add_disk(mddev, rdev);
4748 if (err)
4749 unbind_rdev_from_array(rdev);
4750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 if (err)
4752 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004753 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004754 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004755
NeilBrown17571282006-12-10 02:20:52 -08004756 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004757 if (mddev->degraded)
4758 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004759 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004760 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 return err;
4762 }
4763
4764 /* otherwise, add_new_disk is only allowed
4765 * for major_version==0 superblocks
4766 */
4767 if (mddev->major_version != 0) {
4768 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4769 mdname(mddev));
4770 return -EINVAL;
4771 }
4772
4773 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4774 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004775 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 if (IS_ERR(rdev)) {
4777 printk(KERN_WARNING
4778 "md: error, md_import_device() returned %ld\n",
4779 PTR_ERR(rdev));
4780 return PTR_ERR(rdev);
4781 }
4782 rdev->desc_nr = info->number;
4783 if (info->raid_disk < mddev->raid_disks)
4784 rdev->raid_disk = info->raid_disk;
4785 else
4786 rdev->raid_disk = -1;
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004789 if (info->state & (1<<MD_DISK_SYNC))
4790 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004792 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4793 set_bit(WriteMostly, &rdev->flags);
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 if (!mddev->persistent) {
4796 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004797 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 } else
Andre Noll0f420352008-07-11 22:02:23 +10004799 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004800 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
NeilBrown2bf071b2006-01-06 00:20:55 -08004802 err = bind_rdev_to_array(rdev, mddev);
4803 if (err) {
4804 export_rdev(rdev);
4805 return err;
4806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 }
4808
4809 return 0;
4810}
4811
4812static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4813{
4814 char b[BDEVNAME_SIZE];
4815 mdk_rdev_t *rdev;
4816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 rdev = find_rdev(mddev, dev);
4818 if (!rdev)
4819 return -ENXIO;
4820
4821 if (rdev->raid_disk >= 0)
4822 goto busy;
4823
4824 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004825 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004826 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 return 0;
4829busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004830 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 bdevname(rdev->bdev,b), mdname(mddev));
4832 return -EBUSY;
4833}
4834
4835static int hot_add_disk(mddev_t * mddev, dev_t dev)
4836{
4837 char b[BDEVNAME_SIZE];
4838 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 mdk_rdev_t *rdev;
4840
4841 if (!mddev->pers)
4842 return -ENODEV;
4843
4844 if (mddev->major_version != 0) {
4845 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4846 " version-0 superblocks.\n",
4847 mdname(mddev));
4848 return -EINVAL;
4849 }
4850 if (!mddev->pers->hot_add_disk) {
4851 printk(KERN_WARNING
4852 "%s: personality does not support diskops!\n",
4853 mdname(mddev));
4854 return -EINVAL;
4855 }
4856
NeilBrownd710e132008-10-13 11:55:12 +11004857 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 if (IS_ERR(rdev)) {
4859 printk(KERN_WARNING
4860 "md: error, md_import_device() returned %ld\n",
4861 PTR_ERR(rdev));
4862 return -EINVAL;
4863 }
4864
4865 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004866 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 else
Andre Noll0f420352008-07-11 22:02:23 +10004868 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
NeilBrown8190e752009-06-18 08:48:58 +10004870 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
NeilBrownb2d444d2005-11-08 21:39:31 -08004872 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 printk(KERN_WARNING
4874 "md: can not hot-add faulty %s disk to %s!\n",
4875 bdevname(rdev->bdev,b), mdname(mddev));
4876 err = -EINVAL;
4877 goto abort_export;
4878 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004879 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004881 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004882 err = bind_rdev_to_array(rdev, mddev);
4883 if (err)
4884 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
4886 /*
4887 * The rest should better be atomic, we can have disk failures
4888 * noticed in interrupt contexts ...
4889 */
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 rdev->raid_disk = -1;
4892
NeilBrown850b2b42006-10-03 01:15:46 -07004893 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
4895 /*
4896 * Kick recovery, maybe this spare has to be added to the
4897 * array immediately.
4898 */
4899 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4900 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004901 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 return 0;
4903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904abort_export:
4905 export_rdev(rdev);
4906 return err;
4907}
4908
NeilBrown32a76272005-06-21 17:17:14 -07004909static int set_bitmap_file(mddev_t *mddev, int fd)
4910{
4911 int err;
4912
NeilBrown36fa3062005-09-09 16:23:45 -07004913 if (mddev->pers) {
4914 if (!mddev->pers->quiesce)
4915 return -EBUSY;
4916 if (mddev->recovery || mddev->sync_thread)
4917 return -EBUSY;
4918 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004919 }
4920
NeilBrown36fa3062005-09-09 16:23:45 -07004921
4922 if (fd >= 0) {
4923 if (mddev->bitmap)
4924 return -EEXIST; /* cannot add when bitmap is present */
4925 mddev->bitmap_file = fget(fd);
4926
4927 if (mddev->bitmap_file == NULL) {
4928 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4929 mdname(mddev));
4930 return -EBADF;
4931 }
4932
4933 err = deny_bitmap_write_access(mddev->bitmap_file);
4934 if (err) {
4935 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4936 mdname(mddev));
4937 fput(mddev->bitmap_file);
4938 mddev->bitmap_file = NULL;
4939 return err;
4940 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004941 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004942 } else if (mddev->bitmap == NULL)
4943 return -ENOENT; /* cannot remove what isn't there */
4944 err = 0;
4945 if (mddev->pers) {
4946 mddev->pers->quiesce(mddev, 1);
4947 if (fd >= 0)
4948 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004949 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004950 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004951 fd = -1; /* make sure to put the file */
4952 }
NeilBrown36fa3062005-09-09 16:23:45 -07004953 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004954 }
4955 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004956 if (mddev->bitmap_file) {
4957 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004958 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004959 }
NeilBrown36fa3062005-09-09 16:23:45 -07004960 mddev->bitmap_file = NULL;
4961 }
4962
NeilBrown32a76272005-06-21 17:17:14 -07004963 return err;
4964}
4965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966/*
4967 * set_array_info is used two different ways
4968 * The original usage is when creating a new array.
4969 * In this usage, raid_disks is > 0 and it together with
4970 * level, size, not_persistent,layout,chunksize determine the
4971 * shape of the array.
4972 * This will always create an array with a type-0.90.0 superblock.
4973 * The newer usage is when assembling an array.
4974 * In this case raid_disks will be 0, and the major_version field is
4975 * use to determine which style super-blocks are to be found on the devices.
4976 * The minor and patch _version numbers are also kept incase the
4977 * super_block handler wishes to interpret them.
4978 */
4979static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4980{
4981
4982 if (info->raid_disks == 0) {
4983 /* just setting version number for superblock loading */
4984 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004985 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 super_types[info->major_version].name == NULL) {
4987 /* maybe try to auto-load a module? */
4988 printk(KERN_INFO
4989 "md: superblock version %d not known\n",
4990 info->major_version);
4991 return -EINVAL;
4992 }
4993 mddev->major_version = info->major_version;
4994 mddev->minor_version = info->minor_version;
4995 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004996 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 return 0;
4998 }
4999 mddev->major_version = MD_MAJOR_VERSION;
5000 mddev->minor_version = MD_MINOR_VERSION;
5001 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5002 mddev->ctime = get_seconds();
5003
5004 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005005 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005006 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 mddev->raid_disks = info->raid_disks;
5008 /* don't set md_minor, it is determined by which /dev/md* was
5009 * openned
5010 */
5011 if (info->state & (1<<MD_SB_CLEAN))
5012 mddev->recovery_cp = MaxSector;
5013 else
5014 mddev->recovery_cp = 0;
5015 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005016 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005019 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
5021 mddev->max_disks = MD_SB_DISKS;
5022
NeilBrowne6910632008-02-06 01:39:51 -08005023 if (mddev->persistent)
5024 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005025 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
NeilBrownb2a27032005-11-28 13:44:12 -08005027 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5028 mddev->bitmap_offset = 0;
5029
NeilBrownf6705572006-03-27 01:18:11 -08005030 mddev->reshape_position = MaxSector;
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 /*
5033 * Generate a 128 bit UUID
5034 */
5035 get_random_bytes(mddev->uuid, 16);
5036
NeilBrownf6705572006-03-27 01:18:11 -08005037 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005038 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005039 mddev->new_layout = mddev->layout;
5040 mddev->delta_disks = 0;
5041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 return 0;
5043}
5044
Dan Williams1f403622009-03-31 14:59:03 +11005045void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5046{
Dan Williamsb522adc2009-03-31 15:00:31 +11005047 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5048
5049 if (mddev->external_size)
5050 return;
5051
Dan Williams1f403622009-03-31 14:59:03 +11005052 mddev->array_sectors = array_sectors;
5053}
5054EXPORT_SYMBOL(md_set_array_sectors);
5055
Andre Nolld71f9f82008-07-11 22:02:22 +10005056static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005057{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005058 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005059 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005060 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005061
5062 if (mddev->pers->resize == NULL)
5063 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005064 /* The "num_sectors" is the number of sectors of each device that
5065 * is used. This can only make sense for arrays with redundancy.
5066 * linear and raid0 always use whatever space is available. We can only
5067 * consider changing this number if no resync or reconstruction is
5068 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005069 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005070 * of each device. If num_sectors is zero, we find the largest size
5071 * that fits.
5072
NeilBrowna35b0d62006-01-06 00:20:49 -08005073 */
5074 if (mddev->sync_thread)
5075 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005076 if (mddev->bitmap)
5077 /* Sorry, cannot grow a bitmap yet, just remove it,
5078 * grow, and re-add.
5079 */
5080 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005081 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005082 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005083
Andre Nolld71f9f82008-07-11 22:02:22 +10005084 if (fit && (num_sectors == 0 || num_sectors > avail))
5085 num_sectors = avail;
5086 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005087 return -ENOSPC;
5088 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005089 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08005090 if (!rv) {
5091 struct block_device *bdev;
5092
5093 bdev = bdget_disk(mddev->gendisk, 0);
5094 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005095 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10005096 i_size_write(bdev->bd_inode,
5097 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005098 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08005099 bdput(bdev);
5100 }
5101 }
5102 return rv;
5103}
5104
NeilBrownda943b992006-01-06 00:20:54 -08005105static int update_raid_disks(mddev_t *mddev, int raid_disks)
5106{
5107 int rv;
5108 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005109 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005110 return -EINVAL;
5111 if (raid_disks <= 0 ||
5112 raid_disks >= mddev->max_disks)
5113 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005114 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005115 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005116 mddev->delta_disks = raid_disks - mddev->raid_disks;
5117
5118 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005119 return rv;
5120}
5121
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123/*
5124 * update_array_info is used to change the configuration of an
5125 * on-line array.
5126 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5127 * fields in the info are checked against the array.
5128 * Any differences that cannot be handled will cause an error.
5129 * Normally, only one change can be managed at a time.
5130 */
5131static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5132{
5133 int rv = 0;
5134 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005135 int state = 0;
5136
5137 /* calculate expected state,ignoring low bits */
5138 if (mddev->bitmap && mddev->bitmap_offset)
5139 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 if (mddev->major_version != info->major_version ||
5142 mddev->minor_version != info->minor_version ||
5143/* mddev->patch_version != info->patch_version || */
5144 mddev->ctime != info->ctime ||
5145 mddev->level != info->level ||
5146/* mddev->layout != info->layout || */
5147 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005148 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005149 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5150 ((state^info->state) & 0xfffffe00)
5151 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return -EINVAL;
5153 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005154 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5155 cnt++;
5156 if (mddev->raid_disks != info->raid_disks)
5157 cnt++;
5158 if (mddev->layout != info->layout)
5159 cnt++;
5160 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5161 cnt++;
5162 if (cnt == 0)
5163 return 0;
5164 if (cnt > 1)
5165 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
5167 if (mddev->layout != info->layout) {
5168 /* Change layout
5169 * we don't need to do anything at the md level, the
5170 * personality will take care of it all.
5171 */
NeilBrown50ac1682009-06-18 08:47:55 +10005172 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005174 else {
5175 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005176 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005177 if (rv)
5178 mddev->new_layout = mddev->layout;
5179 return rv;
5180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005182 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005183 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
NeilBrownda943b992006-01-06 00:20:54 -08005185 if (mddev->raid_disks != info->raid_disks)
5186 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
NeilBrown36fa3062005-09-09 16:23:45 -07005188 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5189 if (mddev->pers->quiesce == NULL)
5190 return -EINVAL;
5191 if (mddev->recovery || mddev->sync_thread)
5192 return -EBUSY;
5193 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5194 /* add the bitmap */
5195 if (mddev->bitmap)
5196 return -EEXIST;
5197 if (mddev->default_bitmap_offset == 0)
5198 return -EINVAL;
5199 mddev->bitmap_offset = mddev->default_bitmap_offset;
5200 mddev->pers->quiesce(mddev, 1);
5201 rv = bitmap_create(mddev);
5202 if (rv)
5203 bitmap_destroy(mddev);
5204 mddev->pers->quiesce(mddev, 0);
5205 } else {
5206 /* remove the bitmap */
5207 if (!mddev->bitmap)
5208 return -ENOENT;
5209 if (mddev->bitmap->file)
5210 return -EINVAL;
5211 mddev->pers->quiesce(mddev, 1);
5212 bitmap_destroy(mddev);
5213 mddev->pers->quiesce(mddev, 0);
5214 mddev->bitmap_offset = 0;
5215 }
5216 }
NeilBrown850b2b42006-10-03 01:15:46 -07005217 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return rv;
5219}
5220
5221static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5222{
5223 mdk_rdev_t *rdev;
5224
5225 if (mddev->pers == NULL)
5226 return -ENODEV;
5227
5228 rdev = find_rdev(mddev, dev);
5229 if (!rdev)
5230 return -ENODEV;
5231
5232 md_error(mddev, rdev);
5233 return 0;
5234}
5235
Andre Noll2f9618c2008-04-25 18:57:58 +02005236/*
5237 * We have a problem here : there is no easy way to give a CHS
5238 * virtual geometry. We currently pretend that we have a 2 heads
5239 * 4 sectors (with a BIG number of cylinders...). This drives
5240 * dosfs just mad... ;-)
5241 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005242static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5243{
5244 mddev_t *mddev = bdev->bd_disk->private_data;
5245
5246 geo->heads = 2;
5247 geo->sectors = 4;
5248 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5249 return 0;
5250}
5251
Al Viroa39907f2008-03-02 10:31:15 -05005252static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 unsigned int cmd, unsigned long arg)
5254{
5255 int err = 0;
5256 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 mddev_t *mddev = NULL;
5258
5259 if (!capable(CAP_SYS_ADMIN))
5260 return -EACCES;
5261
5262 /*
5263 * Commands dealing with the RAID driver but not any
5264 * particular array:
5265 */
5266 switch (cmd)
5267 {
5268 case RAID_VERSION:
5269 err = get_version(argp);
5270 goto done;
5271
5272 case PRINT_RAID_DEBUG:
5273 err = 0;
5274 md_print_devices();
5275 goto done;
5276
5277#ifndef MODULE
5278 case RAID_AUTORUN:
5279 err = 0;
5280 autostart_arrays(arg);
5281 goto done;
5282#endif
5283 default:;
5284 }
5285
5286 /*
5287 * Commands creating/starting a new array:
5288 */
5289
Al Viroa39907f2008-03-02 10:31:15 -05005290 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
5292 if (!mddev) {
5293 BUG();
5294 goto abort;
5295 }
5296
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 err = mddev_lock(mddev);
5298 if (err) {
5299 printk(KERN_INFO
5300 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5301 err, cmd);
5302 goto abort;
5303 }
5304
5305 switch (cmd)
5306 {
5307 case SET_ARRAY_INFO:
5308 {
5309 mdu_array_info_t info;
5310 if (!arg)
5311 memset(&info, 0, sizeof(info));
5312 else if (copy_from_user(&info, argp, sizeof(info))) {
5313 err = -EFAULT;
5314 goto abort_unlock;
5315 }
5316 if (mddev->pers) {
5317 err = update_array_info(mddev, &info);
5318 if (err) {
5319 printk(KERN_WARNING "md: couldn't update"
5320 " array info. %d\n", err);
5321 goto abort_unlock;
5322 }
5323 goto done_unlock;
5324 }
5325 if (!list_empty(&mddev->disks)) {
5326 printk(KERN_WARNING
5327 "md: array %s already has disks!\n",
5328 mdname(mddev));
5329 err = -EBUSY;
5330 goto abort_unlock;
5331 }
5332 if (mddev->raid_disks) {
5333 printk(KERN_WARNING
5334 "md: array %s already initialised!\n",
5335 mdname(mddev));
5336 err = -EBUSY;
5337 goto abort_unlock;
5338 }
5339 err = set_array_info(mddev, &info);
5340 if (err) {
5341 printk(KERN_WARNING "md: couldn't set"
5342 " array info. %d\n", err);
5343 goto abort_unlock;
5344 }
5345 }
5346 goto done_unlock;
5347
5348 default:;
5349 }
5350
5351 /*
5352 * Commands querying/configuring an existing array:
5353 */
NeilBrown32a76272005-06-21 17:17:14 -07005354 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005355 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005356 if ((!mddev->raid_disks && !mddev->external)
5357 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5358 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5359 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 err = -ENODEV;
5361 goto abort_unlock;
5362 }
5363
5364 /*
5365 * Commands even a read-only array can execute:
5366 */
5367 switch (cmd)
5368 {
5369 case GET_ARRAY_INFO:
5370 err = get_array_info(mddev, argp);
5371 goto done_unlock;
5372
NeilBrown32a76272005-06-21 17:17:14 -07005373 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005374 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005375 goto done_unlock;
5376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 case GET_DISK_INFO:
5378 err = get_disk_info(mddev, argp);
5379 goto done_unlock;
5380
5381 case RESTART_ARRAY_RW:
5382 err = restart_array(mddev);
5383 goto done_unlock;
5384
5385 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005386 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 goto done_unlock;
5388
5389 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005390 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 goto done_unlock;
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 }
5394
5395 /*
5396 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005397 * superblock, so we do not allow them on read-only arrays.
5398 * However non-MD ioctls (e.g. get-size) will still come through
5399 * here and hit the 'default' below, so only disallow
5400 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005402 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005403 if (mddev->ro == 2) {
5404 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005405 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005406 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5407 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005408 } else {
5409 err = -EROFS;
5410 goto abort_unlock;
5411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 }
5413
5414 switch (cmd)
5415 {
5416 case ADD_NEW_DISK:
5417 {
5418 mdu_disk_info_t info;
5419 if (copy_from_user(&info, argp, sizeof(info)))
5420 err = -EFAULT;
5421 else
5422 err = add_new_disk(mddev, &info);
5423 goto done_unlock;
5424 }
5425
5426 case HOT_REMOVE_DISK:
5427 err = hot_remove_disk(mddev, new_decode_dev(arg));
5428 goto done_unlock;
5429
5430 case HOT_ADD_DISK:
5431 err = hot_add_disk(mddev, new_decode_dev(arg));
5432 goto done_unlock;
5433
5434 case SET_DISK_FAULTY:
5435 err = set_disk_faulty(mddev, new_decode_dev(arg));
5436 goto done_unlock;
5437
5438 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005439 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 goto done_unlock;
5441
NeilBrown32a76272005-06-21 17:17:14 -07005442 case SET_BITMAP_FILE:
5443 err = set_bitmap_file(mddev, (int)arg);
5444 goto done_unlock;
5445
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 err = -EINVAL;
5448 goto abort_unlock;
5449 }
5450
5451done_unlock:
5452abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005453 if (mddev->hold_active == UNTIL_IOCTL &&
5454 err != -EINVAL)
5455 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 mddev_unlock(mddev);
5457
5458 return err;
5459done:
5460 if (err)
5461 MD_BUG();
5462abort:
5463 return err;
5464}
5465
Al Viroa39907f2008-03-02 10:31:15 -05005466static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467{
5468 /*
5469 * Succeed if we can lock the mddev, which confirms that
5470 * it isn't being stopped right now.
5471 */
NeilBrownd3374822009-01-09 08:31:10 +11005472 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 int err;
5474
NeilBrownd3374822009-01-09 08:31:10 +11005475 if (mddev->gendisk != bdev->bd_disk) {
5476 /* we are racing with mddev_put which is discarding this
5477 * bd_disk.
5478 */
5479 mddev_put(mddev);
5480 /* Wait until bdev->bd_disk is definitely gone */
5481 flush_scheduled_work();
5482 /* Then retry the open from the top */
5483 return -ERESTARTSYS;
5484 }
5485 BUG_ON(mddev != bdev->bd_disk->private_data);
5486
NeilBrownd63a5a72006-12-08 02:36:17 -08005487 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 goto out;
5489
5490 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005491 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 mddev_unlock(mddev);
5493
Al Viroa39907f2008-03-02 10:31:15 -05005494 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 out:
5496 return err;
5497}
5498
Al Viroa39907f2008-03-02 10:31:15 -05005499static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500{
Al Viroa39907f2008-03-02 10:31:15 -05005501 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005503 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005504 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 mddev_put(mddev);
5506
5507 return 0;
5508}
5509
Linus Torvalds44ce62942007-05-09 18:51:36 -07005510static int md_media_changed(struct gendisk *disk)
5511{
5512 mddev_t *mddev = disk->private_data;
5513
5514 return mddev->changed;
5515}
5516
5517static int md_revalidate(struct gendisk *disk)
5518{
5519 mddev_t *mddev = disk->private_data;
5520
5521 mddev->changed = 0;
5522 return 0;
5523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524static struct block_device_operations md_fops =
5525{
5526 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005527 .open = md_open,
5528 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005529 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005530 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005531 .media_changed = md_media_changed,
5532 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533};
5534
Adrian Bunk75c96f82005-05-05 16:16:09 -07005535static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536{
5537 mdk_thread_t *thread = arg;
5538
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 /*
5540 * md_thread is a 'system-thread', it's priority should be very
5541 * high. We avoid resource deadlocks individually in each
5542 * raid personality. (RAID5 does preallocation) We also use RR and
5543 * the very same RT priority as kswapd, thus we will never get
5544 * into a priority inversion deadlock.
5545 *
5546 * we definitely have to have equal or higher priority than
5547 * bdflush, otherwise bdflush will deadlock if there are too
5548 * many dirty RAID5 blocks.
5549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
NeilBrown6985c432005-10-19 21:23:47 -07005551 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005552 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
NeilBrown93588e22005-11-15 00:09:12 -08005554 /* We need to wait INTERRUPTIBLE so that
5555 * we don't add to the load-average.
5556 * That means we need to be sure no signals are
5557 * pending
5558 */
5559 if (signal_pending(current))
5560 flush_signals(current);
5561
5562 wait_event_interruptible_timeout
5563 (thread->wqueue,
5564 test_bit(THREAD_WAKEUP, &thread->flags)
5565 || kthread_should_stop(),
5566 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
5568 clear_bit(THREAD_WAKEUP, &thread->flags);
5569
NeilBrown787453c2005-11-08 21:39:43 -08005570 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return 0;
5574}
5575
5576void md_wakeup_thread(mdk_thread_t *thread)
5577{
5578 if (thread) {
5579 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5580 set_bit(THREAD_WAKEUP, &thread->flags);
5581 wake_up(&thread->wqueue);
5582 }
5583}
5584
5585mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5586 const char *name)
5587{
5588 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
NeilBrown9ffae0c2006-01-06 00:20:32 -08005590 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 if (!thread)
5592 return NULL;
5593
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 init_waitqueue_head(&thread->wqueue);
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 thread->run = run;
5597 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005598 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005599 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005600 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 kfree(thread);
5602 return NULL;
5603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return thread;
5605}
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607void md_unregister_thread(mdk_thread_t *thread)
5608{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005609 if (!thread)
5610 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005611 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005612
5613 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 kfree(thread);
5615}
5616
5617void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5618{
5619 if (!mddev) {
5620 MD_BUG();
5621 return;
5622 }
5623
NeilBrownb2d444d2005-11-08 21:39:31 -08005624 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005626
5627 if (mddev->external)
5628 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5631 mdname(mddev),
5632 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5633 __builtin_return_address(0),__builtin_return_address(1),
5634 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005635*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005636 if (!mddev->pers)
5637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 if (!mddev->pers->error_handler)
5639 return;
5640 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005641 if (mddev->degraded)
5642 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005643 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5645 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5646 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005647 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648}
5649
5650/* seq_file implementation /proc/mdstat */
5651
5652static void status_unused(struct seq_file *seq)
5653{
5654 int i = 0;
5655 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
5657 seq_printf(seq, "unused devices: ");
5658
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005659 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 char b[BDEVNAME_SIZE];
5661 i++;
5662 seq_printf(seq, "%s ",
5663 bdevname(rdev->bdev,b));
5664 }
5665 if (!i)
5666 seq_printf(seq, "<none>");
5667
5668 seq_printf(seq, "\n");
5669}
5670
5671
5672static void status_resync(struct seq_file *seq, mddev_t * mddev)
5673{
NeilBrowndd71cf62009-05-07 12:49:35 +10005674 sector_t max_sectors, resync, res;
5675 unsigned long dt, db;
5676 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005677 int scale;
5678 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
NeilBrowndd71cf62009-05-07 12:49:35 +10005680 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005683 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005685 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 /*
5688 * Should not happen.
5689 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005690 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 MD_BUG();
5692 return;
5693 }
NeilBrown4588b422006-03-27 01:18:04 -08005694 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005695 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005696 * u32, as those are the requirements for sector_div.
5697 * Thus 'scale' must be at least 10
5698 */
5699 scale = 10;
5700 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005701 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005702 scale++;
5703 }
5704 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005705 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005706
5707 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 {
NeilBrown4588b422006-03-27 01:18:04 -08005709 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 seq_printf(seq, "[");
5711 for (i = 0; i < x; i++)
5712 seq_printf(seq, "=");
5713 seq_printf(seq, ">");
5714 for (i = 0; i < y; i++)
5715 seq_printf(seq, ".");
5716 seq_printf(seq, "] ");
5717 }
NeilBrown4588b422006-03-27 01:18:04 -08005718 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005719 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5720 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005721 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5722 "check" :
5723 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5724 "resync" : "recovery"))),
5725 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005726 (unsigned long long) resync/2,
5727 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 * dt: time from mark until now
5731 * db: blocks written from mark until now
5732 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005733 *
5734 * rt is a sector_t, so could be 32bit or 64bit.
5735 * So we divide before multiply in case it is 32bit and close
5736 * to the limit.
5737 * We scale the divisor (db) by 32 to avoid loosing precision
5738 * near the end of resync when the number of remaining sectors
5739 * is close to 'db'.
5740 * We then divide rt by 32 after multiplying by db to compensate.
5741 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 */
5743 dt = ((jiffies - mddev->resync_mark) / HZ);
5744 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005745 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5746 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
NeilBrowndd71cf62009-05-07 12:49:35 +10005748 rt = max_sectors - resync; /* number of remaining sectors */
5749 sector_div(rt, db/32+1);
5750 rt *= dt;
5751 rt >>= 5;
5752
5753 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5754 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
NeilBrownff4e8d92006-07-10 04:44:16 -07005756 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757}
5758
5759static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5760{
5761 struct list_head *tmp;
5762 loff_t l = *pos;
5763 mddev_t *mddev;
5764
5765 if (l >= 0x10000)
5766 return NULL;
5767 if (!l--)
5768 /* header */
5769 return (void*)1;
5770
5771 spin_lock(&all_mddevs_lock);
5772 list_for_each(tmp,&all_mddevs)
5773 if (!l--) {
5774 mddev = list_entry(tmp, mddev_t, all_mddevs);
5775 mddev_get(mddev);
5776 spin_unlock(&all_mddevs_lock);
5777 return mddev;
5778 }
5779 spin_unlock(&all_mddevs_lock);
5780 if (!l--)
5781 return (void*)2;/* tail */
5782 return NULL;
5783}
5784
5785static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5786{
5787 struct list_head *tmp;
5788 mddev_t *next_mddev, *mddev = v;
5789
5790 ++*pos;
5791 if (v == (void*)2)
5792 return NULL;
5793
5794 spin_lock(&all_mddevs_lock);
5795 if (v == (void*)1)
5796 tmp = all_mddevs.next;
5797 else
5798 tmp = mddev->all_mddevs.next;
5799 if (tmp != &all_mddevs)
5800 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5801 else {
5802 next_mddev = (void*)2;
5803 *pos = 0x10000;
5804 }
5805 spin_unlock(&all_mddevs_lock);
5806
5807 if (v != (void*)1)
5808 mddev_put(mddev);
5809 return next_mddev;
5810
5811}
5812
5813static void md_seq_stop(struct seq_file *seq, void *v)
5814{
5815 mddev_t *mddev = v;
5816
5817 if (mddev && v != (void*)1 && v != (void*)2)
5818 mddev_put(mddev);
5819}
5820
NeilBrownd7603b72006-01-06 00:20:30 -08005821struct mdstat_info {
5822 int event;
5823};
5824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825static int md_seq_show(struct seq_file *seq, void *v)
5826{
5827 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005828 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005830 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005831 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
5833 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005834 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 seq_printf(seq, "Personalities : ");
5836 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005837 list_for_each_entry(pers, &pers_list, list)
5838 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
5840 spin_unlock(&pers_lock);
5841 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005842 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 return 0;
5844 }
5845 if (v == (void*)2) {
5846 status_unused(seq);
5847 return 0;
5848 }
5849
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005850 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5854 seq_printf(seq, "%s : %sactive", mdname(mddev),
5855 mddev->pers ? "" : "in");
5856 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005857 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005859 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005860 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 seq_printf(seq, " %s", mddev->pers->name);
5862 }
5863
Andre Nolldd8ac332009-03-31 14:33:13 +11005864 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005865 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 char b[BDEVNAME_SIZE];
5867 seq_printf(seq, " %s[%d]",
5868 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005869 if (test_bit(WriteMostly, &rdev->flags))
5870 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005871 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 seq_printf(seq, "(F)");
5873 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005874 } else if (rdev->raid_disk < 0)
5875 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005876 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 }
5878
5879 if (!list_empty(&mddev->disks)) {
5880 if (mddev->pers)
5881 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005882 (unsigned long long)
5883 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 else
5885 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005886 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005888 if (mddev->persistent) {
5889 if (mddev->major_version != 0 ||
5890 mddev->minor_version != 90) {
5891 seq_printf(seq," super %d.%d",
5892 mddev->major_version,
5893 mddev->minor_version);
5894 }
NeilBrowne6910632008-02-06 01:39:51 -08005895 } else if (mddev->external)
5896 seq_printf(seq, " super external:%s",
5897 mddev->metadata_type);
5898 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005899 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
5901 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005902 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005904 if (mddev->pers->sync_request) {
5905 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005906 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005907 seq_printf(seq, "\n ");
5908 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5909 seq_printf(seq, "\tresync=DELAYED\n ");
5910 else if (mddev->recovery_cp < MaxSector)
5911 seq_printf(seq, "\tresync=PENDING\n ");
5912 }
NeilBrown32a76272005-06-21 17:17:14 -07005913 } else
5914 seq_printf(seq, "\n ");
5915
5916 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005917 unsigned long chunk_kb;
5918 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005919 spin_lock_irqsave(&bitmap->lock, flags);
5920 chunk_kb = bitmap->chunksize >> 10;
5921 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5922 "%lu%s chunk",
5923 bitmap->pages - bitmap->missing_pages,
5924 bitmap->pages,
5925 (bitmap->pages - bitmap->missing_pages)
5926 << (PAGE_SHIFT - 10),
5927 chunk_kb ? chunk_kb : bitmap->chunksize,
5928 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005929 if (bitmap->file) {
5930 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005931 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005932 }
NeilBrown78d742d2005-06-21 17:17:15 -07005933
NeilBrown32a76272005-06-21 17:17:14 -07005934 seq_printf(seq, "\n");
5935 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 }
5937
5938 seq_printf(seq, "\n");
5939 }
5940 mddev_unlock(mddev);
5941
5942 return 0;
5943}
5944
Jan Engelhardt110518b2009-05-07 12:49:37 +10005945static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 .start = md_seq_start,
5947 .next = md_seq_next,
5948 .stop = md_seq_stop,
5949 .show = md_seq_show,
5950};
5951
5952static int md_seq_open(struct inode *inode, struct file *file)
5953{
5954 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005955 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5956 if (mi == NULL)
5957 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
5959 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005960 if (error)
5961 kfree(mi);
5962 else {
5963 struct seq_file *p = file->private_data;
5964 p->private = mi;
5965 mi->event = atomic_read(&md_event_count);
5966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 return error;
5968}
5969
NeilBrownd7603b72006-01-06 00:20:30 -08005970static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5971{
5972 struct seq_file *m = filp->private_data;
5973 struct mdstat_info *mi = m->private;
5974 int mask;
5975
5976 poll_wait(filp, &md_event_waiters, wait);
5977
5978 /* always allow read */
5979 mask = POLLIN | POLLRDNORM;
5980
5981 if (mi->event != atomic_read(&md_event_count))
5982 mask |= POLLERR | POLLPRI;
5983 return mask;
5984}
5985
Arjan van de Venfa027c22007-02-12 00:55:33 -08005986static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005987 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 .open = md_seq_open,
5989 .read = seq_read,
5990 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005991 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005992 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993};
5994
NeilBrown2604b702006-01-06 00:20:36 -08005995int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005998 list_add_tail(&p->list, &pers_list);
5999 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 spin_unlock(&pers_lock);
6001 return 0;
6002}
6003
NeilBrown2604b702006-01-06 00:20:36 -08006004int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005{
NeilBrown2604b702006-01-06 00:20:36 -08006006 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006008 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 spin_unlock(&pers_lock);
6010 return 0;
6011}
6012
NeilBrowneea1bf32009-03-31 14:27:02 +11006013static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
6015 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006017 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
6019 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006020 rcu_read_lock();
6021 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006023 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6024 (int)part_stat_read(&disk->part0, sectors[1]) -
6025 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006026 /* sync IO will cause sync_io to increase before the disk_stats
6027 * as sync_io is counted when a request starts, and
6028 * disk_stats is counted when it completes.
6029 * So resync activity will cause curr_events to be smaller than
6030 * when there was no such activity.
6031 * non-sync IO will cause disk_stat to increase without
6032 * increasing sync_io so curr_events will (eventually)
6033 * be larger than it was before. Once it becomes
6034 * substantially larger, the test below will cause
6035 * the array to appear non-idle, and resync will slow
6036 * down.
6037 * If there is a lot of outstanding resync activity when
6038 * we set last_event to curr_events, then all that activity
6039 * completing might cause the array to appear non-idle
6040 * and resync will be slowed down even though there might
6041 * not have been non-resync activity. This will only
6042 * happen once though. 'last_events' will soon reflect
6043 * the state where there is little or no outstanding
6044 * resync requests, and further resync activity will
6045 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006046 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006048 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 rdev->last_events = curr_events;
6050 idle = 0;
6051 }
6052 }
NeilBrown4b809912008-07-21 17:05:25 +10006053 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 return idle;
6055}
6056
6057void md_done_sync(mddev_t *mddev, int blocks, int ok)
6058{
6059 /* another "blocks" (512byte) blocks have been synced */
6060 atomic_sub(blocks, &mddev->recovery_active);
6061 wake_up(&mddev->recovery_wait);
6062 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006063 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 md_wakeup_thread(mddev->thread);
6065 // stop recovery, signal do_sync ....
6066 }
6067}
6068
6069
NeilBrown06d91a52005-06-21 17:17:12 -07006070/* md_write_start(mddev, bi)
6071 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006072 * in superblock) before writing, schedule a superblock update
6073 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006074 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006075void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076{
Neil Brown0fd62b82008-06-28 08:31:36 +10006077 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006078 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006079 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006080
NeilBrownf91de922005-11-08 21:39:36 -08006081 BUG_ON(mddev->ro == 1);
6082 if (mddev->ro == 2) {
6083 /* need to switch to read/write */
6084 mddev->ro = 0;
6085 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6086 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006087 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006088 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006089 }
NeilBrown06d91a52005-06-21 17:17:12 -07006090 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006091 if (mddev->safemode == 1)
6092 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006093 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006094 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006095 if (mddev->in_sync) {
6096 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006097 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006098 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006099 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006100 }
NeilBrowna9701a32005-11-08 21:39:34 -08006101 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006102 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006103 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006104 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006105 wait_event(mddev->sb_wait,
6106 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6107 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108}
6109
6110void md_write_end(mddev_t *mddev)
6111{
6112 if (atomic_dec_and_test(&mddev->writes_pending)) {
6113 if (mddev->safemode == 2)
6114 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006115 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6117 }
6118}
6119
NeilBrown2a2275d2007-01-26 00:57:11 -08006120/* md_allow_write(mddev)
6121 * Calling this ensures that the array is marked 'active' so that writes
6122 * may proceed without blocking. It is important to call this before
6123 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6124 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006125 *
6126 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6127 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006128 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006129int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006130{
6131 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006132 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006133 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006134 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006135 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006136 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006137
6138 spin_lock_irq(&mddev->write_lock);
6139 if (mddev->in_sync) {
6140 mddev->in_sync = 0;
6141 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6142 if (mddev->safemode_delay &&
6143 mddev->safemode == 0)
6144 mddev->safemode = 1;
6145 spin_unlock_irq(&mddev->write_lock);
6146 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006147 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006148 } else
6149 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006150
6151 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6152 return -EAGAIN;
6153 else
6154 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006155}
6156EXPORT_SYMBOL_GPL(md_allow_write);
6157
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158#define SYNC_MARKS 10
6159#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006160void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161{
6162 mddev_t *mddev2;
6163 unsigned int currspeed = 0,
6164 window;
NeilBrown57afd892005-06-21 17:17:13 -07006165 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 unsigned long mark[SYNC_MARKS];
6167 sector_t mark_cnt[SYNC_MARKS];
6168 int last_mark,m;
6169 struct list_head *tmp;
6170 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006171 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006172 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006173 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
6175 /* just incase thread restarts... */
6176 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6177 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006178 if (mddev->ro) /* never try to sync a read-only array */
6179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
NeilBrown61df9d92006-10-03 01:15:57 -07006181 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6182 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6183 desc = "data-check";
6184 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6185 desc = "requested-resync";
6186 else
6187 desc = "resync";
6188 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6189 desc = "reshape";
6190 else
6191 desc = "recovery";
6192
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 /* we overload curr_resync somewhat here.
6194 * 0 == not engaged in resync at all
6195 * 2 == checking that there is no conflict with another sync
6196 * 1 == like 2, but have yielded to allow conflicting resync to
6197 * commense
6198 * other == active in resync - this many blocks
6199 *
6200 * Before starting a resync we must have set curr_resync to
6201 * 2, and then checked that every "conflicting" array has curr_resync
6202 * less than ours. When we find one that is the same or higher
6203 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6204 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6205 * This will mean we have to start checking from the beginning again.
6206 *
6207 */
6208
6209 do {
6210 mddev->curr_resync = 2;
6211
6212 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006213 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006214 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 goto skip;
6216 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006217 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 if (mddev2 == mddev)
6219 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006220 if (!mddev->parallel_resync
6221 && mddev2->curr_resync
6222 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 DEFINE_WAIT(wq);
6224 if (mddev < mddev2 && mddev->curr_resync == 2) {
6225 /* arbitrarily yield */
6226 mddev->curr_resync = 1;
6227 wake_up(&resync_wait);
6228 }
6229 if (mddev > mddev2 && mddev->curr_resync == 1)
6230 /* no need to wait here, we can wait the next
6231 * time 'round when curr_resync == 2
6232 */
6233 continue;
NeilBrown97441972008-09-19 11:49:54 +10006234 /* We need to wait 'interruptible' so as not to
6235 * contribute to the load average, and not to
6236 * be caught by 'softlockup'
6237 */
6238 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006239 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006240 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006241 printk(KERN_INFO "md: delaying %s of %s"
6242 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006244 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006246 if (signal_pending(current))
6247 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 schedule();
6249 finish_wait(&resync_wait, &wq);
6250 goto try_again;
6251 }
6252 finish_wait(&resync_wait, &wq);
6253 }
6254 }
6255 } while (mddev->curr_resync < 2);
6256
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006257 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006258 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006260 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 */
6262 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006263 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006264 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006265 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6266 j = mddev->resync_min;
6267 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006268 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006269
NeilBrownccfcc3c2006-03-27 01:18:09 -08006270 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006271 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006272 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006274 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006275 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006276 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006277 if (rdev->raid_disk >= 0 &&
6278 !test_bit(Faulty, &rdev->flags) &&
6279 !test_bit(In_sync, &rdev->flags) &&
6280 rdev->recovery_offset < j)
6281 j = rdev->recovery_offset;
6282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283
NeilBrown61df9d92006-10-03 01:15:57 -07006284 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6285 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6286 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006287 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006288 "(but not more than %d KB/sec) for %s.\n",
6289 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
NeilBrowneea1bf32009-03-31 14:27:02 +11006291 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006292
NeilBrown57afd892005-06-21 17:17:13 -07006293 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 for (m = 0; m < SYNC_MARKS; m++) {
6295 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006296 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 }
6298 last_mark = 0;
6299 mddev->resync_mark = mark[last_mark];
6300 mddev->resync_mark_cnt = mark_cnt[last_mark];
6301
6302 /*
6303 * Tune reconstruction:
6304 */
6305 window = 32*(PAGE_SIZE/512);
6306 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6307 window/2,(unsigned long long) max_sectors/2);
6308
6309 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 last_check = 0;
6311
6312 if (j>2) {
6313 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006314 "md: resuming %s of %s from checkpoint.\n",
6315 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 mddev->curr_resync = j;
6317 }
6318
6319 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006320 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
NeilBrown57afd892005-06-21 17:17:13 -07006322 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006323
NeilBrown7a91ee12009-05-26 12:57:21 +10006324 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6325 ((mddev->curr_resync > mddev->curr_resync_completed &&
6326 (mddev->curr_resync - mddev->curr_resync_completed)
6327 > (max_sectors >> 4)) ||
6328 (j - mddev->curr_resync_completed)*2
6329 >= mddev->resync_max - mddev->curr_resync_completed
6330 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006331 /* time to update curr_resync_completed */
6332 blk_unplug(mddev->queue);
6333 wait_event(mddev->recovery_wait,
6334 atomic_read(&mddev->recovery_active) == 0);
6335 mddev->curr_resync_completed =
6336 mddev->curr_resync;
6337 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006338 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006339 }
NeilBrownacb180b2009-04-14 16:28:34 +10006340
6341 if (j >= mddev->resync_max)
6342 wait_event(mddev->recovery_wait,
6343 mddev->resync_max > j
6344 || kthread_should_stop());
6345
6346 if (kthread_should_stop())
6347 goto interrupted;
6348
NeilBrown57afd892005-06-21 17:17:13 -07006349 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006350 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006351 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006352 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 goto out;
6354 }
NeilBrown57afd892005-06-21 17:17:13 -07006355
6356 if (!skipped) { /* actual IO requested */
6357 io_sectors += sectors;
6358 atomic_add(sectors, &mddev->recovery_active);
6359 }
6360
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 j += sectors;
6362 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006363 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006364 if (last_check == 0)
6365 /* this is the earliers that rebuilt will be
6366 * visible in /proc/mdstat
6367 */
6368 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006369
6370 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 continue;
6372
NeilBrown57afd892005-06-21 17:17:13 -07006373 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
NeilBrowndfc70642008-05-23 13:04:39 -07006375 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 break;
6377
6378 repeat:
6379 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6380 /* step marks */
6381 int next = (last_mark+1) % SYNC_MARKS;
6382
6383 mddev->resync_mark = mark[next];
6384 mddev->resync_mark_cnt = mark_cnt[next];
6385 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006386 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 last_mark = next;
6388 }
6389
6390
NeilBrownc6207272008-02-06 01:39:52 -08006391 if (kthread_should_stop())
6392 goto interrupted;
6393
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
6395 /*
6396 * this loop exits only if either when we are slower than
6397 * the 'hard' speed limit, or the system was IO-idle for
6398 * a jiffy.
6399 * the system might be non-idle CPU-wise, but we only care
6400 * about not overloading the IO subsystem. (things like an
6401 * e2fsck being done on the RAID array should execute fast)
6402 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006403 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 cond_resched();
6405
NeilBrown57afd892005-06-21 17:17:13 -07006406 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6407 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
NeilBrown88202a02006-01-06 00:21:36 -08006409 if (currspeed > speed_min(mddev)) {
6410 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006411 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006412 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 goto repeat;
6414 }
6415 }
6416 }
NeilBrown61df9d92006-10-03 01:15:57 -07006417 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 /*
6419 * this also signals 'finished resyncing' to md_stop
6420 */
6421 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006422 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
6424 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6425
6426 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006427 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
NeilBrowndfc70642008-05-23 13:04:39 -07006429 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006430 mddev->curr_resync > 2) {
6431 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6432 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6433 if (mddev->curr_resync >= mddev->recovery_cp) {
6434 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006435 "md: checkpointing %s of %s.\n",
6436 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006437 mddev->recovery_cp = mddev->curr_resync;
6438 }
6439 } else
6440 mddev->recovery_cp = MaxSector;
6441 } else {
6442 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6443 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006444 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006445 if (rdev->raid_disk >= 0 &&
6446 !test_bit(Faulty, &rdev->flags) &&
6447 !test_bit(In_sync, &rdev->flags) &&
6448 rdev->recovery_offset < mddev->curr_resync)
6449 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 }
NeilBrown17571282006-12-10 02:20:52 -08006452 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 skip:
6455 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006456 mddev->curr_resync_completed = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006457 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006458 mddev->resync_max = MaxSector;
6459 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 wake_up(&resync_wait);
6461 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6462 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006463 return;
6464
6465 interrupted:
6466 /*
6467 * got a signal, exit.
6468 */
6469 printk(KERN_INFO
6470 "md: md_do_sync() got signal ... exiting\n");
6471 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6472 goto out;
6473
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474}
NeilBrown29269552006-03-27 01:18:10 -08006475EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476
6477
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006478static int remove_and_add_spares(mddev_t *mddev)
6479{
6480 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006481 int spares = 0;
6482
NeilBrown97e4f422009-03-31 14:33:13 +11006483 mddev->curr_resync_completed = 0;
6484
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006485 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006486 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006487 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006488 (test_bit(Faulty, &rdev->flags) ||
6489 ! test_bit(In_sync, &rdev->flags)) &&
6490 atomic_read(&rdev->nr_pending)==0) {
6491 if (mddev->pers->hot_remove_disk(
6492 mddev, rdev->raid_disk)==0) {
6493 char nm[20];
6494 sprintf(nm,"rd%d", rdev->raid_disk);
6495 sysfs_remove_link(&mddev->kobj, nm);
6496 rdev->raid_disk = -1;
6497 }
6498 }
6499
NeilBrown4044ba52009-01-09 08:31:11 +11006500 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006501 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006502 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006503 !test_bit(In_sync, &rdev->flags) &&
6504 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006505 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006506 if (rdev->raid_disk < 0
6507 && !test_bit(Faulty, &rdev->flags)) {
6508 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006509 if (mddev->pers->
6510 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006511 char nm[20];
6512 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006513 if (sysfs_create_link(&mddev->kobj,
6514 &rdev->kobj, nm))
6515 printk(KERN_WARNING
6516 "md: cannot register "
6517 "%s for %s\n",
6518 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006519 spares++;
6520 md_new_event(mddev);
6521 } else
6522 break;
6523 }
NeilBrowndfc70642008-05-23 13:04:39 -07006524 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006525 }
6526 return spares;
6527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528/*
6529 * This routine is regularly called by all per-raid-array threads to
6530 * deal with generic issues like resync and super-block update.
6531 * Raid personalities that don't have a thread (linear/raid0) do not
6532 * need this as they never do any recovery or update the superblock.
6533 *
6534 * It does not do any resync itself, but rather "forks" off other threads
6535 * to do that as needed.
6536 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6537 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006538 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 * and wakeups up this thread which will reap the thread and finish up.
6540 * This thread also removes any faulty devices (with nr_pending == 0).
6541 *
6542 * The overall approach is:
6543 * 1/ if the superblock needs updating, update it.
6544 * 2/ If a recovery thread is running, don't do anything else.
6545 * 3/ If recovery has finished, clean up, possibly marking spares active.
6546 * 4/ If there are any faulty devices, remove them.
6547 * 5/ If array is degraded, try to add spares devices
6548 * 6/ If array has spares or is not in-sync, start a resync thread.
6549 */
6550void md_check_recovery(mddev_t *mddev)
6551{
6552 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
6554
NeilBrown5f404022005-06-21 17:17:16 -07006555 if (mddev->bitmap)
6556 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
6558 if (mddev->ro)
6559 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006560
6561 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006562 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006563 printk(KERN_INFO "md: %s in immediate safe mode\n",
6564 mdname(mddev));
6565 mddev->safemode = 2;
6566 }
6567 flush_signals(current);
6568 }
6569
NeilBrownc89a8ee2008-08-05 15:54:13 +10006570 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6571 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006573 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006575 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006576 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006577 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6578 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 ))
6580 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006581
NeilBrowndf5b89b2006-03-27 01:18:20 -08006582 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006583 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006584
NeilBrownc89a8ee2008-08-05 15:54:13 +10006585 if (mddev->ro) {
6586 /* Only thing we do on a ro array is remove
6587 * failed devices.
6588 */
6589 remove_and_add_spares(mddev);
6590 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6591 goto unlock;
6592 }
6593
NeilBrown31a59e32008-04-30 00:52:30 -07006594 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006595 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006596 spin_lock_irq(&mddev->write_lock);
6597 if (mddev->safemode &&
6598 !atomic_read(&mddev->writes_pending) &&
6599 !mddev->in_sync &&
6600 mddev->recovery_cp == MaxSector) {
6601 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006602 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006603 if (mddev->persistent)
6604 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6605 }
6606 if (mddev->safemode == 1)
6607 mddev->safemode = 0;
6608 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006609 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006610 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006611 }
NeilBrownfca4d842005-06-21 17:17:11 -07006612
NeilBrown850b2b42006-10-03 01:15:46 -07006613 if (mddev->flags)
6614 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006615
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006616 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006617 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006618 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006619
NeilBrown06d91a52005-06-21 17:17:12 -07006620
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6622 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6623 /* resync/recovery still happening */
6624 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6625 goto unlock;
6626 }
6627 if (mddev->sync_thread) {
6628 /* resync has finished, collect result */
6629 md_unregister_thread(mddev->sync_thread);
6630 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006631 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6632 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 /* success...*/
6634 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006635 if (mddev->pers->spare_active(mddev))
6636 sysfs_notify(&mddev->kobj, NULL,
6637 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 }
NeilBrowncea9c222009-03-31 15:15:05 +11006639 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6640 mddev->pers->finish_reshape)
6641 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006642 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006643
6644 /* if array is no-longer degraded, then any saved_raid_disk
6645 * information must be scrapped
6646 */
6647 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006648 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006649 rdev->saved_raid_disk = -1;
6650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 mddev->recovery = 0;
6652 /* flag recovery needed just to double check */
6653 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006654 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006655 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 goto unlock;
6657 }
Neil Brown72a23c22008-06-28 08:31:41 +10006658 /* Set RUNNING before clearing NEEDED to avoid
6659 * any transients in the value of "sync_action".
6660 */
6661 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6662 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006663 /* Clear some bits that don't mean anything, but
6664 * might be left set
6665 */
NeilBrown24dd4692005-11-08 21:39:26 -08006666 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6667 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006669 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6670 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 /* no recovery is running.
6672 * remove any failed drives, then
6673 * add spares if possible.
6674 * Spare are also removed and re-added, to allow
6675 * the personality to fail the re-add.
6676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006678 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006679 if (mddev->pers->check_reshape == NULL ||
6680 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006681 /* Cannot proceed */
6682 goto unlock;
6683 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006684 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006685 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006686 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6687 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006688 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006689 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006690 } else if (mddev->recovery_cp < MaxSector) {
6691 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006692 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006693 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6694 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006698 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6699 /* We are adding a device or devices to an array
6700 * which has the bitmap stored on all devices.
6701 * So make sure all bitmap pages get written
6702 */
6703 bitmap_write_all(mddev->bitmap);
6704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 mddev->sync_thread = md_register_thread(md_do_sync,
6706 mddev,
6707 "%s_resync");
6708 if (!mddev->sync_thread) {
6709 printk(KERN_ERR "%s: could not start resync"
6710 " thread...\n",
6711 mdname(mddev));
6712 /* leave the spares where they are, it shouldn't hurt */
6713 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006714 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006716 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006717 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 }
6719 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006720 if (!mddev->sync_thread) {
6721 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6722 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6723 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006724 if (mddev->sysfs_action)
6725 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 mddev_unlock(mddev);
6728 }
6729}
6730
Dan Williams6bfe0b42008-04-30 00:52:32 -07006731void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6732{
NeilBrown3c0ee632008-10-21 13:25:28 +11006733 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006734 wait_event_timeout(rdev->blocked_wait,
6735 !test_bit(Blocked, &rdev->flags),
6736 msecs_to_jiffies(5000));
6737 rdev_dec_pending(rdev, mddev);
6738}
6739EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6740
Adrian Bunk75c96f82005-05-05 16:16:09 -07006741static int md_notify_reboot(struct notifier_block *this,
6742 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743{
6744 struct list_head *tmp;
6745 mddev_t *mddev;
6746
6747 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6748
6749 printk(KERN_INFO "md: stopping all md devices.\n");
6750
NeilBrown29ac4aa2008-02-06 01:39:58 -08006751 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006752 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006753 /* Force a switch to readonly even array
6754 * appears to still be in use. Hence
6755 * the '100'.
6756 */
NeilBrownd710e132008-10-13 11:55:12 +11006757 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006758 mddev_unlock(mddev);
6759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 /*
6761 * certain more exotic SCSI devices are known to be
6762 * volatile wrt too early system reboots. While the
6763 * right place to handle this issue is the given
6764 * driver, we do want to have a safe RAID driver ...
6765 */
6766 mdelay(1000*1);
6767 }
6768 return NOTIFY_DONE;
6769}
6770
Adrian Bunk75c96f82005-05-05 16:16:09 -07006771static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 .notifier_call = md_notify_reboot,
6773 .next = NULL,
6774 .priority = INT_MAX, /* before any real devices */
6775};
6776
6777static void md_geninit(void)
6778{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6780
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006781 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782}
6783
Adrian Bunk75c96f82005-05-05 16:16:09 -07006784static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006786 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 return -1;
6788 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006789 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 return -1;
6791 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006792 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006793 md_probe, NULL, NULL);
6794 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 md_probe, NULL, NULL);
6796
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006798 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
6800 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802}
6803
6804
6805#ifndef MODULE
6806
6807/*
6808 * Searches all registered partitions for autorun RAID arrays
6809 * at boot time.
6810 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006811
6812static LIST_HEAD(all_detected_devices);
6813struct detected_devices_node {
6814 struct list_head list;
6815 dev_t dev;
6816};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817
6818void md_autodetect_dev(dev_t dev)
6819{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006820 struct detected_devices_node *node_detected_dev;
6821
6822 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6823 if (node_detected_dev) {
6824 node_detected_dev->dev = dev;
6825 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6826 } else {
6827 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6828 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830}
6831
6832
6833static void autostart_arrays(int part)
6834{
6835 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006836 struct detected_devices_node *node_detected_dev;
6837 dev_t dev;
6838 int i_scanned, i_passed;
6839
6840 i_scanned = 0;
6841 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
6843 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6844
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006845 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6846 i_scanned++;
6847 node_detected_dev = list_entry(all_detected_devices.next,
6848 struct detected_devices_node, list);
6849 list_del(&node_detected_dev->list);
6850 dev = node_detected_dev->dev;
6851 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006852 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 if (IS_ERR(rdev))
6854 continue;
6855
NeilBrownb2d444d2005-11-08 21:39:31 -08006856 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 MD_BUG();
6858 continue;
6859 }
NeilBrownd0fae182008-03-04 14:29:31 -08006860 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006862 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006864
6865 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6866 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
6868 autorun_devices(part);
6869}
6870
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006871#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
6873static __exit void md_exit(void)
6874{
6875 mddev_t *mddev;
6876 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006877
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006878 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006879 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006881 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 unregister_blkdev(mdp_major, "mdp");
6883 unregister_reboot_notifier(&md_notifier);
6884 unregister_sysctl_table(raid_table_header);
6885 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006886 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006888 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 }
6890}
6891
Dan Williams685784a2007-07-09 11:56:42 -07006892subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893module_exit(md_exit)
6894
NeilBrownf91de922005-11-08 21:39:36 -08006895static int get_ro(char *buffer, struct kernel_param *kp)
6896{
6897 return sprintf(buffer, "%d", start_readonly);
6898}
6899static int set_ro(const char *val, struct kernel_param *kp)
6900{
6901 char *e;
6902 int num = simple_strtoul(val, &e, 10);
6903 if (*val && (*e == '\0' || *e == '\n')) {
6904 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006905 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006906 }
6907 return -EINVAL;
6908}
6909
NeilBrown80ca3a42006-07-10 04:44:18 -07006910module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6911module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006912
NeilBrownefeb53c2009-01-09 08:31:10 +11006913module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006914
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915EXPORT_SYMBOL(register_md_personality);
6916EXPORT_SYMBOL(unregister_md_personality);
6917EXPORT_SYMBOL(md_error);
6918EXPORT_SYMBOL(md_done_sync);
6919EXPORT_SYMBOL(md_write_start);
6920EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921EXPORT_SYMBOL(md_register_thread);
6922EXPORT_SYMBOL(md_unregister_thread);
6923EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924EXPORT_SYMBOL(md_check_recovery);
6925MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006926MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006927MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);