blob: aad0ac54bf901bfb682df6094f96b78d1eed01ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700105 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .proc_handler = &proc_dointvec,
107 },
108 {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .proc_handler = &proc_dointvec,
115 },
116 { .ctl_name = 0 }
117};
118
119static ctl_table raid_dir_table[] = {
120 {
121 .ctl_name = DEV_RAID,
122 .procname = "raid",
123 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .child = raid_table,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_root_table[] = {
131 {
132 .ctl_name = CTL_DEV,
133 .procname = "dev",
134 .maxlen = 0,
135 .mode = 0555,
136 .child = raid_dir_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static struct block_device_operations md_fops;
142
NeilBrownf91de922005-11-08 21:39:36 -0800143static int start_readonly;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
NeilBrownd7603b72006-01-06 00:20:30 -0800146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
149 * count increases.
150 *
151 * Events are:
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
154 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800155static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800156static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800157void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800158{
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
161}
NeilBrown29269552006-03-27 01:18:10 -0800162EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800163
NeilBrownc331eb02006-05-30 21:27:13 -0700164/* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
166 */
Adrian Bunk05381952006-06-26 00:28:01 -0700167static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700168{
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171}
172
NeilBrownd7603b72006-01-06 00:20:30 -0800173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
176 */
177static LIST_HEAD(all_mddevs);
178static DEFINE_SPINLOCK(all_mddevs_lock);
179
180
181/*
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
187 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800188#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
192 mddev = NULL;}); \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
200 tmp = tmp->next;}) \
201 )
202
203
NeilBrownd710e132008-10-13 11:55:12 +1100204static int md_fail_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
NeilBrown6712ecf2007-09-27 12:47:43 +0200206 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208}
209
210static inline mddev_t *mddev_get(mddev_t *mddev)
211{
212 atomic_inc(&mddev->active);
213 return mddev;
214}
215
NeilBrownd3374822009-01-09 08:31:10 +1100216static void mddev_delayed_delete(struct work_struct *ws)
217{
218 mddev_t *mddev = container_of(ws, mddev_t, del_work);
219 kobject_del(&mddev->kobj);
220 kobject_put(&mddev->kobj);
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223static void mddev_put(mddev_t *mddev)
224{
225 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
226 return;
NeilBrownd3374822009-01-09 08:31:10 +1100227 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
228 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100230 if (mddev->gendisk) {
231 /* we did a probe so need to clean up.
232 * Call schedule_work inside the spinlock
233 * so that flush_scheduled_work() after
234 * mddev_find will succeed in waiting for the
235 * work to be done.
236 */
237 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
238 schedule_work(&mddev->del_work);
239 } else
240 kfree(mddev);
241 }
242 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static mddev_t * mddev_find(dev_t unit)
246{
247 mddev_t *mddev, *new = NULL;
248
249 retry:
250 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
NeilBrownefeb53c2009-01-09 08:31:10 +1100252 if (unit) {
253 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
254 if (mddev->unit == unit) {
255 mddev_get(mddev);
256 spin_unlock(&all_mddevs_lock);
257 kfree(new);
258 return mddev;
259 }
260
261 if (new) {
262 list_add(&new->all_mddevs, &all_mddevs);
263 spin_unlock(&all_mddevs_lock);
264 new->hold_active = UNTIL_IOCTL;
265 return new;
266 }
267 } else if (new) {
268 /* find an unused unit number */
269 static int next_minor = 512;
270 int start = next_minor;
271 int is_free = 0;
272 int dev = 0;
273 while (!is_free) {
274 dev = MKDEV(MD_MAJOR, next_minor);
275 next_minor++;
276 if (next_minor > MINORMASK)
277 next_minor = 0;
278 if (next_minor == start) {
279 /* Oh dear, all in use. */
280 spin_unlock(&all_mddevs_lock);
281 kfree(new);
282 return NULL;
283 }
284
285 is_free = 1;
286 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
287 if (mddev->unit == dev) {
288 is_free = 0;
289 break;
290 }
291 }
292 new->unit = dev;
293 new->md_minor = MINOR(dev);
294 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 list_add(&new->all_mddevs, &all_mddevs);
296 spin_unlock(&all_mddevs_lock);
297 return new;
298 }
299 spin_unlock(&all_mddevs_lock);
300
NeilBrown9ffae0c2006-01-06 00:20:32 -0800301 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!new)
303 return NULL;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 new->unit = unit;
306 if (MAJOR(unit) == MD_MAJOR)
307 new->md_minor = MINOR(unit);
308 else
309 new->md_minor = MINOR(unit) >> MdpMinorShift;
310
NeilBrowndf5b89b2006-03-27 01:18:20 -0800311 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 INIT_LIST_HEAD(&new->disks);
313 INIT_LIST_HEAD(&new->all_mddevs);
314 init_timer(&new->safemode_timer);
315 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000316 atomic_set(&new->openers, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700317 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700318 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700319 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700320 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000321 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800322 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700323 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto retry;
326}
327
328static inline int mddev_lock(mddev_t * mddev)
329{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800330 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static inline int mddev_trylock(mddev_t * mddev)
334{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800335 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338static inline void mddev_unlock(mddev_t * mddev)
339{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800340 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
NeilBrown005eca52005-08-22 13:11:08 -0700342 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
NeilBrown2989ddb2006-01-06 00:20:43 -0800345static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100347 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100349 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (rdev->desc_nr == nr)
351 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return NULL;
354}
355
356static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
357{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 mdk_rdev_t *rdev;
359
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100360 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (rdev->bdev->bd_dev == dev)
362 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return NULL;
365}
366
NeilBrownd9d166c2006-01-06 00:20:51 -0800367static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800368{
369 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800370 list_for_each_entry(pers, &pers_list, list) {
371 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800372 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800373 if (strcmp(pers->name, clevel)==0)
374 return pers;
375 }
NeilBrown2604b702006-01-06 00:20:36 -0800376 return NULL;
377}
378
Andre Nollb73df2d2008-07-11 22:02:23 +1000379/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700380static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Andre Nollb73df2d2008-07-11 22:02:23 +1000382 sector_t num_sectors = bdev->bd_inode->i_size / 512;
383 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Andre Nolle7debaa2008-07-11 22:02:23 +1000386static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Andre Noll0f420352008-07-11 22:02:23 +1000388 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000391 num_sectors &= ~((sector_t)chunk_size/512 - 1);
392 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static int alloc_disk_sb(mdk_rdev_t * rdev)
396{
397 if (rdev->sb_page)
398 MD_BUG();
399
400 rdev->sb_page = alloc_page(GFP_KERNEL);
401 if (!rdev->sb_page) {
402 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000403 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
406 return 0;
407}
408
409static void free_disk_sb(mdk_rdev_t * rdev)
410{
411 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800412 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 rdev->sb_loaded = 0;
414 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000415 rdev->sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 rdev->size = 0;
417 }
418}
419
420
NeilBrown6712ecf2007-09-27 12:47:43 +0200421static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700422{
423 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800424 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700425
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700426 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
427 printk("md: super_written gets error=%d, uptodate=%d\n",
428 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
429 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800430 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700431 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700432
NeilBrowna9701a32005-11-08 21:39:34 -0800433 if (atomic_dec_and_test(&mddev->pending_writes))
434 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700435 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700436}
437
NeilBrown6712ecf2007-09-27 12:47:43 +0200438static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800439{
440 struct bio *bio2 = bio->bi_private;
441 mdk_rdev_t *rdev = bio2->bi_private;
442 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800443
444 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
445 error == -EOPNOTSUPP) {
446 unsigned long flags;
447 /* barriers don't appear to be supported :-( */
448 set_bit(BarriersNotsupp, &rdev->flags);
449 mddev->barriers_work = 0;
450 spin_lock_irqsave(&mddev->write_lock, flags);
451 bio2->bi_next = mddev->biolist;
452 mddev->biolist = bio2;
453 spin_unlock_irqrestore(&mddev->write_lock, flags);
454 wake_up(&mddev->sb_wait);
455 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200456 } else {
457 bio_put(bio2);
458 bio->bi_private = rdev;
459 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800460 }
NeilBrowna9701a32005-11-08 21:39:34 -0800461}
462
NeilBrown7bfa19f2005-06-21 17:17:28 -0700463void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
464 sector_t sector, int size, struct page *page)
465{
466 /* write first size bytes of page to sector of rdev
467 * Increment mddev->pending_writes before returning
468 * and decrement it on completion, waking up sb_wait
469 * if zero is reached.
470 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800471 *
472 * As we might need to resubmit the request if BIO_RW_BARRIER
473 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700474 */
475 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100476 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700477
478 bio->bi_bdev = rdev->bdev;
479 bio->bi_sector = sector;
480 bio_add_page(bio, page, size, 0);
481 bio->bi_private = rdev;
482 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800483 bio->bi_rw = rw;
484
NeilBrown7bfa19f2005-06-21 17:17:28 -0700485 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800486 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
487 struct bio *rbio;
488 rw |= (1<<BIO_RW_BARRIER);
489 rbio = bio_clone(bio, GFP_NOIO);
490 rbio->bi_private = bio;
491 rbio->bi_end_io = super_written_barrier;
492 submit_bio(rw, rbio);
493 } else
494 submit_bio(rw, bio);
495}
496
497void md_super_wait(mddev_t *mddev)
498{
499 /* wait for all superblock writes that were scheduled to complete.
500 * if any had to be retried (due to BARRIER problems), retry them
501 */
502 DEFINE_WAIT(wq);
503 for(;;) {
504 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
505 if (atomic_read(&mddev->pending_writes)==0)
506 break;
507 while (mddev->biolist) {
508 struct bio *bio;
509 spin_lock_irq(&mddev->write_lock);
510 bio = mddev->biolist;
511 mddev->biolist = bio->bi_next ;
512 bio->bi_next = NULL;
513 spin_unlock_irq(&mddev->write_lock);
514 submit_bio(bio->bi_rw, bio);
515 }
516 schedule();
517 }
518 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700519}
520
NeilBrown6712ecf2007-09-27 12:47:43 +0200521static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
NeilBrowna654b9d82005-06-21 17:17:27 -0700526int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct page *page, int rw)
528{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700529 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct completion event;
531 int ret;
532
Jens Axboe93dbb392009-02-16 10:25:40 +0100533 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 bio->bi_bdev = bdev;
536 bio->bi_sector = sector;
537 bio_add_page(bio, page, size, 0);
538 init_completion(&event);
539 bio->bi_private = &event;
540 bio->bi_end_io = bi_complete;
541 submit_bio(rw, bio);
542 wait_for_completion(&event);
543
544 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
545 bio_put(bio);
546 return ret;
547}
NeilBrowna8745db2006-01-06 00:20:34 -0800548EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
NeilBrown0002b272005-09-09 16:23:53 -0700550static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 char b[BDEVNAME_SIZE];
553 if (!rdev->sb_page) {
554 MD_BUG();
555 return -EINVAL;
556 }
557 if (rdev->sb_loaded)
558 return 0;
559
560
Andre Noll0f420352008-07-11 22:02:23 +1000561 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto fail;
563 rdev->sb_loaded = 1;
564 return 0;
565
566fail:
567 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
568 bdevname(rdev->bdev,b));
569 return -EINVAL;
570}
571
572static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
573{
Andre Noll05710462008-07-11 22:02:20 +1000574 return sb1->set_uuid0 == sb2->set_uuid0 &&
575 sb1->set_uuid1 == sb2->set_uuid1 &&
576 sb1->set_uuid2 == sb2->set_uuid2 &&
577 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
581{
582 int ret;
583 mdp_super_t *tmp1, *tmp2;
584
585 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
586 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
587
588 if (!tmp1 || !tmp2) {
589 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100590 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto abort;
592 }
593
594 *tmp1 = *sb1;
595 *tmp2 = *sb2;
596
597 /*
598 * nr_disks is not constant
599 */
600 tmp1->nr_disks = 0;
601 tmp2->nr_disks = 0;
602
Andre Nollce0c8e02008-07-11 22:02:20 +1000603 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700605 kfree(tmp1);
606 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return ret;
608}
609
NeilBrown4d167f02007-05-09 02:35:37 -0700610
611static u32 md_csum_fold(u32 csum)
612{
613 csum = (csum & 0xffff) + (csum >> 16);
614 return (csum & 0xffff) + (csum >> 16);
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static unsigned int calc_sb_csum(mdp_super_t * sb)
618{
NeilBrown4d167f02007-05-09 02:35:37 -0700619 u64 newcsum = 0;
620 u32 *sb32 = (u32*)sb;
621 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 unsigned int disk_csum, csum;
623
624 disk_csum = sb->sb_csum;
625 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700626
627 for (i = 0; i < MD_SB_BYTES/4 ; i++)
628 newcsum += sb32[i];
629 csum = (newcsum & 0xffffffff) + (newcsum>>32);
630
631
632#ifdef CONFIG_ALPHA
633 /* This used to use csum_partial, which was wrong for several
634 * reasons including that different results are returned on
635 * different architectures. It isn't critical that we get exactly
636 * the same return value as before (we always csum_fold before
637 * testing, and that removes any differences). However as we
638 * know that csum_partial always returned a 16bit value on
639 * alphas, do a fold to maximise conformity to previous behaviour.
640 */
641 sb->sb_csum = md_csum_fold(disk_csum);
642#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700644#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return csum;
646}
647
648
649/*
650 * Handle superblock details.
651 * We want to be able to handle multiple superblock formats
652 * so we have a common interface to them all, and an array of
653 * different handlers.
654 * We rely on user-space to write the initial superblock, and support
655 * reading and updating of superblocks.
656 * Interface methods are:
657 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
658 * loads and validates a superblock on dev.
659 * if refdev != NULL, compare superblocks on both devices
660 * Return:
661 * 0 - dev has a superblock that is compatible with refdev
662 * 1 - dev has a superblock that is compatible and newer than refdev
663 * so dev should be used as the refdev in future
664 * -EINVAL superblock incompatible or invalid
665 * -othererror e.g. -EIO
666 *
667 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
668 * Verify that dev is acceptable into mddev.
669 * The first time, mddev->raid_disks will be 0, and data from
670 * dev should be merged in. Subsequent calls check that dev
671 * is new enough. Return 0 or -EINVAL
672 *
673 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
674 * Update the superblock for rdev with data in mddev
675 * This does not write to disc.
676 *
677 */
678
679struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000680 char *name;
681 struct module *owner;
682 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
683 int minor_version);
684 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
685 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
686 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000687 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688};
689
690/*
691 * load_super for 0.90.0
692 */
693static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
694{
695 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
696 mdp_super_t *sb;
697 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /*
Andre Noll0f420352008-07-11 22:02:23 +1000700 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * it's at the end of the disk.
702 *
703 * It also happens to be a multiple of 4Kb.
704 */
Andre Noll0f420352008-07-11 22:02:23 +1000705 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
NeilBrown0002b272005-09-09 16:23:53 -0700707 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (ret) return ret;
709
710 ret = -EINVAL;
711
712 bdevname(rdev->bdev, b);
713 sb = (mdp_super_t*)page_address(rdev->sb_page);
714
715 if (sb->md_magic != MD_SB_MAGIC) {
716 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
717 b);
718 goto abort;
719 }
720
721 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800722 sb->minor_version < 90 ||
723 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
725 sb->major_version, sb->minor_version,
726 b);
727 goto abort;
728 }
729
730 if (sb->raid_disks <= 0)
731 goto abort;
732
NeilBrown4d167f02007-05-09 02:35:37 -0700733 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
735 b);
736 goto abort;
737 }
738
739 rdev->preferred_minor = sb->md_minor;
740 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700741 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
NeilBrowne11e93f2007-05-09 02:35:36 -0700743 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
744 if (sb->level != 1 && sb->level != 4
745 && sb->level != 5 && sb->level != 6
746 && sb->level != 10) {
747 /* FIXME use a better test */
748 printk(KERN_WARNING
749 "md: bitmaps not supported for this level.\n");
750 goto abort;
751 }
752 }
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (sb->level == LEVEL_MULTIPATH)
755 rdev->desc_nr = -1;
756 else
757 rdev->desc_nr = sb->this_disk.number;
758
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700759 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700761 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 __u64 ev1, ev2;
763 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
764 if (!uuid_equal(refsb, sb)) {
765 printk(KERN_WARNING "md: %s has different UUID to %s\n",
766 b, bdevname(refdev->bdev,b2));
767 goto abort;
768 }
769 if (!sb_equal(refsb, sb)) {
770 printk(KERN_WARNING "md: %s has same UUID"
771 " but different superblock to %s\n",
772 b, bdevname(refdev->bdev, b2));
773 goto abort;
774 }
775 ev1 = md_event(sb);
776 ev2 = md_event(refsb);
777 if (ev1 > ev2)
778 ret = 1;
779 else
780 ret = 0;
781 }
Andre Nolle7debaa2008-07-11 22:02:23 +1000782 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
NeilBrown2bf071b2006-01-06 00:20:55 -0800784 if (rdev->size < sb->size && sb->level > 1)
785 /* "this cannot possibly happen" ... */
786 ret = -EINVAL;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 abort:
789 return ret;
790}
791
792/*
793 * validate_super for 0.90.0
794 */
795static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
796{
797 mdp_disk_t *desc;
798 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700799 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
NeilBrown41158c72005-06-21 17:17:25 -0700801 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800802 clear_bit(Faulty, &rdev->flags);
803 clear_bit(In_sync, &rdev->flags);
804 clear_bit(WriteMostly, &rdev->flags);
805 clear_bit(BarriersNotsupp, &rdev->flags);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (mddev->raid_disks == 0) {
808 mddev->major_version = 0;
809 mddev->minor_version = sb->minor_version;
810 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800811 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 mddev->chunk_size = sb->chunk_size;
813 mddev->ctime = sb->ctime;
814 mddev->utime = sb->utime;
815 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800816 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 mddev->layout = sb->layout;
818 mddev->raid_disks = sb->raid_disks;
819 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700820 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700821 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700822 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
NeilBrownf6705572006-03-27 01:18:11 -0800824 if (mddev->minor_version >= 91) {
825 mddev->reshape_position = sb->reshape_position;
826 mddev->delta_disks = sb->delta_disks;
827 mddev->new_level = sb->new_level;
828 mddev->new_layout = sb->new_layout;
829 mddev->new_chunk = sb->new_chunk;
830 } else {
831 mddev->reshape_position = MaxSector;
832 mddev->delta_disks = 0;
833 mddev->new_level = mddev->level;
834 mddev->new_layout = mddev->layout;
835 mddev->new_chunk = mddev->chunk_size;
836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (sb->state & (1<<MD_SB_CLEAN))
839 mddev->recovery_cp = MaxSector;
840 else {
841 if (sb->events_hi == sb->cp_events_hi &&
842 sb->events_lo == sb->cp_events_lo) {
843 mddev->recovery_cp = sb->recovery_cp;
844 } else
845 mddev->recovery_cp = 0;
846 }
847
848 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
849 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
850 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
851 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
852
853 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700854
855 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700856 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700857 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700858
NeilBrown41158c72005-06-21 17:17:25 -0700859 } else if (mddev->pers == NULL) {
860 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ++ev1;
862 if (ev1 < mddev->events)
863 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700864 } else if (mddev->bitmap) {
865 /* if adding to array with a bitmap, then we can accept an
866 * older device ... but not too old.
867 */
NeilBrown41158c72005-06-21 17:17:25 -0700868 if (ev1 < mddev->bitmap->events_cleared)
869 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700870 } else {
871 if (ev1 < mddev->events)
872 /* just a hot-add of a new device, leave raid_disk at -1 */
873 return 0;
874 }
NeilBrown41158c72005-06-21 17:17:25 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 desc = sb->disks + rdev->desc_nr;
878
879 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800880 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700881 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
882 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800883 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 rdev->raid_disk = desc->raid_disk;
885 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700886 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
887 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700888 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800889 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return 0;
891}
892
893/*
894 * sync_super for 0.90.0
895 */
896static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
897{
898 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 mdk_rdev_t *rdev2;
900 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* make rdev->sb match mddev data..
904 *
905 * 1/ zero out disks
906 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
907 * 3/ any empty disks < next_spare become removed
908 *
909 * disks[0] gets initialised to REMOVED because
910 * we cannot be sure from other fields if it has
911 * been initialised or not.
912 */
913 int i;
914 int active=0, working=0,failed=0,spare=0,nr_disks=0;
915
NeilBrown61181562005-09-09 16:24:02 -0700916 rdev->sb_size = MD_SB_BYTES;
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 sb = (mdp_super_t*)page_address(rdev->sb_page);
919
920 memset(sb, 0, sizeof(*sb));
921
922 sb->md_magic = MD_SB_MAGIC;
923 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 sb->patch_version = mddev->patch_version;
925 sb->gvalid_words = 0; /* ignored */
926 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
927 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
928 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
929 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
930
931 sb->ctime = mddev->ctime;
932 sb->level = mddev->level;
933 sb->size = mddev->size;
934 sb->raid_disks = mddev->raid_disks;
935 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800936 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 sb->utime = mddev->utime;
938 sb->state = 0;
939 sb->events_hi = (mddev->events>>32);
940 sb->events_lo = (u32)mddev->events;
941
NeilBrownf6705572006-03-27 01:18:11 -0800942 if (mddev->reshape_position == MaxSector)
943 sb->minor_version = 90;
944 else {
945 sb->minor_version = 91;
946 sb->reshape_position = mddev->reshape_position;
947 sb->new_level = mddev->new_level;
948 sb->delta_disks = mddev->delta_disks;
949 sb->new_layout = mddev->new_layout;
950 sb->new_chunk = mddev->new_chunk;
951 }
952 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (mddev->in_sync)
954 {
955 sb->recovery_cp = mddev->recovery_cp;
956 sb->cp_events_hi = (mddev->events>>32);
957 sb->cp_events_lo = (u32)mddev->events;
958 if (mddev->recovery_cp == MaxSector)
959 sb->state = (1<< MD_SB_CLEAN);
960 } else
961 sb->recovery_cp = 0;
962
963 sb->layout = mddev->layout;
964 sb->chunk_size = mddev->chunk_size;
965
NeilBrowna654b9d82005-06-21 17:17:27 -0700966 if (mddev->bitmap && mddev->bitmap_file == NULL)
967 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100970 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800972 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800973 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
974 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800975 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800977 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800978 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 d = &sb->disks[rdev2->desc_nr];
980 nr_disks++;
981 d->number = rdev2->desc_nr;
982 d->major = MAJOR(rdev2->bdev->bd_dev);
983 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800984 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
985 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 d->raid_disk = rdev2->raid_disk;
987 else
988 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800989 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800991 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 d->state = (1<<MD_DISK_ACTIVE);
993 d->state |= (1<<MD_DISK_SYNC);
994 active++;
995 working++;
996 } else {
997 d->state = 0;
998 spare++;
999 working++;
1000 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001001 if (test_bit(WriteMostly, &rdev2->flags))
1002 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /* now set the "removed" and "faulty" bits on any missing devices */
1005 for (i=0 ; i < mddev->raid_disks ; i++) {
1006 mdp_disk_t *d = &sb->disks[i];
1007 if (d->state == 0 && d->number == 0) {
1008 d->number = i;
1009 d->raid_disk = i;
1010 d->state = (1<<MD_DISK_REMOVED);
1011 d->state |= (1<<MD_DISK_FAULTY);
1012 failed++;
1013 }
1014 }
1015 sb->nr_disks = nr_disks;
1016 sb->active_disks = active;
1017 sb->working_disks = working;
1018 sb->failed_disks = failed;
1019 sb->spare_disks = spare;
1020
1021 sb->this_disk = sb->disks[rdev->desc_nr];
1022 sb->sb_csum = calc_sb_csum(sb);
1023}
1024
1025/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001026 * rdev_size_change for 0.90.0
1027 */
1028static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001029super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001030{
Andre Noll15f4a5f2008-07-21 14:42:12 +10001031 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001032 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001033 if (rdev->mddev->bitmap_offset)
1034 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001035 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001036 if (!num_sectors || num_sectors > rdev->sb_start)
1037 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001038 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001039 rdev->sb_page);
1040 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001041 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001042}
1043
1044
1045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 * version 1 superblock
1047 */
1048
NeilBrown1c05b4b2006-10-21 10:24:08 -07001049static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001051 __le32 disk_csum;
1052 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 unsigned long long newcsum;
1054 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001055 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 int i;
1057
1058 disk_csum = sb->sb_csum;
1059 sb->sb_csum = 0;
1060 newcsum = 0;
1061 for (i=0; size>=4; size -= 4 )
1062 newcsum += le32_to_cpu(*isuper++);
1063
1064 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001065 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1068 sb->sb_csum = disk_csum;
1069 return cpu_to_le32(csum);
1070}
1071
1072static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1073{
1074 struct mdp_superblock_1 *sb;
1075 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001076 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001078 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /*
Andre Noll0f420352008-07-11 22:02:23 +10001081 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * It is always aligned to a 4K boundary and
1083 * depeding on minor_version, it can be:
1084 * 0: At least 8K, but less than 12K, from end of device
1085 * 1: At start of device
1086 * 2: 4K from start of device.
1087 */
1088 switch(minor_version) {
1089 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001090 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1091 sb_start -= 8*2;
1092 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 break;
1094 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001095 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
1097 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001098 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 break;
1100 default:
1101 return -EINVAL;
1102 }
Andre Noll0f420352008-07-11 22:02:23 +10001103 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrown0002b272005-09-09 16:23:53 -07001105 /* superblock is rarely larger than 1K, but it can be larger,
1106 * and it is safe to read 4k, so we do that
1107 */
1108 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (ret) return ret;
1110
1111
1112 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1113
1114 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1115 sb->major_version != cpu_to_le32(1) ||
1116 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001117 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001118 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return -EINVAL;
1120
1121 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1122 printk("md: invalid superblock checksum on %s\n",
1123 bdevname(rdev->bdev,b));
1124 return -EINVAL;
1125 }
1126 if (le64_to_cpu(sb->data_size) < 10) {
1127 printk("md: data_size too small on %s\n",
1128 bdevname(rdev->bdev,b));
1129 return -EINVAL;
1130 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001131 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1132 if (sb->level != cpu_to_le32(1) &&
1133 sb->level != cpu_to_le32(4) &&
1134 sb->level != cpu_to_le32(5) &&
1135 sb->level != cpu_to_le32(6) &&
1136 sb->level != cpu_to_le32(10)) {
1137 printk(KERN_WARNING
1138 "md: bitmaps not supported for this level.\n");
1139 return -EINVAL;
1140 }
1141 }
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 rdev->preferred_minor = 0xffff;
1144 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001145 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
NeilBrown0002b272005-09-09 16:23:53 -07001147 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001148 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001149 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001150 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1151
1152 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001153 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001154 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001155
NeilBrown31b65a02006-07-10 04:44:14 -07001156 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1157 rdev->desc_nr = -1;
1158 else
1159 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1160
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001161 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001162 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001163 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 __u64 ev1, ev2;
1165 struct mdp_superblock_1 *refsb =
1166 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1167
1168 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1169 sb->level != refsb->level ||
1170 sb->layout != refsb->layout ||
1171 sb->chunksize != refsb->chunksize) {
1172 printk(KERN_WARNING "md: %s has strangely different"
1173 " superblock to %s\n",
1174 bdevname(rdev->bdev,b),
1175 bdevname(refdev->bdev,b2));
1176 return -EINVAL;
1177 }
1178 ev1 = le64_to_cpu(sb->events);
1179 ev2 = le64_to_cpu(refsb->events);
1180
1181 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001182 ret = 1;
1183 else
1184 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
NeilBrowna1801f82008-03-04 14:29:31 -08001186 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1188 else
Andre Noll0f420352008-07-11 22:02:23 +10001189 rdev->size = rdev->sb_start / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1191 return -EINVAL;
1192 rdev->size = le64_to_cpu(sb->data_size)/2;
1193 if (le32_to_cpu(sb->chunksize))
1194 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001195
NeilBrown1c05b4b2006-10-21 10:24:08 -07001196 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001197 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1202{
1203 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001204 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
NeilBrown41158c72005-06-21 17:17:25 -07001206 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001207 clear_bit(Faulty, &rdev->flags);
1208 clear_bit(In_sync, &rdev->flags);
1209 clear_bit(WriteMostly, &rdev->flags);
1210 clear_bit(BarriersNotsupp, &rdev->flags);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (mddev->raid_disks == 0) {
1213 mddev->major_version = 1;
1214 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001215 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1217 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1218 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1219 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001220 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 mddev->layout = le32_to_cpu(sb->layout);
1222 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1223 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001224 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001225 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001226 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1229 memcpy(mddev->uuid, sb->set_uuid, 16);
1230
1231 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001232
NeilBrown71c08052005-09-09 16:23:51 -07001233 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001234 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001235 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001236
NeilBrownf6705572006-03-27 01:18:11 -08001237 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1238 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1239 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1240 mddev->new_level = le32_to_cpu(sb->new_level);
1241 mddev->new_layout = le32_to_cpu(sb->new_layout);
1242 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1243 } else {
1244 mddev->reshape_position = MaxSector;
1245 mddev->delta_disks = 0;
1246 mddev->new_level = mddev->level;
1247 mddev->new_layout = mddev->layout;
1248 mddev->new_chunk = mddev->chunk_size;
1249 }
1250
NeilBrown41158c72005-06-21 17:17:25 -07001251 } else if (mddev->pers == NULL) {
1252 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 ++ev1;
1254 if (ev1 < mddev->events)
1255 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001256 } else if (mddev->bitmap) {
1257 /* If adding to array with a bitmap, then we can accept an
1258 * older device, but not too old.
1259 */
NeilBrown41158c72005-06-21 17:17:25 -07001260 if (ev1 < mddev->bitmap->events_cleared)
1261 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001262 } else {
1263 if (ev1 < mddev->events)
1264 /* just a hot-add of a new device, leave raid_disk at -1 */
1265 return 0;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (mddev->level != LEVEL_MULTIPATH) {
1268 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1270 switch(role) {
1271 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
1273 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001274 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
1276 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001277 if ((le32_to_cpu(sb->feature_map) &
1278 MD_FEATURE_RECOVERY_OFFSET))
1279 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1280 else
1281 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 rdev->raid_disk = role;
1283 break;
1284 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001285 if (sb->devflags & WriteMostly1)
1286 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001287 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001288 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291}
1292
1293static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1294{
1295 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 mdk_rdev_t *rdev2;
1297 int max_dev, i;
1298 /* make rdev->sb match mddev and rdev data. */
1299
1300 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1301
1302 sb->feature_map = 0;
1303 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001304 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 memset(sb->pad1, 0, sizeof(sb->pad1));
1306 memset(sb->pad2, 0, sizeof(sb->pad2));
1307 memset(sb->pad3, 0, sizeof(sb->pad3));
1308
1309 sb->utime = cpu_to_le64((__u64)mddev->utime);
1310 sb->events = cpu_to_le64(mddev->events);
1311 if (mddev->in_sync)
1312 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1313 else
1314 sb->resync_offset = cpu_to_le64(0);
1315
NeilBrown1c05b4b2006-10-21 10:24:08 -07001316 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001317
NeilBrownf0ca3402006-02-02 14:28:04 -08001318 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001319 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001320
NeilBrowna654b9d82005-06-21 17:17:27 -07001321 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1322 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001323 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001324 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001325
1326 if (rdev->raid_disk >= 0 &&
1327 !test_bit(In_sync, &rdev->flags) &&
1328 rdev->recovery_offset > 0) {
1329 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1330 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1331 }
1332
NeilBrownf6705572006-03-27 01:18:11 -08001333 if (mddev->reshape_position != MaxSector) {
1334 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1335 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1336 sb->new_layout = cpu_to_le32(mddev->new_layout);
1337 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1338 sb->new_level = cpu_to_le32(mddev->new_level);
1339 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1340 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001343 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (rdev2->desc_nr+1 > max_dev)
1345 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001346
1347 if (max_dev > le32_to_cpu(sb->max_dev))
1348 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 for (i=0; i<max_dev;i++)
1350 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1351
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001352 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001354 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001356 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001358 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1359 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 else
1361 sb->dev_roles[i] = cpu_to_le16(0xffff);
1362 }
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 sb->sb_csum = calc_sb_1_csum(sb);
1365}
1366
Chris Webb0cd17fe2008-06-28 08:31:46 +10001367static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001368super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001369{
1370 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001371 sector_t max_sectors;
1372 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001373 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001374 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001375 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001376 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1377 max_sectors -= rdev->data_offset;
1378 if (!num_sectors || num_sectors > max_sectors)
1379 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001380 } else if (rdev->mddev->bitmap_offset) {
1381 /* minor version 0 with bitmap we can't move */
1382 return 0;
1383 } else {
1384 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001385 sector_t sb_start;
1386 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1387 sb_start &= ~(sector_t)(4*2 - 1);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001388 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1389 if (!num_sectors || num_sectors > max_sectors)
1390 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001391 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001392 }
1393 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001394 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001395 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001396 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001397 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001398 rdev->sb_page);
1399 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001400 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001401}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Adrian Bunk75c96f82005-05-05 16:16:09 -07001403static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 [0] = {
1405 .name = "0.90.0",
1406 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001407 .load_super = super_90_load,
1408 .validate_super = super_90_validate,
1409 .sync_super = super_90_sync,
1410 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 },
1412 [1] = {
1413 .name = "md-1",
1414 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001415 .load_super = super_1_load,
1416 .validate_super = super_1_validate,
1417 .sync_super = super_1_sync,
1418 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 },
1420};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1423{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001424 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
NeilBrown4b809912008-07-21 17:05:25 +10001426 rcu_read_lock();
1427 rdev_for_each_rcu(rdev, mddev1)
1428 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001429 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001430 rdev2->bdev->bd_contains) {
1431 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001432 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001433 }
1434 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return 0;
1436}
1437
1438static LIST_HEAD(pending_raid_disks);
1439
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001440static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev)
1441{
1442 struct mdk_personality *pers = mddev->pers;
1443 struct gendisk *disk = mddev->gendisk;
1444 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1445 struct blk_integrity *bi_mddev = blk_get_integrity(disk);
1446
1447 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1448 if (pers && pers->level >= 4 && pers->level <= 6)
1449 return;
1450
1451 /* If rdev is integrity capable, register profile for mddev */
1452 if (!bi_mddev && bi_rdev) {
1453 if (blk_integrity_register(disk, bi_rdev))
1454 printk(KERN_ERR "%s: %s Could not register integrity!\n",
1455 __func__, disk->disk_name);
1456 else
1457 printk(KERN_NOTICE "Enabling data integrity on %s\n",
1458 disk->disk_name);
1459 return;
1460 }
1461
1462 /* Check that mddev and rdev have matching profiles */
1463 if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) {
1464 printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__,
1465 disk->disk_name, rdev->bdev->bd_disk->disk_name);
1466 printk(KERN_NOTICE "Disabling data integrity on %s\n",
1467 disk->disk_name);
1468 blk_integrity_unregister(disk);
1469 }
1470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1473{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001474 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001475 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001476 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001477 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 if (rdev->mddev) {
1480 MD_BUG();
1481 return -EINVAL;
1482 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001483
1484 /* prevent duplicates */
1485 if (find_rdev(mddev, rdev->bdev->bd_dev))
1486 return -EEXIST;
1487
NeilBrown2bf071b2006-01-06 00:20:55 -08001488 /* make sure rdev->size exceeds mddev->size */
1489 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001490 if (mddev->pers) {
1491 /* Cannot change size, so fail
1492 * If mddev->level <= 0, then we don't care
1493 * about aligning sizes (e.g. linear)
1494 */
1495 if (mddev->level > 0)
1496 return -ENOSPC;
1497 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001498 mddev->size = rdev->size;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /* Verify rdev->desc_nr is unique.
1502 * If it is -1, assign a free number, else
1503 * check number is not in use
1504 */
1505 if (rdev->desc_nr < 0) {
1506 int choice = 0;
1507 if (mddev->pers) choice = mddev->raid_disks;
1508 while (find_rdev_nr(mddev, choice))
1509 choice++;
1510 rdev->desc_nr = choice;
1511 } else {
1512 if (find_rdev_nr(mddev, rdev->desc_nr))
1513 return -EBUSY;
1514 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001515 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1516 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1517 mdname(mddev), mddev->max_disks);
1518 return -EBUSY;
1519 }
NeilBrown19133a42005-11-08 21:39:35 -08001520 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001521 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001522 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001525 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001526
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001527 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001528 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001529
Tejun Heo0762b8b2008-08-25 19:56:12 +09001530 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001531 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1532 kobject_del(&rdev->kobj);
1533 goto fail;
1534 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001535 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1536
NeilBrown4b809912008-07-21 17:05:25 +10001537 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001538 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001539
1540 /* May as well allow recovery to be retried once */
1541 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001542
1543 md_integrity_check(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001545
1546 fail:
1547 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1548 b, mdname(mddev));
1549 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
NeilBrown177a99b2008-02-06 01:39:56 -08001552static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001553{
1554 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1555 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001556 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1560{
1561 char b[BDEVNAME_SIZE];
1562 if (!rdev->mddev) {
1563 MD_BUG();
1564 return;
1565 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001566 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001567 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1569 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001570 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001571 sysfs_put(rdev->sysfs_state);
1572 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001573 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001574 * writing to 'remove' to "dev/state". We also need
1575 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001576 */
NeilBrown4b809912008-07-21 17:05:25 +10001577 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001578 INIT_WORK(&rdev->del_work, md_delayed_delete);
1579 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001580 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/*
1584 * prevent the device from being mounted, repartitioned or
1585 * otherwise reused by a RAID array (or any other kernel
1586 * subsystem), by bd_claiming the device.
1587 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001588static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
1590 int err = 0;
1591 struct block_device *bdev;
1592 char b[BDEVNAME_SIZE];
1593
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001594 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (IS_ERR(bdev)) {
1596 printk(KERN_ERR "md: could not open %s.\n",
1597 __bdevname(dev, b));
1598 return PTR_ERR(bdev);
1599 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001600 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (err) {
1602 printk(KERN_ERR "md: could not bd_claim %s.\n",
1603 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001604 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return err;
1606 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001607 if (!shared)
1608 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 rdev->bdev = bdev;
1610 return err;
1611}
1612
1613static void unlock_rdev(mdk_rdev_t *rdev)
1614{
1615 struct block_device *bdev = rdev->bdev;
1616 rdev->bdev = NULL;
1617 if (!bdev)
1618 MD_BUG();
1619 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001620 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623void md_autodetect_dev(dev_t dev);
1624
1625static void export_rdev(mdk_rdev_t * rdev)
1626{
1627 char b[BDEVNAME_SIZE];
1628 printk(KERN_INFO "md: export_rdev(%s)\n",
1629 bdevname(rdev->bdev,b));
1630 if (rdev->mddev)
1631 MD_BUG();
1632 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001634 if (test_bit(AutoDetected, &rdev->flags))
1635 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636#endif
1637 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001638 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641static void kick_rdev_from_array(mdk_rdev_t * rdev)
1642{
1643 unbind_rdev_from_array(rdev);
1644 export_rdev(rdev);
1645}
1646
1647static void export_array(mddev_t *mddev)
1648{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001649 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
NeilBrownd089c6a2008-02-06 01:39:59 -08001651 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (!rdev->mddev) {
1653 MD_BUG();
1654 continue;
1655 }
1656 kick_rdev_from_array(rdev);
1657 }
1658 if (!list_empty(&mddev->disks))
1659 MD_BUG();
1660 mddev->raid_disks = 0;
1661 mddev->major_version = 0;
1662}
1663
1664static void print_desc(mdp_disk_t *desc)
1665{
1666 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1667 desc->major,desc->minor,desc->raid_disk,desc->state);
1668}
1669
Cheng Renquancd2ac932009-01-09 08:31:08 +11001670static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 int i;
1673
1674 printk(KERN_INFO
1675 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1676 sb->major_version, sb->minor_version, sb->patch_version,
1677 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1678 sb->ctime);
1679 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1680 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1681 sb->md_minor, sb->layout, sb->chunk_size);
1682 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1683 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1684 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1685 sb->failed_disks, sb->spare_disks,
1686 sb->sb_csum, (unsigned long)sb->events_lo);
1687
1688 printk(KERN_INFO);
1689 for (i = 0; i < MD_SB_DISKS; i++) {
1690 mdp_disk_t *desc;
1691
1692 desc = sb->disks + i;
1693 if (desc->number || desc->major || desc->minor ||
1694 desc->raid_disk || (desc->state && (desc->state != 4))) {
1695 printk(" D %2d: ", i);
1696 print_desc(desc);
1697 }
1698 }
1699 printk(KERN_INFO "md: THIS: ");
1700 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Cheng Renquancd2ac932009-01-09 08:31:08 +11001703static void print_sb_1(struct mdp_superblock_1 *sb)
1704{
1705 __u8 *uuid;
1706
1707 uuid = sb->set_uuid;
1708 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1709 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1710 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1711 le32_to_cpu(sb->major_version),
1712 le32_to_cpu(sb->feature_map),
1713 uuid[0], uuid[1], uuid[2], uuid[3],
1714 uuid[4], uuid[5], uuid[6], uuid[7],
1715 uuid[8], uuid[9], uuid[10], uuid[11],
1716 uuid[12], uuid[13], uuid[14], uuid[15],
1717 sb->set_name,
1718 (unsigned long long)le64_to_cpu(sb->ctime)
1719 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1720
1721 uuid = sb->device_uuid;
1722 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1723 " RO:%llu\n"
1724 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1725 ":%02x%02x%02x%02x%02x%02x\n"
1726 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1727 KERN_INFO "md: (MaxDev:%u) \n",
1728 le32_to_cpu(sb->level),
1729 (unsigned long long)le64_to_cpu(sb->size),
1730 le32_to_cpu(sb->raid_disks),
1731 le32_to_cpu(sb->layout),
1732 le32_to_cpu(sb->chunksize),
1733 (unsigned long long)le64_to_cpu(sb->data_offset),
1734 (unsigned long long)le64_to_cpu(sb->data_size),
1735 (unsigned long long)le64_to_cpu(sb->super_offset),
1736 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1737 le32_to_cpu(sb->dev_number),
1738 uuid[0], uuid[1], uuid[2], uuid[3],
1739 uuid[4], uuid[5], uuid[6], uuid[7],
1740 uuid[8], uuid[9], uuid[10], uuid[11],
1741 uuid[12], uuid[13], uuid[14], uuid[15],
1742 sb->devflags,
1743 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1744 (unsigned long long)le64_to_cpu(sb->events),
1745 (unsigned long long)le64_to_cpu(sb->resync_offset),
1746 le32_to_cpu(sb->sb_csum),
1747 le32_to_cpu(sb->max_dev)
1748 );
1749}
1750
1751static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 char b[BDEVNAME_SIZE];
1754 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1755 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001756 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1757 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001759 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1760 switch (major_version) {
1761 case 0:
1762 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1763 break;
1764 case 1:
1765 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1766 break;
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 } else
1769 printk(KERN_INFO "md: no rdev superblock!\n");
1770}
1771
Adrian Bunk5e563412006-06-26 00:27:42 -07001772static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001774 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 mdk_rdev_t *rdev;
1776 mddev_t *mddev;
1777 char b[BDEVNAME_SIZE];
1778
1779 printk("\n");
1780 printk("md: **********************************\n");
1781 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1782 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001783 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
NeilBrown32a76272005-06-21 17:17:14 -07001785 if (mddev->bitmap)
1786 bitmap_print_sb(mddev->bitmap);
1787 else
1788 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001789 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 printk("<%s>", bdevname(rdev->bdev,b));
1791 printk("\n");
1792
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001793 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001794 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796 printk("md: **********************************\n");
1797 printk("\n");
1798}
1799
1800
NeilBrown42543762006-06-26 00:27:57 -07001801static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
NeilBrown42543762006-06-26 00:27:57 -07001803 /* Update each superblock (in-memory image), but
1804 * if we are allowed to, skip spares which already
1805 * have the right event counter, or have one earlier
1806 * (which would mean they aren't being marked as dirty
1807 * with the rest of the array)
1808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001811 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001812 if (rdev->sb_events == mddev->events ||
1813 (nospares &&
1814 rdev->raid_disk < 0 &&
1815 (rdev->sb_events&1)==0 &&
1816 rdev->sb_events+1 == mddev->events)) {
1817 /* Don't update this superblock */
1818 rdev->sb_loaded = 2;
1819 } else {
1820 super_types[mddev->major_version].
1821 sync_super(mddev, rdev);
1822 rdev->sb_loaded = 1;
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825}
1826
NeilBrown850b2b422006-10-03 01:15:46 -07001827static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001830 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001831 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
NeilBrown8377bc82008-04-30 00:52:28 -07001833 if (mddev->external)
1834 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001836 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001837
NeilBrown850b2b422006-10-03 01:15:46 -07001838 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1839 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1840 force_change = 1;
1841 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1842 /* just a clean<-> dirty transition, possibly leave spares alone,
1843 * though if events isn't the right even/odd, we will have to do
1844 * spares after all
1845 */
1846 nospares = 1;
1847 if (force_change)
1848 nospares = 0;
1849 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001850 /* If the array is degraded, then skipping spares is both
1851 * dangerous and fairly pointless.
1852 * Dangerous because a device that was removed from the array
1853 * might have a event_count that still looks up-to-date,
1854 * so it can be re-added without a resync.
1855 * Pointless because if there are any spares to skip,
1856 * then a recovery will happen and soon that array won't
1857 * be degraded any more and the spare can go back to sleep then.
1858 */
NeilBrown850b2b422006-10-03 01:15:46 -07001859 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001860
NeilBrown06d91a52005-06-21 17:17:12 -07001861 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001863
1864 /* If this is just a dirty<->clean transition, and the array is clean
1865 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07001866 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001867 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001868 && (mddev->events & 1)
1869 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001870 mddev->events--;
1871 else {
1872 /* otherwise we have to go forward and ... */
1873 mddev->events ++;
1874 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1875 /* .. if the array isn't clean, insist on an odd 'events' */
1876 if ((mddev->events&1)==0) {
1877 mddev->events++;
1878 nospares = 0;
1879 }
1880 } else {
1881 /* otherwise insist on an even 'events' (for clean states) */
1882 if ((mddev->events&1)) {
1883 mddev->events++;
1884 nospares = 0;
1885 }
1886 }
1887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 if (!mddev->events) {
1890 /*
1891 * oops, this 64-bit counter should never wrap.
1892 * Either we are in around ~1 trillion A.C., assuming
1893 * 1 reboot per second, or we have a bug:
1894 */
1895 MD_BUG();
1896 mddev->events --;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /*
1900 * do not write anything to disk if using
1901 * nonpersistent superblocks
1902 */
NeilBrown06d91a52005-06-21 17:17:12 -07001903 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001904 if (!mddev->external)
1905 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1906
NeilBrowna9701a32005-11-08 21:39:34 -08001907 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001908 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001910 }
NeilBrowne6910632008-02-06 01:39:51 -08001911 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001912 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 dprintk(KERN_INFO
1915 "md: updating %s RAID superblock on device (in sync %d)\n",
1916 mdname(mddev),mddev->in_sync);
1917
NeilBrown4ad13662007-07-17 04:06:13 -07001918 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001919 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 char b[BDEVNAME_SIZE];
1921 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001922 if (rdev->sb_loaded != 1)
1923 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001924 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 dprintk("(skipping faulty ");
1926
1927 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001928 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001929 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001930 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001931 rdev->sb_page);
1932 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1933 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001934 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001935 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 } else
1938 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001939 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* only need to write one superblock... */
1941 break;
1942 }
NeilBrowna9701a32005-11-08 21:39:34 -08001943 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07001944 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001945
NeilBrowna9701a32005-11-08 21:39:34 -08001946 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07001947 if (mddev->in_sync != sync_req ||
1948 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001949 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001950 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001951 goto repeat;
1952 }
NeilBrown850b2b422006-10-03 01:15:46 -07001953 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001954 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001955 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
Andre Noll7f6ce762008-03-23 18:34:54 +01001959/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001960 * We want to accept with case. For this we use cmd_match.
1961 */
1962static int cmd_match(const char *cmd, const char *str)
1963{
1964 /* See if cmd, written into a sysfs file, matches
1965 * str. They must either be the same, or cmd can
1966 * have a trailing newline
1967 */
1968 while (*cmd && *str && *cmd == *str) {
1969 cmd++;
1970 str++;
1971 }
1972 if (*cmd == '\n')
1973 cmd++;
1974 if (*str || *cmd)
1975 return 0;
1976 return 1;
1977}
1978
NeilBrown86e6ffd2005-11-08 21:39:24 -08001979struct rdev_sysfs_entry {
1980 struct attribute attr;
1981 ssize_t (*show)(mdk_rdev_t *, char *);
1982 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1983};
1984
1985static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001986state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001987{
1988 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001989 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001990
NeilBrownb2d444d2005-11-08 21:39:31 -08001991 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001992 len+= sprintf(page+len, "%sfaulty",sep);
1993 sep = ",";
1994 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001995 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001996 len += sprintf(page+len, "%sin_sync",sep);
1997 sep = ",";
1998 }
NeilBrownf6556752006-06-26 00:28:01 -07001999 if (test_bit(WriteMostly, &rdev->flags)) {
2000 len += sprintf(page+len, "%swrite_mostly",sep);
2001 sep = ",";
2002 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002003 if (test_bit(Blocked, &rdev->flags)) {
2004 len += sprintf(page+len, "%sblocked", sep);
2005 sep = ",";
2006 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002007 if (!test_bit(Faulty, &rdev->flags) &&
2008 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002009 len += sprintf(page+len, "%sspare", sep);
2010 sep = ",";
2011 }
2012 return len+sprintf(page+len, "\n");
2013}
2014
NeilBrown45dc2de2006-06-26 00:27:58 -07002015static ssize_t
2016state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2017{
2018 /* can write
2019 * faulty - simulates and error
2020 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002021 * writemostly - sets write_mostly
2022 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002023 * blocked - sets the Blocked flag
2024 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07002025 */
2026 int err = -EINVAL;
2027 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2028 md_error(rdev->mddev, rdev);
2029 err = 0;
2030 } else if (cmd_match(buf, "remove")) {
2031 if (rdev->raid_disk >= 0)
2032 err = -EBUSY;
2033 else {
2034 mddev_t *mddev = rdev->mddev;
2035 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002036 if (mddev->pers)
2037 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002038 md_new_event(mddev);
2039 err = 0;
2040 }
NeilBrownf6556752006-06-26 00:28:01 -07002041 } else if (cmd_match(buf, "writemostly")) {
2042 set_bit(WriteMostly, &rdev->flags);
2043 err = 0;
2044 } else if (cmd_match(buf, "-writemostly")) {
2045 clear_bit(WriteMostly, &rdev->flags);
2046 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002047 } else if (cmd_match(buf, "blocked")) {
2048 set_bit(Blocked, &rdev->flags);
2049 err = 0;
2050 } else if (cmd_match(buf, "-blocked")) {
2051 clear_bit(Blocked, &rdev->flags);
2052 wake_up(&rdev->blocked_wait);
2053 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2054 md_wakeup_thread(rdev->mddev->thread);
2055
2056 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002057 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002058 if (!err && rdev->sysfs_state)
2059 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002060 return err ? err : len;
2061}
NeilBrown80ca3a42006-07-10 04:44:18 -07002062static struct rdev_sysfs_entry rdev_state =
2063__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002064
2065static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002066errors_show(mdk_rdev_t *rdev, char *page)
2067{
2068 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2069}
2070
2071static ssize_t
2072errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2073{
2074 char *e;
2075 unsigned long n = simple_strtoul(buf, &e, 10);
2076 if (*buf && (*e == 0 || *e == '\n')) {
2077 atomic_set(&rdev->corrected_errors, n);
2078 return len;
2079 }
2080 return -EINVAL;
2081}
2082static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002083__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002084
NeilBrown014236d2006-01-06 00:20:55 -08002085static ssize_t
2086slot_show(mdk_rdev_t *rdev, char *page)
2087{
2088 if (rdev->raid_disk < 0)
2089 return sprintf(page, "none\n");
2090 else
2091 return sprintf(page, "%d\n", rdev->raid_disk);
2092}
2093
2094static ssize_t
2095slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2096{
2097 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002098 int err;
2099 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002100 int slot = simple_strtoul(buf, &e, 10);
2101 if (strncmp(buf, "none", 4)==0)
2102 slot = -1;
2103 else if (e==buf || (*e && *e!= '\n'))
2104 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002105 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002106 /* Setting 'slot' on an active array requires also
2107 * updating the 'rd%d' link, and communicating
2108 * with the personality with ->hot_*_disk.
2109 * For now we only support removing
2110 * failed/spare devices. This normally happens automatically,
2111 * but not when the metadata is externally managed.
2112 */
NeilBrownc303da62008-02-06 01:39:51 -08002113 if (rdev->raid_disk == -1)
2114 return -EEXIST;
2115 /* personality does all needed checks */
2116 if (rdev->mddev->pers->hot_add_disk == NULL)
2117 return -EINVAL;
2118 err = rdev->mddev->pers->
2119 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2120 if (err)
2121 return err;
2122 sprintf(nm, "rd%d", rdev->raid_disk);
2123 sysfs_remove_link(&rdev->mddev->kobj, nm);
2124 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2125 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002126 } else if (rdev->mddev->pers) {
2127 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002128 /* Activating a spare .. or possibly reactivating
2129 * if we every get bitmaps working here.
2130 */
2131
2132 if (rdev->raid_disk != -1)
2133 return -EBUSY;
2134
2135 if (rdev->mddev->pers->hot_add_disk == NULL)
2136 return -EINVAL;
2137
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002138 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002139 if (rdev2->raid_disk == slot)
2140 return -EEXIST;
2141
2142 rdev->raid_disk = slot;
2143 if (test_bit(In_sync, &rdev->flags))
2144 rdev->saved_raid_disk = slot;
2145 else
2146 rdev->saved_raid_disk = -1;
2147 err = rdev->mddev->pers->
2148 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002149 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002150 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002151 return err;
Neil Brown52664732008-06-28 08:31:44 +10002152 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002153 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002154 sprintf(nm, "rd%d", rdev->raid_disk);
2155 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2156 printk(KERN_WARNING
2157 "md: cannot register "
2158 "%s for %s\n",
2159 nm, mdname(rdev->mddev));
2160
2161 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002162 } else {
2163 if (slot >= rdev->mddev->raid_disks)
2164 return -ENOSPC;
2165 rdev->raid_disk = slot;
2166 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002167 clear_bit(Faulty, &rdev->flags);
2168 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002169 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002170 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002171 }
NeilBrown014236d2006-01-06 00:20:55 -08002172 return len;
2173}
2174
2175
2176static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002177__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002178
NeilBrown93c8cad2006-01-06 00:20:56 -08002179static ssize_t
2180offset_show(mdk_rdev_t *rdev, char *page)
2181{
Andrew Morton6961ece2006-01-06 00:20:59 -08002182 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002183}
2184
2185static ssize_t
2186offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2187{
2188 char *e;
2189 unsigned long long offset = simple_strtoull(buf, &e, 10);
2190 if (e==buf || (*e && *e != '\n'))
2191 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002192 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002193 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002194 if (rdev->size && rdev->mddev->external)
2195 /* Must set offset before size, so overlap checks
2196 * can be sane */
2197 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002198 rdev->data_offset = offset;
2199 return len;
2200}
2201
2202static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002203__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002204
NeilBrown83303b62006-01-06 00:21:06 -08002205static ssize_t
2206rdev_size_show(mdk_rdev_t *rdev, char *page)
2207{
2208 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2209}
2210
NeilBrownc5d79ad2008-02-06 01:39:54 -08002211static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2212{
2213 /* check if two start/length pairs overlap */
2214 if (s1+l1 <= s2)
2215 return 0;
2216 if (s2+l2 <= s1)
2217 return 0;
2218 return 1;
2219}
2220
NeilBrown83303b62006-01-06 00:21:06 -08002221static ssize_t
2222rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2223{
Neil Brownd7027452008-07-12 10:37:50 +10002224 unsigned long long size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002225 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002226 mddev_t *my_mddev = rdev->mddev;
2227
Neil Brownd7027452008-07-12 10:37:50 +10002228 if (strict_strtoull(buf, 10, &size) < 0)
2229 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002230 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002231 if (my_mddev->persistent) {
2232 size = super_types[my_mddev->major_version].
Andre Noll15f4a5f2008-07-21 14:42:12 +10002233 rdev_size_change(rdev, size * 2);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002234 if (!size)
2235 return -EBUSY;
2236 } else if (!size) {
2237 size = (rdev->bdev->bd_inode->i_size >> 10);
2238 size -= rdev->data_offset/2;
2239 }
Chris Webb0cd17fe2008-06-28 08:31:46 +10002240 }
Chris Webb7d3c6f82008-10-13 11:55:11 +11002241 if (size < my_mddev->size)
2242 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002243
NeilBrown83303b62006-01-06 00:21:06 -08002244 rdev->size = size;
Neil Brownd7027452008-07-12 10:37:50 +10002245 if (size > oldsize && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002246 /* need to check that all other rdevs with the same ->bdev
2247 * do not overlap. We need to unlock the mddev to avoid
2248 * a deadlock. We have already changed rdev->size, and if
2249 * we have to change it back, we will have the lock again.
2250 */
2251 mddev_t *mddev;
2252 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002253 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002254
NeilBrown27c529b2008-03-04 14:29:33 -08002255 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002256 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002257 mdk_rdev_t *rdev2;
2258
2259 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002260 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002261 if (test_bit(AllReserved, &rdev2->flags) ||
2262 (rdev->bdev == rdev2->bdev &&
2263 rdev != rdev2 &&
Andre Nolld07bd3b2008-07-21 14:42:07 +10002264 overlaps(rdev->data_offset, rdev->size * 2,
2265 rdev2->data_offset,
2266 rdev2->size * 2))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002267 overlap = 1;
2268 break;
2269 }
2270 mddev_unlock(mddev);
2271 if (overlap) {
2272 mddev_put(mddev);
2273 break;
2274 }
2275 }
NeilBrown27c529b2008-03-04 14:29:33 -08002276 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002277 if (overlap) {
2278 /* Someone else could have slipped in a size
2279 * change here, but doing so is just silly.
2280 * We put oldsize back because we *know* it is
2281 * safe, and trust userspace not to race with
2282 * itself
2283 */
2284 rdev->size = oldsize;
2285 return -EBUSY;
2286 }
2287 }
NeilBrown83303b62006-01-06 00:21:06 -08002288 return len;
2289}
2290
2291static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002292__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002293
NeilBrown86e6ffd2005-11-08 21:39:24 -08002294static struct attribute *rdev_default_attrs[] = {
2295 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002296 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002297 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002298 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002299 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002300 NULL,
2301};
2302static ssize_t
2303rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2304{
2305 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2306 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002307 mddev_t *mddev = rdev->mddev;
2308 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002309
2310 if (!entry->show)
2311 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002312
2313 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2314 if (!rv) {
2315 if (rdev->mddev == NULL)
2316 rv = -EBUSY;
2317 else
2318 rv = entry->show(rdev, page);
2319 mddev_unlock(mddev);
2320 }
2321 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002322}
2323
2324static ssize_t
2325rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2326 const char *page, size_t length)
2327{
2328 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2329 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002330 ssize_t rv;
2331 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002332
2333 if (!entry->store)
2334 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002335 if (!capable(CAP_SYS_ADMIN))
2336 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002337 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002338 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002339 if (rdev->mddev == NULL)
2340 rv = -EBUSY;
2341 else
2342 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002343 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002344 }
2345 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002346}
2347
2348static void rdev_free(struct kobject *ko)
2349{
2350 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2351 kfree(rdev);
2352}
2353static struct sysfs_ops rdev_sysfs_ops = {
2354 .show = rdev_attr_show,
2355 .store = rdev_attr_store,
2356};
2357static struct kobj_type rdev_ktype = {
2358 .release = rdev_free,
2359 .sysfs_ops = &rdev_sysfs_ops,
2360 .default_attrs = rdev_default_attrs,
2361};
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363/*
2364 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2365 *
2366 * mark the device faulty if:
2367 *
2368 * - the device is nonexistent (zero size)
2369 * - the device has no valid superblock
2370 *
2371 * a faulty rdev _never_ has rdev->sb set.
2372 */
2373static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2374{
2375 char b[BDEVNAME_SIZE];
2376 int err;
2377 mdk_rdev_t *rdev;
2378 sector_t size;
2379
NeilBrown9ffae0c2006-01-06 00:20:32 -08002380 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (!rdev) {
2382 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2383 return ERR_PTR(-ENOMEM);
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 if ((err = alloc_disk_sb(rdev)))
2387 goto abort_free;
2388
NeilBrownc5d79ad2008-02-06 01:39:54 -08002389 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (err)
2391 goto abort_free;
2392
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002393 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002396 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002397 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002398 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002400 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002402 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002403 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2406 if (!size) {
2407 printk(KERN_WARNING
2408 "md: %s has zero or unknown size, marking faulty!\n",
2409 bdevname(rdev->bdev,b));
2410 err = -EINVAL;
2411 goto abort_free;
2412 }
2413
2414 if (super_format >= 0) {
2415 err = super_types[super_format].
2416 load_super(rdev, NULL, super_minor);
2417 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002418 printk(KERN_WARNING
2419 "md: %s does not have a valid v%d.%d "
2420 "superblock, not importing!\n",
2421 bdevname(rdev->bdev,b),
2422 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 goto abort_free;
2424 }
2425 if (err < 0) {
2426 printk(KERN_WARNING
2427 "md: could not read %s's sb, not importing!\n",
2428 bdevname(rdev->bdev,b));
2429 goto abort_free;
2430 }
2431 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002434 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 return rdev;
2437
2438abort_free:
2439 if (rdev->sb_page) {
2440 if (rdev->bdev)
2441 unlock_rdev(rdev);
2442 free_disk_sb(rdev);
2443 }
2444 kfree(rdev);
2445 return ERR_PTR(err);
2446}
2447
2448/*
2449 * Check a full RAID array for plausibility
2450 */
2451
2452
NeilBrowna757e642005-04-16 15:26:42 -07002453static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
2455 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002456 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 char b[BDEVNAME_SIZE];
2458
2459 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002460 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 switch (super_types[mddev->major_version].
2462 load_super(rdev, freshest, mddev->minor_version)) {
2463 case 1:
2464 freshest = rdev;
2465 break;
2466 case 0:
2467 break;
2468 default:
2469 printk( KERN_ERR \
2470 "md: fatal superblock inconsistency in %s"
2471 " -- removing from array\n",
2472 bdevname(rdev->bdev,b));
2473 kick_rdev_from_array(rdev);
2474 }
2475
2476
2477 super_types[mddev->major_version].
2478 validate_super(mddev, freshest);
2479
2480 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002481 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002482 if (rdev->desc_nr >= mddev->max_disks ||
2483 i > mddev->max_disks) {
2484 printk(KERN_WARNING
2485 "md: %s: %s: only %d devices permitted\n",
2486 mdname(mddev), bdevname(rdev->bdev, b),
2487 mddev->max_disks);
2488 kick_rdev_from_array(rdev);
2489 continue;
2490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (rdev != freshest)
2492 if (super_types[mddev->major_version].
2493 validate_super(mddev, rdev)) {
2494 printk(KERN_WARNING "md: kicking non-fresh %s"
2495 " from array!\n",
2496 bdevname(rdev->bdev,b));
2497 kick_rdev_from_array(rdev);
2498 continue;
2499 }
2500 if (mddev->level == LEVEL_MULTIPATH) {
2501 rdev->desc_nr = i++;
2502 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002503 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002504 } else if (rdev->raid_disk >= mddev->raid_disks) {
2505 rdev->raid_disk = -1;
2506 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
2508 }
2509
2510
2511
2512 if (mddev->recovery_cp != MaxSector &&
2513 mddev->level >= 1)
2514 printk(KERN_ERR "md: %s: raid array is not clean"
2515 " -- starting background reconstruction\n",
2516 mdname(mddev));
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
NeilBrown19052c02008-08-05 15:54:13 +10002520static void md_safemode_timeout(unsigned long data);
2521
NeilBrowneae17012005-11-08 21:39:23 -08002522static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002523safe_delay_show(mddev_t *mddev, char *page)
2524{
2525 int msec = (mddev->safemode_delay*1000)/HZ;
2526 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2527}
2528static ssize_t
2529safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2530{
2531 int scale=1;
2532 int dot=0;
2533 int i;
2534 unsigned long msec;
2535 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002536
NeilBrown16f17b32006-06-26 00:27:37 -07002537 /* remove a period, and count digits after it */
2538 if (len >= sizeof(buf))
2539 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002540 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002541 for (i=0; i<len; i++) {
2542 if (dot) {
2543 if (isdigit(buf[i])) {
2544 buf[i-1] = buf[i];
2545 scale *= 10;
2546 }
2547 buf[i] = 0;
2548 } else if (buf[i] == '.') {
2549 dot=1;
2550 buf[i] = 0;
2551 }
2552 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002553 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002554 return -EINVAL;
2555 msec = (msec * 1000) / scale;
2556 if (msec == 0)
2557 mddev->safemode_delay = 0;
2558 else {
NeilBrown19052c02008-08-05 15:54:13 +10002559 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002560 mddev->safemode_delay = (msec*HZ)/1000;
2561 if (mddev->safemode_delay == 0)
2562 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002563 if (mddev->safemode_delay < old_delay)
2564 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002565 }
2566 return len;
2567}
2568static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002569__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002570
2571static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002572level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002573{
NeilBrown2604b702006-01-06 00:20:36 -08002574 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002575 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002576 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002577 else if (mddev->clevel[0])
2578 return sprintf(page, "%s\n", mddev->clevel);
2579 else if (mddev->level != LEVEL_NONE)
2580 return sprintf(page, "%d\n", mddev->level);
2581 else
2582 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002583}
2584
NeilBrownd9d166c2006-01-06 00:20:51 -08002585static ssize_t
2586level_store(mddev_t *mddev, const char *buf, size_t len)
2587{
NeilBrown20a49ff2008-02-06 01:39:57 -08002588 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002589 if (mddev->pers)
2590 return -EBUSY;
2591 if (len == 0)
2592 return 0;
2593 if (len >= sizeof(mddev->clevel))
2594 return -ENOSPC;
2595 strncpy(mddev->clevel, buf, len);
2596 if (mddev->clevel[len-1] == '\n')
2597 len--;
2598 mddev->clevel[len] = 0;
2599 mddev->level = LEVEL_NONE;
2600 return rv;
2601}
2602
2603static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002604__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002605
NeilBrownd4dbd022006-06-26 00:27:59 -07002606
2607static ssize_t
2608layout_show(mddev_t *mddev, char *page)
2609{
2610 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002611 if (mddev->reshape_position != MaxSector &&
2612 mddev->layout != mddev->new_layout)
2613 return sprintf(page, "%d (%d)\n",
2614 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002615 return sprintf(page, "%d\n", mddev->layout);
2616}
2617
2618static ssize_t
2619layout_store(mddev_t *mddev, const char *buf, size_t len)
2620{
2621 char *e;
2622 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002623
2624 if (!*buf || (*e && *e != '\n'))
2625 return -EINVAL;
2626
NeilBrown08a02ec2007-05-09 02:35:38 -07002627 if (mddev->pers)
2628 return -EBUSY;
2629 if (mddev->reshape_position != MaxSector)
2630 mddev->new_layout = n;
2631 else
2632 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002633 return len;
2634}
2635static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002636__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002637
2638
NeilBrowneae17012005-11-08 21:39:23 -08002639static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002640raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002641{
NeilBrownbb636542005-11-08 21:39:45 -08002642 if (mddev->raid_disks == 0)
2643 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002644 if (mddev->reshape_position != MaxSector &&
2645 mddev->delta_disks != 0)
2646 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2647 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002648 return sprintf(page, "%d\n", mddev->raid_disks);
2649}
2650
NeilBrownda943b992006-01-06 00:20:54 -08002651static int update_raid_disks(mddev_t *mddev, int raid_disks);
2652
2653static ssize_t
2654raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2655{
NeilBrownda943b992006-01-06 00:20:54 -08002656 char *e;
2657 int rv = 0;
2658 unsigned long n = simple_strtoul(buf, &e, 10);
2659
2660 if (!*buf || (*e && *e != '\n'))
2661 return -EINVAL;
2662
2663 if (mddev->pers)
2664 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002665 else if (mddev->reshape_position != MaxSector) {
2666 int olddisks = mddev->raid_disks - mddev->delta_disks;
2667 mddev->delta_disks = n - olddisks;
2668 mddev->raid_disks = n;
2669 } else
NeilBrownda943b992006-01-06 00:20:54 -08002670 mddev->raid_disks = n;
2671 return rv ? rv : len;
2672}
2673static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002674__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002675
NeilBrown24dd4692005-11-08 21:39:26 -08002676static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002677chunk_size_show(mddev_t *mddev, char *page)
2678{
NeilBrown08a02ec2007-05-09 02:35:38 -07002679 if (mddev->reshape_position != MaxSector &&
2680 mddev->chunk_size != mddev->new_chunk)
2681 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2682 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002683 return sprintf(page, "%d\n", mddev->chunk_size);
2684}
2685
2686static ssize_t
2687chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2688{
2689 /* can only set chunk_size if array is not yet active */
2690 char *e;
2691 unsigned long n = simple_strtoul(buf, &e, 10);
2692
NeilBrown3b343802006-01-06 00:20:47 -08002693 if (!*buf || (*e && *e != '\n'))
2694 return -EINVAL;
2695
NeilBrown08a02ec2007-05-09 02:35:38 -07002696 if (mddev->pers)
2697 return -EBUSY;
2698 else if (mddev->reshape_position != MaxSector)
2699 mddev->new_chunk = n;
2700 else
2701 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002702 return len;
2703}
2704static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002705__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002706
NeilBrowna94213b2006-06-26 00:28:00 -07002707static ssize_t
2708resync_start_show(mddev_t *mddev, char *page)
2709{
2710 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2711}
2712
2713static ssize_t
2714resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2715{
NeilBrowna94213b2006-06-26 00:28:00 -07002716 char *e;
2717 unsigned long long n = simple_strtoull(buf, &e, 10);
2718
2719 if (mddev->pers)
2720 return -EBUSY;
2721 if (!*buf || (*e && *e != '\n'))
2722 return -EINVAL;
2723
2724 mddev->recovery_cp = n;
2725 return len;
2726}
2727static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002728__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002729
NeilBrown9e653b62006-06-26 00:27:58 -07002730/*
2731 * The array state can be:
2732 *
2733 * clear
2734 * No devices, no size, no level
2735 * Equivalent to STOP_ARRAY ioctl
2736 * inactive
2737 * May have some settings, but array is not active
2738 * all IO results in error
2739 * When written, doesn't tear down array, but just stops it
2740 * suspended (not supported yet)
2741 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002742 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002743 * readonly
2744 * no resync can happen. no superblocks get written.
2745 * write requests fail
2746 * read-auto
2747 * like readonly, but behaves like 'clean' on a write request.
2748 *
2749 * clean - no pending writes, but otherwise active.
2750 * When written to inactive array, starts without resync
2751 * If a write request arrives then
2752 * if metadata is known, mark 'dirty' and switch to 'active'.
2753 * if not known, block and switch to write-pending
2754 * If written to an active array that has pending writes, then fails.
2755 * active
2756 * fully active: IO and resync can be happening.
2757 * When written to inactive array, starts with resync
2758 *
2759 * write-pending
2760 * clean, but writes are blocked waiting for 'active' to be written.
2761 *
2762 * active-idle
2763 * like active, but no writes have been seen for a while (100msec).
2764 *
2765 */
2766enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2767 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002768static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002769 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2770 "write-pending", "active-idle", NULL };
2771
2772static int match_word(const char *word, char **list)
2773{
2774 int n;
2775 for (n=0; list[n]; n++)
2776 if (cmd_match(word, list[n]))
2777 break;
2778 return n;
2779}
2780
2781static ssize_t
2782array_state_show(mddev_t *mddev, char *page)
2783{
2784 enum array_state st = inactive;
2785
2786 if (mddev->pers)
2787 switch(mddev->ro) {
2788 case 1:
2789 st = readonly;
2790 break;
2791 case 2:
2792 st = read_auto;
2793 break;
2794 case 0:
2795 if (mddev->in_sync)
2796 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002797 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2798 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002799 else if (mddev->safemode)
2800 st = active_idle;
2801 else
2802 st = active;
2803 }
2804 else {
2805 if (list_empty(&mddev->disks) &&
2806 mddev->raid_disks == 0 &&
2807 mddev->size == 0)
2808 st = clear;
2809 else
2810 st = inactive;
2811 }
2812 return sprintf(page, "%s\n", array_states[st]);
2813}
2814
Neil Browndf5b20c2008-07-11 22:02:22 +10002815static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002816static int do_md_run(mddev_t * mddev);
2817static int restart_array(mddev_t *mddev);
2818
2819static ssize_t
2820array_state_store(mddev_t *mddev, const char *buf, size_t len)
2821{
2822 int err = -EINVAL;
2823 enum array_state st = match_word(buf, array_states);
2824 switch(st) {
2825 case bad_word:
2826 break;
2827 case clear:
2828 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10002829 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08002830 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002831 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002832 break;
2833 case inactive:
2834 /* stopping an active array */
2835 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10002836 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002837 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002838 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002839 } else
2840 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002841 break;
2842 case suspended:
2843 break; /* not supported yet */
2844 case readonly:
2845 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002846 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002847 else {
2848 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002849 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002850 err = do_md_run(mddev);
2851 }
2852 break;
2853 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002854 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11002855 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10002856 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11002857 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07002858 err = restart_array(mddev);
2859 if (err == 0) {
2860 mddev->ro = 2;
2861 set_disk_ro(mddev->gendisk, 0);
2862 }
NeilBrown9e653b62006-06-26 00:27:58 -07002863 } else {
2864 mddev->ro = 2;
2865 err = do_md_run(mddev);
2866 }
2867 break;
2868 case clean:
2869 if (mddev->pers) {
2870 restart_array(mddev);
2871 spin_lock_irq(&mddev->write_lock);
2872 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002873 if (mddev->in_sync == 0) {
2874 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002875 if (mddev->safemode == 1)
2876 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002877 if (mddev->persistent)
2878 set_bit(MD_CHANGE_CLEAN,
2879 &mddev->flags);
2880 }
2881 err = 0;
2882 } else
2883 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002884 spin_unlock_irq(&mddev->write_lock);
2885 } else {
2886 mddev->ro = 0;
2887 mddev->recovery_cp = MaxSector;
2888 err = do_md_run(mddev);
2889 }
2890 break;
2891 case active:
2892 if (mddev->pers) {
2893 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002894 if (mddev->external)
2895 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002896 wake_up(&mddev->sb_wait);
2897 err = 0;
2898 } else {
2899 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002900 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002901 err = do_md_run(mddev);
2902 }
2903 break;
2904 case write_pending:
2905 case active_idle:
2906 /* these cannot be set */
2907 break;
2908 }
2909 if (err)
2910 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002911 else {
NeilBrownb62b7592008-10-21 13:25:21 +11002912 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07002913 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002914 }
NeilBrown9e653b62006-06-26 00:27:58 -07002915}
NeilBrown80ca3a42006-07-10 04:44:18 -07002916static struct md_sysfs_entry md_array_state =
2917__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002918
NeilBrown6d7ff7382006-01-06 00:21:16 -08002919static ssize_t
2920null_show(mddev_t *mddev, char *page)
2921{
2922 return -EINVAL;
2923}
2924
2925static ssize_t
2926new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2927{
2928 /* buf must be %d:%d\n? giving major and minor numbers */
2929 /* The new device is added to the array.
2930 * If the array has a persistent superblock, we read the
2931 * superblock to initialise info and check validity.
2932 * Otherwise, only checking done is that in bind_rdev_to_array,
2933 * which mainly checks size.
2934 */
2935 char *e;
2936 int major = simple_strtoul(buf, &e, 10);
2937 int minor;
2938 dev_t dev;
2939 mdk_rdev_t *rdev;
2940 int err;
2941
2942 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2943 return -EINVAL;
2944 minor = simple_strtoul(e+1, &e, 10);
2945 if (*e && *e != '\n')
2946 return -EINVAL;
2947 dev = MKDEV(major, minor);
2948 if (major != MAJOR(dev) ||
2949 minor != MINOR(dev))
2950 return -EOVERFLOW;
2951
2952
2953 if (mddev->persistent) {
2954 rdev = md_import_device(dev, mddev->major_version,
2955 mddev->minor_version);
2956 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2957 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2958 mdk_rdev_t, same_set);
2959 err = super_types[mddev->major_version]
2960 .load_super(rdev, rdev0, mddev->minor_version);
2961 if (err < 0)
2962 goto out;
2963 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002964 } else if (mddev->external)
2965 rdev = md_import_device(dev, -2, -1);
2966 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08002967 rdev = md_import_device(dev, -1, -1);
2968
2969 if (IS_ERR(rdev))
2970 return PTR_ERR(rdev);
2971 err = bind_rdev_to_array(rdev, mddev);
2972 out:
2973 if (err)
2974 export_rdev(rdev);
2975 return err ? err : len;
2976}
2977
2978static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002979__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002980
2981static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002982bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2983{
2984 char *end;
2985 unsigned long chunk, end_chunk;
2986
2987 if (!mddev->bitmap)
2988 goto out;
2989 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2990 while (*buf) {
2991 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2992 if (buf == end) break;
2993 if (*end == '-') { /* range */
2994 buf = end + 1;
2995 end_chunk = simple_strtoul(buf, &end, 0);
2996 if (buf == end) break;
2997 }
2998 if (*end && !isspace(*end)) break;
2999 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3000 buf = end;
3001 while (isspace(*buf)) buf++;
3002 }
3003 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3004out:
3005 return len;
3006}
3007
3008static struct md_sysfs_entry md_bitmap =
3009__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3010
3011static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003012size_show(mddev_t *mddev, char *page)
3013{
3014 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
3015}
3016
Andre Nolld71f9f82008-07-11 22:02:22 +10003017static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003018
3019static ssize_t
3020size_store(mddev_t *mddev, const char *buf, size_t len)
3021{
3022 /* If array is inactive, we can reduce the component size, but
3023 * not increase it (except from 0).
3024 * If array is active, we can try an on-line resize
3025 */
3026 char *e;
3027 int err = 0;
3028 unsigned long long size = simple_strtoull(buf, &e, 10);
3029 if (!*buf || *buf == '\n' ||
3030 (*e && *e != '\n'))
3031 return -EINVAL;
3032
3033 if (mddev->pers) {
Andre Nolld71f9f82008-07-11 22:02:22 +10003034 err = update_size(mddev, size * 2);
NeilBrown850b2b422006-10-03 01:15:46 -07003035 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003036 } else {
3037 if (mddev->size == 0 ||
3038 mddev->size > size)
3039 mddev->size = size;
3040 else
3041 err = -ENOSPC;
3042 }
3043 return err ? err : len;
3044}
3045
3046static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003047__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003048
NeilBrown8bb93aa2006-01-06 00:20:50 -08003049
3050/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003051 * This is one of
3052 * 'none' for arrays with no metadata (good luck...)
3053 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003054 * or N.M for internally known formats
3055 */
3056static ssize_t
3057metadata_show(mddev_t *mddev, char *page)
3058{
3059 if (mddev->persistent)
3060 return sprintf(page, "%d.%d\n",
3061 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003062 else if (mddev->external)
3063 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003064 else
3065 return sprintf(page, "none\n");
3066}
3067
3068static ssize_t
3069metadata_store(mddev_t *mddev, const char *buf, size_t len)
3070{
3071 int major, minor;
3072 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003073 /* Changing the details of 'external' metadata is
3074 * always permitted. Otherwise there must be
3075 * no devices attached to the array.
3076 */
3077 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3078 ;
3079 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003080 return -EBUSY;
3081
3082 if (cmd_match(buf, "none")) {
3083 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003084 mddev->external = 0;
3085 mddev->major_version = 0;
3086 mddev->minor_version = 90;
3087 return len;
3088 }
3089 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003090 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003091 if (namelen >= sizeof(mddev->metadata_type))
3092 namelen = sizeof(mddev->metadata_type)-1;
3093 strncpy(mddev->metadata_type, buf+9, namelen);
3094 mddev->metadata_type[namelen] = 0;
3095 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3096 mddev->metadata_type[--namelen] = 0;
3097 mddev->persistent = 0;
3098 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003099 mddev->major_version = 0;
3100 mddev->minor_version = 90;
3101 return len;
3102 }
3103 major = simple_strtoul(buf, &e, 10);
3104 if (e==buf || *e != '.')
3105 return -EINVAL;
3106 buf = e+1;
3107 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003108 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003109 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003110 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003111 return -ENOENT;
3112 mddev->major_version = major;
3113 mddev->minor_version = minor;
3114 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003115 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003116 return len;
3117}
3118
3119static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003120__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003121
NeilBrowna35b0d62006-01-06 00:20:49 -08003122static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003123action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003124{
NeilBrown7eec3142005-11-08 21:39:44 -08003125 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08003126 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003127 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003128 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3129 type = "reshape";
3130 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003131 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3132 type = "resync";
3133 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3134 type = "check";
3135 else
3136 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003137 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003138 type = "recover";
3139 }
3140 return sprintf(page, "%s\n", type);
3141}
3142
3143static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003144action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003145{
NeilBrown7eec3142005-11-08 21:39:44 -08003146 if (!mddev->pers || !mddev->pers->sync_request)
3147 return -EINVAL;
3148
NeilBrownbce74da2006-01-06 00:20:41 -08003149 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003150 if (mddev->sync_thread) {
3151 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3152 md_unregister_thread(mddev->sync_thread);
3153 mddev->sync_thread = NULL;
3154 mddev->recovery = 0;
3155 }
NeilBrown03c902e2006-01-06 00:20:46 -08003156 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3157 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003158 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003159 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003160 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003161 else if (cmd_match(page, "recover")) {
3162 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3163 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3164 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003165 int err;
3166 if (mddev->pers->start_reshape == NULL)
3167 return -EINVAL;
3168 err = mddev->pers->start_reshape(mddev);
3169 if (err)
3170 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003171 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003172 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003173 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003174 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003175 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003176 return -EINVAL;
3177 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3178 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003179 }
NeilBrown03c902e2006-01-06 00:20:46 -08003180 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003181 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003182 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003183 return len;
3184}
3185
NeilBrown9d888832005-11-08 21:39:26 -08003186static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003187mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003188{
3189 return sprintf(page, "%llu\n",
3190 (unsigned long long) mddev->resync_mismatches);
3191}
3192
NeilBrown80ca3a42006-07-10 04:44:18 -07003193static struct md_sysfs_entry md_scan_mode =
3194__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003195
NeilBrown96de1e62005-11-08 21:39:39 -08003196
NeilBrown80ca3a42006-07-10 04:44:18 -07003197static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003198
NeilBrown88202a02006-01-06 00:21:36 -08003199static ssize_t
3200sync_min_show(mddev_t *mddev, char *page)
3201{
3202 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3203 mddev->sync_speed_min ? "local": "system");
3204}
3205
3206static ssize_t
3207sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3208{
3209 int min;
3210 char *e;
3211 if (strncmp(buf, "system", 6)==0) {
3212 mddev->sync_speed_min = 0;
3213 return len;
3214 }
3215 min = simple_strtoul(buf, &e, 10);
3216 if (buf == e || (*e && *e != '\n') || min <= 0)
3217 return -EINVAL;
3218 mddev->sync_speed_min = min;
3219 return len;
3220}
3221
3222static struct md_sysfs_entry md_sync_min =
3223__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3224
3225static ssize_t
3226sync_max_show(mddev_t *mddev, char *page)
3227{
3228 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3229 mddev->sync_speed_max ? "local": "system");
3230}
3231
3232static ssize_t
3233sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3234{
3235 int max;
3236 char *e;
3237 if (strncmp(buf, "system", 6)==0) {
3238 mddev->sync_speed_max = 0;
3239 return len;
3240 }
3241 max = simple_strtoul(buf, &e, 10);
3242 if (buf == e || (*e && *e != '\n') || max <= 0)
3243 return -EINVAL;
3244 mddev->sync_speed_max = max;
3245 return len;
3246}
3247
3248static struct md_sysfs_entry md_sync_max =
3249__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3250
Iustin Popd7f3d292007-10-16 23:30:54 -07003251static ssize_t
3252degraded_show(mddev_t *mddev, char *page)
3253{
3254 return sprintf(page, "%d\n", mddev->degraded);
3255}
3256static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003257
3258static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003259sync_force_parallel_show(mddev_t *mddev, char *page)
3260{
3261 return sprintf(page, "%d\n", mddev->parallel_resync);
3262}
3263
3264static ssize_t
3265sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3266{
3267 long n;
3268
3269 if (strict_strtol(buf, 10, &n))
3270 return -EINVAL;
3271
3272 if (n != 0 && n != 1)
3273 return -EINVAL;
3274
3275 mddev->parallel_resync = n;
3276
3277 if (mddev->sync_thread)
3278 wake_up(&resync_wait);
3279
3280 return len;
3281}
3282
3283/* force parallel resync, even with shared block devices */
3284static struct md_sysfs_entry md_sync_force_parallel =
3285__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3286 sync_force_parallel_show, sync_force_parallel_store);
3287
3288static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003289sync_speed_show(mddev_t *mddev, char *page)
3290{
3291 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003292 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3293 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003294 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003295 db = resync - mddev->resync_mark_cnt;
3296 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003297}
3298
NeilBrown80ca3a42006-07-10 04:44:18 -07003299static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003300
3301static ssize_t
3302sync_completed_show(mddev_t *mddev, char *page)
3303{
3304 unsigned long max_blocks, resync;
3305
3306 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3307 max_blocks = mddev->resync_max_sectors;
3308 else
3309 max_blocks = mddev->size << 1;
3310
3311 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3312 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3313}
3314
NeilBrown80ca3a42006-07-10 04:44:18 -07003315static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003316
NeilBrowne464eaf2006-03-27 01:18:14 -08003317static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003318min_sync_show(mddev_t *mddev, char *page)
3319{
3320 return sprintf(page, "%llu\n",
3321 (unsigned long long)mddev->resync_min);
3322}
3323static ssize_t
3324min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3325{
3326 unsigned long long min;
3327 if (strict_strtoull(buf, 10, &min))
3328 return -EINVAL;
3329 if (min > mddev->resync_max)
3330 return -EINVAL;
3331 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3332 return -EBUSY;
3333
3334 /* Must be a multiple of chunk_size */
3335 if (mddev->chunk_size) {
3336 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3337 return -EINVAL;
3338 }
3339 mddev->resync_min = min;
3340
3341 return len;
3342}
3343
3344static struct md_sysfs_entry md_min_sync =
3345__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3346
3347static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003348max_sync_show(mddev_t *mddev, char *page)
3349{
3350 if (mddev->resync_max == MaxSector)
3351 return sprintf(page, "max\n");
3352 else
3353 return sprintf(page, "%llu\n",
3354 (unsigned long long)mddev->resync_max);
3355}
3356static ssize_t
3357max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3358{
3359 if (strncmp(buf, "max", 3) == 0)
3360 mddev->resync_max = MaxSector;
3361 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003362 unsigned long long max;
3363 if (strict_strtoull(buf, 10, &max))
3364 return -EINVAL;
3365 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003366 return -EINVAL;
3367 if (max < mddev->resync_max &&
3368 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3369 return -EBUSY;
3370
3371 /* Must be a multiple of chunk_size */
3372 if (mddev->chunk_size) {
3373 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3374 return -EINVAL;
3375 }
3376 mddev->resync_max = max;
3377 }
3378 wake_up(&mddev->recovery_wait);
3379 return len;
3380}
3381
3382static struct md_sysfs_entry md_max_sync =
3383__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3384
3385static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003386suspend_lo_show(mddev_t *mddev, char *page)
3387{
3388 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3389}
3390
3391static ssize_t
3392suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3393{
3394 char *e;
3395 unsigned long long new = simple_strtoull(buf, &e, 10);
3396
3397 if (mddev->pers->quiesce == NULL)
3398 return -EINVAL;
3399 if (buf == e || (*e && *e != '\n'))
3400 return -EINVAL;
3401 if (new >= mddev->suspend_hi ||
3402 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3403 mddev->suspend_lo = new;
3404 mddev->pers->quiesce(mddev, 2);
3405 return len;
3406 } else
3407 return -EINVAL;
3408}
3409static struct md_sysfs_entry md_suspend_lo =
3410__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3411
3412
3413static ssize_t
3414suspend_hi_show(mddev_t *mddev, char *page)
3415{
3416 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3417}
3418
3419static ssize_t
3420suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3421{
3422 char *e;
3423 unsigned long long new = simple_strtoull(buf, &e, 10);
3424
3425 if (mddev->pers->quiesce == NULL)
3426 return -EINVAL;
3427 if (buf == e || (*e && *e != '\n'))
3428 return -EINVAL;
3429 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3430 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3431 mddev->suspend_hi = new;
3432 mddev->pers->quiesce(mddev, 1);
3433 mddev->pers->quiesce(mddev, 0);
3434 return len;
3435 } else
3436 return -EINVAL;
3437}
3438static struct md_sysfs_entry md_suspend_hi =
3439__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3440
NeilBrown08a02ec2007-05-09 02:35:38 -07003441static ssize_t
3442reshape_position_show(mddev_t *mddev, char *page)
3443{
3444 if (mddev->reshape_position != MaxSector)
3445 return sprintf(page, "%llu\n",
3446 (unsigned long long)mddev->reshape_position);
3447 strcpy(page, "none\n");
3448 return 5;
3449}
3450
3451static ssize_t
3452reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3453{
3454 char *e;
3455 unsigned long long new = simple_strtoull(buf, &e, 10);
3456 if (mddev->pers)
3457 return -EBUSY;
3458 if (buf == e || (*e && *e != '\n'))
3459 return -EINVAL;
3460 mddev->reshape_position = new;
3461 mddev->delta_disks = 0;
3462 mddev->new_level = mddev->level;
3463 mddev->new_layout = mddev->layout;
3464 mddev->new_chunk = mddev->chunk_size;
3465 return len;
3466}
3467
3468static struct md_sysfs_entry md_reshape_position =
3469__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3470 reshape_position_store);
3471
NeilBrowne464eaf2006-03-27 01:18:14 -08003472
NeilBrowneae17012005-11-08 21:39:23 -08003473static struct attribute *md_default_attrs[] = {
3474 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003475 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003476 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003477 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003478 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003479 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003480 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003481 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003482 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003483 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003484 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003485 NULL,
3486};
3487
3488static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003489 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003490 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003491 &md_sync_min.attr,
3492 &md_sync_max.attr,
3493 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003494 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003495 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003496 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003497 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003498 &md_suspend_lo.attr,
3499 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003500 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003501 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003502 NULL,
3503};
NeilBrown411036f2005-11-08 21:39:40 -08003504static struct attribute_group md_redundancy_group = {
3505 .name = NULL,
3506 .attrs = md_redundancy_attrs,
3507};
3508
NeilBrowneae17012005-11-08 21:39:23 -08003509
3510static ssize_t
3511md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3512{
3513 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3514 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003515 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003516
3517 if (!entry->show)
3518 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003519 rv = mddev_lock(mddev);
3520 if (!rv) {
3521 rv = entry->show(mddev, page);
3522 mddev_unlock(mddev);
3523 }
NeilBrown96de1e62005-11-08 21:39:39 -08003524 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003525}
3526
3527static ssize_t
3528md_attr_store(struct kobject *kobj, struct attribute *attr,
3529 const char *page, size_t length)
3530{
3531 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3532 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003533 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003534
3535 if (!entry->store)
3536 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003537 if (!capable(CAP_SYS_ADMIN))
3538 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003539 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003540 if (mddev->hold_active == UNTIL_IOCTL)
3541 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003542 if (!rv) {
3543 rv = entry->store(mddev, page, length);
3544 mddev_unlock(mddev);
3545 }
NeilBrown96de1e62005-11-08 21:39:39 -08003546 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003547}
3548
3549static void md_free(struct kobject *ko)
3550{
3551 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003552
3553 if (mddev->sysfs_state)
3554 sysfs_put(mddev->sysfs_state);
3555
3556 if (mddev->gendisk) {
3557 del_gendisk(mddev->gendisk);
3558 put_disk(mddev->gendisk);
3559 }
3560 if (mddev->queue)
3561 blk_cleanup_queue(mddev->queue);
3562
NeilBrowneae17012005-11-08 21:39:23 -08003563 kfree(mddev);
3564}
3565
3566static struct sysfs_ops md_sysfs_ops = {
3567 .show = md_attr_show,
3568 .store = md_attr_store,
3569};
3570static struct kobj_type md_ktype = {
3571 .release = md_free,
3572 .sysfs_ops = &md_sysfs_ops,
3573 .default_attrs = md_default_attrs,
3574};
3575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576int mdp_major = 0;
3577
NeilBrownefeb53c2009-01-09 08:31:10 +11003578static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003580 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 mddev_t *mddev = mddev_find(dev);
3582 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003583 int partitioned;
3584 int shift;
3585 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003586 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003589 return -ENODEV;
3590
3591 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3592 shift = partitioned ? MdpMinorShift : 0;
3593 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
NeilBrownd3374822009-01-09 08:31:10 +11003595 /* wait for any previous instance if this device
3596 * to be completed removed (mddev_delayed_delete).
3597 */
3598 flush_scheduled_work();
3599
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003600 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003602 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003604 return -EEXIST;
3605 }
3606
3607 if (name) {
3608 /* Need to ensure that 'name' is not a duplicate.
3609 */
3610 mddev_t *mddev2;
3611 spin_lock(&all_mddevs_lock);
3612
3613 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3614 if (mddev2->gendisk &&
3615 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3616 spin_unlock(&all_mddevs_lock);
3617 return -EEXIST;
3618 }
3619 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
NeilBrown8b765392009-01-09 08:31:08 +11003621
3622 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3623 if (!mddev->queue) {
3624 mutex_unlock(&disks_mutex);
3625 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003626 return -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003627 }
3628 /* Can be unlocked because the queue is new: no concurrency */
3629 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3630
3631 blk_queue_make_request(mddev->queue, md_fail_request);
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 disk = alloc_disk(1 << shift);
3634 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003635 mutex_unlock(&disks_mutex);
NeilBrown8b765392009-01-09 08:31:08 +11003636 blk_cleanup_queue(mddev->queue);
3637 mddev->queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003639 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003641 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003643 if (name)
3644 strcpy(disk->disk_name, name);
3645 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003647 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 disk->fops = &md_fops;
3650 disk->private_data = mddev;
3651 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003652 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003653 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003654 * remove it now.
3655 */
3656 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 add_disk(disk);
3658 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003659 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3660 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003661 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003662 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003663 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3664 disk->disk_name);
NeilBrownb62b7592008-10-21 13:25:21 +11003665 else {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003666 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003667 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3668 }
NeilBrownd3374822009-01-09 08:31:10 +11003669 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003670 return 0;
3671}
3672
3673static struct kobject *md_probe(dev_t dev, int *part, void *data)
3674{
3675 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return NULL;
3677}
3678
NeilBrownefeb53c2009-01-09 08:31:10 +11003679static int add_named_array(const char *val, struct kernel_param *kp)
3680{
3681 /* val must be "md_*" where * is not all digits.
3682 * We allocate an array with a large free minor number, and
3683 * set the name to val. val must not already be an active name.
3684 */
3685 int len = strlen(val);
3686 char buf[DISK_NAME_LEN];
3687
3688 while (len && val[len-1] == '\n')
3689 len--;
3690 if (len >= DISK_NAME_LEN)
3691 return -E2BIG;
3692 strlcpy(buf, val, len+1);
3693 if (strncmp(buf, "md_", 3) != 0)
3694 return -EINVAL;
3695 return md_alloc(0, buf);
3696}
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698static void md_safemode_timeout(unsigned long data)
3699{
3700 mddev_t *mddev = (mddev_t *) data;
3701
Neil Brown0fd62b82008-06-28 08:31:36 +10003702 if (!atomic_read(&mddev->writes_pending)) {
3703 mddev->safemode = 1;
3704 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11003705 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 md_wakeup_thread(mddev->thread);
3708}
3709
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003710static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712static int do_md_run(mddev_t * mddev)
3713{
NeilBrown2604b702006-01-06 00:20:36 -08003714 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 int chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 mdk_rdev_t *rdev;
3717 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003718 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 char b[BDEVNAME_SIZE];
3720
NeilBrowna757e642005-04-16 15:26:42 -07003721 if (list_empty(&mddev->disks))
3722 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
3725 if (mddev->pers)
3726 return -EBUSY;
3727
3728 /*
3729 * Analyze all RAID superblock(s)
3730 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003731 if (!mddev->raid_disks) {
3732 if (!mddev->persistent)
3733 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003734 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
3737 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
NeilBrown2604b702006-01-06 00:20:36 -08003739 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 if (chunk_size > MAX_CHUNK_SIZE) {
3741 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3742 chunk_size, MAX_CHUNK_SIZE);
3743 return -EINVAL;
3744 }
3745 /*
NeilBrown4bbf3772008-10-13 11:55:12 +11003746 * chunk-size has to be a power of 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 */
3748 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003749 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 return -EINVAL;
3751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
3753 /* devices must have minimum size of one chunk */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003754 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003755 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 continue;
3757 if (rdev->size < chunk_size / 1024) {
3758 printk(KERN_WARNING
3759 "md: Dev %s smaller than chunk_size:"
3760 " %lluk < %dk\n",
3761 bdevname(rdev->bdev,b),
3762 (unsigned long long)rdev->size,
3763 chunk_size / 1024);
3764 return -EINVAL;
3765 }
3766 }
3767 }
3768
NeilBrownd9d166c2006-01-06 00:20:51 -08003769 if (mddev->level != LEVEL_NONE)
3770 request_module("md-level-%d", mddev->level);
3771 else if (mddev->clevel[0])
3772 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
3774 /*
3775 * Drop all container device buffers, from now on
3776 * the only valid external interface is through the md
3777 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003779 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003780 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 continue;
3782 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003783 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003784
3785 /* perform some consistency tests on the device.
3786 * We don't want the data to overlap the metadata,
3787 * Internal Bitmap issues has handled elsewhere.
3788 */
Andre Noll0f420352008-07-11 22:02:23 +10003789 if (rdev->data_offset < rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003790 if (mddev->size &&
3791 rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +10003792 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003793 printk("md: %s: data overlaps metadata\n",
3794 mdname(mddev));
3795 return -EINVAL;
3796 }
3797 } else {
Andre Noll0f420352008-07-11 22:02:23 +10003798 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07003799 > rdev->data_offset) {
3800 printk("md: %s: metadata overlaps data\n",
3801 mdname(mddev));
3802 return -EINVAL;
3803 }
3804 }
NeilBrown3c0ee632008-10-21 13:25:28 +11003805 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 }
3807
3808 md_probe(mddev->unit, NULL, NULL);
3809 disk = mddev->gendisk;
3810 if (!disk)
3811 return -ENOMEM;
3812
3813 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003814 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003815 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003817 if (mddev->level != LEVEL_NONE)
3818 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3819 mddev->level);
3820 else
3821 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3822 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return -EINVAL;
3824 }
NeilBrown2604b702006-01-06 00:20:36 -08003825 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003827 mddev->level = pers->level;
3828 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11003830 if (pers->level >= 4 && pers->level <= 6)
3831 /* Cannot support integrity (yet) */
3832 blk_integrity_unregister(mddev->gendisk);
3833
NeilBrownf6705572006-03-27 01:18:11 -08003834 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003835 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003836 /* This personality cannot handle reshaping... */
3837 mddev->pers = NULL;
3838 module_put(pers->owner);
3839 return -EINVAL;
3840 }
3841
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003842 if (pers->sync_request) {
3843 /* Warn if this is a potentially silly
3844 * configuration.
3845 */
3846 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3847 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003848 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003849
3850 list_for_each_entry(rdev, &mddev->disks, same_set)
3851 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003852 if (rdev < rdev2 &&
3853 rdev->bdev->bd_contains ==
3854 rdev2->bdev->bd_contains) {
3855 printk(KERN_WARNING
3856 "%s: WARNING: %s appears to be"
3857 " on the same physical disk as"
3858 " %s.\n",
3859 mdname(mddev),
3860 bdevname(rdev->bdev,b),
3861 bdevname(rdev2->bdev,b2));
3862 warned = 1;
3863 }
3864 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003865
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003866 if (warned)
3867 printk(KERN_WARNING
3868 "True protection against single-disk"
3869 " failure might be compromised.\n");
3870 }
3871
NeilBrown657390d2005-08-26 18:34:16 -07003872 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003874 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003875 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
NeilBrownf91de922005-11-08 21:39:36 -08003877 if (start_readonly)
3878 mddev->ro = 2; /* read-only, but switch on first write */
3879
NeilBrownb15c2e52006-01-06 00:20:16 -08003880 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003881 if (err)
3882 printk(KERN_ERR "md: pers->run() failed ...\n");
3883 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003884 err = bitmap_create(mddev);
3885 if (err) {
3886 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3887 mdname(mddev), err);
3888 mddev->pers->stop(mddev);
3889 }
3890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 module_put(mddev->pers->owner);
3893 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003894 bitmap_destroy(mddev);
3895 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003897 if (mddev->pers->sync_request) {
3898 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3899 printk(KERN_WARNING
3900 "md: cannot register extra attributes for %s\n",
3901 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11003902 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08003903 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003904 mddev->ro = 0;
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 atomic_set(&mddev->writes_pending,0);
3907 mddev->safemode = 0;
3908 mddev->safemode_timer.function = md_safemode_timeout;
3909 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003910 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003912
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003913 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003914 if (rdev->raid_disk >= 0) {
3915 char nm[20];
3916 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003917 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3918 printk("md: cannot register %s for %s\n",
3919 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
3922 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3923
NeilBrown850b2b422006-10-03 01:15:46 -07003924 if (mddev->flags)
3925 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Andre Nollf233ea52008-07-21 17:05:22 +10003927 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
3929 /* If we call blk_queue_make_request here, it will
3930 * re-initialise max_sectors etc which may have been
3931 * refined inside -> run. So just set the bits we need to set.
3932 * Most initialisation happended when we called
3933 * blk_queue_make_request(..., md_fail_request)
3934 * earlier.
3935 */
3936 mddev->queue->queuedata = mddev;
3937 mddev->queue->make_request_fn = mddev->pers->make_request;
3938
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003939 /* If there is a partially-recovered drive we need to
3940 * start recovery here. If we leave it to md_check_recovery,
3941 * it will remove the drives and not do the right thing
3942 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003943 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003944 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003945 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003946 if (rdev->raid_disk >= 0 &&
3947 !test_bit(In_sync, &rdev->flags) &&
3948 !test_bit(Faulty, &rdev->flags))
3949 /* complete an interrupted recovery */
3950 spares++;
3951 if (spares && mddev->pers->sync_request) {
3952 mddev->recovery = 0;
3953 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3954 mddev->sync_thread = md_register_thread(md_do_sync,
3955 mddev,
3956 "%s_resync");
3957 if (!mddev->sync_thread) {
3958 printk(KERN_ERR "%s: could not start resync"
3959 " thread...\n",
3960 mdname(mddev));
3961 /* leave the spares where they are, it shouldn't hurt */
3962 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003963 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003964 }
3965 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003966 md_wakeup_thread(mddev->thread);
3967 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003968
Linus Torvalds44ce62942007-05-09 18:51:36 -07003969 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003970 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11003971 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11003972 if (mddev->sysfs_action)
3973 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10003974 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09003975 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return 0;
3977}
3978
3979static int restart_array(mddev_t *mddev)
3980{
3981 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Andre Noll80fab1d2008-07-11 22:02:21 +10003983 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003985 return -ENXIO;
3986 if (!mddev->pers)
3987 return -EINVAL;
3988 if (!mddev->ro)
3989 return -EBUSY;
3990 mddev->safemode = 0;
3991 mddev->ro = 0;
3992 set_disk_ro(disk, 0);
3993 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3994 mdname(mddev));
3995 /* Kick recovery or resync if necessary */
3996 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3997 md_wakeup_thread(mddev->thread);
3998 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11003999 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
NeilBrownacc55e22006-06-26 00:27:47 -07004003/* similar to deny_write_access, but accounts for our holding a reference
4004 * to the file ourselves */
4005static int deny_bitmap_write_access(struct file * file)
4006{
4007 struct inode *inode = file->f_mapping->host;
4008
4009 spin_lock(&inode->i_lock);
4010 if (atomic_read(&inode->i_writecount) > 1) {
4011 spin_unlock(&inode->i_lock);
4012 return -ETXTBSY;
4013 }
4014 atomic_set(&inode->i_writecount, -1);
4015 spin_unlock(&inode->i_lock);
4016
4017 return 0;
4018}
4019
4020static void restore_bitmap_write_access(struct file *file)
4021{
4022 struct inode *inode = file->f_mapping->host;
4023
4024 spin_lock(&inode->i_lock);
4025 atomic_set(&inode->i_writecount, 1);
4026 spin_unlock(&inode->i_lock);
4027}
4028
NeilBrown9e653b62006-06-26 00:27:58 -07004029/* mode:
4030 * 0 - completely stop and dis-assemble array
4031 * 1 - switch to readonly
4032 * 2 - stop but do not disassemble array
4033 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004034static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
4036 int err = 0;
4037 struct gendisk *disk = mddev->gendisk;
4038
NeilBrownf2ea68c2008-07-21 17:05:25 +10004039 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004040 printk("md: %s still in use.\n",mdname(mddev));
4041 return -EBUSY;
4042 }
4043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004047 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4049 md_unregister_thread(mddev->sync_thread);
4050 mddev->sync_thread = NULL;
4051 }
4052
4053 del_timer_sync(&mddev->safemode_timer);
4054
NeilBrown9e653b62006-06-26 00:27:58 -07004055 switch(mode) {
4056 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004058 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 goto out;
4060 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004061 break;
4062 case 0: /* disassemble */
4063 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004064 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004065 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 if (mddev->ro)
4067 set_disk_ro(disk, 0);
4068 blk_queue_make_request(mddev->queue, md_fail_request);
4069 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004070 mddev->queue->merge_bvec_fn = NULL;
4071 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004072 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown0c3573f2009-01-09 08:31:05 +11004073 if (mddev->pers->sync_request) {
NeilBrown411036f2005-11-08 21:39:40 -08004074 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
NeilBrown0c3573f2009-01-09 08:31:05 +11004075 if (mddev->sysfs_action)
4076 sysfs_put(mddev->sysfs_action);
4077 mddev->sysfs_action = NULL;
4078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 module_put(mddev->pers->owner);
4080 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004081 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004082 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004083
4084 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004085 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (mddev->ro)
4088 mddev->ro = 0;
4089 }
NeilBrown850b2b422006-10-03 01:15:46 -07004090 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 /* mark array as shutdown cleanly */
4092 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07004093 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 }
NeilBrown9e653b62006-06-26 00:27:58 -07004095 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004097 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
NeilBrown32a76272005-06-21 17:17:14 -07004099
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 /*
4101 * Free resources if final stop
4102 */
NeilBrown9e653b62006-06-26 00:27:58 -07004103 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004104 mdk_rdev_t *rdev;
NeilBrown0d4ca602006-12-10 02:20:44 -08004105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4107
NeilBrown978f9462006-02-02 14:28:05 -08004108 bitmap_destroy(mddev);
4109 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004110 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004111 fput(mddev->bitmap_file);
4112 mddev->bitmap_file = NULL;
4113 }
4114 mddev->bitmap_offset = 0;
4115
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004116 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004117 if (rdev->raid_disk >= 0) {
4118 char nm[20];
4119 sprintf(nm, "rd%d", rdev->raid_disk);
4120 sysfs_remove_link(&mddev->kobj, nm);
4121 }
4122
NeilBrown177a99b2008-02-06 01:39:56 -08004123 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004124 flush_scheduled_work();
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 export_array(mddev);
4127
Andre Nollf233ea52008-07-21 17:05:22 +10004128 mddev->array_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004129 mddev->size = 0;
4130 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004131 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004132 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004133 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004134 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004135 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004136 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004137 mddev->level = LEVEL_NONE;
4138 mddev->clevel[0] = 0;
4139 mddev->flags = 0;
4140 mddev->ro = 0;
4141 mddev->metadata_type[0] = 0;
4142 mddev->chunk_size = 0;
4143 mddev->ctime = mddev->utime = 0;
4144 mddev->layout = 0;
4145 mddev->max_disks = 0;
4146 mddev->events = 0;
4147 mddev->delta_disks = 0;
4148 mddev->new_level = LEVEL_NONE;
4149 mddev->new_layout = 0;
4150 mddev->new_chunk = 0;
4151 mddev->curr_resync = 0;
4152 mddev->resync_mismatches = 0;
4153 mddev->suspend_lo = mddev->suspend_hi = 0;
4154 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4155 mddev->recovery = 0;
4156 mddev->in_sync = 0;
4157 mddev->changed = 0;
4158 mddev->degraded = 0;
4159 mddev->barriers_work = 0;
4160 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004161 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004162 if (mddev->hold_active == UNTIL_STOP)
4163 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004164
NeilBrowna8a55c32006-06-26 00:27:39 -07004165 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4167 mdname(mddev));
4168 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004169 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004170 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004171 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172out:
4173 return err;
4174}
4175
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004176#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177static void autorun_array(mddev_t *mddev)
4178{
4179 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 int err;
4181
NeilBrowna757e642005-04-16 15:26:42 -07004182 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
4185 printk(KERN_INFO "md: running: ");
4186
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004187 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 char b[BDEVNAME_SIZE];
4189 printk("<%s>", bdevname(rdev->bdev,b));
4190 }
4191 printk("\n");
4192
NeilBrownd710e132008-10-13 11:55:12 +11004193 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 if (err) {
4195 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004196 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
4198}
4199
4200/*
4201 * lets try to run arrays based on all disks that have arrived
4202 * until now. (those are in pending_raid_disks)
4203 *
4204 * the method: pick the first pending disk, collect all disks with
4205 * the same UUID, remove all from the pending list and put them into
4206 * the 'same_array' list. Then order this list based on superblock
4207 * update time (freshest comes first), kick out 'old' disks and
4208 * compare superblocks. If everything's fine then run it.
4209 *
4210 * If "unit" is allocated, then bump its reference count
4211 */
4212static void autorun_devices(int part)
4213{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004214 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 mddev_t *mddev;
4216 char b[BDEVNAME_SIZE];
4217
4218 printk(KERN_INFO "md: autorun ...\n");
4219 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004220 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004222 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 rdev0 = list_entry(pending_raid_disks.next,
4224 mdk_rdev_t, same_set);
4225
4226 printk(KERN_INFO "md: considering %s ...\n",
4227 bdevname(rdev0->bdev,b));
4228 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004229 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (super_90_load(rdev, rdev0, 0) >= 0) {
4231 printk(KERN_INFO "md: adding %s ...\n",
4232 bdevname(rdev->bdev,b));
4233 list_move(&rdev->same_set, &candidates);
4234 }
4235 /*
4236 * now we have a set of devices, with all of them having
4237 * mostly sane superblocks. It's time to allocate the
4238 * mddev.
4239 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004240 if (part) {
4241 dev = MKDEV(mdp_major,
4242 rdev0->preferred_minor << MdpMinorShift);
4243 unit = MINOR(dev) >> MdpMinorShift;
4244 } else {
4245 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4246 unit = MINOR(dev);
4247 }
4248 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4250 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4251 break;
4252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
4254 md_probe(dev, NULL, NULL);
4255 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004256 if (!mddev || !mddev->gendisk) {
4257 if (mddev)
4258 mddev_put(mddev);
4259 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 "md: cannot allocate memory for md drive.\n");
4261 break;
4262 }
4263 if (mddev_lock(mddev))
4264 printk(KERN_WARNING "md: %s locked, cannot run\n",
4265 mdname(mddev));
4266 else if (mddev->raid_disks || mddev->major_version
4267 || !list_empty(&mddev->disks)) {
4268 printk(KERN_WARNING
4269 "md: %s already running, cannot run %s\n",
4270 mdname(mddev), bdevname(rdev0->bdev,b));
4271 mddev_unlock(mddev);
4272 } else {
4273 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004274 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004275 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 list_del_init(&rdev->same_set);
4277 if (bind_rdev_to_array(rdev, mddev))
4278 export_rdev(rdev);
4279 }
4280 autorun_array(mddev);
4281 mddev_unlock(mddev);
4282 }
4283 /* on success, candidates will be empty, on error
4284 * it won't...
4285 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004286 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004287 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 mddev_put(mddev);
4291 }
4292 printk(KERN_INFO "md: ... autorun DONE.\n");
4293}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004294#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296static int get_version(void __user * arg)
4297{
4298 mdu_version_t ver;
4299
4300 ver.major = MD_MAJOR_VERSION;
4301 ver.minor = MD_MINOR_VERSION;
4302 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4303
4304 if (copy_to_user(arg, &ver, sizeof(ver)))
4305 return -EFAULT;
4306
4307 return 0;
4308}
4309
4310static int get_array_info(mddev_t * mddev, void __user * arg)
4311{
4312 mdu_array_info_t info;
4313 int nr,working,active,failed,spare;
4314 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
4316 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004317 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004319 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 failed++;
4321 else {
4322 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004323 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 active++;
4325 else
4326 spare++;
4327 }
4328 }
4329
4330 info.major_version = mddev->major_version;
4331 info.minor_version = mddev->minor_version;
4332 info.patch_version = MD_PATCHLEVEL_VERSION;
4333 info.ctime = mddev->ctime;
4334 info.level = mddev->level;
4335 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004336 if (info.size != mddev->size) /* overflow */
4337 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 info.nr_disks = nr;
4339 info.raid_disks = mddev->raid_disks;
4340 info.md_minor = mddev->md_minor;
4341 info.not_persistent= !mddev->persistent;
4342
4343 info.utime = mddev->utime;
4344 info.state = 0;
4345 if (mddev->in_sync)
4346 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004347 if (mddev->bitmap && mddev->bitmap_offset)
4348 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 info.active_disks = active;
4350 info.working_disks = working;
4351 info.failed_disks = failed;
4352 info.spare_disks = spare;
4353
4354 info.layout = mddev->layout;
4355 info.chunk_size = mddev->chunk_size;
4356
4357 if (copy_to_user(arg, &info, sizeof(info)))
4358 return -EFAULT;
4359
4360 return 0;
4361}
4362
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004363static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004364{
4365 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4366 char *ptr, *buf = NULL;
4367 int err = -ENOMEM;
4368
Dan Williamsb5470dc2008-06-27 21:44:04 -07004369 if (md_allow_write(mddev))
4370 file = kmalloc(sizeof(*file), GFP_NOIO);
4371 else
4372 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004373
NeilBrown32a76272005-06-21 17:17:14 -07004374 if (!file)
4375 goto out;
4376
4377 /* bitmap disabled, zero the first byte and copy out */
4378 if (!mddev->bitmap || !mddev->bitmap->file) {
4379 file->pathname[0] = '\0';
4380 goto copy_out;
4381 }
4382
4383 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4384 if (!buf)
4385 goto out;
4386
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004387 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4388 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004389 goto out;
4390
4391 strcpy(file->pathname, ptr);
4392
4393copy_out:
4394 err = 0;
4395 if (copy_to_user(arg, file, sizeof(*file)))
4396 err = -EFAULT;
4397out:
4398 kfree(buf);
4399 kfree(file);
4400 return err;
4401}
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403static int get_disk_info(mddev_t * mddev, void __user * arg)
4404{
4405 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 mdk_rdev_t *rdev;
4407
4408 if (copy_from_user(&info, arg, sizeof(info)))
4409 return -EFAULT;
4410
Andre Noll26ef3792008-07-11 22:02:21 +10004411 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 if (rdev) {
4413 info.major = MAJOR(rdev->bdev->bd_dev);
4414 info.minor = MINOR(rdev->bdev->bd_dev);
4415 info.raid_disk = rdev->raid_disk;
4416 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004417 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004419 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 info.state |= (1<<MD_DISK_ACTIVE);
4421 info.state |= (1<<MD_DISK_SYNC);
4422 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004423 if (test_bit(WriteMostly, &rdev->flags))
4424 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 } else {
4426 info.major = info.minor = 0;
4427 info.raid_disk = -1;
4428 info.state = (1<<MD_DISK_REMOVED);
4429 }
4430
4431 if (copy_to_user(arg, &info, sizeof(info)))
4432 return -EFAULT;
4433
4434 return 0;
4435}
4436
4437static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4438{
4439 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4440 mdk_rdev_t *rdev;
4441 dev_t dev = MKDEV(info->major,info->minor);
4442
4443 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4444 return -EOVERFLOW;
4445
4446 if (!mddev->raid_disks) {
4447 int err;
4448 /* expecting a device which has a superblock */
4449 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4450 if (IS_ERR(rdev)) {
4451 printk(KERN_WARNING
4452 "md: md_import_device returned %ld\n",
4453 PTR_ERR(rdev));
4454 return PTR_ERR(rdev);
4455 }
4456 if (!list_empty(&mddev->disks)) {
4457 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4458 mdk_rdev_t, same_set);
4459 int err = super_types[mddev->major_version]
4460 .load_super(rdev, rdev0, mddev->minor_version);
4461 if (err < 0) {
4462 printk(KERN_WARNING
4463 "md: %s has different UUID to %s\n",
4464 bdevname(rdev->bdev,b),
4465 bdevname(rdev0->bdev,b2));
4466 export_rdev(rdev);
4467 return -EINVAL;
4468 }
4469 }
4470 err = bind_rdev_to_array(rdev, mddev);
4471 if (err)
4472 export_rdev(rdev);
4473 return err;
4474 }
4475
4476 /*
4477 * add_new_disk can be used once the array is assembled
4478 * to add "hot spares". They must already have a superblock
4479 * written
4480 */
4481 if (mddev->pers) {
4482 int err;
4483 if (!mddev->pers->hot_add_disk) {
4484 printk(KERN_WARNING
4485 "%s: personality does not support diskops!\n",
4486 mdname(mddev));
4487 return -EINVAL;
4488 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004489 if (mddev->persistent)
4490 rdev = md_import_device(dev, mddev->major_version,
4491 mddev->minor_version);
4492 else
4493 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 if (IS_ERR(rdev)) {
4495 printk(KERN_WARNING
4496 "md: md_import_device returned %ld\n",
4497 PTR_ERR(rdev));
4498 return PTR_ERR(rdev);
4499 }
NeilBrown41158c72005-06-21 17:17:25 -07004500 /* set save_raid_disk if appropriate */
4501 if (!mddev->persistent) {
4502 if (info->state & (1<<MD_DISK_SYNC) &&
4503 info->raid_disk < mddev->raid_disks)
4504 rdev->raid_disk = info->raid_disk;
4505 else
4506 rdev->raid_disk = -1;
4507 } else
4508 super_types[mddev->major_version].
4509 validate_super(mddev, rdev);
4510 rdev->saved_raid_disk = rdev->raid_disk;
4511
NeilBrownb2d444d2005-11-08 21:39:31 -08004512 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004513 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4514 set_bit(WriteMostly, &rdev->flags);
4515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 rdev->raid_disk = -1;
4517 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004518 if (!err && !mddev->pers->hot_remove_disk) {
4519 /* If there is hot_add_disk but no hot_remove_disk
4520 * then added disks for geometry changes,
4521 * and should be added immediately.
4522 */
4523 super_types[mddev->major_version].
4524 validate_super(mddev, rdev);
4525 err = mddev->pers->hot_add_disk(mddev, rdev);
4526 if (err)
4527 unbind_rdev_from_array(rdev);
4528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 if (err)
4530 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004531 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004532 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004533
NeilBrown17571282006-12-10 02:20:52 -08004534 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004535 if (mddev->degraded)
4536 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004537 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004538 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return err;
4540 }
4541
4542 /* otherwise, add_new_disk is only allowed
4543 * for major_version==0 superblocks
4544 */
4545 if (mddev->major_version != 0) {
4546 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4547 mdname(mddev));
4548 return -EINVAL;
4549 }
4550
4551 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4552 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004553 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 if (IS_ERR(rdev)) {
4555 printk(KERN_WARNING
4556 "md: error, md_import_device() returned %ld\n",
4557 PTR_ERR(rdev));
4558 return PTR_ERR(rdev);
4559 }
4560 rdev->desc_nr = info->number;
4561 if (info->raid_disk < mddev->raid_disks)
4562 rdev->raid_disk = info->raid_disk;
4563 else
4564 rdev->raid_disk = -1;
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004567 if (info->state & (1<<MD_DISK_SYNC))
4568 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004570 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4571 set_bit(WriteMostly, &rdev->flags);
4572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (!mddev->persistent) {
4574 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004575 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 } else
Andre Noll0f420352008-07-11 22:02:23 +10004577 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolle7debaa2008-07-11 22:02:23 +10004578 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
NeilBrown2bf071b2006-01-06 00:20:55 -08004580 err = bind_rdev_to_array(rdev, mddev);
4581 if (err) {
4582 export_rdev(rdev);
4583 return err;
4584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 }
4586
4587 return 0;
4588}
4589
4590static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4591{
4592 char b[BDEVNAME_SIZE];
4593 mdk_rdev_t *rdev;
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 rdev = find_rdev(mddev, dev);
4596 if (!rdev)
4597 return -ENXIO;
4598
4599 if (rdev->raid_disk >= 0)
4600 goto busy;
4601
4602 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07004603 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004604 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 return 0;
4607busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004608 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 bdevname(rdev->bdev,b), mdname(mddev));
4610 return -EBUSY;
4611}
4612
4613static int hot_add_disk(mddev_t * mddev, dev_t dev)
4614{
4615 char b[BDEVNAME_SIZE];
4616 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 mdk_rdev_t *rdev;
4618
4619 if (!mddev->pers)
4620 return -ENODEV;
4621
4622 if (mddev->major_version != 0) {
4623 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4624 " version-0 superblocks.\n",
4625 mdname(mddev));
4626 return -EINVAL;
4627 }
4628 if (!mddev->pers->hot_add_disk) {
4629 printk(KERN_WARNING
4630 "%s: personality does not support diskops!\n",
4631 mdname(mddev));
4632 return -EINVAL;
4633 }
4634
NeilBrownd710e132008-10-13 11:55:12 +11004635 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (IS_ERR(rdev)) {
4637 printk(KERN_WARNING
4638 "md: error, md_import_device() returned %ld\n",
4639 PTR_ERR(rdev));
4640 return -EINVAL;
4641 }
4642
4643 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004644 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 else
Andre Noll0f420352008-07-11 22:02:23 +10004646 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Andre Nolle7debaa2008-07-11 22:02:23 +10004648 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
NeilBrownb2d444d2005-11-08 21:39:31 -08004650 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 printk(KERN_WARNING
4652 "md: can not hot-add faulty %s disk to %s!\n",
4653 bdevname(rdev->bdev,b), mdname(mddev));
4654 err = -EINVAL;
4655 goto abort_export;
4656 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004657 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004659 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004660 err = bind_rdev_to_array(rdev, mddev);
4661 if (err)
4662 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
4664 /*
4665 * The rest should better be atomic, we can have disk failures
4666 * noticed in interrupt contexts ...
4667 */
4668
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 rdev->raid_disk = -1;
4670
NeilBrown850b2b422006-10-03 01:15:46 -07004671 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 /*
4674 * Kick recovery, maybe this spare has to be added to the
4675 * array immediately.
4676 */
4677 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4678 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004679 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return 0;
4681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682abort_export:
4683 export_rdev(rdev);
4684 return err;
4685}
4686
NeilBrown32a76272005-06-21 17:17:14 -07004687static int set_bitmap_file(mddev_t *mddev, int fd)
4688{
4689 int err;
4690
NeilBrown36fa3062005-09-09 16:23:45 -07004691 if (mddev->pers) {
4692 if (!mddev->pers->quiesce)
4693 return -EBUSY;
4694 if (mddev->recovery || mddev->sync_thread)
4695 return -EBUSY;
4696 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004697 }
4698
NeilBrown36fa3062005-09-09 16:23:45 -07004699
4700 if (fd >= 0) {
4701 if (mddev->bitmap)
4702 return -EEXIST; /* cannot add when bitmap is present */
4703 mddev->bitmap_file = fget(fd);
4704
4705 if (mddev->bitmap_file == NULL) {
4706 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4707 mdname(mddev));
4708 return -EBADF;
4709 }
4710
4711 err = deny_bitmap_write_access(mddev->bitmap_file);
4712 if (err) {
4713 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4714 mdname(mddev));
4715 fput(mddev->bitmap_file);
4716 mddev->bitmap_file = NULL;
4717 return err;
4718 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004719 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004720 } else if (mddev->bitmap == NULL)
4721 return -ENOENT; /* cannot remove what isn't there */
4722 err = 0;
4723 if (mddev->pers) {
4724 mddev->pers->quiesce(mddev, 1);
4725 if (fd >= 0)
4726 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004727 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004728 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004729 fd = -1; /* make sure to put the file */
4730 }
NeilBrown36fa3062005-09-09 16:23:45 -07004731 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004732 }
4733 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004734 if (mddev->bitmap_file) {
4735 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004736 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004737 }
NeilBrown36fa3062005-09-09 16:23:45 -07004738 mddev->bitmap_file = NULL;
4739 }
4740
NeilBrown32a76272005-06-21 17:17:14 -07004741 return err;
4742}
4743
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744/*
4745 * set_array_info is used two different ways
4746 * The original usage is when creating a new array.
4747 * In this usage, raid_disks is > 0 and it together with
4748 * level, size, not_persistent,layout,chunksize determine the
4749 * shape of the array.
4750 * This will always create an array with a type-0.90.0 superblock.
4751 * The newer usage is when assembling an array.
4752 * In this case raid_disks will be 0, and the major_version field is
4753 * use to determine which style super-blocks are to be found on the devices.
4754 * The minor and patch _version numbers are also kept incase the
4755 * super_block handler wishes to interpret them.
4756 */
4757static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4758{
4759
4760 if (info->raid_disks == 0) {
4761 /* just setting version number for superblock loading */
4762 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004763 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 super_types[info->major_version].name == NULL) {
4765 /* maybe try to auto-load a module? */
4766 printk(KERN_INFO
4767 "md: superblock version %d not known\n",
4768 info->major_version);
4769 return -EINVAL;
4770 }
4771 mddev->major_version = info->major_version;
4772 mddev->minor_version = info->minor_version;
4773 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004774 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return 0;
4776 }
4777 mddev->major_version = MD_MAJOR_VERSION;
4778 mddev->minor_version = MD_MINOR_VERSION;
4779 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4780 mddev->ctime = get_seconds();
4781
4782 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004783 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 mddev->size = info->size;
4785 mddev->raid_disks = info->raid_disks;
4786 /* don't set md_minor, it is determined by which /dev/md* was
4787 * openned
4788 */
4789 if (info->state & (1<<MD_SB_CLEAN))
4790 mddev->recovery_cp = MaxSector;
4791 else
4792 mddev->recovery_cp = 0;
4793 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004794 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 mddev->layout = info->layout;
4797 mddev->chunk_size = info->chunk_size;
4798
4799 mddev->max_disks = MD_SB_DISKS;
4800
NeilBrowne6910632008-02-06 01:39:51 -08004801 if (mddev->persistent)
4802 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07004803 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
NeilBrownb2a27032005-11-28 13:44:12 -08004805 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4806 mddev->bitmap_offset = 0;
4807
NeilBrownf6705572006-03-27 01:18:11 -08004808 mddev->reshape_position = MaxSector;
4809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 /*
4811 * Generate a 128 bit UUID
4812 */
4813 get_random_bytes(mddev->uuid, 16);
4814
NeilBrownf6705572006-03-27 01:18:11 -08004815 mddev->new_level = mddev->level;
4816 mddev->new_chunk = mddev->chunk_size;
4817 mddev->new_layout = mddev->layout;
4818 mddev->delta_disks = 0;
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return 0;
4821}
4822
Andre Nolld71f9f82008-07-11 22:02:22 +10004823static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004824{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004825 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08004826 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10004827 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004828
4829 if (mddev->pers->resize == NULL)
4830 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10004831 /* The "num_sectors" is the number of sectors of each device that
4832 * is used. This can only make sense for arrays with redundancy.
4833 * linear and raid0 always use whatever space is available. We can only
4834 * consider changing this number if no resync or reconstruction is
4835 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10004836 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10004837 * of each device. If num_sectors is zero, we find the largest size
4838 * that fits.
4839
NeilBrowna35b0d62006-01-06 00:20:49 -08004840 */
4841 if (mddev->sync_thread)
4842 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10004843 if (mddev->bitmap)
4844 /* Sorry, cannot grow a bitmap yet, just remove it,
4845 * grow, and re-add.
4846 */
4847 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004848 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004849 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004850 avail = rdev->size * 2;
4851
Andre Nolld71f9f82008-07-11 22:02:22 +10004852 if (fit && (num_sectors == 0 || num_sectors > avail))
4853 num_sectors = avail;
4854 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004855 return -ENOSPC;
4856 }
Andre Nolld71f9f82008-07-11 22:02:22 +10004857 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004858 if (!rv) {
4859 struct block_device *bdev;
4860
4861 bdev = bdget_disk(mddev->gendisk, 0);
4862 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004863 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004864 i_size_write(bdev->bd_inode,
4865 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004866 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004867 bdput(bdev);
4868 }
4869 }
4870 return rv;
4871}
4872
NeilBrownda943b992006-01-06 00:20:54 -08004873static int update_raid_disks(mddev_t *mddev, int raid_disks)
4874{
4875 int rv;
4876 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004877 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004878 return -EINVAL;
4879 if (raid_disks <= 0 ||
4880 raid_disks >= mddev->max_disks)
4881 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004882 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004883 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004884 mddev->delta_disks = raid_disks - mddev->raid_disks;
4885
4886 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004887 return rv;
4888}
4889
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891/*
4892 * update_array_info is used to change the configuration of an
4893 * on-line array.
4894 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4895 * fields in the info are checked against the array.
4896 * Any differences that cannot be handled will cause an error.
4897 * Normally, only one change can be managed at a time.
4898 */
4899static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4900{
4901 int rv = 0;
4902 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004903 int state = 0;
4904
4905 /* calculate expected state,ignoring low bits */
4906 if (mddev->bitmap && mddev->bitmap_offset)
4907 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909 if (mddev->major_version != info->major_version ||
4910 mddev->minor_version != info->minor_version ||
4911/* mddev->patch_version != info->patch_version || */
4912 mddev->ctime != info->ctime ||
4913 mddev->level != info->level ||
4914/* mddev->layout != info->layout || */
4915 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004916 mddev->chunk_size != info->chunk_size ||
4917 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4918 ((state^info->state) & 0xfffffe00)
4919 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 return -EINVAL;
4921 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004922 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (mddev->raid_disks != info->raid_disks) cnt++;
4924 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004925 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 if (cnt == 0) return 0;
4927 if (cnt > 1) return -EINVAL;
4928
4929 if (mddev->layout != info->layout) {
4930 /* Change layout
4931 * we don't need to do anything at the md level, the
4932 * personality will take care of it all.
4933 */
4934 if (mddev->pers->reconfig == NULL)
4935 return -EINVAL;
4936 else
4937 return mddev->pers->reconfig(mddev, info->layout, -1);
4938 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004939 if (info->size >= 0 && mddev->size != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10004940 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
NeilBrownda943b992006-01-06 00:20:54 -08004942 if (mddev->raid_disks != info->raid_disks)
4943 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
NeilBrown36fa3062005-09-09 16:23:45 -07004945 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4946 if (mddev->pers->quiesce == NULL)
4947 return -EINVAL;
4948 if (mddev->recovery || mddev->sync_thread)
4949 return -EBUSY;
4950 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4951 /* add the bitmap */
4952 if (mddev->bitmap)
4953 return -EEXIST;
4954 if (mddev->default_bitmap_offset == 0)
4955 return -EINVAL;
4956 mddev->bitmap_offset = mddev->default_bitmap_offset;
4957 mddev->pers->quiesce(mddev, 1);
4958 rv = bitmap_create(mddev);
4959 if (rv)
4960 bitmap_destroy(mddev);
4961 mddev->pers->quiesce(mddev, 0);
4962 } else {
4963 /* remove the bitmap */
4964 if (!mddev->bitmap)
4965 return -ENOENT;
4966 if (mddev->bitmap->file)
4967 return -EINVAL;
4968 mddev->pers->quiesce(mddev, 1);
4969 bitmap_destroy(mddev);
4970 mddev->pers->quiesce(mddev, 0);
4971 mddev->bitmap_offset = 0;
4972 }
4973 }
NeilBrown850b2b422006-10-03 01:15:46 -07004974 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 return rv;
4976}
4977
4978static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4979{
4980 mdk_rdev_t *rdev;
4981
4982 if (mddev->pers == NULL)
4983 return -ENODEV;
4984
4985 rdev = find_rdev(mddev, dev);
4986 if (!rdev)
4987 return -ENODEV;
4988
4989 md_error(mddev, rdev);
4990 return 0;
4991}
4992
Andre Noll2f9618c2008-04-25 18:57:58 +02004993/*
4994 * We have a problem here : there is no easy way to give a CHS
4995 * virtual geometry. We currently pretend that we have a 2 heads
4996 * 4 sectors (with a BIG number of cylinders...). This drives
4997 * dosfs just mad... ;-)
4998 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004999static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5000{
5001 mddev_t *mddev = bdev->bd_disk->private_data;
5002
5003 geo->heads = 2;
5004 geo->sectors = 4;
5005 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5006 return 0;
5007}
5008
Al Viroa39907f2008-03-02 10:31:15 -05005009static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 unsigned int cmd, unsigned long arg)
5011{
5012 int err = 0;
5013 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 mddev_t *mddev = NULL;
5015
5016 if (!capable(CAP_SYS_ADMIN))
5017 return -EACCES;
5018
5019 /*
5020 * Commands dealing with the RAID driver but not any
5021 * particular array:
5022 */
5023 switch (cmd)
5024 {
5025 case RAID_VERSION:
5026 err = get_version(argp);
5027 goto done;
5028
5029 case PRINT_RAID_DEBUG:
5030 err = 0;
5031 md_print_devices();
5032 goto done;
5033
5034#ifndef MODULE
5035 case RAID_AUTORUN:
5036 err = 0;
5037 autostart_arrays(arg);
5038 goto done;
5039#endif
5040 default:;
5041 }
5042
5043 /*
5044 * Commands creating/starting a new array:
5045 */
5046
Al Viroa39907f2008-03-02 10:31:15 -05005047 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
5049 if (!mddev) {
5050 BUG();
5051 goto abort;
5052 }
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 err = mddev_lock(mddev);
5055 if (err) {
5056 printk(KERN_INFO
5057 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5058 err, cmd);
5059 goto abort;
5060 }
5061
5062 switch (cmd)
5063 {
5064 case SET_ARRAY_INFO:
5065 {
5066 mdu_array_info_t info;
5067 if (!arg)
5068 memset(&info, 0, sizeof(info));
5069 else if (copy_from_user(&info, argp, sizeof(info))) {
5070 err = -EFAULT;
5071 goto abort_unlock;
5072 }
5073 if (mddev->pers) {
5074 err = update_array_info(mddev, &info);
5075 if (err) {
5076 printk(KERN_WARNING "md: couldn't update"
5077 " array info. %d\n", err);
5078 goto abort_unlock;
5079 }
5080 goto done_unlock;
5081 }
5082 if (!list_empty(&mddev->disks)) {
5083 printk(KERN_WARNING
5084 "md: array %s already has disks!\n",
5085 mdname(mddev));
5086 err = -EBUSY;
5087 goto abort_unlock;
5088 }
5089 if (mddev->raid_disks) {
5090 printk(KERN_WARNING
5091 "md: array %s already initialised!\n",
5092 mdname(mddev));
5093 err = -EBUSY;
5094 goto abort_unlock;
5095 }
5096 err = set_array_info(mddev, &info);
5097 if (err) {
5098 printk(KERN_WARNING "md: couldn't set"
5099 " array info. %d\n", err);
5100 goto abort_unlock;
5101 }
5102 }
5103 goto done_unlock;
5104
5105 default:;
5106 }
5107
5108 /*
5109 * Commands querying/configuring an existing array:
5110 */
NeilBrown32a76272005-06-21 17:17:14 -07005111 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005112 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005113 if ((!mddev->raid_disks && !mddev->external)
5114 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5115 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5116 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 err = -ENODEV;
5118 goto abort_unlock;
5119 }
5120
5121 /*
5122 * Commands even a read-only array can execute:
5123 */
5124 switch (cmd)
5125 {
5126 case GET_ARRAY_INFO:
5127 err = get_array_info(mddev, argp);
5128 goto done_unlock;
5129
NeilBrown32a76272005-06-21 17:17:14 -07005130 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005131 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005132 goto done_unlock;
5133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 case GET_DISK_INFO:
5135 err = get_disk_info(mddev, argp);
5136 goto done_unlock;
5137
5138 case RESTART_ARRAY_RW:
5139 err = restart_array(mddev);
5140 goto done_unlock;
5141
5142 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005143 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 goto done_unlock;
5145
5146 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005147 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 goto done_unlock;
5149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 }
5151
5152 /*
5153 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005154 * superblock, so we do not allow them on read-only arrays.
5155 * However non-MD ioctls (e.g. get-size) will still come through
5156 * here and hit the 'default' below, so only disallow
5157 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005159 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005160 if (mddev->ro == 2) {
5161 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005162 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005163 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5164 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005165 } else {
5166 err = -EROFS;
5167 goto abort_unlock;
5168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 }
5170
5171 switch (cmd)
5172 {
5173 case ADD_NEW_DISK:
5174 {
5175 mdu_disk_info_t info;
5176 if (copy_from_user(&info, argp, sizeof(info)))
5177 err = -EFAULT;
5178 else
5179 err = add_new_disk(mddev, &info);
5180 goto done_unlock;
5181 }
5182
5183 case HOT_REMOVE_DISK:
5184 err = hot_remove_disk(mddev, new_decode_dev(arg));
5185 goto done_unlock;
5186
5187 case HOT_ADD_DISK:
5188 err = hot_add_disk(mddev, new_decode_dev(arg));
5189 goto done_unlock;
5190
5191 case SET_DISK_FAULTY:
5192 err = set_disk_faulty(mddev, new_decode_dev(arg));
5193 goto done_unlock;
5194
5195 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005196 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 goto done_unlock;
5198
NeilBrown32a76272005-06-21 17:17:14 -07005199 case SET_BITMAP_FILE:
5200 err = set_bitmap_file(mddev, (int)arg);
5201 goto done_unlock;
5202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 err = -EINVAL;
5205 goto abort_unlock;
5206 }
5207
5208done_unlock:
5209abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005210 if (mddev->hold_active == UNTIL_IOCTL &&
5211 err != -EINVAL)
5212 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 mddev_unlock(mddev);
5214
5215 return err;
5216done:
5217 if (err)
5218 MD_BUG();
5219abort:
5220 return err;
5221}
5222
Al Viroa39907f2008-03-02 10:31:15 -05005223static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224{
5225 /*
5226 * Succeed if we can lock the mddev, which confirms that
5227 * it isn't being stopped right now.
5228 */
NeilBrownd3374822009-01-09 08:31:10 +11005229 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 int err;
5231
NeilBrownd3374822009-01-09 08:31:10 +11005232 if (mddev->gendisk != bdev->bd_disk) {
5233 /* we are racing with mddev_put which is discarding this
5234 * bd_disk.
5235 */
5236 mddev_put(mddev);
5237 /* Wait until bdev->bd_disk is definitely gone */
5238 flush_scheduled_work();
5239 /* Then retry the open from the top */
5240 return -ERESTARTSYS;
5241 }
5242 BUG_ON(mddev != bdev->bd_disk->private_data);
5243
NeilBrownd63a5a72006-12-08 02:36:17 -08005244 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 goto out;
5246
5247 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005248 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 mddev_unlock(mddev);
5250
Al Viroa39907f2008-03-02 10:31:15 -05005251 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 out:
5253 return err;
5254}
5255
Al Viroa39907f2008-03-02 10:31:15 -05005256static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Al Viroa39907f2008-03-02 10:31:15 -05005258 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005260 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005261 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 mddev_put(mddev);
5263
5264 return 0;
5265}
5266
Linus Torvalds44ce62942007-05-09 18:51:36 -07005267static int md_media_changed(struct gendisk *disk)
5268{
5269 mddev_t *mddev = disk->private_data;
5270
5271 return mddev->changed;
5272}
5273
5274static int md_revalidate(struct gendisk *disk)
5275{
5276 mddev_t *mddev = disk->private_data;
5277
5278 mddev->changed = 0;
5279 return 0;
5280}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281static struct block_device_operations md_fops =
5282{
5283 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005284 .open = md_open,
5285 .release = md_release,
5286 .locked_ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005287 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005288 .media_changed = md_media_changed,
5289 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290};
5291
Adrian Bunk75c96f82005-05-05 16:16:09 -07005292static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293{
5294 mdk_thread_t *thread = arg;
5295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 /*
5297 * md_thread is a 'system-thread', it's priority should be very
5298 * high. We avoid resource deadlocks individually in each
5299 * raid personality. (RAID5 does preallocation) We also use RR and
5300 * the very same RT priority as kswapd, thus we will never get
5301 * into a priority inversion deadlock.
5302 *
5303 * we definitely have to have equal or higher priority than
5304 * bdflush, otherwise bdflush will deadlock if there are too
5305 * many dirty RAID5 blocks.
5306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
NeilBrown6985c432005-10-19 21:23:47 -07005308 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005309 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
NeilBrown93588e22005-11-15 00:09:12 -08005311 /* We need to wait INTERRUPTIBLE so that
5312 * we don't add to the load-average.
5313 * That means we need to be sure no signals are
5314 * pending
5315 */
5316 if (signal_pending(current))
5317 flush_signals(current);
5318
5319 wait_event_interruptible_timeout
5320 (thread->wqueue,
5321 test_bit(THREAD_WAKEUP, &thread->flags)
5322 || kthread_should_stop(),
5323 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
5325 clear_bit(THREAD_WAKEUP, &thread->flags);
5326
NeilBrown787453c2005-11-08 21:39:43 -08005327 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 return 0;
5331}
5332
5333void md_wakeup_thread(mdk_thread_t *thread)
5334{
5335 if (thread) {
5336 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5337 set_bit(THREAD_WAKEUP, &thread->flags);
5338 wake_up(&thread->wqueue);
5339 }
5340}
5341
5342mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5343 const char *name)
5344{
5345 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
NeilBrown9ffae0c2006-01-06 00:20:32 -08005347 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 if (!thread)
5349 return NULL;
5350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 init_waitqueue_head(&thread->wqueue);
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 thread->run = run;
5354 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005355 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005356 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005357 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 kfree(thread);
5359 return NULL;
5360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 return thread;
5362}
5363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364void md_unregister_thread(mdk_thread_t *thread)
5365{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005366 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005367
5368 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 kfree(thread);
5370}
5371
5372void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5373{
5374 if (!mddev) {
5375 MD_BUG();
5376 return;
5377 }
5378
NeilBrownb2d444d2005-11-08 21:39:31 -08005379 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005381
5382 if (mddev->external)
5383 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5386 mdname(mddev),
5387 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5388 __builtin_return_address(0),__builtin_return_address(1),
5389 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005390*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005391 if (!mddev->pers)
5392 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 if (!mddev->pers->error_handler)
5394 return;
5395 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005396 if (mddev->degraded)
5397 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005398 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5400 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5401 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005402 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
5405/* seq_file implementation /proc/mdstat */
5406
5407static void status_unused(struct seq_file *seq)
5408{
5409 int i = 0;
5410 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
5412 seq_printf(seq, "unused devices: ");
5413
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005414 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 char b[BDEVNAME_SIZE];
5416 i++;
5417 seq_printf(seq, "%s ",
5418 bdevname(rdev->bdev,b));
5419 }
5420 if (!i)
5421 seq_printf(seq, "<none>");
5422
5423 seq_printf(seq, "\n");
5424}
5425
5426
5427static void status_resync(struct seq_file *seq, mddev_t * mddev)
5428{
NeilBrown4588b422006-03-27 01:18:04 -08005429 sector_t max_blocks, resync, res;
5430 unsigned long dt, db, rt;
5431 int scale;
5432 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
5434 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5435
5436 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5437 max_blocks = mddev->resync_max_sectors >> 1;
5438 else
5439 max_blocks = mddev->size;
5440
5441 /*
5442 * Should not happen.
5443 */
5444 if (!max_blocks) {
5445 MD_BUG();
5446 return;
5447 }
NeilBrown4588b422006-03-27 01:18:04 -08005448 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5449 * in a sector_t, and (max_blocks>>scale) will fit in a
5450 * u32, as those are the requirements for sector_div.
5451 * Thus 'scale' must be at least 10
5452 */
5453 scale = 10;
5454 if (sizeof(sector_t) > sizeof(unsigned long)) {
5455 while ( max_blocks/2 > (1ULL<<(scale+32)))
5456 scale++;
5457 }
5458 res = (resync>>scale)*1000;
5459 sector_div(res, (u32)((max_blocks>>scale)+1));
5460
5461 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 {
NeilBrown4588b422006-03-27 01:18:04 -08005463 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 seq_printf(seq, "[");
5465 for (i = 0; i < x; i++)
5466 seq_printf(seq, "=");
5467 seq_printf(seq, ">");
5468 for (i = 0; i < y; i++)
5469 seq_printf(seq, ".");
5470 seq_printf(seq, "] ");
5471 }
NeilBrown4588b422006-03-27 01:18:04 -08005472 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005473 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5474 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005475 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5476 "check" :
5477 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5478 "resync" : "recovery"))),
5479 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005480 (unsigned long long) resync,
5481 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
5483 /*
5484 * We do not want to overflow, so the order of operands and
5485 * the * 100 / 100 trick are important. We do a +1 to be
5486 * safe against division by zero. We only estimate anyway.
5487 *
5488 * dt: time from mark until now
5489 * db: blocks written from mark until now
5490 * rt: remaining time
5491 */
5492 dt = ((jiffies - mddev->resync_mark) / HZ);
5493 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005494 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5495 - mddev->resync_mark_cnt;
5496 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
5498 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5499
NeilBrownff4e8d92006-07-10 04:44:16 -07005500 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501}
5502
5503static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5504{
5505 struct list_head *tmp;
5506 loff_t l = *pos;
5507 mddev_t *mddev;
5508
5509 if (l >= 0x10000)
5510 return NULL;
5511 if (!l--)
5512 /* header */
5513 return (void*)1;
5514
5515 spin_lock(&all_mddevs_lock);
5516 list_for_each(tmp,&all_mddevs)
5517 if (!l--) {
5518 mddev = list_entry(tmp, mddev_t, all_mddevs);
5519 mddev_get(mddev);
5520 spin_unlock(&all_mddevs_lock);
5521 return mddev;
5522 }
5523 spin_unlock(&all_mddevs_lock);
5524 if (!l--)
5525 return (void*)2;/* tail */
5526 return NULL;
5527}
5528
5529static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5530{
5531 struct list_head *tmp;
5532 mddev_t *next_mddev, *mddev = v;
5533
5534 ++*pos;
5535 if (v == (void*)2)
5536 return NULL;
5537
5538 spin_lock(&all_mddevs_lock);
5539 if (v == (void*)1)
5540 tmp = all_mddevs.next;
5541 else
5542 tmp = mddev->all_mddevs.next;
5543 if (tmp != &all_mddevs)
5544 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5545 else {
5546 next_mddev = (void*)2;
5547 *pos = 0x10000;
5548 }
5549 spin_unlock(&all_mddevs_lock);
5550
5551 if (v != (void*)1)
5552 mddev_put(mddev);
5553 return next_mddev;
5554
5555}
5556
5557static void md_seq_stop(struct seq_file *seq, void *v)
5558{
5559 mddev_t *mddev = v;
5560
5561 if (mddev && v != (void*)1 && v != (void*)2)
5562 mddev_put(mddev);
5563}
5564
NeilBrownd7603b72006-01-06 00:20:30 -08005565struct mdstat_info {
5566 int event;
5567};
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569static int md_seq_show(struct seq_file *seq, void *v)
5570{
5571 mddev_t *mddev = v;
5572 sector_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005574 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005575 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
5577 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005578 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 seq_printf(seq, "Personalities : ");
5580 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005581 list_for_each_entry(pers, &pers_list, list)
5582 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
5584 spin_unlock(&pers_lock);
5585 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005586 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 return 0;
5588 }
5589 if (v == (void*)2) {
5590 status_unused(seq);
5591 return 0;
5592 }
5593
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005594 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5598 seq_printf(seq, "%s : %sactive", mdname(mddev),
5599 mddev->pers ? "" : "in");
5600 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005601 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005603 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005604 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 seq_printf(seq, " %s", mddev->pers->name);
5606 }
5607
5608 size = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005609 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 char b[BDEVNAME_SIZE];
5611 seq_printf(seq, " %s[%d]",
5612 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005613 if (test_bit(WriteMostly, &rdev->flags))
5614 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005615 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 seq_printf(seq, "(F)");
5617 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005618 } else if (rdev->raid_disk < 0)
5619 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 size += rdev->size;
5621 }
5622
5623 if (!list_empty(&mddev->disks)) {
5624 if (mddev->pers)
5625 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005626 (unsigned long long)
5627 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 else
5629 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005630 (unsigned long long)size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005632 if (mddev->persistent) {
5633 if (mddev->major_version != 0 ||
5634 mddev->minor_version != 90) {
5635 seq_printf(seq," super %d.%d",
5636 mddev->major_version,
5637 mddev->minor_version);
5638 }
NeilBrowne6910632008-02-06 01:39:51 -08005639 } else if (mddev->external)
5640 seq_printf(seq, " super external:%s",
5641 mddev->metadata_type);
5642 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005643 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
5645 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005646 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005648 if (mddev->pers->sync_request) {
5649 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005650 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005651 seq_printf(seq, "\n ");
5652 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5653 seq_printf(seq, "\tresync=DELAYED\n ");
5654 else if (mddev->recovery_cp < MaxSector)
5655 seq_printf(seq, "\tresync=PENDING\n ");
5656 }
NeilBrown32a76272005-06-21 17:17:14 -07005657 } else
5658 seq_printf(seq, "\n ");
5659
5660 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005661 unsigned long chunk_kb;
5662 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005663 spin_lock_irqsave(&bitmap->lock, flags);
5664 chunk_kb = bitmap->chunksize >> 10;
5665 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5666 "%lu%s chunk",
5667 bitmap->pages - bitmap->missing_pages,
5668 bitmap->pages,
5669 (bitmap->pages - bitmap->missing_pages)
5670 << (PAGE_SHIFT - 10),
5671 chunk_kb ? chunk_kb : bitmap->chunksize,
5672 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005673 if (bitmap->file) {
5674 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005675 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005676 }
NeilBrown78d742d2005-06-21 17:17:15 -07005677
NeilBrown32a76272005-06-21 17:17:14 -07005678 seq_printf(seq, "\n");
5679 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 }
5681
5682 seq_printf(seq, "\n");
5683 }
5684 mddev_unlock(mddev);
5685
5686 return 0;
5687}
5688
5689static struct seq_operations md_seq_ops = {
5690 .start = md_seq_start,
5691 .next = md_seq_next,
5692 .stop = md_seq_stop,
5693 .show = md_seq_show,
5694};
5695
5696static int md_seq_open(struct inode *inode, struct file *file)
5697{
5698 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005699 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5700 if (mi == NULL)
5701 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
5703 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005704 if (error)
5705 kfree(mi);
5706 else {
5707 struct seq_file *p = file->private_data;
5708 p->private = mi;
5709 mi->event = atomic_read(&md_event_count);
5710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 return error;
5712}
5713
NeilBrownd7603b72006-01-06 00:20:30 -08005714static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5715{
5716 struct seq_file *m = filp->private_data;
5717 struct mdstat_info *mi = m->private;
5718 int mask;
5719
5720 poll_wait(filp, &md_event_waiters, wait);
5721
5722 /* always allow read */
5723 mask = POLLIN | POLLRDNORM;
5724
5725 if (mi->event != atomic_read(&md_event_count))
5726 mask |= POLLERR | POLLPRI;
5727 return mask;
5728}
5729
Arjan van de Venfa027c22007-02-12 00:55:33 -08005730static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005731 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 .open = md_seq_open,
5733 .read = seq_read,
5734 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005735 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005736 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737};
5738
NeilBrown2604b702006-01-06 00:20:36 -08005739int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005742 list_add_tail(&p->list, &pers_list);
5743 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 spin_unlock(&pers_lock);
5745 return 0;
5746}
5747
NeilBrown2604b702006-01-06 00:20:36 -08005748int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749{
NeilBrown2604b702006-01-06 00:20:36 -08005750 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005752 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 spin_unlock(&pers_lock);
5754 return 0;
5755}
5756
NeilBrowneea1bf32009-03-31 14:27:02 +11005757static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758{
5759 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11005761 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10005764 rcu_read_lock();
5765 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11005767 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
5768 (int)part_stat_read(&disk->part0, sectors[1]) -
5769 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005770 /* sync IO will cause sync_io to increase before the disk_stats
5771 * as sync_io is counted when a request starts, and
5772 * disk_stats is counted when it completes.
5773 * So resync activity will cause curr_events to be smaller than
5774 * when there was no such activity.
5775 * non-sync IO will cause disk_stat to increase without
5776 * increasing sync_io so curr_events will (eventually)
5777 * be larger than it was before. Once it becomes
5778 * substantially larger, the test below will cause
5779 * the array to appear non-idle, and resync will slow
5780 * down.
5781 * If there is a lot of outstanding resync activity when
5782 * we set last_event to curr_events, then all that activity
5783 * completing might cause the array to appear non-idle
5784 * and resync will be slowed down even though there might
5785 * not have been non-resync activity. This will only
5786 * happen once though. 'last_events' will soon reflect
5787 * the state where there is little or no outstanding
5788 * resync requests, and further resync activity will
5789 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005790 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 */
NeilBrowneea1bf32009-03-31 14:27:02 +11005792 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 rdev->last_events = curr_events;
5794 idle = 0;
5795 }
5796 }
NeilBrown4b809912008-07-21 17:05:25 +10005797 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 return idle;
5799}
5800
5801void md_done_sync(mddev_t *mddev, int blocks, int ok)
5802{
5803 /* another "blocks" (512byte) blocks have been synced */
5804 atomic_sub(blocks, &mddev->recovery_active);
5805 wake_up(&mddev->recovery_wait);
5806 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005807 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 md_wakeup_thread(mddev->thread);
5809 // stop recovery, signal do_sync ....
5810 }
5811}
5812
5813
NeilBrown06d91a52005-06-21 17:17:12 -07005814/* md_write_start(mddev, bi)
5815 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005816 * in superblock) before writing, schedule a superblock update
5817 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005818 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005819void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820{
Neil Brown0fd62b82008-06-28 08:31:36 +10005821 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005822 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005823 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005824
NeilBrownf91de922005-11-08 21:39:36 -08005825 BUG_ON(mddev->ro == 1);
5826 if (mddev->ro == 2) {
5827 /* need to switch to read/write */
5828 mddev->ro = 0;
5829 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5830 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005831 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005832 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005833 }
NeilBrown06d91a52005-06-21 17:17:12 -07005834 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005835 if (mddev->safemode == 1)
5836 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005837 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005838 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005839 if (mddev->in_sync) {
5840 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005841 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005842 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005843 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005844 }
NeilBrowna9701a32005-11-08 21:39:34 -08005845 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005846 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005847 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11005848 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07005849 wait_event(mddev->sb_wait,
5850 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5851 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852}
5853
5854void md_write_end(mddev_t *mddev)
5855{
5856 if (atomic_dec_and_test(&mddev->writes_pending)) {
5857 if (mddev->safemode == 2)
5858 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005859 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5861 }
5862}
5863
NeilBrown2a2275d2007-01-26 00:57:11 -08005864/* md_allow_write(mddev)
5865 * Calling this ensures that the array is marked 'active' so that writes
5866 * may proceed without blocking. It is important to call this before
5867 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5868 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005869 *
5870 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5871 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005872 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005873int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005874{
5875 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005876 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005877 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005878 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005879 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005880 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005881
5882 spin_lock_irq(&mddev->write_lock);
5883 if (mddev->in_sync) {
5884 mddev->in_sync = 0;
5885 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5886 if (mddev->safemode_delay &&
5887 mddev->safemode == 0)
5888 mddev->safemode = 1;
5889 spin_unlock_irq(&mddev->write_lock);
5890 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11005891 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08005892 } else
5893 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005894
5895 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5896 return -EAGAIN;
5897 else
5898 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005899}
5900EXPORT_SYMBOL_GPL(md_allow_write);
5901
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902#define SYNC_MARKS 10
5903#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005904void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905{
5906 mddev_t *mddev2;
5907 unsigned int currspeed = 0,
5908 window;
NeilBrown57afd892005-06-21 17:17:13 -07005909 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 unsigned long mark[SYNC_MARKS];
5911 sector_t mark_cnt[SYNC_MARKS];
5912 int last_mark,m;
5913 struct list_head *tmp;
5914 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005915 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005916 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005917 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 /* just incase thread restarts... */
5920 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5921 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005922 if (mddev->ro) /* never try to sync a read-only array */
5923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
NeilBrown61df9d92006-10-03 01:15:57 -07005925 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5926 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5927 desc = "data-check";
5928 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5929 desc = "requested-resync";
5930 else
5931 desc = "resync";
5932 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5933 desc = "reshape";
5934 else
5935 desc = "recovery";
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 /* we overload curr_resync somewhat here.
5938 * 0 == not engaged in resync at all
5939 * 2 == checking that there is no conflict with another sync
5940 * 1 == like 2, but have yielded to allow conflicting resync to
5941 * commense
5942 * other == active in resync - this many blocks
5943 *
5944 * Before starting a resync we must have set curr_resync to
5945 * 2, and then checked that every "conflicting" array has curr_resync
5946 * less than ours. When we find one that is the same or higher
5947 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5948 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5949 * This will mean we have to start checking from the beginning again.
5950 *
5951 */
5952
5953 do {
5954 mddev->curr_resync = 2;
5955
5956 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005957 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005958 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 goto skip;
5960 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005961 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 if (mddev2 == mddev)
5963 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005964 if (!mddev->parallel_resync
5965 && mddev2->curr_resync
5966 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 DEFINE_WAIT(wq);
5968 if (mddev < mddev2 && mddev->curr_resync == 2) {
5969 /* arbitrarily yield */
5970 mddev->curr_resync = 1;
5971 wake_up(&resync_wait);
5972 }
5973 if (mddev > mddev2 && mddev->curr_resync == 1)
5974 /* no need to wait here, we can wait the next
5975 * time 'round when curr_resync == 2
5976 */
5977 continue;
NeilBrown97441972008-09-19 11:49:54 +10005978 /* We need to wait 'interruptible' so as not to
5979 * contribute to the load average, and not to
5980 * be caught by 'softlockup'
5981 */
5982 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08005983 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005984 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005985 printk(KERN_INFO "md: delaying %s of %s"
5986 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005988 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10005990 if (signal_pending(current))
5991 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 schedule();
5993 finish_wait(&resync_wait, &wq);
5994 goto try_again;
5995 }
5996 finish_wait(&resync_wait, &wq);
5997 }
5998 }
5999 } while (mddev->curr_resync < 2);
6000
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006001 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006002 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006004 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 */
6006 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006007 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006008 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006009 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6010 j = mddev->resync_min;
6011 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006012 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006013
NeilBrownccfcc3c2006-03-27 01:18:09 -08006014 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6015 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006016 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 /* recovery follows the physical size of devices */
6018 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006019 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006020 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006021 if (rdev->raid_disk >= 0 &&
6022 !test_bit(Faulty, &rdev->flags) &&
6023 !test_bit(In_sync, &rdev->flags) &&
6024 rdev->recovery_offset < j)
6025 j = rdev->recovery_offset;
6026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
NeilBrown61df9d92006-10-03 01:15:57 -07006028 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6029 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6030 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006031 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006032 "(but not more than %d KB/sec) for %s.\n",
6033 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
NeilBrowneea1bf32009-03-31 14:27:02 +11006035 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006036
NeilBrown57afd892005-06-21 17:17:13 -07006037 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 for (m = 0; m < SYNC_MARKS; m++) {
6039 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006040 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 }
6042 last_mark = 0;
6043 mddev->resync_mark = mark[last_mark];
6044 mddev->resync_mark_cnt = mark_cnt[last_mark];
6045
6046 /*
6047 * Tune reconstruction:
6048 */
6049 window = 32*(PAGE_SIZE/512);
6050 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6051 window/2,(unsigned long long) max_sectors/2);
6052
6053 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 last_check = 0;
6055
6056 if (j>2) {
6057 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006058 "md: resuming %s of %s from checkpoint.\n",
6059 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 mddev->curr_resync = j;
6061 }
6062
6063 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006064 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
NeilBrown57afd892005-06-21 17:17:13 -07006066 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006067 if (j >= mddev->resync_max) {
6068 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6069 wait_event(mddev->recovery_wait,
6070 mddev->resync_max > j
6071 || kthread_should_stop());
6072 }
6073 if (kthread_should_stop())
6074 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07006075 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006076 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006077 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006078 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 goto out;
6080 }
NeilBrown57afd892005-06-21 17:17:13 -07006081
6082 if (!skipped) { /* actual IO requested */
6083 io_sectors += sectors;
6084 atomic_add(sectors, &mddev->recovery_active);
6085 }
6086
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 j += sectors;
6088 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006089 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006090 if (last_check == 0)
6091 /* this is the earliers that rebuilt will be
6092 * visible in /proc/mdstat
6093 */
6094 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006095
6096 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 continue;
6098
NeilBrown57afd892005-06-21 17:17:13 -07006099 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
NeilBrowndfc70642008-05-23 13:04:39 -07006101 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 break;
6103
6104 repeat:
6105 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6106 /* step marks */
6107 int next = (last_mark+1) % SYNC_MARKS;
6108
6109 mddev->resync_mark = mark[next];
6110 mddev->resync_mark_cnt = mark_cnt[next];
6111 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006112 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 last_mark = next;
6114 }
6115
6116
NeilBrownc6207272008-02-06 01:39:52 -08006117 if (kthread_should_stop())
6118 goto interrupted;
6119
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
6121 /*
6122 * this loop exits only if either when we are slower than
6123 * the 'hard' speed limit, or the system was IO-idle for
6124 * a jiffy.
6125 * the system might be non-idle CPU-wise, but we only care
6126 * about not overloading the IO subsystem. (things like an
6127 * e2fsck being done on the RAID array should execute fast)
6128 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006129 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 cond_resched();
6131
NeilBrown57afd892005-06-21 17:17:13 -07006132 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6133 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
NeilBrown88202a02006-01-06 00:21:36 -08006135 if (currspeed > speed_min(mddev)) {
6136 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006137 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006138 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 goto repeat;
6140 }
6141 }
6142 }
NeilBrown61df9d92006-10-03 01:15:57 -07006143 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 /*
6145 * this also signals 'finished resyncing' to md_stop
6146 */
6147 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006148 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
6150 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6151
6152 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006153 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
NeilBrowndfc70642008-05-23 13:04:39 -07006155 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006156 mddev->curr_resync > 2) {
6157 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6158 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6159 if (mddev->curr_resync >= mddev->recovery_cp) {
6160 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006161 "md: checkpointing %s of %s.\n",
6162 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006163 mddev->recovery_cp = mddev->curr_resync;
6164 }
6165 } else
6166 mddev->recovery_cp = MaxSector;
6167 } else {
6168 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6169 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006170 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006171 if (rdev->raid_disk >= 0 &&
6172 !test_bit(Faulty, &rdev->flags) &&
6173 !test_bit(In_sync, &rdev->flags) &&
6174 rdev->recovery_offset < mddev->curr_resync)
6175 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 }
NeilBrown17571282006-12-10 02:20:52 -08006178 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 skip:
6181 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006182 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006183 mddev->resync_max = MaxSector;
6184 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 wake_up(&resync_wait);
6186 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6187 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006188 return;
6189
6190 interrupted:
6191 /*
6192 * got a signal, exit.
6193 */
6194 printk(KERN_INFO
6195 "md: md_do_sync() got signal ... exiting\n");
6196 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6197 goto out;
6198
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199}
NeilBrown29269552006-03-27 01:18:10 -08006200EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
6202
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006203static int remove_and_add_spares(mddev_t *mddev)
6204{
6205 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006206 int spares = 0;
6207
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006208 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006209 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006210 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006211 (test_bit(Faulty, &rdev->flags) ||
6212 ! test_bit(In_sync, &rdev->flags)) &&
6213 atomic_read(&rdev->nr_pending)==0) {
6214 if (mddev->pers->hot_remove_disk(
6215 mddev, rdev->raid_disk)==0) {
6216 char nm[20];
6217 sprintf(nm,"rd%d", rdev->raid_disk);
6218 sysfs_remove_link(&mddev->kobj, nm);
6219 rdev->raid_disk = -1;
6220 }
6221 }
6222
NeilBrown4044ba52009-01-09 08:31:11 +11006223 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006224 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006225 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006226 !test_bit(In_sync, &rdev->flags) &&
6227 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006228 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006229 if (rdev->raid_disk < 0
6230 && !test_bit(Faulty, &rdev->flags)) {
6231 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006232 if (mddev->pers->
6233 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006234 char nm[20];
6235 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006236 if (sysfs_create_link(&mddev->kobj,
6237 &rdev->kobj, nm))
6238 printk(KERN_WARNING
6239 "md: cannot register "
6240 "%s for %s\n",
6241 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006242 spares++;
6243 md_new_event(mddev);
6244 } else
6245 break;
6246 }
NeilBrowndfc70642008-05-23 13:04:39 -07006247 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006248 }
6249 return spares;
6250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251/*
6252 * This routine is regularly called by all per-raid-array threads to
6253 * deal with generic issues like resync and super-block update.
6254 * Raid personalities that don't have a thread (linear/raid0) do not
6255 * need this as they never do any recovery or update the superblock.
6256 *
6257 * It does not do any resync itself, but rather "forks" off other threads
6258 * to do that as needed.
6259 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6260 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006261 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 * and wakeups up this thread which will reap the thread and finish up.
6263 * This thread also removes any faulty devices (with nr_pending == 0).
6264 *
6265 * The overall approach is:
6266 * 1/ if the superblock needs updating, update it.
6267 * 2/ If a recovery thread is running, don't do anything else.
6268 * 3/ If recovery has finished, clean up, possibly marking spares active.
6269 * 4/ If there are any faulty devices, remove them.
6270 * 5/ If array is degraded, try to add spares devices
6271 * 6/ If array has spares or is not in-sync, start a resync thread.
6272 */
6273void md_check_recovery(mddev_t *mddev)
6274{
6275 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
6277
NeilBrown5f404022005-06-21 17:17:16 -07006278 if (mddev->bitmap)
6279 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280
6281 if (mddev->ro)
6282 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006283
6284 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006285 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006286 printk(KERN_INFO "md: %s in immediate safe mode\n",
6287 mdname(mddev));
6288 mddev->safemode = 2;
6289 }
6290 flush_signals(current);
6291 }
6292
NeilBrownc89a8ee2008-08-05 15:54:13 +10006293 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6294 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006296 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006298 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006299 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006300 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6301 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 ))
6303 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006304
NeilBrowndf5b89b2006-03-27 01:18:20 -08006305 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006306 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006307
NeilBrownc89a8ee2008-08-05 15:54:13 +10006308 if (mddev->ro) {
6309 /* Only thing we do on a ro array is remove
6310 * failed devices.
6311 */
6312 remove_and_add_spares(mddev);
6313 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6314 goto unlock;
6315 }
6316
NeilBrown31a59e32008-04-30 00:52:30 -07006317 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006318 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006319 spin_lock_irq(&mddev->write_lock);
6320 if (mddev->safemode &&
6321 !atomic_read(&mddev->writes_pending) &&
6322 !mddev->in_sync &&
6323 mddev->recovery_cp == MaxSector) {
6324 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006325 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006326 if (mddev->persistent)
6327 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6328 }
6329 if (mddev->safemode == 1)
6330 mddev->safemode = 0;
6331 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006332 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006333 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006334 }
NeilBrownfca4d842005-06-21 17:17:11 -07006335
NeilBrown850b2b422006-10-03 01:15:46 -07006336 if (mddev->flags)
6337 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006338
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006339 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006340 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006341 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006342
NeilBrown06d91a52005-06-21 17:17:12 -07006343
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6345 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6346 /* resync/recovery still happening */
6347 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6348 goto unlock;
6349 }
6350 if (mddev->sync_thread) {
6351 /* resync has finished, collect result */
6352 md_unregister_thread(mddev->sync_thread);
6353 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006354 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6355 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 /* success...*/
6357 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006358 if (mddev->pers->spare_active(mddev))
6359 sysfs_notify(&mddev->kobj, NULL,
6360 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 }
NeilBrown850b2b422006-10-03 01:15:46 -07006362 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006363
6364 /* if array is no-longer degraded, then any saved_raid_disk
6365 * information must be scrapped
6366 */
6367 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006368 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006369 rdev->saved_raid_disk = -1;
6370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 mddev->recovery = 0;
6372 /* flag recovery needed just to double check */
6373 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006374 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006375 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 goto unlock;
6377 }
Neil Brown72a23c22008-06-28 08:31:41 +10006378 /* Set RUNNING before clearing NEEDED to avoid
6379 * any transients in the value of "sync_action".
6380 */
6381 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6382 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006383 /* Clear some bits that don't mean anything, but
6384 * might be left set
6385 */
NeilBrown24dd4692005-11-08 21:39:26 -08006386 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6387 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006389 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6390 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 /* no recovery is running.
6392 * remove any failed drives, then
6393 * add spares if possible.
6394 * Spare are also removed and re-added, to allow
6395 * the personality to fail the re-add.
6396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006398 if (mddev->reshape_position != MaxSector) {
6399 if (mddev->pers->check_reshape(mddev) != 0)
6400 /* Cannot proceed */
6401 goto unlock;
6402 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006403 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006404 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006405 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6406 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006407 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006408 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006409 } else if (mddev->recovery_cp < MaxSector) {
6410 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006411 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006412 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6413 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006417 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6418 /* We are adding a device or devices to an array
6419 * which has the bitmap stored on all devices.
6420 * So make sure all bitmap pages get written
6421 */
6422 bitmap_write_all(mddev->bitmap);
6423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 mddev->sync_thread = md_register_thread(md_do_sync,
6425 mddev,
6426 "%s_resync");
6427 if (!mddev->sync_thread) {
6428 printk(KERN_ERR "%s: could not start resync"
6429 " thread...\n",
6430 mdname(mddev));
6431 /* leave the spares where they are, it shouldn't hurt */
6432 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006433 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006435 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006436 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 }
6438 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006439 if (!mddev->sync_thread) {
6440 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6441 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6442 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006443 if (mddev->sysfs_action)
6444 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 mddev_unlock(mddev);
6447 }
6448}
6449
Dan Williams6bfe0b42008-04-30 00:52:32 -07006450void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6451{
NeilBrown3c0ee632008-10-21 13:25:28 +11006452 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006453 wait_event_timeout(rdev->blocked_wait,
6454 !test_bit(Blocked, &rdev->flags),
6455 msecs_to_jiffies(5000));
6456 rdev_dec_pending(rdev, mddev);
6457}
6458EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6459
Adrian Bunk75c96f82005-05-05 16:16:09 -07006460static int md_notify_reboot(struct notifier_block *this,
6461 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462{
6463 struct list_head *tmp;
6464 mddev_t *mddev;
6465
6466 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6467
6468 printk(KERN_INFO "md: stopping all md devices.\n");
6469
NeilBrown29ac4aa2008-02-06 01:39:58 -08006470 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006471 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006472 /* Force a switch to readonly even array
6473 * appears to still be in use. Hence
6474 * the '100'.
6475 */
NeilBrownd710e132008-10-13 11:55:12 +11006476 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006477 mddev_unlock(mddev);
6478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 /*
6480 * certain more exotic SCSI devices are known to be
6481 * volatile wrt too early system reboots. While the
6482 * right place to handle this issue is the given
6483 * driver, we do want to have a safe RAID driver ...
6484 */
6485 mdelay(1000*1);
6486 }
6487 return NOTIFY_DONE;
6488}
6489
Adrian Bunk75c96f82005-05-05 16:16:09 -07006490static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 .notifier_call = md_notify_reboot,
6492 .next = NULL,
6493 .priority = INT_MAX, /* before any real devices */
6494};
6495
6496static void md_geninit(void)
6497{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6499
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006500 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501}
6502
Adrian Bunk75c96f82005-05-05 16:16:09 -07006503static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006505 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 return -1;
6507 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006508 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 return -1;
6510 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006511 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006512 md_probe, NULL, NULL);
6513 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 md_probe, NULL, NULL);
6515
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006517 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518
6519 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521}
6522
6523
6524#ifndef MODULE
6525
6526/*
6527 * Searches all registered partitions for autorun RAID arrays
6528 * at boot time.
6529 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006530
6531static LIST_HEAD(all_detected_devices);
6532struct detected_devices_node {
6533 struct list_head list;
6534 dev_t dev;
6535};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
6537void md_autodetect_dev(dev_t dev)
6538{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006539 struct detected_devices_node *node_detected_dev;
6540
6541 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6542 if (node_detected_dev) {
6543 node_detected_dev->dev = dev;
6544 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6545 } else {
6546 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6547 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549}
6550
6551
6552static void autostart_arrays(int part)
6553{
6554 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006555 struct detected_devices_node *node_detected_dev;
6556 dev_t dev;
6557 int i_scanned, i_passed;
6558
6559 i_scanned = 0;
6560 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
6562 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6563
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006564 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6565 i_scanned++;
6566 node_detected_dev = list_entry(all_detected_devices.next,
6567 struct detected_devices_node, list);
6568 list_del(&node_detected_dev->list);
6569 dev = node_detected_dev->dev;
6570 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006571 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 if (IS_ERR(rdev))
6573 continue;
6574
NeilBrownb2d444d2005-11-08 21:39:31 -08006575 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 MD_BUG();
6577 continue;
6578 }
NeilBrownd0fae182008-03-04 14:29:31 -08006579 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006581 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006583
6584 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6585 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
6587 autorun_devices(part);
6588}
6589
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006590#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
6592static __exit void md_exit(void)
6593{
6594 mddev_t *mddev;
6595 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006596
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006597 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006598 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006600 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 unregister_blkdev(mdp_major, "mdp");
6602 unregister_reboot_notifier(&md_notifier);
6603 unregister_sysctl_table(raid_table_header);
6604 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006605 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006607 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 }
6609}
6610
Dan Williams685784a2007-07-09 11:56:42 -07006611subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612module_exit(md_exit)
6613
NeilBrownf91de922005-11-08 21:39:36 -08006614static int get_ro(char *buffer, struct kernel_param *kp)
6615{
6616 return sprintf(buffer, "%d", start_readonly);
6617}
6618static int set_ro(const char *val, struct kernel_param *kp)
6619{
6620 char *e;
6621 int num = simple_strtoul(val, &e, 10);
6622 if (*val && (*e == '\0' || *e == '\n')) {
6623 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006624 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006625 }
6626 return -EINVAL;
6627}
6628
NeilBrown80ca3a42006-07-10 04:44:18 -07006629module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6630module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006631
NeilBrownefeb53c2009-01-09 08:31:10 +11006632module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006633
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634EXPORT_SYMBOL(register_md_personality);
6635EXPORT_SYMBOL(unregister_md_personality);
6636EXPORT_SYMBOL(md_error);
6637EXPORT_SYMBOL(md_done_sync);
6638EXPORT_SYMBOL(md_write_start);
6639EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640EXPORT_SYMBOL(md_register_thread);
6641EXPORT_SYMBOL(md_unregister_thread);
6642EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643EXPORT_SYMBOL(md_check_recovery);
6644MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006645MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006646MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);