blob: a29187d1fcf5be0b6abc676511b6a64e27d3e514 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070037#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sysctl.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>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define MAJOR_NR MD_MAJOR
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* 63 partitions with the alternate major number (mdp) */
51#define MdpMinorShift 6
52
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700105 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .proc_handler = &proc_dointvec,
107 },
108 {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .proc_handler = &proc_dointvec,
115 },
116 { .ctl_name = 0 }
117};
118
119static ctl_table raid_dir_table[] = {
120 {
121 .ctl_name = DEV_RAID,
122 .procname = "raid",
123 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .child = raid_table,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_root_table[] = {
131 {
132 .ctl_name = CTL_DEV,
133 .procname = "dev",
134 .maxlen = 0,
135 .mode = 0555,
136 .child = raid_dir_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static struct block_device_operations md_fops;
142
NeilBrownf91de922005-11-08 21:39:36 -0800143static int start_readonly;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
NeilBrownd7603b72006-01-06 00:20:30 -0800146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
149 * count increases.
150 *
151 * Events are:
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
154 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800155static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800156static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800157void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800158{
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
161}
NeilBrown29269552006-03-27 01:18:10 -0800162EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800163
NeilBrownc331eb02006-05-30 21:27:13 -0700164/* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
166 */
Adrian Bunk05381952006-06-26 00:28:01 -0700167static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700168{
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171}
172
NeilBrownd7603b72006-01-06 00:20:30 -0800173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
176 */
177static LIST_HEAD(all_mddevs);
178static DEFINE_SPINLOCK(all_mddevs_lock);
179
180
181/*
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
187 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800188#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
192 mddev = NULL;}); \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
200 tmp = tmp->next;}) \
201 )
202
203
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
216static void mddev_put(mddev_t *mddev)
217{
218 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
219 return;
220 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
221 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800222 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500223 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800224 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800225 } else
226 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229static mddev_t * mddev_find(dev_t unit)
230{
231 mddev_t *mddev, *new = NULL;
232
233 retry:
234 spin_lock(&all_mddevs_lock);
235 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
236 if (mddev->unit == unit) {
237 mddev_get(mddev);
238 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700239 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return mddev;
241 }
242
243 if (new) {
244 list_add(&new->all_mddevs, &all_mddevs);
245 spin_unlock(&all_mddevs_lock);
246 return new;
247 }
248 spin_unlock(&all_mddevs_lock);
249
NeilBrown9ffae0c2006-01-06 00:20:32 -0800250 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!new)
252 return NULL;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 new->unit = unit;
255 if (MAJOR(unit) == MD_MAJOR)
256 new->md_minor = MINOR(unit);
257 else
258 new->md_minor = MINOR(unit) >> MdpMinorShift;
259
NeilBrowndf5b89b2006-03-27 01:18:20 -0800260 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 INIT_LIST_HEAD(&new->disks);
262 INIT_LIST_HEAD(&new->all_mddevs);
263 init_timer(&new->safemode_timer);
264 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000265 atomic_set(&new->openers, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700266 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700267 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700268 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700269 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000270 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800271 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700272 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 new->queue = blk_alloc_queue(GFP_KERNEL);
275 if (!new->queue) {
276 kfree(new);
277 return NULL;
278 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200279 /* Can be unlocked because the queue is new: no concurrency */
280 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 blk_queue_make_request(new->queue, md_fail_request);
283
284 goto retry;
285}
286
287static inline int mddev_lock(mddev_t * mddev)
288{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800289 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static inline int mddev_trylock(mddev_t * mddev)
293{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800294 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297static inline void mddev_unlock(mddev_t * mddev)
298{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800299 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
NeilBrown005eca52005-08-22 13:11:08 -0700301 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
NeilBrown2989ddb2006-01-06 00:20:43 -0800304static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 mdk_rdev_t * rdev;
307 struct list_head *tmp;
308
NeilBrownd089c6a2008-02-06 01:39:59 -0800309 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (rdev->desc_nr == nr)
311 return rdev;
312 }
313 return NULL;
314}
315
316static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
317{
318 struct list_head *tmp;
319 mdk_rdev_t *rdev;
320
NeilBrownd089c6a2008-02-06 01:39:59 -0800321 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (rdev->bdev->bd_dev == dev)
323 return rdev;
324 }
325 return NULL;
326}
327
NeilBrownd9d166c2006-01-06 00:20:51 -0800328static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800329{
330 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800331 list_for_each_entry(pers, &pers_list, list) {
332 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800333 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800334 if (strcmp(pers->name, clevel)==0)
335 return pers;
336 }
NeilBrown2604b702006-01-06 00:20:36 -0800337 return NULL;
338}
339
Andre Nollb73df2d2008-07-11 22:02:23 +1000340/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700341static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
Andre Nollb73df2d2008-07-11 22:02:23 +1000343 sector_t num_sectors = bdev->bd_inode->i_size / 512;
344 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Andre Nolle7debaa2008-07-11 22:02:23 +1000347static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Andre Noll0f420352008-07-11 22:02:23 +1000349 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000352 num_sectors &= ~((sector_t)chunk_size/512 - 1);
353 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356static int alloc_disk_sb(mdk_rdev_t * rdev)
357{
358 if (rdev->sb_page)
359 MD_BUG();
360
361 rdev->sb_page = alloc_page(GFP_KERNEL);
362 if (!rdev->sb_page) {
363 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000364 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
367 return 0;
368}
369
370static void free_disk_sb(mdk_rdev_t * rdev)
371{
372 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800373 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rdev->sb_loaded = 0;
375 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000376 rdev->sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 rdev->size = 0;
378 }
379}
380
381
NeilBrown6712ecf2007-09-27 12:47:43 +0200382static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700383{
384 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800385 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700386
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700387 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
388 printk("md: super_written gets error=%d, uptodate=%d\n",
389 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
390 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800391 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700392 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700393
NeilBrowna9701a32005-11-08 21:39:34 -0800394 if (atomic_dec_and_test(&mddev->pending_writes))
395 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700396 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700397}
398
NeilBrown6712ecf2007-09-27 12:47:43 +0200399static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800400{
401 struct bio *bio2 = bio->bi_private;
402 mdk_rdev_t *rdev = bio2->bi_private;
403 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800404
405 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
406 error == -EOPNOTSUPP) {
407 unsigned long flags;
408 /* barriers don't appear to be supported :-( */
409 set_bit(BarriersNotsupp, &rdev->flags);
410 mddev->barriers_work = 0;
411 spin_lock_irqsave(&mddev->write_lock, flags);
412 bio2->bi_next = mddev->biolist;
413 mddev->biolist = bio2;
414 spin_unlock_irqrestore(&mddev->write_lock, flags);
415 wake_up(&mddev->sb_wait);
416 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200417 } else {
418 bio_put(bio2);
419 bio->bi_private = rdev;
420 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800421 }
NeilBrowna9701a32005-11-08 21:39:34 -0800422}
423
NeilBrown7bfa19f2005-06-21 17:17:28 -0700424void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
425 sector_t sector, int size, struct page *page)
426{
427 /* write first size bytes of page to sector of rdev
428 * Increment mddev->pending_writes before returning
429 * and decrement it on completion, waking up sb_wait
430 * if zero is reached.
431 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800432 *
433 * As we might need to resubmit the request if BIO_RW_BARRIER
434 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700435 */
436 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800437 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700438
439 bio->bi_bdev = rdev->bdev;
440 bio->bi_sector = sector;
441 bio_add_page(bio, page, size, 0);
442 bio->bi_private = rdev;
443 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800444 bio->bi_rw = rw;
445
NeilBrown7bfa19f2005-06-21 17:17:28 -0700446 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800447 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
448 struct bio *rbio;
449 rw |= (1<<BIO_RW_BARRIER);
450 rbio = bio_clone(bio, GFP_NOIO);
451 rbio->bi_private = bio;
452 rbio->bi_end_io = super_written_barrier;
453 submit_bio(rw, rbio);
454 } else
455 submit_bio(rw, bio);
456}
457
458void md_super_wait(mddev_t *mddev)
459{
460 /* wait for all superblock writes that were scheduled to complete.
461 * if any had to be retried (due to BARRIER problems), retry them
462 */
463 DEFINE_WAIT(wq);
464 for(;;) {
465 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
466 if (atomic_read(&mddev->pending_writes)==0)
467 break;
468 while (mddev->biolist) {
469 struct bio *bio;
470 spin_lock_irq(&mddev->write_lock);
471 bio = mddev->biolist;
472 mddev->biolist = bio->bi_next ;
473 bio->bi_next = NULL;
474 spin_unlock_irq(&mddev->write_lock);
475 submit_bio(bio->bi_rw, bio);
476 }
477 schedule();
478 }
479 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700480}
481
NeilBrown6712ecf2007-09-27 12:47:43 +0200482static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
NeilBrowna654b9d82005-06-21 17:17:27 -0700487int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct page *page, int rw)
489{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700490 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct completion event;
492 int ret;
493
494 rw |= (1 << BIO_RW_SYNC);
495
496 bio->bi_bdev = bdev;
497 bio->bi_sector = sector;
498 bio_add_page(bio, page, size, 0);
499 init_completion(&event);
500 bio->bi_private = &event;
501 bio->bi_end_io = bi_complete;
502 submit_bio(rw, bio);
503 wait_for_completion(&event);
504
505 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
506 bio_put(bio);
507 return ret;
508}
NeilBrowna8745db2006-01-06 00:20:34 -0800509EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
NeilBrown0002b272005-09-09 16:23:53 -0700511static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 char b[BDEVNAME_SIZE];
514 if (!rdev->sb_page) {
515 MD_BUG();
516 return -EINVAL;
517 }
518 if (rdev->sb_loaded)
519 return 0;
520
521
Andre Noll0f420352008-07-11 22:02:23 +1000522 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto fail;
524 rdev->sb_loaded = 1;
525 return 0;
526
527fail:
528 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
529 bdevname(rdev->bdev,b));
530 return -EINVAL;
531}
532
533static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
534{
Andre Noll05710462008-07-11 22:02:20 +1000535 return sb1->set_uuid0 == sb2->set_uuid0 &&
536 sb1->set_uuid1 == sb2->set_uuid1 &&
537 sb1->set_uuid2 == sb2->set_uuid2 &&
538 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
542{
543 int ret;
544 mdp_super_t *tmp1, *tmp2;
545
546 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
547 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
548
549 if (!tmp1 || !tmp2) {
550 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100551 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto abort;
553 }
554
555 *tmp1 = *sb1;
556 *tmp2 = *sb2;
557
558 /*
559 * nr_disks is not constant
560 */
561 tmp1->nr_disks = 0;
562 tmp2->nr_disks = 0;
563
Andre Nollce0c8e02008-07-11 22:02:20 +1000564 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700566 kfree(tmp1);
567 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return ret;
569}
570
NeilBrown4d167f02007-05-09 02:35:37 -0700571
572static u32 md_csum_fold(u32 csum)
573{
574 csum = (csum & 0xffff) + (csum >> 16);
575 return (csum & 0xffff) + (csum >> 16);
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static unsigned int calc_sb_csum(mdp_super_t * sb)
579{
NeilBrown4d167f02007-05-09 02:35:37 -0700580 u64 newcsum = 0;
581 u32 *sb32 = (u32*)sb;
582 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned int disk_csum, csum;
584
585 disk_csum = sb->sb_csum;
586 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700587
588 for (i = 0; i < MD_SB_BYTES/4 ; i++)
589 newcsum += sb32[i];
590 csum = (newcsum & 0xffffffff) + (newcsum>>32);
591
592
593#ifdef CONFIG_ALPHA
594 /* This used to use csum_partial, which was wrong for several
595 * reasons including that different results are returned on
596 * different architectures. It isn't critical that we get exactly
597 * the same return value as before (we always csum_fold before
598 * testing, and that removes any differences). However as we
599 * know that csum_partial always returned a 16bit value on
600 * alphas, do a fold to maximise conformity to previous behaviour.
601 */
602 sb->sb_csum = md_csum_fold(disk_csum);
603#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return csum;
607}
608
609
610/*
611 * Handle superblock details.
612 * We want to be able to handle multiple superblock formats
613 * so we have a common interface to them all, and an array of
614 * different handlers.
615 * We rely on user-space to write the initial superblock, and support
616 * reading and updating of superblocks.
617 * Interface methods are:
618 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
619 * loads and validates a superblock on dev.
620 * if refdev != NULL, compare superblocks on both devices
621 * Return:
622 * 0 - dev has a superblock that is compatible with refdev
623 * 1 - dev has a superblock that is compatible and newer than refdev
624 * so dev should be used as the refdev in future
625 * -EINVAL superblock incompatible or invalid
626 * -othererror e.g. -EIO
627 *
628 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
629 * Verify that dev is acceptable into mddev.
630 * The first time, mddev->raid_disks will be 0, and data from
631 * dev should be merged in. Subsequent calls check that dev
632 * is new enough. Return 0 or -EINVAL
633 *
634 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
635 * Update the superblock for rdev with data in mddev
636 * This does not write to disc.
637 *
638 */
639
640struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000641 char *name;
642 struct module *owner;
643 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
644 int minor_version);
645 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
646 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
647 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000648 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649};
650
651/*
652 * load_super for 0.90.0
653 */
654static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
655{
656 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
657 mdp_super_t *sb;
658 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /*
Andre Noll0f420352008-07-11 22:02:23 +1000661 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * it's at the end of the disk.
663 *
664 * It also happens to be a multiple of 4Kb.
665 */
Andre Noll0f420352008-07-11 22:02:23 +1000666 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
NeilBrown0002b272005-09-09 16:23:53 -0700668 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (ret) return ret;
670
671 ret = -EINVAL;
672
673 bdevname(rdev->bdev, b);
674 sb = (mdp_super_t*)page_address(rdev->sb_page);
675
676 if (sb->md_magic != MD_SB_MAGIC) {
677 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
678 b);
679 goto abort;
680 }
681
682 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800683 sb->minor_version < 90 ||
684 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
686 sb->major_version, sb->minor_version,
687 b);
688 goto abort;
689 }
690
691 if (sb->raid_disks <= 0)
692 goto abort;
693
NeilBrown4d167f02007-05-09 02:35:37 -0700694 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
696 b);
697 goto abort;
698 }
699
700 rdev->preferred_minor = sb->md_minor;
701 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700702 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
NeilBrowne11e93f2007-05-09 02:35:36 -0700704 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
705 if (sb->level != 1 && sb->level != 4
706 && sb->level != 5 && sb->level != 6
707 && sb->level != 10) {
708 /* FIXME use a better test */
709 printk(KERN_WARNING
710 "md: bitmaps not supported for this level.\n");
711 goto abort;
712 }
713 }
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (sb->level == LEVEL_MULTIPATH)
716 rdev->desc_nr = -1;
717 else
718 rdev->desc_nr = sb->this_disk.number;
719
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700720 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700722 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 __u64 ev1, ev2;
724 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
725 if (!uuid_equal(refsb, sb)) {
726 printk(KERN_WARNING "md: %s has different UUID to %s\n",
727 b, bdevname(refdev->bdev,b2));
728 goto abort;
729 }
730 if (!sb_equal(refsb, sb)) {
731 printk(KERN_WARNING "md: %s has same UUID"
732 " but different superblock to %s\n",
733 b, bdevname(refdev->bdev, b2));
734 goto abort;
735 }
736 ev1 = md_event(sb);
737 ev2 = md_event(refsb);
738 if (ev1 > ev2)
739 ret = 1;
740 else
741 ret = 0;
742 }
Andre Nolle7debaa2008-07-11 22:02:23 +1000743 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
NeilBrown2bf071b2006-01-06 00:20:55 -0800745 if (rdev->size < sb->size && sb->level > 1)
746 /* "this cannot possibly happen" ... */
747 ret = -EINVAL;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 abort:
750 return ret;
751}
752
753/*
754 * validate_super for 0.90.0
755 */
756static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
757{
758 mdp_disk_t *desc;
759 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700760 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
NeilBrown41158c72005-06-21 17:17:25 -0700762 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800763 clear_bit(Faulty, &rdev->flags);
764 clear_bit(In_sync, &rdev->flags);
765 clear_bit(WriteMostly, &rdev->flags);
766 clear_bit(BarriersNotsupp, &rdev->flags);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (mddev->raid_disks == 0) {
769 mddev->major_version = 0;
770 mddev->minor_version = sb->minor_version;
771 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800772 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 mddev->chunk_size = sb->chunk_size;
774 mddev->ctime = sb->ctime;
775 mddev->utime = sb->utime;
776 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800777 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 mddev->layout = sb->layout;
779 mddev->raid_disks = sb->raid_disks;
780 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700781 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700782 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700783 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
NeilBrownf6705572006-03-27 01:18:11 -0800785 if (mddev->minor_version >= 91) {
786 mddev->reshape_position = sb->reshape_position;
787 mddev->delta_disks = sb->delta_disks;
788 mddev->new_level = sb->new_level;
789 mddev->new_layout = sb->new_layout;
790 mddev->new_chunk = sb->new_chunk;
791 } else {
792 mddev->reshape_position = MaxSector;
793 mddev->delta_disks = 0;
794 mddev->new_level = mddev->level;
795 mddev->new_layout = mddev->layout;
796 mddev->new_chunk = mddev->chunk_size;
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (sb->state & (1<<MD_SB_CLEAN))
800 mddev->recovery_cp = MaxSector;
801 else {
802 if (sb->events_hi == sb->cp_events_hi &&
803 sb->events_lo == sb->cp_events_lo) {
804 mddev->recovery_cp = sb->recovery_cp;
805 } else
806 mddev->recovery_cp = 0;
807 }
808
809 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
810 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
811 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
812 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
813
814 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700815
816 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700817 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700818 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700819
NeilBrown41158c72005-06-21 17:17:25 -0700820 } else if (mddev->pers == NULL) {
821 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ++ev1;
823 if (ev1 < mddev->events)
824 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700825 } else if (mddev->bitmap) {
826 /* if adding to array with a bitmap, then we can accept an
827 * older device ... but not too old.
828 */
NeilBrown41158c72005-06-21 17:17:25 -0700829 if (ev1 < mddev->bitmap->events_cleared)
830 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700831 } else {
832 if (ev1 < mddev->events)
833 /* just a hot-add of a new device, leave raid_disk at -1 */
834 return 0;
835 }
NeilBrown41158c72005-06-21 17:17:25 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 desc = sb->disks + rdev->desc_nr;
839
840 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800841 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700842 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
843 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800844 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 rdev->raid_disk = desc->raid_disk;
846 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700847 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
848 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700849 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800850 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 0;
852}
853
854/*
855 * sync_super for 0.90.0
856 */
857static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
858{
859 mdp_super_t *sb;
860 struct list_head *tmp;
861 mdk_rdev_t *rdev2;
862 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* make rdev->sb match mddev data..
866 *
867 * 1/ zero out disks
868 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
869 * 3/ any empty disks < next_spare become removed
870 *
871 * disks[0] gets initialised to REMOVED because
872 * we cannot be sure from other fields if it has
873 * been initialised or not.
874 */
875 int i;
876 int active=0, working=0,failed=0,spare=0,nr_disks=0;
877
NeilBrown61181562005-09-09 16:24:02 -0700878 rdev->sb_size = MD_SB_BYTES;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sb = (mdp_super_t*)page_address(rdev->sb_page);
881
882 memset(sb, 0, sizeof(*sb));
883
884 sb->md_magic = MD_SB_MAGIC;
885 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 sb->patch_version = mddev->patch_version;
887 sb->gvalid_words = 0; /* ignored */
888 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
889 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
890 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
891 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
892
893 sb->ctime = mddev->ctime;
894 sb->level = mddev->level;
895 sb->size = mddev->size;
896 sb->raid_disks = mddev->raid_disks;
897 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800898 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 sb->utime = mddev->utime;
900 sb->state = 0;
901 sb->events_hi = (mddev->events>>32);
902 sb->events_lo = (u32)mddev->events;
903
NeilBrownf6705572006-03-27 01:18:11 -0800904 if (mddev->reshape_position == MaxSector)
905 sb->minor_version = 90;
906 else {
907 sb->minor_version = 91;
908 sb->reshape_position = mddev->reshape_position;
909 sb->new_level = mddev->new_level;
910 sb->delta_disks = mddev->delta_disks;
911 sb->new_layout = mddev->new_layout;
912 sb->new_chunk = mddev->new_chunk;
913 }
914 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (mddev->in_sync)
916 {
917 sb->recovery_cp = mddev->recovery_cp;
918 sb->cp_events_hi = (mddev->events>>32);
919 sb->cp_events_lo = (u32)mddev->events;
920 if (mddev->recovery_cp == MaxSector)
921 sb->state = (1<< MD_SB_CLEAN);
922 } else
923 sb->recovery_cp = 0;
924
925 sb->layout = mddev->layout;
926 sb->chunk_size = mddev->chunk_size;
927
NeilBrowna654b9d82005-06-21 17:17:27 -0700928 if (mddev->bitmap && mddev->bitmap_file == NULL)
929 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800932 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800934 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800935 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
936 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800937 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800939 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800940 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 d = &sb->disks[rdev2->desc_nr];
942 nr_disks++;
943 d->number = rdev2->desc_nr;
944 d->major = MAJOR(rdev2->bdev->bd_dev);
945 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800946 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
947 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 d->raid_disk = rdev2->raid_disk;
949 else
950 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800951 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800953 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 d->state = (1<<MD_DISK_ACTIVE);
955 d->state |= (1<<MD_DISK_SYNC);
956 active++;
957 working++;
958 } else {
959 d->state = 0;
960 spare++;
961 working++;
962 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700963 if (test_bit(WriteMostly, &rdev2->flags))
964 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* now set the "removed" and "faulty" bits on any missing devices */
967 for (i=0 ; i < mddev->raid_disks ; i++) {
968 mdp_disk_t *d = &sb->disks[i];
969 if (d->state == 0 && d->number == 0) {
970 d->number = i;
971 d->raid_disk = i;
972 d->state = (1<<MD_DISK_REMOVED);
973 d->state |= (1<<MD_DISK_FAULTY);
974 failed++;
975 }
976 }
977 sb->nr_disks = nr_disks;
978 sb->active_disks = active;
979 sb->working_disks = working;
980 sb->failed_disks = failed;
981 sb->spare_disks = spare;
982
983 sb->this_disk = sb->disks[rdev->desc_nr];
984 sb->sb_csum = calc_sb_csum(sb);
985}
986
987/*
Chris Webb0cd17fe2008-06-28 08:31:46 +1000988 * rdev_size_change for 0.90.0
989 */
990static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +1000991super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +1000992{
Andre Noll15f4a5f2008-07-21 14:42:12 +1000993 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +1000994 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +1000995 if (rdev->mddev->bitmap_offset)
996 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +1000997 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +1000998 if (!num_sectors || num_sectors > rdev->sb_start)
999 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001000 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001001 rdev->sb_page);
1002 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001003 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001004}
1005
1006
1007/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * version 1 superblock
1009 */
1010
NeilBrown1c05b4b2006-10-21 10:24:08 -07001011static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001013 __le32 disk_csum;
1014 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 unsigned long long newcsum;
1016 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001017 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int i;
1019
1020 disk_csum = sb->sb_csum;
1021 sb->sb_csum = 0;
1022 newcsum = 0;
1023 for (i=0; size>=4; size -= 4 )
1024 newcsum += le32_to_cpu(*isuper++);
1025
1026 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001027 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1030 sb->sb_csum = disk_csum;
1031 return cpu_to_le32(csum);
1032}
1033
1034static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1035{
1036 struct mdp_superblock_1 *sb;
1037 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001038 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001040 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /*
Andre Noll0f420352008-07-11 22:02:23 +10001043 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * It is always aligned to a 4K boundary and
1045 * depeding on minor_version, it can be:
1046 * 0: At least 8K, but less than 12K, from end of device
1047 * 1: At start of device
1048 * 2: 4K from start of device.
1049 */
1050 switch(minor_version) {
1051 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001052 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1053 sb_start -= 8*2;
1054 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001057 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 break;
1059 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001060 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
1062 default:
1063 return -EINVAL;
1064 }
Andre Noll0f420352008-07-11 22:02:23 +10001065 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
NeilBrown0002b272005-09-09 16:23:53 -07001067 /* superblock is rarely larger than 1K, but it can be larger,
1068 * and it is safe to read 4k, so we do that
1069 */
1070 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (ret) return ret;
1072
1073
1074 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1075
1076 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1077 sb->major_version != cpu_to_le32(1) ||
1078 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001079 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001080 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -EINVAL;
1082
1083 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1084 printk("md: invalid superblock checksum on %s\n",
1085 bdevname(rdev->bdev,b));
1086 return -EINVAL;
1087 }
1088 if (le64_to_cpu(sb->data_size) < 10) {
1089 printk("md: data_size too small on %s\n",
1090 bdevname(rdev->bdev,b));
1091 return -EINVAL;
1092 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001093 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1094 if (sb->level != cpu_to_le32(1) &&
1095 sb->level != cpu_to_le32(4) &&
1096 sb->level != cpu_to_le32(5) &&
1097 sb->level != cpu_to_le32(6) &&
1098 sb->level != cpu_to_le32(10)) {
1099 printk(KERN_WARNING
1100 "md: bitmaps not supported for this level.\n");
1101 return -EINVAL;
1102 }
1103 }
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 rdev->preferred_minor = 0xffff;
1106 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001107 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
NeilBrown0002b272005-09-09 16:23:53 -07001109 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001110 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001111 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001112 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1113
1114 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001115 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001116 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001117
NeilBrown31b65a02006-07-10 04:44:14 -07001118 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1119 rdev->desc_nr = -1;
1120 else
1121 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1122
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001123 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001124 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001125 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 __u64 ev1, ev2;
1127 struct mdp_superblock_1 *refsb =
1128 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1129
1130 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1131 sb->level != refsb->level ||
1132 sb->layout != refsb->layout ||
1133 sb->chunksize != refsb->chunksize) {
1134 printk(KERN_WARNING "md: %s has strangely different"
1135 " superblock to %s\n",
1136 bdevname(rdev->bdev,b),
1137 bdevname(refdev->bdev,b2));
1138 return -EINVAL;
1139 }
1140 ev1 = le64_to_cpu(sb->events);
1141 ev2 = le64_to_cpu(refsb->events);
1142
1143 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001144 ret = 1;
1145 else
1146 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
NeilBrowna1801f82008-03-04 14:29:31 -08001148 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1150 else
Andre Noll0f420352008-07-11 22:02:23 +10001151 rdev->size = rdev->sb_start / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1153 return -EINVAL;
1154 rdev->size = le64_to_cpu(sb->data_size)/2;
1155 if (le32_to_cpu(sb->chunksize))
1156 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001157
NeilBrown1c05b4b2006-10-21 10:24:08 -07001158 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001159 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
1163static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1164{
1165 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001166 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
NeilBrown41158c72005-06-21 17:17:25 -07001168 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001169 clear_bit(Faulty, &rdev->flags);
1170 clear_bit(In_sync, &rdev->flags);
1171 clear_bit(WriteMostly, &rdev->flags);
1172 clear_bit(BarriersNotsupp, &rdev->flags);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (mddev->raid_disks == 0) {
1175 mddev->major_version = 1;
1176 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001177 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1179 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1180 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1181 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001182 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 mddev->layout = le32_to_cpu(sb->layout);
1184 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1185 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001186 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001187 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001188 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1191 memcpy(mddev->uuid, sb->set_uuid, 16);
1192
1193 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001194
NeilBrown71c08052005-09-09 16:23:51 -07001195 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001196 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001197 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001198
NeilBrownf6705572006-03-27 01:18:11 -08001199 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1200 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1201 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1202 mddev->new_level = le32_to_cpu(sb->new_level);
1203 mddev->new_layout = le32_to_cpu(sb->new_layout);
1204 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1205 } else {
1206 mddev->reshape_position = MaxSector;
1207 mddev->delta_disks = 0;
1208 mddev->new_level = mddev->level;
1209 mddev->new_layout = mddev->layout;
1210 mddev->new_chunk = mddev->chunk_size;
1211 }
1212
NeilBrown41158c72005-06-21 17:17:25 -07001213 } else if (mddev->pers == NULL) {
1214 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ++ev1;
1216 if (ev1 < mddev->events)
1217 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001218 } else if (mddev->bitmap) {
1219 /* If adding to array with a bitmap, then we can accept an
1220 * older device, but not too old.
1221 */
NeilBrown41158c72005-06-21 17:17:25 -07001222 if (ev1 < mddev->bitmap->events_cleared)
1223 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001224 } else {
1225 if (ev1 < mddev->events)
1226 /* just a hot-add of a new device, leave raid_disk at -1 */
1227 return 0;
1228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (mddev->level != LEVEL_MULTIPATH) {
1230 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1232 switch(role) {
1233 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 break;
1235 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001236 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 break;
1238 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001239 if ((le32_to_cpu(sb->feature_map) &
1240 MD_FEATURE_RECOVERY_OFFSET))
1241 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1242 else
1243 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 rdev->raid_disk = role;
1245 break;
1246 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001247 if (sb->devflags & WriteMostly1)
1248 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001249 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001250 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return 0;
1253}
1254
1255static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1256{
1257 struct mdp_superblock_1 *sb;
1258 struct list_head *tmp;
1259 mdk_rdev_t *rdev2;
1260 int max_dev, i;
1261 /* make rdev->sb match mddev and rdev data. */
1262
1263 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1264
1265 sb->feature_map = 0;
1266 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001267 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 memset(sb->pad1, 0, sizeof(sb->pad1));
1269 memset(sb->pad2, 0, sizeof(sb->pad2));
1270 memset(sb->pad3, 0, sizeof(sb->pad3));
1271
1272 sb->utime = cpu_to_le64((__u64)mddev->utime);
1273 sb->events = cpu_to_le64(mddev->events);
1274 if (mddev->in_sync)
1275 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1276 else
1277 sb->resync_offset = cpu_to_le64(0);
1278
NeilBrown1c05b4b2006-10-21 10:24:08 -07001279 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001280
NeilBrownf0ca3402006-02-02 14:28:04 -08001281 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001282 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001283
NeilBrowna654b9d82005-06-21 17:17:27 -07001284 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1285 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001286 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001287 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001288
1289 if (rdev->raid_disk >= 0 &&
1290 !test_bit(In_sync, &rdev->flags) &&
1291 rdev->recovery_offset > 0) {
1292 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1293 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1294 }
1295
NeilBrownf6705572006-03-27 01:18:11 -08001296 if (mddev->reshape_position != MaxSector) {
1297 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1298 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1299 sb->new_layout = cpu_to_le32(mddev->new_layout);
1300 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1301 sb->new_level = cpu_to_le32(mddev->new_level);
1302 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1303 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001306 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (rdev2->desc_nr+1 > max_dev)
1308 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001309
1310 if (max_dev > le32_to_cpu(sb->max_dev))
1311 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 for (i=0; i<max_dev;i++)
1313 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1314
NeilBrownd089c6a2008-02-06 01:39:59 -08001315 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001317 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001319 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001321 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1322 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 else
1324 sb->dev_roles[i] = cpu_to_le16(0xffff);
1325 }
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 sb->sb_csum = calc_sb_1_csum(sb);
1328}
1329
Chris Webb0cd17fe2008-06-28 08:31:46 +10001330static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001331super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001332{
1333 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001334 sector_t max_sectors;
1335 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001336 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001337 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001338 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001339 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1340 max_sectors -= rdev->data_offset;
1341 if (!num_sectors || num_sectors > max_sectors)
1342 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001343 } else if (rdev->mddev->bitmap_offset) {
1344 /* minor version 0 with bitmap we can't move */
1345 return 0;
1346 } else {
1347 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001348 sector_t sb_start;
1349 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1350 sb_start &= ~(sector_t)(4*2 - 1);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001351 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1352 if (!num_sectors || num_sectors > max_sectors)
1353 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001354 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001355 }
1356 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001357 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001358 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001359 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001360 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001361 rdev->sb_page);
1362 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001363 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Adrian Bunk75c96f82005-05-05 16:16:09 -07001366static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 [0] = {
1368 .name = "0.90.0",
1369 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001370 .load_super = super_90_load,
1371 .validate_super = super_90_validate,
1372 .sync_super = super_90_sync,
1373 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 },
1375 [1] = {
1376 .name = "md-1",
1377 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001378 .load_super = super_1_load,
1379 .validate_super = super_1_validate,
1380 .sync_super = super_1_sync,
1381 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 },
1383};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1386{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001387 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
NeilBrown4b809912008-07-21 17:05:25 +10001389 rcu_read_lock();
1390 rdev_for_each_rcu(rdev, mddev1)
1391 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001392 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001393 rdev2->bdev->bd_contains) {
1394 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001395 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001396 }
1397 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 0;
1399}
1400
1401static LIST_HEAD(pending_raid_disks);
1402
1403static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1404{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001405 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001406 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001407 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001408 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 if (rdev->mddev) {
1411 MD_BUG();
1412 return -EINVAL;
1413 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001414
1415 /* prevent duplicates */
1416 if (find_rdev(mddev, rdev->bdev->bd_dev))
1417 return -EEXIST;
1418
NeilBrown2bf071b2006-01-06 00:20:55 -08001419 /* make sure rdev->size exceeds mddev->size */
1420 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001421 if (mddev->pers) {
1422 /* Cannot change size, so fail
1423 * If mddev->level <= 0, then we don't care
1424 * about aligning sizes (e.g. linear)
1425 */
1426 if (mddev->level > 0)
1427 return -ENOSPC;
1428 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001429 mddev->size = rdev->size;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /* Verify rdev->desc_nr is unique.
1433 * If it is -1, assign a free number, else
1434 * check number is not in use
1435 */
1436 if (rdev->desc_nr < 0) {
1437 int choice = 0;
1438 if (mddev->pers) choice = mddev->raid_disks;
1439 while (find_rdev_nr(mddev, choice))
1440 choice++;
1441 rdev->desc_nr = choice;
1442 } else {
1443 if (find_rdev_nr(mddev, rdev->desc_nr))
1444 return -EBUSY;
1445 }
NeilBrown19133a42005-11-08 21:39:35 -08001446 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001447 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001448 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001451 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001452
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001453 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001454 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001455
Tejun Heo0762b8b2008-08-25 19:56:12 +09001456 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001457 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1458 kobject_del(&rdev->kobj);
1459 goto fail;
1460 }
NeilBrown4b809912008-07-21 17:05:25 +10001461 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001462 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001464
1465 fail:
1466 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1467 b, mdname(mddev));
1468 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
NeilBrown177a99b2008-02-06 01:39:56 -08001471static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001472{
1473 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1474 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001475 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1479{
1480 char b[BDEVNAME_SIZE];
1481 if (!rdev->mddev) {
1482 MD_BUG();
1483 return;
1484 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001485 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001486 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1488 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001489 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001490
1491 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001492 * writing to 'remove' to "dev/state". We also need
1493 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001494 */
NeilBrown4b809912008-07-21 17:05:25 +10001495 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001496 INIT_WORK(&rdev->del_work, md_delayed_delete);
1497 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001498 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501/*
1502 * prevent the device from being mounted, repartitioned or
1503 * otherwise reused by a RAID array (or any other kernel
1504 * subsystem), by bd_claiming the device.
1505 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001506static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
1508 int err = 0;
1509 struct block_device *bdev;
1510 char b[BDEVNAME_SIZE];
1511
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001512 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (IS_ERR(bdev)) {
1514 printk(KERN_ERR "md: could not open %s.\n",
1515 __bdevname(dev, b));
1516 return PTR_ERR(bdev);
1517 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001518 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (err) {
1520 printk(KERN_ERR "md: could not bd_claim %s.\n",
1521 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001522 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return err;
1524 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001525 if (!shared)
1526 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 rdev->bdev = bdev;
1528 return err;
1529}
1530
1531static void unlock_rdev(mdk_rdev_t *rdev)
1532{
1533 struct block_device *bdev = rdev->bdev;
1534 rdev->bdev = NULL;
1535 if (!bdev)
1536 MD_BUG();
1537 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001538 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541void md_autodetect_dev(dev_t dev);
1542
1543static void export_rdev(mdk_rdev_t * rdev)
1544{
1545 char b[BDEVNAME_SIZE];
1546 printk(KERN_INFO "md: export_rdev(%s)\n",
1547 bdevname(rdev->bdev,b));
1548 if (rdev->mddev)
1549 MD_BUG();
1550 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001552 if (test_bit(AutoDetected, &rdev->flags))
1553 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554#endif
1555 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001556 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559static void kick_rdev_from_array(mdk_rdev_t * rdev)
1560{
1561 unbind_rdev_from_array(rdev);
1562 export_rdev(rdev);
1563}
1564
1565static void export_array(mddev_t *mddev)
1566{
1567 struct list_head *tmp;
1568 mdk_rdev_t *rdev;
1569
NeilBrownd089c6a2008-02-06 01:39:59 -08001570 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (!rdev->mddev) {
1572 MD_BUG();
1573 continue;
1574 }
1575 kick_rdev_from_array(rdev);
1576 }
1577 if (!list_empty(&mddev->disks))
1578 MD_BUG();
1579 mddev->raid_disks = 0;
1580 mddev->major_version = 0;
1581}
1582
1583static void print_desc(mdp_disk_t *desc)
1584{
1585 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1586 desc->major,desc->minor,desc->raid_disk,desc->state);
1587}
1588
1589static void print_sb(mdp_super_t *sb)
1590{
1591 int i;
1592
1593 printk(KERN_INFO
1594 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1595 sb->major_version, sb->minor_version, sb->patch_version,
1596 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1597 sb->ctime);
1598 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1599 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1600 sb->md_minor, sb->layout, sb->chunk_size);
1601 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1602 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1603 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1604 sb->failed_disks, sb->spare_disks,
1605 sb->sb_csum, (unsigned long)sb->events_lo);
1606
1607 printk(KERN_INFO);
1608 for (i = 0; i < MD_SB_DISKS; i++) {
1609 mdp_disk_t *desc;
1610
1611 desc = sb->disks + i;
1612 if (desc->number || desc->major || desc->minor ||
1613 desc->raid_disk || (desc->state && (desc->state != 4))) {
1614 printk(" D %2d: ", i);
1615 print_desc(desc);
1616 }
1617 }
1618 printk(KERN_INFO "md: THIS: ");
1619 print_desc(&sb->this_disk);
1620
1621}
1622
1623static void print_rdev(mdk_rdev_t *rdev)
1624{
1625 char b[BDEVNAME_SIZE];
1626 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1627 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001628 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1629 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (rdev->sb_loaded) {
1631 printk(KERN_INFO "md: rdev superblock:\n");
1632 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1633 } else
1634 printk(KERN_INFO "md: no rdev superblock!\n");
1635}
1636
Adrian Bunk5e563412006-06-26 00:27:42 -07001637static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 struct list_head *tmp, *tmp2;
1640 mdk_rdev_t *rdev;
1641 mddev_t *mddev;
1642 char b[BDEVNAME_SIZE];
1643
1644 printk("\n");
1645 printk("md: **********************************\n");
1646 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1647 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001648 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
NeilBrown32a76272005-06-21 17:17:14 -07001650 if (mddev->bitmap)
1651 bitmap_print_sb(mddev->bitmap);
1652 else
1653 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001654 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 printk("<%s>", bdevname(rdev->bdev,b));
1656 printk("\n");
1657
NeilBrownd089c6a2008-02-06 01:39:59 -08001658 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 print_rdev(rdev);
1660 }
1661 printk("md: **********************************\n");
1662 printk("\n");
1663}
1664
1665
NeilBrown42543762006-06-26 00:27:57 -07001666static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
NeilBrown42543762006-06-26 00:27:57 -07001668 /* Update each superblock (in-memory image), but
1669 * if we are allowed to, skip spares which already
1670 * have the right event counter, or have one earlier
1671 * (which would mean they aren't being marked as dirty
1672 * with the rest of the array)
1673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 mdk_rdev_t *rdev;
1675 struct list_head *tmp;
1676
NeilBrownd089c6a2008-02-06 01:39:59 -08001677 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001678 if (rdev->sb_events == mddev->events ||
1679 (nospares &&
1680 rdev->raid_disk < 0 &&
1681 (rdev->sb_events&1)==0 &&
1682 rdev->sb_events+1 == mddev->events)) {
1683 /* Don't update this superblock */
1684 rdev->sb_loaded = 2;
1685 } else {
1686 super_types[mddev->major_version].
1687 sync_super(mddev, rdev);
1688 rdev->sb_loaded = 1;
1689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691}
1692
NeilBrown850b2b42006-10-03 01:15:46 -07001693static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct list_head *tmp;
1696 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001697 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001698 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
NeilBrown8377bc82008-04-30 00:52:28 -07001700 if (mddev->external)
1701 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001703 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001704
NeilBrown850b2b42006-10-03 01:15:46 -07001705 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1706 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1707 force_change = 1;
1708 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1709 /* just a clean<-> dirty transition, possibly leave spares alone,
1710 * though if events isn't the right even/odd, we will have to do
1711 * spares after all
1712 */
1713 nospares = 1;
1714 if (force_change)
1715 nospares = 0;
1716 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001717 /* If the array is degraded, then skipping spares is both
1718 * dangerous and fairly pointless.
1719 * Dangerous because a device that was removed from the array
1720 * might have a event_count that still looks up-to-date,
1721 * so it can be re-added without a resync.
1722 * Pointless because if there are any spares to skip,
1723 * then a recovery will happen and soon that array won't
1724 * be degraded any more and the spare can go back to sleep then.
1725 */
NeilBrown850b2b42006-10-03 01:15:46 -07001726 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001727
NeilBrown06d91a52005-06-21 17:17:12 -07001728 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001730
1731 /* If this is just a dirty<->clean transition, and the array is clean
1732 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001733 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001734 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001735 && (mddev->events & 1)
1736 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001737 mddev->events--;
1738 else {
1739 /* otherwise we have to go forward and ... */
1740 mddev->events ++;
1741 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1742 /* .. if the array isn't clean, insist on an odd 'events' */
1743 if ((mddev->events&1)==0) {
1744 mddev->events++;
1745 nospares = 0;
1746 }
1747 } else {
1748 /* otherwise insist on an even 'events' (for clean states) */
1749 if ((mddev->events&1)) {
1750 mddev->events++;
1751 nospares = 0;
1752 }
1753 }
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (!mddev->events) {
1757 /*
1758 * oops, this 64-bit counter should never wrap.
1759 * Either we are in around ~1 trillion A.C., assuming
1760 * 1 reboot per second, or we have a bug:
1761 */
1762 MD_BUG();
1763 mddev->events --;
1764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /*
1767 * do not write anything to disk if using
1768 * nonpersistent superblocks
1769 */
NeilBrown06d91a52005-06-21 17:17:12 -07001770 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001771 if (!mddev->external)
1772 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1773
NeilBrowna9701a32005-11-08 21:39:34 -08001774 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001775 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001777 }
NeilBrowne6910632008-02-06 01:39:51 -08001778 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001779 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 dprintk(KERN_INFO
1782 "md: updating %s RAID superblock on device (in sync %d)\n",
1783 mdname(mddev),mddev->in_sync);
1784
NeilBrown4ad13662007-07-17 04:06:13 -07001785 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001786 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 char b[BDEVNAME_SIZE];
1788 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001789 if (rdev->sb_loaded != 1)
1790 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001791 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 dprintk("(skipping faulty ");
1793
1794 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001795 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001796 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001797 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001798 rdev->sb_page);
1799 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1800 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001801 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001802 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 } else
1805 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001806 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /* only need to write one superblock... */
1808 break;
1809 }
NeilBrowna9701a32005-11-08 21:39:34 -08001810 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001811 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001812
NeilBrowna9701a32005-11-08 21:39:34 -08001813 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001814 if (mddev->in_sync != sync_req ||
1815 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001816 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001817 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001818 goto repeat;
1819 }
NeilBrown850b2b42006-10-03 01:15:46 -07001820 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001821 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001822 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
Andre Noll7f6ce762008-03-23 18:34:54 +01001826/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001827 * We want to accept with case. For this we use cmd_match.
1828 */
1829static int cmd_match(const char *cmd, const char *str)
1830{
1831 /* See if cmd, written into a sysfs file, matches
1832 * str. They must either be the same, or cmd can
1833 * have a trailing newline
1834 */
1835 while (*cmd && *str && *cmd == *str) {
1836 cmd++;
1837 str++;
1838 }
1839 if (*cmd == '\n')
1840 cmd++;
1841 if (*str || *cmd)
1842 return 0;
1843 return 1;
1844}
1845
NeilBrown86e6ffd2005-11-08 21:39:24 -08001846struct rdev_sysfs_entry {
1847 struct attribute attr;
1848 ssize_t (*show)(mdk_rdev_t *, char *);
1849 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1850};
1851
1852static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001853state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001854{
1855 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001856 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001857
NeilBrownb2d444d2005-11-08 21:39:31 -08001858 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001859 len+= sprintf(page+len, "%sfaulty",sep);
1860 sep = ",";
1861 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001862 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001863 len += sprintf(page+len, "%sin_sync",sep);
1864 sep = ",";
1865 }
NeilBrownf6556752006-06-26 00:28:01 -07001866 if (test_bit(WriteMostly, &rdev->flags)) {
1867 len += sprintf(page+len, "%swrite_mostly",sep);
1868 sep = ",";
1869 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001870 if (test_bit(Blocked, &rdev->flags)) {
1871 len += sprintf(page+len, "%sblocked", sep);
1872 sep = ",";
1873 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001874 if (!test_bit(Faulty, &rdev->flags) &&
1875 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001876 len += sprintf(page+len, "%sspare", sep);
1877 sep = ",";
1878 }
1879 return len+sprintf(page+len, "\n");
1880}
1881
NeilBrown45dc2de2006-06-26 00:27:58 -07001882static ssize_t
1883state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1884{
1885 /* can write
1886 * faulty - simulates and error
1887 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001888 * writemostly - sets write_mostly
1889 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001890 * blocked - sets the Blocked flag
1891 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001892 */
1893 int err = -EINVAL;
1894 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1895 md_error(rdev->mddev, rdev);
1896 err = 0;
1897 } else if (cmd_match(buf, "remove")) {
1898 if (rdev->raid_disk >= 0)
1899 err = -EBUSY;
1900 else {
1901 mddev_t *mddev = rdev->mddev;
1902 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001903 if (mddev->pers)
1904 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001905 md_new_event(mddev);
1906 err = 0;
1907 }
NeilBrownf6556752006-06-26 00:28:01 -07001908 } else if (cmd_match(buf, "writemostly")) {
1909 set_bit(WriteMostly, &rdev->flags);
1910 err = 0;
1911 } else if (cmd_match(buf, "-writemostly")) {
1912 clear_bit(WriteMostly, &rdev->flags);
1913 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001914 } else if (cmd_match(buf, "blocked")) {
1915 set_bit(Blocked, &rdev->flags);
1916 err = 0;
1917 } else if (cmd_match(buf, "-blocked")) {
1918 clear_bit(Blocked, &rdev->flags);
1919 wake_up(&rdev->blocked_wait);
1920 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1921 md_wakeup_thread(rdev->mddev->thread);
1922
1923 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001924 }
Neil Brown52664732008-06-28 08:31:44 +10001925 if (!err)
1926 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrown45dc2de2006-06-26 00:27:58 -07001927 return err ? err : len;
1928}
NeilBrown80ca3a42006-07-10 04:44:18 -07001929static struct rdev_sysfs_entry rdev_state =
1930__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001931
1932static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001933errors_show(mdk_rdev_t *rdev, char *page)
1934{
1935 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1936}
1937
1938static ssize_t
1939errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1940{
1941 char *e;
1942 unsigned long n = simple_strtoul(buf, &e, 10);
1943 if (*buf && (*e == 0 || *e == '\n')) {
1944 atomic_set(&rdev->corrected_errors, n);
1945 return len;
1946 }
1947 return -EINVAL;
1948}
1949static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001950__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001951
NeilBrown014236d2006-01-06 00:20:55 -08001952static ssize_t
1953slot_show(mdk_rdev_t *rdev, char *page)
1954{
1955 if (rdev->raid_disk < 0)
1956 return sprintf(page, "none\n");
1957 else
1958 return sprintf(page, "%d\n", rdev->raid_disk);
1959}
1960
1961static ssize_t
1962slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1963{
1964 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001965 int err;
1966 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001967 int slot = simple_strtoul(buf, &e, 10);
1968 if (strncmp(buf, "none", 4)==0)
1969 slot = -1;
1970 else if (e==buf || (*e && *e!= '\n'))
1971 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001972 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001973 /* Setting 'slot' on an active array requires also
1974 * updating the 'rd%d' link, and communicating
1975 * with the personality with ->hot_*_disk.
1976 * For now we only support removing
1977 * failed/spare devices. This normally happens automatically,
1978 * but not when the metadata is externally managed.
1979 */
NeilBrownc303da62008-02-06 01:39:51 -08001980 if (rdev->raid_disk == -1)
1981 return -EEXIST;
1982 /* personality does all needed checks */
1983 if (rdev->mddev->pers->hot_add_disk == NULL)
1984 return -EINVAL;
1985 err = rdev->mddev->pers->
1986 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1987 if (err)
1988 return err;
1989 sprintf(nm, "rd%d", rdev->raid_disk);
1990 sysfs_remove_link(&rdev->mddev->kobj, nm);
1991 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1992 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10001993 } else if (rdev->mddev->pers) {
1994 mdk_rdev_t *rdev2;
1995 struct list_head *tmp;
1996 /* Activating a spare .. or possibly reactivating
1997 * if we every get bitmaps working here.
1998 */
1999
2000 if (rdev->raid_disk != -1)
2001 return -EBUSY;
2002
2003 if (rdev->mddev->pers->hot_add_disk == NULL)
2004 return -EINVAL;
2005
2006 rdev_for_each(rdev2, tmp, rdev->mddev)
2007 if (rdev2->raid_disk == slot)
2008 return -EEXIST;
2009
2010 rdev->raid_disk = slot;
2011 if (test_bit(In_sync, &rdev->flags))
2012 rdev->saved_raid_disk = slot;
2013 else
2014 rdev->saved_raid_disk = -1;
2015 err = rdev->mddev->pers->
2016 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002017 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002018 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002019 return err;
Neil Brown52664732008-06-28 08:31:44 +10002020 } else
2021 sysfs_notify(&rdev->kobj, NULL, "state");
Neil Brown6c2fce22008-06-28 08:31:31 +10002022 sprintf(nm, "rd%d", rdev->raid_disk);
2023 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2024 printk(KERN_WARNING
2025 "md: cannot register "
2026 "%s for %s\n",
2027 nm, mdname(rdev->mddev));
2028
2029 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002030 } else {
2031 if (slot >= rdev->mddev->raid_disks)
2032 return -ENOSPC;
2033 rdev->raid_disk = slot;
2034 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002035 clear_bit(Faulty, &rdev->flags);
2036 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002037 set_bit(In_sync, &rdev->flags);
Neil Brown52664732008-06-28 08:31:44 +10002038 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc303da62008-02-06 01:39:51 -08002039 }
NeilBrown014236d2006-01-06 00:20:55 -08002040 return len;
2041}
2042
2043
2044static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002045__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002046
NeilBrown93c8cad2006-01-06 00:20:56 -08002047static ssize_t
2048offset_show(mdk_rdev_t *rdev, char *page)
2049{
Andrew Morton6961ece2006-01-06 00:20:59 -08002050 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002051}
2052
2053static ssize_t
2054offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2055{
2056 char *e;
2057 unsigned long long offset = simple_strtoull(buf, &e, 10);
2058 if (e==buf || (*e && *e != '\n'))
2059 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002060 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002061 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002062 if (rdev->size && rdev->mddev->external)
2063 /* Must set offset before size, so overlap checks
2064 * can be sane */
2065 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002066 rdev->data_offset = offset;
2067 return len;
2068}
2069
2070static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002071__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002072
NeilBrown83303b62006-01-06 00:21:06 -08002073static ssize_t
2074rdev_size_show(mdk_rdev_t *rdev, char *page)
2075{
2076 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2077}
2078
NeilBrownc5d79ad2008-02-06 01:39:54 -08002079static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2080{
2081 /* check if two start/length pairs overlap */
2082 if (s1+l1 <= s2)
2083 return 0;
2084 if (s2+l2 <= s1)
2085 return 0;
2086 return 1;
2087}
2088
NeilBrown83303b62006-01-06 00:21:06 -08002089static ssize_t
2090rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2091{
Neil Brownd7027452008-07-12 10:37:50 +10002092 unsigned long long size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002093 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002094 mddev_t *my_mddev = rdev->mddev;
2095
Neil Brownd7027452008-07-12 10:37:50 +10002096 if (strict_strtoull(buf, 10, &size) < 0)
2097 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002098 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002099 if (my_mddev->persistent) {
2100 size = super_types[my_mddev->major_version].
Andre Noll15f4a5f2008-07-21 14:42:12 +10002101 rdev_size_change(rdev, size * 2);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002102 if (!size)
2103 return -EBUSY;
2104 } else if (!size) {
2105 size = (rdev->bdev->bd_inode->i_size >> 10);
2106 size -= rdev->data_offset/2;
2107 }
Chris Webb0cd17fe2008-06-28 08:31:46 +10002108 }
Chris Webb7d3c6f82008-10-13 11:55:11 +11002109 if (size < my_mddev->size)
2110 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002111
NeilBrown83303b62006-01-06 00:21:06 -08002112 rdev->size = size;
Neil Brownd7027452008-07-12 10:37:50 +10002113 if (size > oldsize && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002114 /* need to check that all other rdevs with the same ->bdev
2115 * do not overlap. We need to unlock the mddev to avoid
2116 * a deadlock. We have already changed rdev->size, and if
2117 * we have to change it back, we will have the lock again.
2118 */
2119 mddev_t *mddev;
2120 int overlap = 0;
2121 struct list_head *tmp, *tmp2;
2122
NeilBrown27c529b2008-03-04 14:29:33 -08002123 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002124 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002125 mdk_rdev_t *rdev2;
2126
2127 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002128 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002129 if (test_bit(AllReserved, &rdev2->flags) ||
2130 (rdev->bdev == rdev2->bdev &&
2131 rdev != rdev2 &&
Andre Nolld07bd3b2008-07-21 14:42:07 +10002132 overlaps(rdev->data_offset, rdev->size * 2,
2133 rdev2->data_offset,
2134 rdev2->size * 2))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002135 overlap = 1;
2136 break;
2137 }
2138 mddev_unlock(mddev);
2139 if (overlap) {
2140 mddev_put(mddev);
2141 break;
2142 }
2143 }
NeilBrown27c529b2008-03-04 14:29:33 -08002144 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002145 if (overlap) {
2146 /* Someone else could have slipped in a size
2147 * change here, but doing so is just silly.
2148 * We put oldsize back because we *know* it is
2149 * safe, and trust userspace not to race with
2150 * itself
2151 */
2152 rdev->size = oldsize;
2153 return -EBUSY;
2154 }
2155 }
NeilBrown83303b62006-01-06 00:21:06 -08002156 return len;
2157}
2158
2159static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002160__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002161
NeilBrown86e6ffd2005-11-08 21:39:24 -08002162static struct attribute *rdev_default_attrs[] = {
2163 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002164 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002165 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002166 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002167 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002168 NULL,
2169};
2170static ssize_t
2171rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2172{
2173 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2174 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002175 mddev_t *mddev = rdev->mddev;
2176 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002177
2178 if (!entry->show)
2179 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002180
2181 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2182 if (!rv) {
2183 if (rdev->mddev == NULL)
2184 rv = -EBUSY;
2185 else
2186 rv = entry->show(rdev, page);
2187 mddev_unlock(mddev);
2188 }
2189 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002190}
2191
2192static ssize_t
2193rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2194 const char *page, size_t length)
2195{
2196 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2197 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002198 ssize_t rv;
2199 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002200
2201 if (!entry->store)
2202 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002203 if (!capable(CAP_SYS_ADMIN))
2204 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002205 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002206 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002207 if (rdev->mddev == NULL)
2208 rv = -EBUSY;
2209 else
2210 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002211 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002212 }
2213 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002214}
2215
2216static void rdev_free(struct kobject *ko)
2217{
2218 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2219 kfree(rdev);
2220}
2221static struct sysfs_ops rdev_sysfs_ops = {
2222 .show = rdev_attr_show,
2223 .store = rdev_attr_store,
2224};
2225static struct kobj_type rdev_ktype = {
2226 .release = rdev_free,
2227 .sysfs_ops = &rdev_sysfs_ops,
2228 .default_attrs = rdev_default_attrs,
2229};
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231/*
2232 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2233 *
2234 * mark the device faulty if:
2235 *
2236 * - the device is nonexistent (zero size)
2237 * - the device has no valid superblock
2238 *
2239 * a faulty rdev _never_ has rdev->sb set.
2240 */
2241static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2242{
2243 char b[BDEVNAME_SIZE];
2244 int err;
2245 mdk_rdev_t *rdev;
2246 sector_t size;
2247
NeilBrown9ffae0c2006-01-06 00:20:32 -08002248 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (!rdev) {
2250 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2251 return ERR_PTR(-ENOMEM);
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 if ((err = alloc_disk_sb(rdev)))
2255 goto abort_free;
2256
NeilBrownc5d79ad2008-02-06 01:39:54 -08002257 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (err)
2259 goto abort_free;
2260
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002261 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002264 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002265 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002266 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002268 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002270 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002271 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2274 if (!size) {
2275 printk(KERN_WARNING
2276 "md: %s has zero or unknown size, marking faulty!\n",
2277 bdevname(rdev->bdev,b));
2278 err = -EINVAL;
2279 goto abort_free;
2280 }
2281
2282 if (super_format >= 0) {
2283 err = super_types[super_format].
2284 load_super(rdev, NULL, super_minor);
2285 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002286 printk(KERN_WARNING
2287 "md: %s does not have a valid v%d.%d "
2288 "superblock, not importing!\n",
2289 bdevname(rdev->bdev,b),
2290 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto abort_free;
2292 }
2293 if (err < 0) {
2294 printk(KERN_WARNING
2295 "md: could not read %s's sb, not importing!\n",
2296 bdevname(rdev->bdev,b));
2297 goto abort_free;
2298 }
2299 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002302 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 return rdev;
2305
2306abort_free:
2307 if (rdev->sb_page) {
2308 if (rdev->bdev)
2309 unlock_rdev(rdev);
2310 free_disk_sb(rdev);
2311 }
2312 kfree(rdev);
2313 return ERR_PTR(err);
2314}
2315
2316/*
2317 * Check a full RAID array for plausibility
2318 */
2319
2320
NeilBrowna757e642005-04-16 15:26:42 -07002321static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 int i;
2324 struct list_head *tmp;
2325 mdk_rdev_t *rdev, *freshest;
2326 char b[BDEVNAME_SIZE];
2327
2328 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002329 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 switch (super_types[mddev->major_version].
2331 load_super(rdev, freshest, mddev->minor_version)) {
2332 case 1:
2333 freshest = rdev;
2334 break;
2335 case 0:
2336 break;
2337 default:
2338 printk( KERN_ERR \
2339 "md: fatal superblock inconsistency in %s"
2340 " -- removing from array\n",
2341 bdevname(rdev->bdev,b));
2342 kick_rdev_from_array(rdev);
2343 }
2344
2345
2346 super_types[mddev->major_version].
2347 validate_super(mddev, freshest);
2348
2349 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002350 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (rdev != freshest)
2352 if (super_types[mddev->major_version].
2353 validate_super(mddev, rdev)) {
2354 printk(KERN_WARNING "md: kicking non-fresh %s"
2355 " from array!\n",
2356 bdevname(rdev->bdev,b));
2357 kick_rdev_from_array(rdev);
2358 continue;
2359 }
2360 if (mddev->level == LEVEL_MULTIPATH) {
2361 rdev->desc_nr = i++;
2362 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002363 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002364 } else if (rdev->raid_disk >= mddev->raid_disks) {
2365 rdev->raid_disk = -1;
2366 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368 }
2369
2370
2371
2372 if (mddev->recovery_cp != MaxSector &&
2373 mddev->level >= 1)
2374 printk(KERN_ERR "md: %s: raid array is not clean"
2375 " -- starting background reconstruction\n",
2376 mdname(mddev));
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
NeilBrown19052c02008-08-05 15:54:13 +10002380static void md_safemode_timeout(unsigned long data);
2381
NeilBrowneae17012005-11-08 21:39:23 -08002382static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002383safe_delay_show(mddev_t *mddev, char *page)
2384{
2385 int msec = (mddev->safemode_delay*1000)/HZ;
2386 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2387}
2388static ssize_t
2389safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2390{
2391 int scale=1;
2392 int dot=0;
2393 int i;
2394 unsigned long msec;
2395 char buf[30];
2396 char *e;
2397 /* remove a period, and count digits after it */
2398 if (len >= sizeof(buf))
2399 return -EINVAL;
2400 strlcpy(buf, cbuf, len);
2401 buf[len] = 0;
2402 for (i=0; i<len; i++) {
2403 if (dot) {
2404 if (isdigit(buf[i])) {
2405 buf[i-1] = buf[i];
2406 scale *= 10;
2407 }
2408 buf[i] = 0;
2409 } else if (buf[i] == '.') {
2410 dot=1;
2411 buf[i] = 0;
2412 }
2413 }
2414 msec = simple_strtoul(buf, &e, 10);
2415 if (e == buf || (*e && *e != '\n'))
2416 return -EINVAL;
2417 msec = (msec * 1000) / scale;
2418 if (msec == 0)
2419 mddev->safemode_delay = 0;
2420 else {
NeilBrown19052c02008-08-05 15:54:13 +10002421 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002422 mddev->safemode_delay = (msec*HZ)/1000;
2423 if (mddev->safemode_delay == 0)
2424 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002425 if (mddev->safemode_delay < old_delay)
2426 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002427 }
2428 return len;
2429}
2430static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002431__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002432
2433static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002434level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002435{
NeilBrown2604b702006-01-06 00:20:36 -08002436 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002437 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002438 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002439 else if (mddev->clevel[0])
2440 return sprintf(page, "%s\n", mddev->clevel);
2441 else if (mddev->level != LEVEL_NONE)
2442 return sprintf(page, "%d\n", mddev->level);
2443 else
2444 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002445}
2446
NeilBrownd9d166c2006-01-06 00:20:51 -08002447static ssize_t
2448level_store(mddev_t *mddev, const char *buf, size_t len)
2449{
NeilBrown20a49ff2008-02-06 01:39:57 -08002450 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002451 if (mddev->pers)
2452 return -EBUSY;
2453 if (len == 0)
2454 return 0;
2455 if (len >= sizeof(mddev->clevel))
2456 return -ENOSPC;
2457 strncpy(mddev->clevel, buf, len);
2458 if (mddev->clevel[len-1] == '\n')
2459 len--;
2460 mddev->clevel[len] = 0;
2461 mddev->level = LEVEL_NONE;
2462 return rv;
2463}
2464
2465static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002466__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002467
NeilBrownd4dbd022006-06-26 00:27:59 -07002468
2469static ssize_t
2470layout_show(mddev_t *mddev, char *page)
2471{
2472 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002473 if (mddev->reshape_position != MaxSector &&
2474 mddev->layout != mddev->new_layout)
2475 return sprintf(page, "%d (%d)\n",
2476 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002477 return sprintf(page, "%d\n", mddev->layout);
2478}
2479
2480static ssize_t
2481layout_store(mddev_t *mddev, const char *buf, size_t len)
2482{
2483 char *e;
2484 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002485
2486 if (!*buf || (*e && *e != '\n'))
2487 return -EINVAL;
2488
NeilBrown08a02ec2007-05-09 02:35:38 -07002489 if (mddev->pers)
2490 return -EBUSY;
2491 if (mddev->reshape_position != MaxSector)
2492 mddev->new_layout = n;
2493 else
2494 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002495 return len;
2496}
2497static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002498__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002499
2500
NeilBrowneae17012005-11-08 21:39:23 -08002501static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002502raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002503{
NeilBrownbb636542005-11-08 21:39:45 -08002504 if (mddev->raid_disks == 0)
2505 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002506 if (mddev->reshape_position != MaxSector &&
2507 mddev->delta_disks != 0)
2508 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2509 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002510 return sprintf(page, "%d\n", mddev->raid_disks);
2511}
2512
NeilBrownda943b92006-01-06 00:20:54 -08002513static int update_raid_disks(mddev_t *mddev, int raid_disks);
2514
2515static ssize_t
2516raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2517{
NeilBrownda943b92006-01-06 00:20:54 -08002518 char *e;
2519 int rv = 0;
2520 unsigned long n = simple_strtoul(buf, &e, 10);
2521
2522 if (!*buf || (*e && *e != '\n'))
2523 return -EINVAL;
2524
2525 if (mddev->pers)
2526 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002527 else if (mddev->reshape_position != MaxSector) {
2528 int olddisks = mddev->raid_disks - mddev->delta_disks;
2529 mddev->delta_disks = n - olddisks;
2530 mddev->raid_disks = n;
2531 } else
NeilBrownda943b92006-01-06 00:20:54 -08002532 mddev->raid_disks = n;
2533 return rv ? rv : len;
2534}
2535static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002536__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002537
NeilBrown24dd4692005-11-08 21:39:26 -08002538static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002539chunk_size_show(mddev_t *mddev, char *page)
2540{
NeilBrown08a02ec2007-05-09 02:35:38 -07002541 if (mddev->reshape_position != MaxSector &&
2542 mddev->chunk_size != mddev->new_chunk)
2543 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2544 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002545 return sprintf(page, "%d\n", mddev->chunk_size);
2546}
2547
2548static ssize_t
2549chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2550{
2551 /* can only set chunk_size if array is not yet active */
2552 char *e;
2553 unsigned long n = simple_strtoul(buf, &e, 10);
2554
NeilBrown3b343802006-01-06 00:20:47 -08002555 if (!*buf || (*e && *e != '\n'))
2556 return -EINVAL;
2557
NeilBrown08a02ec2007-05-09 02:35:38 -07002558 if (mddev->pers)
2559 return -EBUSY;
2560 else if (mddev->reshape_position != MaxSector)
2561 mddev->new_chunk = n;
2562 else
2563 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002564 return len;
2565}
2566static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002567__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002568
NeilBrowna94213b2006-06-26 00:28:00 -07002569static ssize_t
2570resync_start_show(mddev_t *mddev, char *page)
2571{
2572 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2573}
2574
2575static ssize_t
2576resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2577{
NeilBrowna94213b2006-06-26 00:28:00 -07002578 char *e;
2579 unsigned long long n = simple_strtoull(buf, &e, 10);
2580
2581 if (mddev->pers)
2582 return -EBUSY;
2583 if (!*buf || (*e && *e != '\n'))
2584 return -EINVAL;
2585
2586 mddev->recovery_cp = n;
2587 return len;
2588}
2589static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002590__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002591
NeilBrown9e653b62006-06-26 00:27:58 -07002592/*
2593 * The array state can be:
2594 *
2595 * clear
2596 * No devices, no size, no level
2597 * Equivalent to STOP_ARRAY ioctl
2598 * inactive
2599 * May have some settings, but array is not active
2600 * all IO results in error
2601 * When written, doesn't tear down array, but just stops it
2602 * suspended (not supported yet)
2603 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002604 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002605 * readonly
2606 * no resync can happen. no superblocks get written.
2607 * write requests fail
2608 * read-auto
2609 * like readonly, but behaves like 'clean' on a write request.
2610 *
2611 * clean - no pending writes, but otherwise active.
2612 * When written to inactive array, starts without resync
2613 * If a write request arrives then
2614 * if metadata is known, mark 'dirty' and switch to 'active'.
2615 * if not known, block and switch to write-pending
2616 * If written to an active array that has pending writes, then fails.
2617 * active
2618 * fully active: IO and resync can be happening.
2619 * When written to inactive array, starts with resync
2620 *
2621 * write-pending
2622 * clean, but writes are blocked waiting for 'active' to be written.
2623 *
2624 * active-idle
2625 * like active, but no writes have been seen for a while (100msec).
2626 *
2627 */
2628enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2629 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002630static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002631 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2632 "write-pending", "active-idle", NULL };
2633
2634static int match_word(const char *word, char **list)
2635{
2636 int n;
2637 for (n=0; list[n]; n++)
2638 if (cmd_match(word, list[n]))
2639 break;
2640 return n;
2641}
2642
2643static ssize_t
2644array_state_show(mddev_t *mddev, char *page)
2645{
2646 enum array_state st = inactive;
2647
2648 if (mddev->pers)
2649 switch(mddev->ro) {
2650 case 1:
2651 st = readonly;
2652 break;
2653 case 2:
2654 st = read_auto;
2655 break;
2656 case 0:
2657 if (mddev->in_sync)
2658 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002659 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2660 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002661 else if (mddev->safemode)
2662 st = active_idle;
2663 else
2664 st = active;
2665 }
2666 else {
2667 if (list_empty(&mddev->disks) &&
2668 mddev->raid_disks == 0 &&
2669 mddev->size == 0)
2670 st = clear;
2671 else
2672 st = inactive;
2673 }
2674 return sprintf(page, "%s\n", array_states[st]);
2675}
2676
Neil Browndf5b20c2008-07-11 22:02:22 +10002677static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002678static int do_md_run(mddev_t * mddev);
2679static int restart_array(mddev_t *mddev);
2680
2681static ssize_t
2682array_state_store(mddev_t *mddev, const char *buf, size_t len)
2683{
2684 int err = -EINVAL;
2685 enum array_state st = match_word(buf, array_states);
2686 switch(st) {
2687 case bad_word:
2688 break;
2689 case clear:
2690 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10002691 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08002692 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002693 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002694 break;
2695 case inactive:
2696 /* stopping an active array */
2697 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10002698 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002699 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002700 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002701 } else
2702 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002703 break;
2704 case suspended:
2705 break; /* not supported yet */
2706 case readonly:
2707 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002708 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002709 else {
2710 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002711 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002712 err = do_md_run(mddev);
2713 }
2714 break;
2715 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002716 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11002717 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10002718 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11002719 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07002720 err = restart_array(mddev);
2721 if (err == 0) {
2722 mddev->ro = 2;
2723 set_disk_ro(mddev->gendisk, 0);
2724 }
NeilBrown9e653b62006-06-26 00:27:58 -07002725 } else {
2726 mddev->ro = 2;
2727 err = do_md_run(mddev);
2728 }
2729 break;
2730 case clean:
2731 if (mddev->pers) {
2732 restart_array(mddev);
2733 spin_lock_irq(&mddev->write_lock);
2734 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002735 if (mddev->in_sync == 0) {
2736 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002737 if (mddev->safemode == 1)
2738 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002739 if (mddev->persistent)
2740 set_bit(MD_CHANGE_CLEAN,
2741 &mddev->flags);
2742 }
2743 err = 0;
2744 } else
2745 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002746 spin_unlock_irq(&mddev->write_lock);
2747 } else {
2748 mddev->ro = 0;
2749 mddev->recovery_cp = MaxSector;
2750 err = do_md_run(mddev);
2751 }
2752 break;
2753 case active:
2754 if (mddev->pers) {
2755 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002756 if (mddev->external)
2757 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002758 wake_up(&mddev->sb_wait);
2759 err = 0;
2760 } else {
2761 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002762 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002763 err = do_md_run(mddev);
2764 }
2765 break;
2766 case write_pending:
2767 case active_idle:
2768 /* these cannot be set */
2769 break;
2770 }
2771 if (err)
2772 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002773 else {
2774 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown9e653b62006-06-26 00:27:58 -07002775 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002776 }
NeilBrown9e653b62006-06-26 00:27:58 -07002777}
NeilBrown80ca3a42006-07-10 04:44:18 -07002778static struct md_sysfs_entry md_array_state =
2779__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002780
NeilBrown6d7ff732006-01-06 00:21:16 -08002781static ssize_t
2782null_show(mddev_t *mddev, char *page)
2783{
2784 return -EINVAL;
2785}
2786
2787static ssize_t
2788new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2789{
2790 /* buf must be %d:%d\n? giving major and minor numbers */
2791 /* The new device is added to the array.
2792 * If the array has a persistent superblock, we read the
2793 * superblock to initialise info and check validity.
2794 * Otherwise, only checking done is that in bind_rdev_to_array,
2795 * which mainly checks size.
2796 */
2797 char *e;
2798 int major = simple_strtoul(buf, &e, 10);
2799 int minor;
2800 dev_t dev;
2801 mdk_rdev_t *rdev;
2802 int err;
2803
2804 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2805 return -EINVAL;
2806 minor = simple_strtoul(e+1, &e, 10);
2807 if (*e && *e != '\n')
2808 return -EINVAL;
2809 dev = MKDEV(major, minor);
2810 if (major != MAJOR(dev) ||
2811 minor != MINOR(dev))
2812 return -EOVERFLOW;
2813
2814
2815 if (mddev->persistent) {
2816 rdev = md_import_device(dev, mddev->major_version,
2817 mddev->minor_version);
2818 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2819 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2820 mdk_rdev_t, same_set);
2821 err = super_types[mddev->major_version]
2822 .load_super(rdev, rdev0, mddev->minor_version);
2823 if (err < 0)
2824 goto out;
2825 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002826 } else if (mddev->external)
2827 rdev = md_import_device(dev, -2, -1);
2828 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002829 rdev = md_import_device(dev, -1, -1);
2830
2831 if (IS_ERR(rdev))
2832 return PTR_ERR(rdev);
2833 err = bind_rdev_to_array(rdev, mddev);
2834 out:
2835 if (err)
2836 export_rdev(rdev);
2837 return err ? err : len;
2838}
2839
2840static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002841__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002842
2843static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002844bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2845{
2846 char *end;
2847 unsigned long chunk, end_chunk;
2848
2849 if (!mddev->bitmap)
2850 goto out;
2851 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2852 while (*buf) {
2853 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2854 if (buf == end) break;
2855 if (*end == '-') { /* range */
2856 buf = end + 1;
2857 end_chunk = simple_strtoul(buf, &end, 0);
2858 if (buf == end) break;
2859 }
2860 if (*end && !isspace(*end)) break;
2861 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2862 buf = end;
2863 while (isspace(*buf)) buf++;
2864 }
2865 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2866out:
2867 return len;
2868}
2869
2870static struct md_sysfs_entry md_bitmap =
2871__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2872
2873static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002874size_show(mddev_t *mddev, char *page)
2875{
2876 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2877}
2878
Andre Nolld71f9f82008-07-11 22:02:22 +10002879static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08002880
2881static ssize_t
2882size_store(mddev_t *mddev, const char *buf, size_t len)
2883{
2884 /* If array is inactive, we can reduce the component size, but
2885 * not increase it (except from 0).
2886 * If array is active, we can try an on-line resize
2887 */
2888 char *e;
2889 int err = 0;
2890 unsigned long long size = simple_strtoull(buf, &e, 10);
2891 if (!*buf || *buf == '\n' ||
2892 (*e && *e != '\n'))
2893 return -EINVAL;
2894
2895 if (mddev->pers) {
Andre Nolld71f9f82008-07-11 22:02:22 +10002896 err = update_size(mddev, size * 2);
NeilBrown850b2b42006-10-03 01:15:46 -07002897 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002898 } else {
2899 if (mddev->size == 0 ||
2900 mddev->size > size)
2901 mddev->size = size;
2902 else
2903 err = -ENOSPC;
2904 }
2905 return err ? err : len;
2906}
2907
2908static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002909__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002910
NeilBrown8bb93aa2006-01-06 00:20:50 -08002911
2912/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002913 * This is one of
2914 * 'none' for arrays with no metadata (good luck...)
2915 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002916 * or N.M for internally known formats
2917 */
2918static ssize_t
2919metadata_show(mddev_t *mddev, char *page)
2920{
2921 if (mddev->persistent)
2922 return sprintf(page, "%d.%d\n",
2923 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002924 else if (mddev->external)
2925 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002926 else
2927 return sprintf(page, "none\n");
2928}
2929
2930static ssize_t
2931metadata_store(mddev_t *mddev, const char *buf, size_t len)
2932{
2933 int major, minor;
2934 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11002935 /* Changing the details of 'external' metadata is
2936 * always permitted. Otherwise there must be
2937 * no devices attached to the array.
2938 */
2939 if (mddev->external && strncmp(buf, "external:", 9) == 0)
2940 ;
2941 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08002942 return -EBUSY;
2943
2944 if (cmd_match(buf, "none")) {
2945 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002946 mddev->external = 0;
2947 mddev->major_version = 0;
2948 mddev->minor_version = 90;
2949 return len;
2950 }
2951 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002952 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002953 if (namelen >= sizeof(mddev->metadata_type))
2954 namelen = sizeof(mddev->metadata_type)-1;
2955 strncpy(mddev->metadata_type, buf+9, namelen);
2956 mddev->metadata_type[namelen] = 0;
2957 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2958 mddev->metadata_type[--namelen] = 0;
2959 mddev->persistent = 0;
2960 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002961 mddev->major_version = 0;
2962 mddev->minor_version = 90;
2963 return len;
2964 }
2965 major = simple_strtoul(buf, &e, 10);
2966 if (e==buf || *e != '.')
2967 return -EINVAL;
2968 buf = e+1;
2969 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002970 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002971 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002972 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002973 return -ENOENT;
2974 mddev->major_version = major;
2975 mddev->minor_version = minor;
2976 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002977 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002978 return len;
2979}
2980
2981static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002982__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002983
NeilBrowna35b0d62006-01-06 00:20:49 -08002984static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002985action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002986{
NeilBrown7eec3142005-11-08 21:39:44 -08002987 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002988 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002989 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002990 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2991 type = "reshape";
2992 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002993 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2994 type = "resync";
2995 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2996 type = "check";
2997 else
2998 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10002999 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003000 type = "recover";
3001 }
3002 return sprintf(page, "%s\n", type);
3003}
3004
3005static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003006action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003007{
NeilBrown7eec3142005-11-08 21:39:44 -08003008 if (!mddev->pers || !mddev->pers->sync_request)
3009 return -EINVAL;
3010
NeilBrownbce74da2006-01-06 00:20:41 -08003011 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003012 if (mddev->sync_thread) {
3013 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3014 md_unregister_thread(mddev->sync_thread);
3015 mddev->sync_thread = NULL;
3016 mddev->recovery = 0;
3017 }
NeilBrown03c902e2006-01-06 00:20:46 -08003018 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3019 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003020 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003021 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003022 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003023 else if (cmd_match(page, "recover")) {
3024 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3025 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3026 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003027 int err;
3028 if (mddev->pers->start_reshape == NULL)
3029 return -EINVAL;
3030 err = mddev->pers->start_reshape(mddev);
3031 if (err)
3032 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003033 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003034 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003035 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003036 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003037 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003038 return -EINVAL;
3039 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3040 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003041 }
NeilBrown03c902e2006-01-06 00:20:46 -08003042 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003043 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10003044 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08003045 return len;
3046}
3047
NeilBrown9d888832005-11-08 21:39:26 -08003048static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003049mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003050{
3051 return sprintf(page, "%llu\n",
3052 (unsigned long long) mddev->resync_mismatches);
3053}
3054
NeilBrown80ca3a42006-07-10 04:44:18 -07003055static struct md_sysfs_entry md_scan_mode =
3056__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003057
NeilBrown96de1e62005-11-08 21:39:39 -08003058
NeilBrown80ca3a42006-07-10 04:44:18 -07003059static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003060
NeilBrown88202a02006-01-06 00:21:36 -08003061static ssize_t
3062sync_min_show(mddev_t *mddev, char *page)
3063{
3064 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3065 mddev->sync_speed_min ? "local": "system");
3066}
3067
3068static ssize_t
3069sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3070{
3071 int min;
3072 char *e;
3073 if (strncmp(buf, "system", 6)==0) {
3074 mddev->sync_speed_min = 0;
3075 return len;
3076 }
3077 min = simple_strtoul(buf, &e, 10);
3078 if (buf == e || (*e && *e != '\n') || min <= 0)
3079 return -EINVAL;
3080 mddev->sync_speed_min = min;
3081 return len;
3082}
3083
3084static struct md_sysfs_entry md_sync_min =
3085__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3086
3087static ssize_t
3088sync_max_show(mddev_t *mddev, char *page)
3089{
3090 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3091 mddev->sync_speed_max ? "local": "system");
3092}
3093
3094static ssize_t
3095sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3096{
3097 int max;
3098 char *e;
3099 if (strncmp(buf, "system", 6)==0) {
3100 mddev->sync_speed_max = 0;
3101 return len;
3102 }
3103 max = simple_strtoul(buf, &e, 10);
3104 if (buf == e || (*e && *e != '\n') || max <= 0)
3105 return -EINVAL;
3106 mddev->sync_speed_max = max;
3107 return len;
3108}
3109
3110static struct md_sysfs_entry md_sync_max =
3111__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3112
Iustin Popd7f3d292007-10-16 23:30:54 -07003113static ssize_t
3114degraded_show(mddev_t *mddev, char *page)
3115{
3116 return sprintf(page, "%d\n", mddev->degraded);
3117}
3118static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003119
3120static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003121sync_force_parallel_show(mddev_t *mddev, char *page)
3122{
3123 return sprintf(page, "%d\n", mddev->parallel_resync);
3124}
3125
3126static ssize_t
3127sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3128{
3129 long n;
3130
3131 if (strict_strtol(buf, 10, &n))
3132 return -EINVAL;
3133
3134 if (n != 0 && n != 1)
3135 return -EINVAL;
3136
3137 mddev->parallel_resync = n;
3138
3139 if (mddev->sync_thread)
3140 wake_up(&resync_wait);
3141
3142 return len;
3143}
3144
3145/* force parallel resync, even with shared block devices */
3146static struct md_sysfs_entry md_sync_force_parallel =
3147__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3148 sync_force_parallel_show, sync_force_parallel_store);
3149
3150static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003151sync_speed_show(mddev_t *mddev, char *page)
3152{
3153 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003154 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3155 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003156 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003157 db = resync - mddev->resync_mark_cnt;
3158 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003159}
3160
NeilBrown80ca3a42006-07-10 04:44:18 -07003161static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003162
3163static ssize_t
3164sync_completed_show(mddev_t *mddev, char *page)
3165{
3166 unsigned long max_blocks, resync;
3167
3168 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3169 max_blocks = mddev->resync_max_sectors;
3170 else
3171 max_blocks = mddev->size << 1;
3172
3173 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3174 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3175}
3176
NeilBrown80ca3a42006-07-10 04:44:18 -07003177static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003178
NeilBrowne464eaf2006-03-27 01:18:14 -08003179static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003180min_sync_show(mddev_t *mddev, char *page)
3181{
3182 return sprintf(page, "%llu\n",
3183 (unsigned long long)mddev->resync_min);
3184}
3185static ssize_t
3186min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3187{
3188 unsigned long long min;
3189 if (strict_strtoull(buf, 10, &min))
3190 return -EINVAL;
3191 if (min > mddev->resync_max)
3192 return -EINVAL;
3193 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3194 return -EBUSY;
3195
3196 /* Must be a multiple of chunk_size */
3197 if (mddev->chunk_size) {
3198 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3199 return -EINVAL;
3200 }
3201 mddev->resync_min = min;
3202
3203 return len;
3204}
3205
3206static struct md_sysfs_entry md_min_sync =
3207__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3208
3209static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003210max_sync_show(mddev_t *mddev, char *page)
3211{
3212 if (mddev->resync_max == MaxSector)
3213 return sprintf(page, "max\n");
3214 else
3215 return sprintf(page, "%llu\n",
3216 (unsigned long long)mddev->resync_max);
3217}
3218static ssize_t
3219max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3220{
3221 if (strncmp(buf, "max", 3) == 0)
3222 mddev->resync_max = MaxSector;
3223 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003224 unsigned long long max;
3225 if (strict_strtoull(buf, 10, &max))
3226 return -EINVAL;
3227 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003228 return -EINVAL;
3229 if (max < mddev->resync_max &&
3230 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3231 return -EBUSY;
3232
3233 /* Must be a multiple of chunk_size */
3234 if (mddev->chunk_size) {
3235 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3236 return -EINVAL;
3237 }
3238 mddev->resync_max = max;
3239 }
3240 wake_up(&mddev->recovery_wait);
3241 return len;
3242}
3243
3244static struct md_sysfs_entry md_max_sync =
3245__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3246
3247static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003248suspend_lo_show(mddev_t *mddev, char *page)
3249{
3250 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3251}
3252
3253static ssize_t
3254suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3255{
3256 char *e;
3257 unsigned long long new = simple_strtoull(buf, &e, 10);
3258
3259 if (mddev->pers->quiesce == NULL)
3260 return -EINVAL;
3261 if (buf == e || (*e && *e != '\n'))
3262 return -EINVAL;
3263 if (new >= mddev->suspend_hi ||
3264 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3265 mddev->suspend_lo = new;
3266 mddev->pers->quiesce(mddev, 2);
3267 return len;
3268 } else
3269 return -EINVAL;
3270}
3271static struct md_sysfs_entry md_suspend_lo =
3272__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3273
3274
3275static ssize_t
3276suspend_hi_show(mddev_t *mddev, char *page)
3277{
3278 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3279}
3280
3281static ssize_t
3282suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3283{
3284 char *e;
3285 unsigned long long new = simple_strtoull(buf, &e, 10);
3286
3287 if (mddev->pers->quiesce == NULL)
3288 return -EINVAL;
3289 if (buf == e || (*e && *e != '\n'))
3290 return -EINVAL;
3291 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3292 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3293 mddev->suspend_hi = new;
3294 mddev->pers->quiesce(mddev, 1);
3295 mddev->pers->quiesce(mddev, 0);
3296 return len;
3297 } else
3298 return -EINVAL;
3299}
3300static struct md_sysfs_entry md_suspend_hi =
3301__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3302
NeilBrown08a02ec2007-05-09 02:35:38 -07003303static ssize_t
3304reshape_position_show(mddev_t *mddev, char *page)
3305{
3306 if (mddev->reshape_position != MaxSector)
3307 return sprintf(page, "%llu\n",
3308 (unsigned long long)mddev->reshape_position);
3309 strcpy(page, "none\n");
3310 return 5;
3311}
3312
3313static ssize_t
3314reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3315{
3316 char *e;
3317 unsigned long long new = simple_strtoull(buf, &e, 10);
3318 if (mddev->pers)
3319 return -EBUSY;
3320 if (buf == e || (*e && *e != '\n'))
3321 return -EINVAL;
3322 mddev->reshape_position = new;
3323 mddev->delta_disks = 0;
3324 mddev->new_level = mddev->level;
3325 mddev->new_layout = mddev->layout;
3326 mddev->new_chunk = mddev->chunk_size;
3327 return len;
3328}
3329
3330static struct md_sysfs_entry md_reshape_position =
3331__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3332 reshape_position_store);
3333
NeilBrowne464eaf2006-03-27 01:18:14 -08003334
NeilBrowneae17012005-11-08 21:39:23 -08003335static struct attribute *md_default_attrs[] = {
3336 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003337 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003338 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003339 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003340 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003341 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003342 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003343 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003344 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003345 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003346 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003347 NULL,
3348};
3349
3350static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003351 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003352 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003353 &md_sync_min.attr,
3354 &md_sync_max.attr,
3355 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003356 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003357 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003358 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003359 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003360 &md_suspend_lo.attr,
3361 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003362 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003363 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003364 NULL,
3365};
NeilBrown411036f2005-11-08 21:39:40 -08003366static struct attribute_group md_redundancy_group = {
3367 .name = NULL,
3368 .attrs = md_redundancy_attrs,
3369};
3370
NeilBrowneae17012005-11-08 21:39:23 -08003371
3372static ssize_t
3373md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3374{
3375 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3376 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003377 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003378
3379 if (!entry->show)
3380 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003381 rv = mddev_lock(mddev);
3382 if (!rv) {
3383 rv = entry->show(mddev, page);
3384 mddev_unlock(mddev);
3385 }
NeilBrown96de1e62005-11-08 21:39:39 -08003386 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003387}
3388
3389static ssize_t
3390md_attr_store(struct kobject *kobj, struct attribute *attr,
3391 const char *page, size_t length)
3392{
3393 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3394 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003395 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003396
3397 if (!entry->store)
3398 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003399 if (!capable(CAP_SYS_ADMIN))
3400 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003401 rv = mddev_lock(mddev);
3402 if (!rv) {
3403 rv = entry->store(mddev, page, length);
3404 mddev_unlock(mddev);
3405 }
NeilBrown96de1e62005-11-08 21:39:39 -08003406 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003407}
3408
3409static void md_free(struct kobject *ko)
3410{
3411 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3412 kfree(mddev);
3413}
3414
3415static struct sysfs_ops md_sysfs_ops = {
3416 .show = md_attr_show,
3417 .store = md_attr_store,
3418};
3419static struct kobj_type md_ktype = {
3420 .release = md_free,
3421 .sysfs_ops = &md_sysfs_ops,
3422 .default_attrs = md_default_attrs,
3423};
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425int mdp_major = 0;
3426
3427static struct kobject *md_probe(dev_t dev, int *part, void *data)
3428{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003429 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 mddev_t *mddev = mddev_find(dev);
3431 struct gendisk *disk;
3432 int partitioned = (MAJOR(dev) != MD_MAJOR);
3433 int shift = partitioned ? MdpMinorShift : 0;
3434 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003435 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 if (!mddev)
3438 return NULL;
3439
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003440 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003442 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 mddev_put(mddev);
3444 return NULL;
3445 }
3446 disk = alloc_disk(1 << shift);
3447 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003448 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 mddev_put(mddev);
3450 return NULL;
3451 }
3452 disk->major = MAJOR(dev);
3453 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003454 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003456 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 disk->fops = &md_fops;
3459 disk->private_data = mddev;
3460 disk->queue = mddev->queue;
3461 add_disk(disk);
3462 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003463 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3464 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003465 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003466 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003467 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3468 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003469 else
3470 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return NULL;
3472}
3473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474static void md_safemode_timeout(unsigned long data)
3475{
3476 mddev_t *mddev = (mddev_t *) data;
3477
Neil Brown0fd62b82008-06-28 08:31:36 +10003478 if (!atomic_read(&mddev->writes_pending)) {
3479 mddev->safemode = 1;
3480 if (mddev->external)
Dan Williamsd8e64402008-07-23 13:09:48 -07003481 set_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags);
Neil Brown0fd62b82008-06-28 08:31:36 +10003482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 md_wakeup_thread(mddev->thread);
3484}
3485
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003486static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
3488static int do_md_run(mddev_t * mddev)
3489{
NeilBrown2604b702006-01-06 00:20:36 -08003490 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 int chunk_size;
3492 struct list_head *tmp;
3493 mdk_rdev_t *rdev;
3494 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003495 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 char b[BDEVNAME_SIZE];
3497
NeilBrowna757e642005-04-16 15:26:42 -07003498 if (list_empty(&mddev->disks))
3499 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 if (mddev->pers)
3503 return -EBUSY;
3504
3505 /*
3506 * Analyze all RAID superblock(s)
3507 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003508 if (!mddev->raid_disks) {
3509 if (!mddev->persistent)
3510 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003511 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
NeilBrown2604b702006-01-06 00:20:36 -08003516 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 if (chunk_size > MAX_CHUNK_SIZE) {
3518 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3519 chunk_size, MAX_CHUNK_SIZE);
3520 return -EINVAL;
3521 }
3522 /*
3523 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3524 */
3525 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003526 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return -EINVAL;
3528 }
3529 if (chunk_size < PAGE_SIZE) {
3530 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3531 chunk_size, PAGE_SIZE);
3532 return -EINVAL;
3533 }
3534
3535 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003536 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003537 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 continue;
3539 if (rdev->size < chunk_size / 1024) {
3540 printk(KERN_WARNING
3541 "md: Dev %s smaller than chunk_size:"
3542 " %lluk < %dk\n",
3543 bdevname(rdev->bdev,b),
3544 (unsigned long long)rdev->size,
3545 chunk_size / 1024);
3546 return -EINVAL;
3547 }
3548 }
3549 }
3550
NeilBrownd9d166c2006-01-06 00:20:51 -08003551 if (mddev->level != LEVEL_NONE)
3552 request_module("md-level-%d", mddev->level);
3553 else if (mddev->clevel[0])
3554 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 /*
3557 * Drop all container device buffers, from now on
3558 * the only valid external interface is through the md
3559 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003561 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003562 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 continue;
3564 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003565 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003566
3567 /* perform some consistency tests on the device.
3568 * We don't want the data to overlap the metadata,
3569 * Internal Bitmap issues has handled elsewhere.
3570 */
Andre Noll0f420352008-07-11 22:02:23 +10003571 if (rdev->data_offset < rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003572 if (mddev->size &&
3573 rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +10003574 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003575 printk("md: %s: data overlaps metadata\n",
3576 mdname(mddev));
3577 return -EINVAL;
3578 }
3579 } else {
Andre Noll0f420352008-07-11 22:02:23 +10003580 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07003581 > rdev->data_offset) {
3582 printk("md: %s: metadata overlaps data\n",
3583 mdname(mddev));
3584 return -EINVAL;
3585 }
3586 }
Neil Brown52664732008-06-28 08:31:44 +10003587 sysfs_notify(&rdev->kobj, NULL, "state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 }
3589
3590 md_probe(mddev->unit, NULL, NULL);
3591 disk = mddev->gendisk;
3592 if (!disk)
3593 return -ENOMEM;
3594
3595 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003596 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003597 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003599 if (mddev->level != LEVEL_NONE)
3600 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3601 mddev->level);
3602 else
3603 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3604 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return -EINVAL;
3606 }
NeilBrown2604b702006-01-06 00:20:36 -08003607 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003609 mddev->level = pers->level;
3610 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
NeilBrownf6705572006-03-27 01:18:11 -08003612 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003613 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003614 /* This personality cannot handle reshaping... */
3615 mddev->pers = NULL;
3616 module_put(pers->owner);
3617 return -EINVAL;
3618 }
3619
NeilBrown7dd5e7c2007-02-28 20:11:35 -08003620 if (pers->sync_request) {
3621 /* Warn if this is a potentially silly
3622 * configuration.
3623 */
3624 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3625 mdk_rdev_t *rdev2;
3626 struct list_head *tmp2;
3627 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003628 rdev_for_each(rdev, tmp, mddev) {
3629 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08003630 if (rdev < rdev2 &&
3631 rdev->bdev->bd_contains ==
3632 rdev2->bdev->bd_contains) {
3633 printk(KERN_WARNING
3634 "%s: WARNING: %s appears to be"
3635 " on the same physical disk as"
3636 " %s.\n",
3637 mdname(mddev),
3638 bdevname(rdev->bdev,b),
3639 bdevname(rdev2->bdev,b2));
3640 warned = 1;
3641 }
3642 }
3643 }
3644 if (warned)
3645 printk(KERN_WARNING
3646 "True protection against single-disk"
3647 " failure might be compromised.\n");
3648 }
3649
NeilBrown657390d2005-08-26 18:34:16 -07003650 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003652 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003653 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
NeilBrownf91de922005-11-08 21:39:36 -08003655 if (start_readonly)
3656 mddev->ro = 2; /* read-only, but switch on first write */
3657
NeilBrownb15c2e52006-01-06 00:20:16 -08003658 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003659 if (err)
3660 printk(KERN_ERR "md: pers->run() failed ...\n");
3661 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003662 err = bitmap_create(mddev);
3663 if (err) {
3664 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3665 mdname(mddev), err);
3666 mddev->pers->stop(mddev);
3667 }
3668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 module_put(mddev->pers->owner);
3671 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003672 bitmap_destroy(mddev);
3673 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003675 if (mddev->pers->sync_request) {
3676 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3677 printk(KERN_WARNING
3678 "md: cannot register extra attributes for %s\n",
3679 mdname(mddev));
3680 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003681 mddev->ro = 0;
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 atomic_set(&mddev->writes_pending,0);
3684 mddev->safemode = 0;
3685 mddev->safemode_timer.function = md_safemode_timeout;
3686 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003687 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003689
NeilBrownd089c6a2008-02-06 01:39:59 -08003690 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003691 if (rdev->raid_disk >= 0) {
3692 char nm[20];
3693 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003694 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3695 printk("md: cannot register %s for %s\n",
3696 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
3699 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3700
NeilBrown850b2b42006-10-03 01:15:46 -07003701 if (mddev->flags)
3702 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Andre Nollf233ea52008-07-21 17:05:22 +10003704 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706 /* If we call blk_queue_make_request here, it will
3707 * re-initialise max_sectors etc which may have been
3708 * refined inside -> run. So just set the bits we need to set.
3709 * Most initialisation happended when we called
3710 * blk_queue_make_request(..., md_fail_request)
3711 * earlier.
3712 */
3713 mddev->queue->queuedata = mddev;
3714 mddev->queue->make_request_fn = mddev->pers->make_request;
3715
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003716 /* If there is a partially-recovered drive we need to
3717 * start recovery here. If we leave it to md_check_recovery,
3718 * it will remove the drives and not do the right thing
3719 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003720 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003721 struct list_head *rtmp;
3722 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003723 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003724 if (rdev->raid_disk >= 0 &&
3725 !test_bit(In_sync, &rdev->flags) &&
3726 !test_bit(Faulty, &rdev->flags))
3727 /* complete an interrupted recovery */
3728 spares++;
3729 if (spares && mddev->pers->sync_request) {
3730 mddev->recovery = 0;
3731 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3732 mddev->sync_thread = md_register_thread(md_do_sync,
3733 mddev,
3734 "%s_resync");
3735 if (!mddev->sync_thread) {
3736 printk(KERN_ERR "%s: could not start resync"
3737 " thread...\n",
3738 mdname(mddev));
3739 /* leave the spares where they are, it shouldn't hurt */
3740 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003741 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003742 }
3743 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003744 md_wakeup_thread(mddev->thread);
3745 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003746
Linus Torvalds44ce6292007-05-09 18:51:36 -07003747 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003748 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003749 sysfs_notify(&mddev->kobj, NULL, "array_state");
Neil Brown72a23c22008-06-28 08:31:41 +10003750 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003751 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09003752 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return 0;
3754}
3755
3756static int restart_array(mddev_t *mddev)
3757{
3758 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Andre Noll80fab1d2008-07-11 22:02:21 +10003760 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003762 return -ENXIO;
3763 if (!mddev->pers)
3764 return -EINVAL;
3765 if (!mddev->ro)
3766 return -EBUSY;
3767 mddev->safemode = 0;
3768 mddev->ro = 0;
3769 set_disk_ro(disk, 0);
3770 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3771 mdname(mddev));
3772 /* Kick recovery or resync if necessary */
3773 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3774 md_wakeup_thread(mddev->thread);
3775 md_wakeup_thread(mddev->sync_thread);
3776 sysfs_notify(&mddev->kobj, NULL, "array_state");
3777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
3779
NeilBrownacc55e22006-06-26 00:27:47 -07003780/* similar to deny_write_access, but accounts for our holding a reference
3781 * to the file ourselves */
3782static int deny_bitmap_write_access(struct file * file)
3783{
3784 struct inode *inode = file->f_mapping->host;
3785
3786 spin_lock(&inode->i_lock);
3787 if (atomic_read(&inode->i_writecount) > 1) {
3788 spin_unlock(&inode->i_lock);
3789 return -ETXTBSY;
3790 }
3791 atomic_set(&inode->i_writecount, -1);
3792 spin_unlock(&inode->i_lock);
3793
3794 return 0;
3795}
3796
3797static void restore_bitmap_write_access(struct file *file)
3798{
3799 struct inode *inode = file->f_mapping->host;
3800
3801 spin_lock(&inode->i_lock);
3802 atomic_set(&inode->i_writecount, 1);
3803 spin_unlock(&inode->i_lock);
3804}
3805
NeilBrown9e653b62006-06-26 00:27:58 -07003806/* mode:
3807 * 0 - completely stop and dis-assemble array
3808 * 1 - switch to readonly
3809 * 2 - stop but do not disassemble array
3810 */
Neil Browndf5b20c2008-07-11 22:02:22 +10003811static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 int err = 0;
3814 struct gendisk *disk = mddev->gendisk;
3815
NeilBrownf2ea68c2008-07-21 17:05:25 +10003816 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10003817 printk("md: %s still in use.\n",mdname(mddev));
3818 return -EBUSY;
3819 }
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
3823 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003824 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3826 md_unregister_thread(mddev->sync_thread);
3827 mddev->sync_thread = NULL;
3828 }
3829
3830 del_timer_sync(&mddev->safemode_timer);
3831
NeilBrown9e653b62006-06-26 00:27:58 -07003832 switch(mode) {
3833 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003835 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 goto out;
3837 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003838 break;
3839 case 0: /* disassemble */
3840 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003841 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003842 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 if (mddev->ro)
3844 set_disk_ro(disk, 0);
3845 blk_queue_make_request(mddev->queue, md_fail_request);
3846 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003847 mddev->queue->merge_bvec_fn = NULL;
3848 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003849 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003850 if (mddev->pers->sync_request)
3851 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 module_put(mddev->pers->owner);
3854 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003855 /* tell userspace to handle 'inactive' */
3856 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003857
3858 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07003859 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 if (mddev->ro)
3862 mddev->ro = 0;
3863 }
NeilBrown850b2b42006-10-03 01:15:46 -07003864 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 /* mark array as shutdown cleanly */
3866 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003867 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 }
NeilBrown9e653b62006-06-26 00:27:58 -07003869 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003871 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 }
NeilBrown32a76272005-06-21 17:17:14 -07003873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 /*
3875 * Free resources if final stop
3876 */
NeilBrown9e653b62006-06-26 00:27:58 -07003877 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003878 mdk_rdev_t *rdev;
3879 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3882
NeilBrown978f9462006-02-02 14:28:05 -08003883 bitmap_destroy(mddev);
3884 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003885 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003886 fput(mddev->bitmap_file);
3887 mddev->bitmap_file = NULL;
3888 }
3889 mddev->bitmap_offset = 0;
3890
NeilBrownd089c6a2008-02-06 01:39:59 -08003891 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003892 if (rdev->raid_disk >= 0) {
3893 char nm[20];
3894 sprintf(nm, "rd%d", rdev->raid_disk);
3895 sysfs_remove_link(&mddev->kobj, nm);
3896 }
3897
NeilBrown177a99b2008-02-06 01:39:56 -08003898 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003899 flush_scheduled_work();
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 export_array(mddev);
3902
Andre Nollf233ea52008-07-21 17:05:22 +10003903 mddev->array_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003904 mddev->size = 0;
3905 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003906 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003907 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003908 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003909 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003910 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003911 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003912 mddev->level = LEVEL_NONE;
3913 mddev->clevel[0] = 0;
3914 mddev->flags = 0;
3915 mddev->ro = 0;
3916 mddev->metadata_type[0] = 0;
3917 mddev->chunk_size = 0;
3918 mddev->ctime = mddev->utime = 0;
3919 mddev->layout = 0;
3920 mddev->max_disks = 0;
3921 mddev->events = 0;
3922 mddev->delta_disks = 0;
3923 mddev->new_level = LEVEL_NONE;
3924 mddev->new_layout = 0;
3925 mddev->new_chunk = 0;
3926 mddev->curr_resync = 0;
3927 mddev->resync_mismatches = 0;
3928 mddev->suspend_lo = mddev->suspend_hi = 0;
3929 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3930 mddev->recovery = 0;
3931 mddev->in_sync = 0;
3932 mddev->changed = 0;
3933 mddev->degraded = 0;
3934 mddev->barriers_work = 0;
3935 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003936
NeilBrowna8a55c32006-06-26 00:27:39 -07003937 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3939 mdname(mddev));
3940 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003941 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003942 sysfs_notify(&mddev->kobj, NULL, "array_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943out:
3944 return err;
3945}
3946
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003947#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948static void autorun_array(mddev_t *mddev)
3949{
3950 mdk_rdev_t *rdev;
3951 struct list_head *tmp;
3952 int err;
3953
NeilBrowna757e642005-04-16 15:26:42 -07003954 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
3957 printk(KERN_INFO "md: running: ");
3958
NeilBrownd089c6a2008-02-06 01:39:59 -08003959 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 char b[BDEVNAME_SIZE];
3961 printk("<%s>", bdevname(rdev->bdev,b));
3962 }
3963 printk("\n");
3964
NeilBrownd710e132008-10-13 11:55:12 +11003965 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 if (err) {
3967 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11003968 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
3970}
3971
3972/*
3973 * lets try to run arrays based on all disks that have arrived
3974 * until now. (those are in pending_raid_disks)
3975 *
3976 * the method: pick the first pending disk, collect all disks with
3977 * the same UUID, remove all from the pending list and put them into
3978 * the 'same_array' list. Then order this list based on superblock
3979 * update time (freshest comes first), kick out 'old' disks and
3980 * compare superblocks. If everything's fine then run it.
3981 *
3982 * If "unit" is allocated, then bump its reference count
3983 */
3984static void autorun_devices(int part)
3985{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 struct list_head *tmp;
3987 mdk_rdev_t *rdev0, *rdev;
3988 mddev_t *mddev;
3989 char b[BDEVNAME_SIZE];
3990
3991 printk(KERN_INFO "md: autorun ...\n");
3992 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003993 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003995 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 rdev0 = list_entry(pending_raid_disks.next,
3997 mdk_rdev_t, same_set);
3998
3999 printk(KERN_INFO "md: considering %s ...\n",
4000 bdevname(rdev0->bdev,b));
4001 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08004002 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 if (super_90_load(rdev, rdev0, 0) >= 0) {
4004 printk(KERN_INFO "md: adding %s ...\n",
4005 bdevname(rdev->bdev,b));
4006 list_move(&rdev->same_set, &candidates);
4007 }
4008 /*
4009 * now we have a set of devices, with all of them having
4010 * mostly sane superblocks. It's time to allocate the
4011 * mddev.
4012 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004013 if (part) {
4014 dev = MKDEV(mdp_major,
4015 rdev0->preferred_minor << MdpMinorShift);
4016 unit = MINOR(dev) >> MdpMinorShift;
4017 } else {
4018 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4019 unit = MINOR(dev);
4020 }
4021 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4023 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4024 break;
4025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
4027 md_probe(dev, NULL, NULL);
4028 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004029 if (!mddev || !mddev->gendisk) {
4030 if (mddev)
4031 mddev_put(mddev);
4032 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 "md: cannot allocate memory for md drive.\n");
4034 break;
4035 }
4036 if (mddev_lock(mddev))
4037 printk(KERN_WARNING "md: %s locked, cannot run\n",
4038 mdname(mddev));
4039 else if (mddev->raid_disks || mddev->major_version
4040 || !list_empty(&mddev->disks)) {
4041 printk(KERN_WARNING
4042 "md: %s already running, cannot run %s\n",
4043 mdname(mddev), bdevname(rdev0->bdev,b));
4044 mddev_unlock(mddev);
4045 } else {
4046 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004047 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004048 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 list_del_init(&rdev->same_set);
4050 if (bind_rdev_to_array(rdev, mddev))
4051 export_rdev(rdev);
4052 }
4053 autorun_array(mddev);
4054 mddev_unlock(mddev);
4055 }
4056 /* on success, candidates will be empty, on error
4057 * it won't...
4058 */
NeilBrown4b809912008-07-21 17:05:25 +10004059 rdev_for_each_list(rdev, tmp, candidates) {
4060 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 mddev_put(mddev);
4064 }
4065 printk(KERN_INFO "md: ... autorun DONE.\n");
4066}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004067#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069static int get_version(void __user * arg)
4070{
4071 mdu_version_t ver;
4072
4073 ver.major = MD_MAJOR_VERSION;
4074 ver.minor = MD_MINOR_VERSION;
4075 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4076
4077 if (copy_to_user(arg, &ver, sizeof(ver)))
4078 return -EFAULT;
4079
4080 return 0;
4081}
4082
4083static int get_array_info(mddev_t * mddev, void __user * arg)
4084{
4085 mdu_array_info_t info;
4086 int nr,working,active,failed,spare;
4087 mdk_rdev_t *rdev;
4088 struct list_head *tmp;
4089
4090 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004091 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004093 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 failed++;
4095 else {
4096 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004097 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 active++;
4099 else
4100 spare++;
4101 }
4102 }
4103
4104 info.major_version = mddev->major_version;
4105 info.minor_version = mddev->minor_version;
4106 info.patch_version = MD_PATCHLEVEL_VERSION;
4107 info.ctime = mddev->ctime;
4108 info.level = mddev->level;
4109 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004110 if (info.size != mddev->size) /* overflow */
4111 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 info.nr_disks = nr;
4113 info.raid_disks = mddev->raid_disks;
4114 info.md_minor = mddev->md_minor;
4115 info.not_persistent= !mddev->persistent;
4116
4117 info.utime = mddev->utime;
4118 info.state = 0;
4119 if (mddev->in_sync)
4120 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004121 if (mddev->bitmap && mddev->bitmap_offset)
4122 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 info.active_disks = active;
4124 info.working_disks = working;
4125 info.failed_disks = failed;
4126 info.spare_disks = spare;
4127
4128 info.layout = mddev->layout;
4129 info.chunk_size = mddev->chunk_size;
4130
4131 if (copy_to_user(arg, &info, sizeof(info)))
4132 return -EFAULT;
4133
4134 return 0;
4135}
4136
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004137static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004138{
4139 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4140 char *ptr, *buf = NULL;
4141 int err = -ENOMEM;
4142
Dan Williamsb5470dc2008-06-27 21:44:04 -07004143 if (md_allow_write(mddev))
4144 file = kmalloc(sizeof(*file), GFP_NOIO);
4145 else
4146 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004147
NeilBrown32a76272005-06-21 17:17:14 -07004148 if (!file)
4149 goto out;
4150
4151 /* bitmap disabled, zero the first byte and copy out */
4152 if (!mddev->bitmap || !mddev->bitmap->file) {
4153 file->pathname[0] = '\0';
4154 goto copy_out;
4155 }
4156
4157 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4158 if (!buf)
4159 goto out;
4160
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004161 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4162 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004163 goto out;
4164
4165 strcpy(file->pathname, ptr);
4166
4167copy_out:
4168 err = 0;
4169 if (copy_to_user(arg, file, sizeof(*file)))
4170 err = -EFAULT;
4171out:
4172 kfree(buf);
4173 kfree(file);
4174 return err;
4175}
4176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177static int get_disk_info(mddev_t * mddev, void __user * arg)
4178{
4179 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 mdk_rdev_t *rdev;
4181
4182 if (copy_from_user(&info, arg, sizeof(info)))
4183 return -EFAULT;
4184
Andre Noll26ef3792008-07-11 22:02:21 +10004185 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 if (rdev) {
4187 info.major = MAJOR(rdev->bdev->bd_dev);
4188 info.minor = MINOR(rdev->bdev->bd_dev);
4189 info.raid_disk = rdev->raid_disk;
4190 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004191 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004193 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 info.state |= (1<<MD_DISK_ACTIVE);
4195 info.state |= (1<<MD_DISK_SYNC);
4196 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004197 if (test_bit(WriteMostly, &rdev->flags))
4198 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 } else {
4200 info.major = info.minor = 0;
4201 info.raid_disk = -1;
4202 info.state = (1<<MD_DISK_REMOVED);
4203 }
4204
4205 if (copy_to_user(arg, &info, sizeof(info)))
4206 return -EFAULT;
4207
4208 return 0;
4209}
4210
4211static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4212{
4213 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4214 mdk_rdev_t *rdev;
4215 dev_t dev = MKDEV(info->major,info->minor);
4216
4217 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4218 return -EOVERFLOW;
4219
4220 if (!mddev->raid_disks) {
4221 int err;
4222 /* expecting a device which has a superblock */
4223 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4224 if (IS_ERR(rdev)) {
4225 printk(KERN_WARNING
4226 "md: md_import_device returned %ld\n",
4227 PTR_ERR(rdev));
4228 return PTR_ERR(rdev);
4229 }
4230 if (!list_empty(&mddev->disks)) {
4231 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4232 mdk_rdev_t, same_set);
4233 int err = super_types[mddev->major_version]
4234 .load_super(rdev, rdev0, mddev->minor_version);
4235 if (err < 0) {
4236 printk(KERN_WARNING
4237 "md: %s has different UUID to %s\n",
4238 bdevname(rdev->bdev,b),
4239 bdevname(rdev0->bdev,b2));
4240 export_rdev(rdev);
4241 return -EINVAL;
4242 }
4243 }
4244 err = bind_rdev_to_array(rdev, mddev);
4245 if (err)
4246 export_rdev(rdev);
4247 return err;
4248 }
4249
4250 /*
4251 * add_new_disk can be used once the array is assembled
4252 * to add "hot spares". They must already have a superblock
4253 * written
4254 */
4255 if (mddev->pers) {
4256 int err;
4257 if (!mddev->pers->hot_add_disk) {
4258 printk(KERN_WARNING
4259 "%s: personality does not support diskops!\n",
4260 mdname(mddev));
4261 return -EINVAL;
4262 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004263 if (mddev->persistent)
4264 rdev = md_import_device(dev, mddev->major_version,
4265 mddev->minor_version);
4266 else
4267 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (IS_ERR(rdev)) {
4269 printk(KERN_WARNING
4270 "md: md_import_device returned %ld\n",
4271 PTR_ERR(rdev));
4272 return PTR_ERR(rdev);
4273 }
NeilBrown41158c72005-06-21 17:17:25 -07004274 /* set save_raid_disk if appropriate */
4275 if (!mddev->persistent) {
4276 if (info->state & (1<<MD_DISK_SYNC) &&
4277 info->raid_disk < mddev->raid_disks)
4278 rdev->raid_disk = info->raid_disk;
4279 else
4280 rdev->raid_disk = -1;
4281 } else
4282 super_types[mddev->major_version].
4283 validate_super(mddev, rdev);
4284 rdev->saved_raid_disk = rdev->raid_disk;
4285
NeilBrownb2d444d2005-11-08 21:39:31 -08004286 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004287 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4288 set_bit(WriteMostly, &rdev->flags);
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 rdev->raid_disk = -1;
4291 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004292 if (!err && !mddev->pers->hot_remove_disk) {
4293 /* If there is hot_add_disk but no hot_remove_disk
4294 * then added disks for geometry changes,
4295 * and should be added immediately.
4296 */
4297 super_types[mddev->major_version].
4298 validate_super(mddev, rdev);
4299 err = mddev->pers->hot_add_disk(mddev, rdev);
4300 if (err)
4301 unbind_rdev_from_array(rdev);
4302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (err)
4304 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004305 else
4306 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc3617772005-06-21 17:17:10 -07004307
NeilBrown17571282006-12-10 02:20:52 -08004308 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004309 if (mddev->degraded)
4310 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004311 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004312 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return err;
4314 }
4315
4316 /* otherwise, add_new_disk is only allowed
4317 * for major_version==0 superblocks
4318 */
4319 if (mddev->major_version != 0) {
4320 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4321 mdname(mddev));
4322 return -EINVAL;
4323 }
4324
4325 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4326 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004327 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 if (IS_ERR(rdev)) {
4329 printk(KERN_WARNING
4330 "md: error, md_import_device() returned %ld\n",
4331 PTR_ERR(rdev));
4332 return PTR_ERR(rdev);
4333 }
4334 rdev->desc_nr = info->number;
4335 if (info->raid_disk < mddev->raid_disks)
4336 rdev->raid_disk = info->raid_disk;
4337 else
4338 rdev->raid_disk = -1;
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004341 if (info->state & (1<<MD_DISK_SYNC))
4342 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004344 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4345 set_bit(WriteMostly, &rdev->flags);
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 if (!mddev->persistent) {
4348 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004349 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 } else
Andre Noll0f420352008-07-11 22:02:23 +10004351 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolle7debaa2008-07-11 22:02:23 +10004352 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
NeilBrown2bf071b2006-01-06 00:20:55 -08004354 err = bind_rdev_to_array(rdev, mddev);
4355 if (err) {
4356 export_rdev(rdev);
4357 return err;
4358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 }
4360
4361 return 0;
4362}
4363
4364static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4365{
4366 char b[BDEVNAME_SIZE];
4367 mdk_rdev_t *rdev;
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 rdev = find_rdev(mddev, dev);
4370 if (!rdev)
4371 return -ENXIO;
4372
4373 if (rdev->raid_disk >= 0)
4374 goto busy;
4375
4376 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004377 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004378 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 return 0;
4381busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004382 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 bdevname(rdev->bdev,b), mdname(mddev));
4384 return -EBUSY;
4385}
4386
4387static int hot_add_disk(mddev_t * mddev, dev_t dev)
4388{
4389 char b[BDEVNAME_SIZE];
4390 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 mdk_rdev_t *rdev;
4392
4393 if (!mddev->pers)
4394 return -ENODEV;
4395
4396 if (mddev->major_version != 0) {
4397 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4398 " version-0 superblocks.\n",
4399 mdname(mddev));
4400 return -EINVAL;
4401 }
4402 if (!mddev->pers->hot_add_disk) {
4403 printk(KERN_WARNING
4404 "%s: personality does not support diskops!\n",
4405 mdname(mddev));
4406 return -EINVAL;
4407 }
4408
NeilBrownd710e132008-10-13 11:55:12 +11004409 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 if (IS_ERR(rdev)) {
4411 printk(KERN_WARNING
4412 "md: error, md_import_device() returned %ld\n",
4413 PTR_ERR(rdev));
4414 return -EINVAL;
4415 }
4416
4417 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004418 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 else
Andre Noll0f420352008-07-11 22:02:23 +10004420 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Andre Nolle7debaa2008-07-11 22:02:23 +10004422 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
NeilBrownb2d444d2005-11-08 21:39:31 -08004424 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 printk(KERN_WARNING
4426 "md: can not hot-add faulty %s disk to %s!\n",
4427 bdevname(rdev->bdev,b), mdname(mddev));
4428 err = -EINVAL;
4429 goto abort_export;
4430 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004431 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004433 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004434 err = bind_rdev_to_array(rdev, mddev);
4435 if (err)
4436 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
4438 /*
4439 * The rest should better be atomic, we can have disk failures
4440 * noticed in interrupt contexts ...
4441 */
4442
4443 if (rdev->desc_nr == mddev->max_disks) {
4444 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4445 mdname(mddev));
4446 err = -EBUSY;
4447 goto abort_unbind_export;
4448 }
4449
4450 rdev->raid_disk = -1;
4451
NeilBrown850b2b42006-10-03 01:15:46 -07004452 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
4454 /*
4455 * Kick recovery, maybe this spare has to be added to the
4456 * array immediately.
4457 */
4458 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4459 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004460 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 return 0;
4462
4463abort_unbind_export:
4464 unbind_rdev_from_array(rdev);
4465
4466abort_export:
4467 export_rdev(rdev);
4468 return err;
4469}
4470
NeilBrown32a76272005-06-21 17:17:14 -07004471static int set_bitmap_file(mddev_t *mddev, int fd)
4472{
4473 int err;
4474
NeilBrown36fa3062005-09-09 16:23:45 -07004475 if (mddev->pers) {
4476 if (!mddev->pers->quiesce)
4477 return -EBUSY;
4478 if (mddev->recovery || mddev->sync_thread)
4479 return -EBUSY;
4480 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004481 }
4482
NeilBrown36fa3062005-09-09 16:23:45 -07004483
4484 if (fd >= 0) {
4485 if (mddev->bitmap)
4486 return -EEXIST; /* cannot add when bitmap is present */
4487 mddev->bitmap_file = fget(fd);
4488
4489 if (mddev->bitmap_file == NULL) {
4490 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4491 mdname(mddev));
4492 return -EBADF;
4493 }
4494
4495 err = deny_bitmap_write_access(mddev->bitmap_file);
4496 if (err) {
4497 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4498 mdname(mddev));
4499 fput(mddev->bitmap_file);
4500 mddev->bitmap_file = NULL;
4501 return err;
4502 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004503 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004504 } else if (mddev->bitmap == NULL)
4505 return -ENOENT; /* cannot remove what isn't there */
4506 err = 0;
4507 if (mddev->pers) {
4508 mddev->pers->quiesce(mddev, 1);
4509 if (fd >= 0)
4510 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004511 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004512 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004513 fd = -1; /* make sure to put the file */
4514 }
NeilBrown36fa3062005-09-09 16:23:45 -07004515 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004516 }
4517 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004518 if (mddev->bitmap_file) {
4519 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004520 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004521 }
NeilBrown36fa3062005-09-09 16:23:45 -07004522 mddev->bitmap_file = NULL;
4523 }
4524
NeilBrown32a76272005-06-21 17:17:14 -07004525 return err;
4526}
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528/*
4529 * set_array_info is used two different ways
4530 * The original usage is when creating a new array.
4531 * In this usage, raid_disks is > 0 and it together with
4532 * level, size, not_persistent,layout,chunksize determine the
4533 * shape of the array.
4534 * This will always create an array with a type-0.90.0 superblock.
4535 * The newer usage is when assembling an array.
4536 * In this case raid_disks will be 0, and the major_version field is
4537 * use to determine which style super-blocks are to be found on the devices.
4538 * The minor and patch _version numbers are also kept incase the
4539 * super_block handler wishes to interpret them.
4540 */
4541static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4542{
4543
4544 if (info->raid_disks == 0) {
4545 /* just setting version number for superblock loading */
4546 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004547 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 super_types[info->major_version].name == NULL) {
4549 /* maybe try to auto-load a module? */
4550 printk(KERN_INFO
4551 "md: superblock version %d not known\n",
4552 info->major_version);
4553 return -EINVAL;
4554 }
4555 mddev->major_version = info->major_version;
4556 mddev->minor_version = info->minor_version;
4557 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004558 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 return 0;
4560 }
4561 mddev->major_version = MD_MAJOR_VERSION;
4562 mddev->minor_version = MD_MINOR_VERSION;
4563 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4564 mddev->ctime = get_seconds();
4565
4566 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004567 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 mddev->size = info->size;
4569 mddev->raid_disks = info->raid_disks;
4570 /* don't set md_minor, it is determined by which /dev/md* was
4571 * openned
4572 */
4573 if (info->state & (1<<MD_SB_CLEAN))
4574 mddev->recovery_cp = MaxSector;
4575 else
4576 mddev->recovery_cp = 0;
4577 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004578 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580 mddev->layout = info->layout;
4581 mddev->chunk_size = info->chunk_size;
4582
4583 mddev->max_disks = MD_SB_DISKS;
4584
NeilBrowne6910632008-02-06 01:39:51 -08004585 if (mddev->persistent)
4586 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004587 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
NeilBrownb2a27032005-11-28 13:44:12 -08004589 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4590 mddev->bitmap_offset = 0;
4591
NeilBrownf6705572006-03-27 01:18:11 -08004592 mddev->reshape_position = MaxSector;
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 /*
4595 * Generate a 128 bit UUID
4596 */
4597 get_random_bytes(mddev->uuid, 16);
4598
NeilBrownf6705572006-03-27 01:18:11 -08004599 mddev->new_level = mddev->level;
4600 mddev->new_chunk = mddev->chunk_size;
4601 mddev->new_layout = mddev->layout;
4602 mddev->delta_disks = 0;
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return 0;
4605}
4606
Andre Nolld71f9f82008-07-11 22:02:22 +10004607static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004608{
4609 mdk_rdev_t * rdev;
4610 int rv;
4611 struct list_head *tmp;
Andre Nolld71f9f82008-07-11 22:02:22 +10004612 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004613
4614 if (mddev->pers->resize == NULL)
4615 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10004616 /* The "num_sectors" is the number of sectors of each device that
4617 * is used. This can only make sense for arrays with redundancy.
4618 * linear and raid0 always use whatever space is available. We can only
4619 * consider changing this number if no resync or reconstruction is
4620 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10004621 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10004622 * of each device. If num_sectors is zero, we find the largest size
4623 * that fits.
4624
NeilBrowna35b0d62006-01-06 00:20:49 -08004625 */
4626 if (mddev->sync_thread)
4627 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10004628 if (mddev->bitmap)
4629 /* Sorry, cannot grow a bitmap yet, just remove it,
4630 * grow, and re-add.
4631 */
4632 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004633 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004634 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004635 avail = rdev->size * 2;
4636
Andre Nolld71f9f82008-07-11 22:02:22 +10004637 if (fit && (num_sectors == 0 || num_sectors > avail))
4638 num_sectors = avail;
4639 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004640 return -ENOSPC;
4641 }
Andre Nolld71f9f82008-07-11 22:02:22 +10004642 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004643 if (!rv) {
4644 struct block_device *bdev;
4645
4646 bdev = bdget_disk(mddev->gendisk, 0);
4647 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004648 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004649 i_size_write(bdev->bd_inode,
4650 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004651 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004652 bdput(bdev);
4653 }
4654 }
4655 return rv;
4656}
4657
NeilBrownda943b92006-01-06 00:20:54 -08004658static int update_raid_disks(mddev_t *mddev, int raid_disks)
4659{
4660 int rv;
4661 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004662 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08004663 return -EINVAL;
4664 if (raid_disks <= 0 ||
4665 raid_disks >= mddev->max_disks)
4666 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004667 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08004668 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004669 mddev->delta_disks = raid_disks - mddev->raid_disks;
4670
4671 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08004672 return rv;
4673}
4674
4675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676/*
4677 * update_array_info is used to change the configuration of an
4678 * on-line array.
4679 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4680 * fields in the info are checked against the array.
4681 * Any differences that cannot be handled will cause an error.
4682 * Normally, only one change can be managed at a time.
4683 */
4684static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4685{
4686 int rv = 0;
4687 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004688 int state = 0;
4689
4690 /* calculate expected state,ignoring low bits */
4691 if (mddev->bitmap && mddev->bitmap_offset)
4692 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 if (mddev->major_version != info->major_version ||
4695 mddev->minor_version != info->minor_version ||
4696/* mddev->patch_version != info->patch_version || */
4697 mddev->ctime != info->ctime ||
4698 mddev->level != info->level ||
4699/* mddev->layout != info->layout || */
4700 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004701 mddev->chunk_size != info->chunk_size ||
4702 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4703 ((state^info->state) & 0xfffffe00)
4704 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 return -EINVAL;
4706 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004707 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 if (mddev->raid_disks != info->raid_disks) cnt++;
4709 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004710 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 if (cnt == 0) return 0;
4712 if (cnt > 1) return -EINVAL;
4713
4714 if (mddev->layout != info->layout) {
4715 /* Change layout
4716 * we don't need to do anything at the md level, the
4717 * personality will take care of it all.
4718 */
4719 if (mddev->pers->reconfig == NULL)
4720 return -EINVAL;
4721 else
4722 return mddev->pers->reconfig(mddev, info->layout, -1);
4723 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004724 if (info->size >= 0 && mddev->size != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10004725 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
NeilBrownda943b92006-01-06 00:20:54 -08004727 if (mddev->raid_disks != info->raid_disks)
4728 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
NeilBrown36fa3062005-09-09 16:23:45 -07004730 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4731 if (mddev->pers->quiesce == NULL)
4732 return -EINVAL;
4733 if (mddev->recovery || mddev->sync_thread)
4734 return -EBUSY;
4735 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4736 /* add the bitmap */
4737 if (mddev->bitmap)
4738 return -EEXIST;
4739 if (mddev->default_bitmap_offset == 0)
4740 return -EINVAL;
4741 mddev->bitmap_offset = mddev->default_bitmap_offset;
4742 mddev->pers->quiesce(mddev, 1);
4743 rv = bitmap_create(mddev);
4744 if (rv)
4745 bitmap_destroy(mddev);
4746 mddev->pers->quiesce(mddev, 0);
4747 } else {
4748 /* remove the bitmap */
4749 if (!mddev->bitmap)
4750 return -ENOENT;
4751 if (mddev->bitmap->file)
4752 return -EINVAL;
4753 mddev->pers->quiesce(mddev, 1);
4754 bitmap_destroy(mddev);
4755 mddev->pers->quiesce(mddev, 0);
4756 mddev->bitmap_offset = 0;
4757 }
4758 }
NeilBrown850b2b42006-10-03 01:15:46 -07004759 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 return rv;
4761}
4762
4763static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4764{
4765 mdk_rdev_t *rdev;
4766
4767 if (mddev->pers == NULL)
4768 return -ENODEV;
4769
4770 rdev = find_rdev(mddev, dev);
4771 if (!rdev)
4772 return -ENODEV;
4773
4774 md_error(mddev, rdev);
4775 return 0;
4776}
4777
Andre Noll2f9618c2008-04-25 18:57:58 +02004778/*
4779 * We have a problem here : there is no easy way to give a CHS
4780 * virtual geometry. We currently pretend that we have a 2 heads
4781 * 4 sectors (with a BIG number of cylinders...). This drives
4782 * dosfs just mad... ;-)
4783 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004784static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4785{
4786 mddev_t *mddev = bdev->bd_disk->private_data;
4787
4788 geo->heads = 2;
4789 geo->sectors = 4;
4790 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4791 return 0;
4792}
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794static int md_ioctl(struct inode *inode, struct file *file,
4795 unsigned int cmd, unsigned long arg)
4796{
4797 int err = 0;
4798 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 mddev_t *mddev = NULL;
4800
4801 if (!capable(CAP_SYS_ADMIN))
4802 return -EACCES;
4803
4804 /*
4805 * Commands dealing with the RAID driver but not any
4806 * particular array:
4807 */
4808 switch (cmd)
4809 {
4810 case RAID_VERSION:
4811 err = get_version(argp);
4812 goto done;
4813
4814 case PRINT_RAID_DEBUG:
4815 err = 0;
4816 md_print_devices();
4817 goto done;
4818
4819#ifndef MODULE
4820 case RAID_AUTORUN:
4821 err = 0;
4822 autostart_arrays(arg);
4823 goto done;
4824#endif
4825 default:;
4826 }
4827
4828 /*
4829 * Commands creating/starting a new array:
4830 */
4831
4832 mddev = inode->i_bdev->bd_disk->private_data;
4833
4834 if (!mddev) {
4835 BUG();
4836 goto abort;
4837 }
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 err = mddev_lock(mddev);
4840 if (err) {
4841 printk(KERN_INFO
4842 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4843 err, cmd);
4844 goto abort;
4845 }
4846
4847 switch (cmd)
4848 {
4849 case SET_ARRAY_INFO:
4850 {
4851 mdu_array_info_t info;
4852 if (!arg)
4853 memset(&info, 0, sizeof(info));
4854 else if (copy_from_user(&info, argp, sizeof(info))) {
4855 err = -EFAULT;
4856 goto abort_unlock;
4857 }
4858 if (mddev->pers) {
4859 err = update_array_info(mddev, &info);
4860 if (err) {
4861 printk(KERN_WARNING "md: couldn't update"
4862 " array info. %d\n", err);
4863 goto abort_unlock;
4864 }
4865 goto done_unlock;
4866 }
4867 if (!list_empty(&mddev->disks)) {
4868 printk(KERN_WARNING
4869 "md: array %s already has disks!\n",
4870 mdname(mddev));
4871 err = -EBUSY;
4872 goto abort_unlock;
4873 }
4874 if (mddev->raid_disks) {
4875 printk(KERN_WARNING
4876 "md: array %s already initialised!\n",
4877 mdname(mddev));
4878 err = -EBUSY;
4879 goto abort_unlock;
4880 }
4881 err = set_array_info(mddev, &info);
4882 if (err) {
4883 printk(KERN_WARNING "md: couldn't set"
4884 " array info. %d\n", err);
4885 goto abort_unlock;
4886 }
4887 }
4888 goto done_unlock;
4889
4890 default:;
4891 }
4892
4893 /*
4894 * Commands querying/configuring an existing array:
4895 */
NeilBrown32a76272005-06-21 17:17:14 -07004896 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004897 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004898 if ((!mddev->raid_disks && !mddev->external)
4899 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4900 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4901 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 err = -ENODEV;
4903 goto abort_unlock;
4904 }
4905
4906 /*
4907 * Commands even a read-only array can execute:
4908 */
4909 switch (cmd)
4910 {
4911 case GET_ARRAY_INFO:
4912 err = get_array_info(mddev, argp);
4913 goto done_unlock;
4914
NeilBrown32a76272005-06-21 17:17:14 -07004915 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004916 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004917 goto done_unlock;
4918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 case GET_DISK_INFO:
4920 err = get_disk_info(mddev, argp);
4921 goto done_unlock;
4922
4923 case RESTART_ARRAY_RW:
4924 err = restart_array(mddev);
4925 goto done_unlock;
4926
4927 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11004928 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 goto done_unlock;
4930
4931 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11004932 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 goto done_unlock;
4934
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 }
4936
4937 /*
4938 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004939 * superblock, so we do not allow them on read-only arrays.
4940 * However non-MD ioctls (e.g. get-size) will still come through
4941 * here and hit the 'default' below, so only disallow
4942 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 */
Andre Nollbb57fc62008-04-25 19:06:35 +02004944 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004945 if (mddev->ro == 2) {
4946 mddev->ro = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10004947 sysfs_notify(&mddev->kobj, NULL, "array_state");
4948 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4949 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004950 } else {
4951 err = -EROFS;
4952 goto abort_unlock;
4953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
4955
4956 switch (cmd)
4957 {
4958 case ADD_NEW_DISK:
4959 {
4960 mdu_disk_info_t info;
4961 if (copy_from_user(&info, argp, sizeof(info)))
4962 err = -EFAULT;
4963 else
4964 err = add_new_disk(mddev, &info);
4965 goto done_unlock;
4966 }
4967
4968 case HOT_REMOVE_DISK:
4969 err = hot_remove_disk(mddev, new_decode_dev(arg));
4970 goto done_unlock;
4971
4972 case HOT_ADD_DISK:
4973 err = hot_add_disk(mddev, new_decode_dev(arg));
4974 goto done_unlock;
4975
4976 case SET_DISK_FAULTY:
4977 err = set_disk_faulty(mddev, new_decode_dev(arg));
4978 goto done_unlock;
4979
4980 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11004981 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 goto done_unlock;
4983
NeilBrown32a76272005-06-21 17:17:14 -07004984 case SET_BITMAP_FILE:
4985 err = set_bitmap_file(mddev, (int)arg);
4986 goto done_unlock;
4987
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 err = -EINVAL;
4990 goto abort_unlock;
4991 }
4992
4993done_unlock:
4994abort_unlock:
4995 mddev_unlock(mddev);
4996
4997 return err;
4998done:
4999 if (err)
5000 MD_BUG();
5001abort:
5002 return err;
5003}
5004
5005static int md_open(struct inode *inode, struct file *file)
5006{
5007 /*
5008 * Succeed if we can lock the mddev, which confirms that
5009 * it isn't being stopped right now.
5010 */
5011 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5012 int err;
5013
NeilBrownd63a5a72006-12-08 02:36:17 -08005014 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 goto out;
5016
5017 err = 0;
5018 mddev_get(mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005019 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 mddev_unlock(mddev);
5021
5022 check_disk_change(inode->i_bdev);
5023 out:
5024 return err;
5025}
5026
5027static int md_release(struct inode *inode, struct file * file)
5028{
5029 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5030
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005031 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005032 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 mddev_put(mddev);
5034
5035 return 0;
5036}
5037
Linus Torvalds44ce6292007-05-09 18:51:36 -07005038static int md_media_changed(struct gendisk *disk)
5039{
5040 mddev_t *mddev = disk->private_data;
5041
5042 return mddev->changed;
5043}
5044
5045static int md_revalidate(struct gendisk *disk)
5046{
5047 mddev_t *mddev = disk->private_data;
5048
5049 mddev->changed = 0;
5050 return 0;
5051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052static struct block_device_operations md_fops =
5053{
5054 .owner = THIS_MODULE,
5055 .open = md_open,
5056 .release = md_release,
5057 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005058 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005059 .media_changed = md_media_changed,
5060 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061};
5062
Adrian Bunk75c96f82005-05-05 16:16:09 -07005063static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
5065 mdk_thread_t *thread = arg;
5066
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 /*
5068 * md_thread is a 'system-thread', it's priority should be very
5069 * high. We avoid resource deadlocks individually in each
5070 * raid personality. (RAID5 does preallocation) We also use RR and
5071 * the very same RT priority as kswapd, thus we will never get
5072 * into a priority inversion deadlock.
5073 *
5074 * we definitely have to have equal or higher priority than
5075 * bdflush, otherwise bdflush will deadlock if there are too
5076 * many dirty RAID5 blocks.
5077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
NeilBrown6985c432005-10-19 21:23:47 -07005079 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005080 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
NeilBrown93588e22005-11-15 00:09:12 -08005082 /* We need to wait INTERRUPTIBLE so that
5083 * we don't add to the load-average.
5084 * That means we need to be sure no signals are
5085 * pending
5086 */
5087 if (signal_pending(current))
5088 flush_signals(current);
5089
5090 wait_event_interruptible_timeout
5091 (thread->wqueue,
5092 test_bit(THREAD_WAKEUP, &thread->flags)
5093 || kthread_should_stop(),
5094 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096 clear_bit(THREAD_WAKEUP, &thread->flags);
5097
NeilBrown787453c2005-11-08 21:39:43 -08005098 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 return 0;
5102}
5103
5104void md_wakeup_thread(mdk_thread_t *thread)
5105{
5106 if (thread) {
5107 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5108 set_bit(THREAD_WAKEUP, &thread->flags);
5109 wake_up(&thread->wqueue);
5110 }
5111}
5112
5113mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5114 const char *name)
5115{
5116 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
NeilBrown9ffae0c2006-01-06 00:20:32 -08005118 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 if (!thread)
5120 return NULL;
5121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 init_waitqueue_head(&thread->wqueue);
5123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 thread->run = run;
5125 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005126 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005127 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005128 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 kfree(thread);
5130 return NULL;
5131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 return thread;
5133}
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135void md_unregister_thread(mdk_thread_t *thread)
5136{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005137 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005138
5139 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 kfree(thread);
5141}
5142
5143void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5144{
5145 if (!mddev) {
5146 MD_BUG();
5147 return;
5148 }
5149
NeilBrownb2d444d2005-11-08 21:39:31 -08005150 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005152
5153 if (mddev->external)
5154 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5157 mdname(mddev),
5158 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5159 __builtin_return_address(0),__builtin_return_address(1),
5160 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005161*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005162 if (!mddev->pers)
5163 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 if (!mddev->pers->error_handler)
5165 return;
5166 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005167 if (mddev->degraded)
5168 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005169 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5171 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5172 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005173 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174}
5175
5176/* seq_file implementation /proc/mdstat */
5177
5178static void status_unused(struct seq_file *seq)
5179{
5180 int i = 0;
5181 mdk_rdev_t *rdev;
5182 struct list_head *tmp;
5183
5184 seq_printf(seq, "unused devices: ");
5185
NeilBrown73c34432008-02-06 01:39:59 -08005186 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 char b[BDEVNAME_SIZE];
5188 i++;
5189 seq_printf(seq, "%s ",
5190 bdevname(rdev->bdev,b));
5191 }
5192 if (!i)
5193 seq_printf(seq, "<none>");
5194
5195 seq_printf(seq, "\n");
5196}
5197
5198
5199static void status_resync(struct seq_file *seq, mddev_t * mddev)
5200{
NeilBrown4588b422006-03-27 01:18:04 -08005201 sector_t max_blocks, resync, res;
5202 unsigned long dt, db, rt;
5203 int scale;
5204 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
5206 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5207
5208 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5209 max_blocks = mddev->resync_max_sectors >> 1;
5210 else
5211 max_blocks = mddev->size;
5212
5213 /*
5214 * Should not happen.
5215 */
5216 if (!max_blocks) {
5217 MD_BUG();
5218 return;
5219 }
NeilBrown4588b422006-03-27 01:18:04 -08005220 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5221 * in a sector_t, and (max_blocks>>scale) will fit in a
5222 * u32, as those are the requirements for sector_div.
5223 * Thus 'scale' must be at least 10
5224 */
5225 scale = 10;
5226 if (sizeof(sector_t) > sizeof(unsigned long)) {
5227 while ( max_blocks/2 > (1ULL<<(scale+32)))
5228 scale++;
5229 }
5230 res = (resync>>scale)*1000;
5231 sector_div(res, (u32)((max_blocks>>scale)+1));
5232
5233 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 {
NeilBrown4588b422006-03-27 01:18:04 -08005235 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 seq_printf(seq, "[");
5237 for (i = 0; i < x; i++)
5238 seq_printf(seq, "=");
5239 seq_printf(seq, ">");
5240 for (i = 0; i < y; i++)
5241 seq_printf(seq, ".");
5242 seq_printf(seq, "] ");
5243 }
NeilBrown4588b422006-03-27 01:18:04 -08005244 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005245 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5246 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005247 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5248 "check" :
5249 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5250 "resync" : "recovery"))),
5251 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005252 (unsigned long long) resync,
5253 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254
5255 /*
5256 * We do not want to overflow, so the order of operands and
5257 * the * 100 / 100 trick are important. We do a +1 to be
5258 * safe against division by zero. We only estimate anyway.
5259 *
5260 * dt: time from mark until now
5261 * db: blocks written from mark until now
5262 * rt: remaining time
5263 */
5264 dt = ((jiffies - mddev->resync_mark) / HZ);
5265 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005266 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5267 - mddev->resync_mark_cnt;
5268 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
5270 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5271
NeilBrownff4e8d92006-07-10 04:44:16 -07005272 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273}
5274
5275static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5276{
5277 struct list_head *tmp;
5278 loff_t l = *pos;
5279 mddev_t *mddev;
5280
5281 if (l >= 0x10000)
5282 return NULL;
5283 if (!l--)
5284 /* header */
5285 return (void*)1;
5286
5287 spin_lock(&all_mddevs_lock);
5288 list_for_each(tmp,&all_mddevs)
5289 if (!l--) {
5290 mddev = list_entry(tmp, mddev_t, all_mddevs);
5291 mddev_get(mddev);
5292 spin_unlock(&all_mddevs_lock);
5293 return mddev;
5294 }
5295 spin_unlock(&all_mddevs_lock);
5296 if (!l--)
5297 return (void*)2;/* tail */
5298 return NULL;
5299}
5300
5301static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5302{
5303 struct list_head *tmp;
5304 mddev_t *next_mddev, *mddev = v;
5305
5306 ++*pos;
5307 if (v == (void*)2)
5308 return NULL;
5309
5310 spin_lock(&all_mddevs_lock);
5311 if (v == (void*)1)
5312 tmp = all_mddevs.next;
5313 else
5314 tmp = mddev->all_mddevs.next;
5315 if (tmp != &all_mddevs)
5316 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5317 else {
5318 next_mddev = (void*)2;
5319 *pos = 0x10000;
5320 }
5321 spin_unlock(&all_mddevs_lock);
5322
5323 if (v != (void*)1)
5324 mddev_put(mddev);
5325 return next_mddev;
5326
5327}
5328
5329static void md_seq_stop(struct seq_file *seq, void *v)
5330{
5331 mddev_t *mddev = v;
5332
5333 if (mddev && v != (void*)1 && v != (void*)2)
5334 mddev_put(mddev);
5335}
5336
NeilBrownd7603b72006-01-06 00:20:30 -08005337struct mdstat_info {
5338 int event;
5339};
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341static int md_seq_show(struct seq_file *seq, void *v)
5342{
5343 mddev_t *mddev = v;
5344 sector_t size;
5345 struct list_head *tmp2;
5346 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005347 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005348 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349
5350 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005351 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 seq_printf(seq, "Personalities : ");
5353 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005354 list_for_each_entry(pers, &pers_list, list)
5355 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 spin_unlock(&pers_lock);
5358 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005359 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return 0;
5361 }
5362 if (v == (void*)2) {
5363 status_unused(seq);
5364 return 0;
5365 }
5366
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005367 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005369
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5371 seq_printf(seq, "%s : %sactive", mdname(mddev),
5372 mddev->pers ? "" : "in");
5373 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005374 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005376 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005377 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 seq_printf(seq, " %s", mddev->pers->name);
5379 }
5380
5381 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005382 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 char b[BDEVNAME_SIZE];
5384 seq_printf(seq, " %s[%d]",
5385 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005386 if (test_bit(WriteMostly, &rdev->flags))
5387 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005388 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 seq_printf(seq, "(F)");
5390 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005391 } else if (rdev->raid_disk < 0)
5392 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 size += rdev->size;
5394 }
5395
5396 if (!list_empty(&mddev->disks)) {
5397 if (mddev->pers)
5398 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005399 (unsigned long long)
5400 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 else
5402 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005403 (unsigned long long)size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005405 if (mddev->persistent) {
5406 if (mddev->major_version != 0 ||
5407 mddev->minor_version != 90) {
5408 seq_printf(seq," super %d.%d",
5409 mddev->major_version,
5410 mddev->minor_version);
5411 }
NeilBrowne6910632008-02-06 01:39:51 -08005412 } else if (mddev->external)
5413 seq_printf(seq, " super external:%s",
5414 mddev->metadata_type);
5415 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005416 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005419 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005421 if (mddev->pers->sync_request) {
5422 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005423 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005424 seq_printf(seq, "\n ");
5425 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5426 seq_printf(seq, "\tresync=DELAYED\n ");
5427 else if (mddev->recovery_cp < MaxSector)
5428 seq_printf(seq, "\tresync=PENDING\n ");
5429 }
NeilBrown32a76272005-06-21 17:17:14 -07005430 } else
5431 seq_printf(seq, "\n ");
5432
5433 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005434 unsigned long chunk_kb;
5435 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005436 spin_lock_irqsave(&bitmap->lock, flags);
5437 chunk_kb = bitmap->chunksize >> 10;
5438 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5439 "%lu%s chunk",
5440 bitmap->pages - bitmap->missing_pages,
5441 bitmap->pages,
5442 (bitmap->pages - bitmap->missing_pages)
5443 << (PAGE_SHIFT - 10),
5444 chunk_kb ? chunk_kb : bitmap->chunksize,
5445 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005446 if (bitmap->file) {
5447 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005448 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005449 }
NeilBrown78d742d2005-06-21 17:17:15 -07005450
NeilBrown32a76272005-06-21 17:17:14 -07005451 seq_printf(seq, "\n");
5452 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454
5455 seq_printf(seq, "\n");
5456 }
5457 mddev_unlock(mddev);
5458
5459 return 0;
5460}
5461
5462static struct seq_operations md_seq_ops = {
5463 .start = md_seq_start,
5464 .next = md_seq_next,
5465 .stop = md_seq_stop,
5466 .show = md_seq_show,
5467};
5468
5469static int md_seq_open(struct inode *inode, struct file *file)
5470{
5471 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005472 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5473 if (mi == NULL)
5474 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
5476 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005477 if (error)
5478 kfree(mi);
5479 else {
5480 struct seq_file *p = file->private_data;
5481 p->private = mi;
5482 mi->event = atomic_read(&md_event_count);
5483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 return error;
5485}
5486
NeilBrownd7603b72006-01-06 00:20:30 -08005487static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5488{
5489 struct seq_file *m = filp->private_data;
5490 struct mdstat_info *mi = m->private;
5491 int mask;
5492
5493 poll_wait(filp, &md_event_waiters, wait);
5494
5495 /* always allow read */
5496 mask = POLLIN | POLLRDNORM;
5497
5498 if (mi->event != atomic_read(&md_event_count))
5499 mask |= POLLERR | POLLPRI;
5500 return mask;
5501}
5502
Arjan van de Venfa027c22007-02-12 00:55:33 -08005503static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005504 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 .open = md_seq_open,
5506 .read = seq_read,
5507 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07005508 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005509 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510};
5511
NeilBrown2604b702006-01-06 00:20:36 -08005512int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005515 list_add_tail(&p->list, &pers_list);
5516 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 spin_unlock(&pers_lock);
5518 return 0;
5519}
5520
NeilBrown2604b702006-01-06 00:20:36 -08005521int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
NeilBrown2604b702006-01-06 00:20:36 -08005523 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005525 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 spin_unlock(&pers_lock);
5527 return 0;
5528}
5529
5530static int is_mddev_idle(mddev_t *mddev)
5531{
5532 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005534 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10005537 rcu_read_lock();
5538 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Tejun Heo074a7ac2008-08-25 19:56:14 +09005540 curr_events = part_stat_read(&disk->part0, sectors[0]) +
5541 part_stat_read(&disk->part0, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005543 /* sync IO will cause sync_io to increase before the disk_stats
5544 * as sync_io is counted when a request starts, and
5545 * disk_stats is counted when it completes.
5546 * So resync activity will cause curr_events to be smaller than
5547 * when there was no such activity.
5548 * non-sync IO will cause disk_stat to increase without
5549 * increasing sync_io so curr_events will (eventually)
5550 * be larger than it was before. Once it becomes
5551 * substantially larger, the test below will cause
5552 * the array to appear non-idle, and resync will slow
5553 * down.
5554 * If there is a lot of outstanding resync activity when
5555 * we set last_event to curr_events, then all that activity
5556 * completing might cause the array to appear non-idle
5557 * and resync will be slowed down even though there might
5558 * not have been non-resync activity. This will only
5559 * happen once though. 'last_events' will soon reflect
5560 * the state where there is little or no outstanding
5561 * resync requests, and further resync activity will
5562 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005563 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005565 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 rdev->last_events = curr_events;
5567 idle = 0;
5568 }
5569 }
NeilBrown4b809912008-07-21 17:05:25 +10005570 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 return idle;
5572}
5573
5574void md_done_sync(mddev_t *mddev, int blocks, int ok)
5575{
5576 /* another "blocks" (512byte) blocks have been synced */
5577 atomic_sub(blocks, &mddev->recovery_active);
5578 wake_up(&mddev->recovery_wait);
5579 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005580 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 md_wakeup_thread(mddev->thread);
5582 // stop recovery, signal do_sync ....
5583 }
5584}
5585
5586
NeilBrown06d91a52005-06-21 17:17:12 -07005587/* md_write_start(mddev, bi)
5588 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005589 * in superblock) before writing, schedule a superblock update
5590 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005591 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005592void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593{
Neil Brown0fd62b82008-06-28 08:31:36 +10005594 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005595 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005596 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005597
NeilBrownf91de922005-11-08 21:39:36 -08005598 BUG_ON(mddev->ro == 1);
5599 if (mddev->ro == 2) {
5600 /* need to switch to read/write */
5601 mddev->ro = 0;
5602 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5603 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005604 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005605 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005606 }
NeilBrown06d91a52005-06-21 17:17:12 -07005607 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005608 if (mddev->safemode == 1)
5609 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005610 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005611 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005612 if (mddev->in_sync) {
5613 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005614 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005615 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005616 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005617 }
NeilBrowna9701a32005-11-08 21:39:34 -08005618 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005619 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005620 if (did_change)
5621 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown09a44cc2008-05-23 13:04:36 -07005622 wait_event(mddev->sb_wait,
5623 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5624 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625}
5626
5627void md_write_end(mddev_t *mddev)
5628{
5629 if (atomic_dec_and_test(&mddev->writes_pending)) {
5630 if (mddev->safemode == 2)
5631 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005632 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5634 }
5635}
5636
NeilBrown2a2275d2007-01-26 00:57:11 -08005637/* md_allow_write(mddev)
5638 * Calling this ensures that the array is marked 'active' so that writes
5639 * may proceed without blocking. It is important to call this before
5640 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5641 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005642 *
5643 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5644 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005645 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005646int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005647{
5648 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005649 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005650 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005651 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005652 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005653 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005654
5655 spin_lock_irq(&mddev->write_lock);
5656 if (mddev->in_sync) {
5657 mddev->in_sync = 0;
5658 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5659 if (mddev->safemode_delay &&
5660 mddev->safemode == 0)
5661 mddev->safemode = 1;
5662 spin_unlock_irq(&mddev->write_lock);
5663 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005664 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown2a2275d2007-01-26 00:57:11 -08005665 } else
5666 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005667
5668 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5669 return -EAGAIN;
5670 else
5671 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005672}
5673EXPORT_SYMBOL_GPL(md_allow_write);
5674
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675#define SYNC_MARKS 10
5676#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005677void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
5679 mddev_t *mddev2;
5680 unsigned int currspeed = 0,
5681 window;
NeilBrown57afd892005-06-21 17:17:13 -07005682 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 unsigned long mark[SYNC_MARKS];
5684 sector_t mark_cnt[SYNC_MARKS];
5685 int last_mark,m;
5686 struct list_head *tmp;
5687 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005688 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005689 struct list_head *rtmp;
5690 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005691 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693 /* just incase thread restarts... */
5694 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5695 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005696 if (mddev->ro) /* never try to sync a read-only array */
5697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
NeilBrown61df9d92006-10-03 01:15:57 -07005699 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5700 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5701 desc = "data-check";
5702 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5703 desc = "requested-resync";
5704 else
5705 desc = "resync";
5706 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5707 desc = "reshape";
5708 else
5709 desc = "recovery";
5710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 /* we overload curr_resync somewhat here.
5712 * 0 == not engaged in resync at all
5713 * 2 == checking that there is no conflict with another sync
5714 * 1 == like 2, but have yielded to allow conflicting resync to
5715 * commense
5716 * other == active in resync - this many blocks
5717 *
5718 * Before starting a resync we must have set curr_resync to
5719 * 2, and then checked that every "conflicting" array has curr_resync
5720 * less than ours. When we find one that is the same or higher
5721 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5722 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5723 * This will mean we have to start checking from the beginning again.
5724 *
5725 */
5726
5727 do {
5728 mddev->curr_resync = 2;
5729
5730 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005731 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005732 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 goto skip;
5734 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005735 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 if (mddev2 == mddev)
5737 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005738 if (!mddev->parallel_resync
5739 && mddev2->curr_resync
5740 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 DEFINE_WAIT(wq);
5742 if (mddev < mddev2 && mddev->curr_resync == 2) {
5743 /* arbitrarily yield */
5744 mddev->curr_resync = 1;
5745 wake_up(&resync_wait);
5746 }
5747 if (mddev > mddev2 && mddev->curr_resync == 1)
5748 /* no need to wait here, we can wait the next
5749 * time 'round when curr_resync == 2
5750 */
5751 continue;
NeilBrown97441972008-09-19 11:49:54 +10005752 /* We need to wait 'interruptible' so as not to
5753 * contribute to the load average, and not to
5754 * be caught by 'softlockup'
5755 */
5756 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08005757 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005758 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005759 printk(KERN_INFO "md: delaying %s of %s"
5760 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005762 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10005764 if (signal_pending(current))
5765 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 schedule();
5767 finish_wait(&resync_wait, &wq);
5768 goto try_again;
5769 }
5770 finish_wait(&resync_wait, &wq);
5771 }
5772 }
5773 } while (mddev->curr_resync < 2);
5774
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005775 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005776 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005778 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 */
5780 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005781 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005782 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005783 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5784 j = mddev->resync_min;
5785 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005786 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005787
NeilBrownccfcc3c2006-03-27 01:18:09 -08005788 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5789 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005790 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 /* recovery follows the physical size of devices */
5792 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005793 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005794 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005795 if (rdev->raid_disk >= 0 &&
5796 !test_bit(Faulty, &rdev->flags) &&
5797 !test_bit(In_sync, &rdev->flags) &&
5798 rdev->recovery_offset < j)
5799 j = rdev->recovery_offset;
5800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
NeilBrown61df9d92006-10-03 01:15:57 -07005802 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5803 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5804 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005805 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005806 "(but not more than %d KB/sec) for %s.\n",
5807 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
5809 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005810
NeilBrown57afd892005-06-21 17:17:13 -07005811 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 for (m = 0; m < SYNC_MARKS; m++) {
5813 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005814 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 }
5816 last_mark = 0;
5817 mddev->resync_mark = mark[last_mark];
5818 mddev->resync_mark_cnt = mark_cnt[last_mark];
5819
5820 /*
5821 * Tune reconstruction:
5822 */
5823 window = 32*(PAGE_SIZE/512);
5824 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5825 window/2,(unsigned long long) max_sectors/2);
5826
5827 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 last_check = 0;
5829
5830 if (j>2) {
5831 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005832 "md: resuming %s of %s from checkpoint.\n",
5833 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 mddev->curr_resync = j;
5835 }
5836
5837 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005838 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
NeilBrown57afd892005-06-21 17:17:13 -07005840 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005841 if (j >= mddev->resync_max) {
5842 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5843 wait_event(mddev->recovery_wait,
5844 mddev->resync_max > j
5845 || kthread_should_stop());
5846 }
5847 if (kthread_should_stop())
5848 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005849 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005850 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005851 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005852 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 goto out;
5854 }
NeilBrown57afd892005-06-21 17:17:13 -07005855
5856 if (!skipped) { /* actual IO requested */
5857 io_sectors += sectors;
5858 atomic_add(sectors, &mddev->recovery_active);
5859 }
5860
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 j += sectors;
5862 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005863 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005864 if (last_check == 0)
5865 /* this is the earliers that rebuilt will be
5866 * visible in /proc/mdstat
5867 */
5868 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005869
5870 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 continue;
5872
NeilBrown57afd892005-06-21 17:17:13 -07005873 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
NeilBrowndfc70642008-05-23 13:04:39 -07005875 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 break;
5877
5878 repeat:
5879 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5880 /* step marks */
5881 int next = (last_mark+1) % SYNC_MARKS;
5882
5883 mddev->resync_mark = mark[next];
5884 mddev->resync_mark_cnt = mark_cnt[next];
5885 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005886 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 last_mark = next;
5888 }
5889
5890
NeilBrownc6207272008-02-06 01:39:52 -08005891 if (kthread_should_stop())
5892 goto interrupted;
5893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
5895 /*
5896 * this loop exits only if either when we are slower than
5897 * the 'hard' speed limit, or the system was IO-idle for
5898 * a jiffy.
5899 * the system might be non-idle CPU-wise, but we only care
5900 * about not overloading the IO subsystem. (things like an
5901 * e2fsck being done on the RAID array should execute fast)
5902 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005903 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 cond_resched();
5905
NeilBrown57afd892005-06-21 17:17:13 -07005906 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5907 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
NeilBrown88202a02006-01-06 00:21:36 -08005909 if (currspeed > speed_min(mddev)) {
5910 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005912 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 goto repeat;
5914 }
5915 }
5916 }
NeilBrown61df9d92006-10-03 01:15:57 -07005917 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 /*
5919 * this also signals 'finished resyncing' to md_stop
5920 */
5921 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005922 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5925
5926 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005927 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928
NeilBrowndfc70642008-05-23 13:04:39 -07005929 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005930 mddev->curr_resync > 2) {
5931 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5932 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5933 if (mddev->curr_resync >= mddev->recovery_cp) {
5934 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005935 "md: checkpointing %s of %s.\n",
5936 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005937 mddev->recovery_cp = mddev->curr_resync;
5938 }
5939 } else
5940 mddev->recovery_cp = MaxSector;
5941 } else {
5942 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5943 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005944 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005945 if (rdev->raid_disk >= 0 &&
5946 !test_bit(Faulty, &rdev->flags) &&
5947 !test_bit(In_sync, &rdev->flags) &&
5948 rdev->recovery_offset < mddev->curr_resync)
5949 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 }
NeilBrown17571282006-12-10 02:20:52 -08005952 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 skip:
5955 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005956 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005957 mddev->resync_max = MaxSector;
5958 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 wake_up(&resync_wait);
5960 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5961 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005962 return;
5963
5964 interrupted:
5965 /*
5966 * got a signal, exit.
5967 */
5968 printk(KERN_INFO
5969 "md: md_do_sync() got signal ... exiting\n");
5970 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5971 goto out;
5972
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973}
NeilBrown29269552006-03-27 01:18:10 -08005974EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
5976
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005977static int remove_and_add_spares(mddev_t *mddev)
5978{
5979 mdk_rdev_t *rdev;
5980 struct list_head *rtmp;
5981 int spares = 0;
5982
NeilBrownd089c6a2008-02-06 01:39:59 -08005983 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005984 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005985 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005986 (test_bit(Faulty, &rdev->flags) ||
5987 ! test_bit(In_sync, &rdev->flags)) &&
5988 atomic_read(&rdev->nr_pending)==0) {
5989 if (mddev->pers->hot_remove_disk(
5990 mddev, rdev->raid_disk)==0) {
5991 char nm[20];
5992 sprintf(nm,"rd%d", rdev->raid_disk);
5993 sysfs_remove_link(&mddev->kobj, nm);
5994 rdev->raid_disk = -1;
5995 }
5996 }
5997
NeilBrownc89a8ee2008-08-05 15:54:13 +10005998 if (mddev->degraded && ! mddev->ro) {
NeilBrowndfc70642008-05-23 13:04:39 -07005999 rdev_for_each(rdev, rtmp, mddev) {
6000 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006001 !test_bit(In_sync, &rdev->flags) &&
6002 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006003 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006004 if (rdev->raid_disk < 0
6005 && !test_bit(Faulty, &rdev->flags)) {
6006 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006007 if (mddev->pers->
6008 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006009 char nm[20];
6010 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006011 if (sysfs_create_link(&mddev->kobj,
6012 &rdev->kobj, nm))
6013 printk(KERN_WARNING
6014 "md: cannot register "
6015 "%s for %s\n",
6016 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006017 spares++;
6018 md_new_event(mddev);
6019 } else
6020 break;
6021 }
NeilBrowndfc70642008-05-23 13:04:39 -07006022 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006023 }
6024 return spares;
6025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026/*
6027 * This routine is regularly called by all per-raid-array threads to
6028 * deal with generic issues like resync and super-block update.
6029 * Raid personalities that don't have a thread (linear/raid0) do not
6030 * need this as they never do any recovery or update the superblock.
6031 *
6032 * It does not do any resync itself, but rather "forks" off other threads
6033 * to do that as needed.
6034 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6035 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006036 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 * and wakeups up this thread which will reap the thread and finish up.
6038 * This thread also removes any faulty devices (with nr_pending == 0).
6039 *
6040 * The overall approach is:
6041 * 1/ if the superblock needs updating, update it.
6042 * 2/ If a recovery thread is running, don't do anything else.
6043 * 3/ If recovery has finished, clean up, possibly marking spares active.
6044 * 4/ If there are any faulty devices, remove them.
6045 * 5/ If array is degraded, try to add spares devices
6046 * 6/ If array has spares or is not in-sync, start a resync thread.
6047 */
6048void md_check_recovery(mddev_t *mddev)
6049{
6050 mdk_rdev_t *rdev;
6051 struct list_head *rtmp;
6052
6053
NeilBrown5f404022005-06-21 17:17:16 -07006054 if (mddev->bitmap)
6055 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
Dan Williamsd8e64402008-07-23 13:09:48 -07006057 if (test_and_clear_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags))
6058 sysfs_notify(&mddev->kobj, NULL, "array_state");
6059
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 if (mddev->ro)
6061 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006062
6063 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006064 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006065 printk(KERN_INFO "md: %s in immediate safe mode\n",
6066 mdname(mddev));
6067 mddev->safemode = 2;
6068 }
6069 flush_signals(current);
6070 }
6071
NeilBrownc89a8ee2008-08-05 15:54:13 +10006072 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6073 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006075 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006077 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006078 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006079 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6080 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 ))
6082 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006083
NeilBrowndf5b89b2006-03-27 01:18:20 -08006084 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006085 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006086
NeilBrownc89a8ee2008-08-05 15:54:13 +10006087 if (mddev->ro) {
6088 /* Only thing we do on a ro array is remove
6089 * failed devices.
6090 */
6091 remove_and_add_spares(mddev);
6092 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6093 goto unlock;
6094 }
6095
NeilBrown31a59e32008-04-30 00:52:30 -07006096 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006097 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006098 spin_lock_irq(&mddev->write_lock);
6099 if (mddev->safemode &&
6100 !atomic_read(&mddev->writes_pending) &&
6101 !mddev->in_sync &&
6102 mddev->recovery_cp == MaxSector) {
6103 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006104 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006105 if (mddev->persistent)
6106 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6107 }
6108 if (mddev->safemode == 1)
6109 mddev->safemode = 0;
6110 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006111 if (did_change)
6112 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrownfca4d842005-06-21 17:17:11 -07006113 }
NeilBrownfca4d842005-06-21 17:17:11 -07006114
NeilBrown850b2b42006-10-03 01:15:46 -07006115 if (mddev->flags)
6116 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006117
Neil Brown52664732008-06-28 08:31:44 +10006118 rdev_for_each(rdev, rtmp, mddev)
6119 if (test_and_clear_bit(StateChanged, &rdev->flags))
6120 sysfs_notify(&rdev->kobj, NULL, "state");
6121
NeilBrown06d91a52005-06-21 17:17:12 -07006122
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6124 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6125 /* resync/recovery still happening */
6126 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6127 goto unlock;
6128 }
6129 if (mddev->sync_thread) {
6130 /* resync has finished, collect result */
6131 md_unregister_thread(mddev->sync_thread);
6132 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006133 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6134 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 /* success...*/
6136 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006137 if (mddev->pers->spare_active(mddev))
6138 sysfs_notify(&mddev->kobj, NULL,
6139 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 }
NeilBrown850b2b42006-10-03 01:15:46 -07006141 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006142
6143 /* if array is no-longer degraded, then any saved_raid_disk
6144 * information must be scrapped
6145 */
6146 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006147 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006148 rdev->saved_raid_disk = -1;
6149
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 mddev->recovery = 0;
6151 /* flag recovery needed just to double check */
6152 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006153 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006154 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 goto unlock;
6156 }
Neil Brown72a23c22008-06-28 08:31:41 +10006157 /* Set RUNNING before clearing NEEDED to avoid
6158 * any transients in the value of "sync_action".
6159 */
6160 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6161 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006162 /* Clear some bits that don't mean anything, but
6163 * might be left set
6164 */
NeilBrown24dd4692005-11-08 21:39:26 -08006165 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6166 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006168 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6169 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 /* no recovery is running.
6171 * remove any failed drives, then
6172 * add spares if possible.
6173 * Spare are also removed and re-added, to allow
6174 * the personality to fail the re-add.
6175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006177 if (mddev->reshape_position != MaxSector) {
6178 if (mddev->pers->check_reshape(mddev) != 0)
6179 /* Cannot proceed */
6180 goto unlock;
6181 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006182 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006183 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006184 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6185 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006186 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006187 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006188 } else if (mddev->recovery_cp < MaxSector) {
6189 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006190 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006191 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6192 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006194
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006196 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6197 /* We are adding a device or devices to an array
6198 * which has the bitmap stored on all devices.
6199 * So make sure all bitmap pages get written
6200 */
6201 bitmap_write_all(mddev->bitmap);
6202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 mddev->sync_thread = md_register_thread(md_do_sync,
6204 mddev,
6205 "%s_resync");
6206 if (!mddev->sync_thread) {
6207 printk(KERN_ERR "%s: could not start resync"
6208 " thread...\n",
6209 mdname(mddev));
6210 /* leave the spares where they are, it shouldn't hurt */
6211 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006212 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006214 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006215 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 }
6217 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006218 if (!mddev->sync_thread) {
6219 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6220 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6221 &mddev->recovery))
6222 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 mddev_unlock(mddev);
6225 }
6226}
6227
Dan Williams6bfe0b42008-04-30 00:52:32 -07006228void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6229{
6230 sysfs_notify(&rdev->kobj, NULL, "state");
6231 wait_event_timeout(rdev->blocked_wait,
6232 !test_bit(Blocked, &rdev->flags),
6233 msecs_to_jiffies(5000));
6234 rdev_dec_pending(rdev, mddev);
6235}
6236EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6237
Adrian Bunk75c96f82005-05-05 16:16:09 -07006238static int md_notify_reboot(struct notifier_block *this,
6239 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240{
6241 struct list_head *tmp;
6242 mddev_t *mddev;
6243
6244 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6245
6246 printk(KERN_INFO "md: stopping all md devices.\n");
6247
NeilBrown29ac4aa2008-02-06 01:39:58 -08006248 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006249 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006250 /* Force a switch to readonly even array
6251 * appears to still be in use. Hence
6252 * the '100'.
6253 */
NeilBrownd710e132008-10-13 11:55:12 +11006254 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006255 mddev_unlock(mddev);
6256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 /*
6258 * certain more exotic SCSI devices are known to be
6259 * volatile wrt too early system reboots. While the
6260 * right place to handle this issue is the given
6261 * driver, we do want to have a safe RAID driver ...
6262 */
6263 mdelay(1000*1);
6264 }
6265 return NOTIFY_DONE;
6266}
6267
Adrian Bunk75c96f82005-05-05 16:16:09 -07006268static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 .notifier_call = md_notify_reboot,
6270 .next = NULL,
6271 .priority = INT_MAX, /* before any real devices */
6272};
6273
6274static void md_geninit(void)
6275{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6277
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006278 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279}
6280
Adrian Bunk75c96f82005-05-05 16:16:09 -07006281static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 if (register_blkdev(MAJOR_NR, "md"))
6284 return -1;
6285 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6286 unregister_blkdev(MAJOR_NR, "md");
6287 return -1;
6288 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006289 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6290 md_probe, NULL, NULL);
6291 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 md_probe, NULL, NULL);
6293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006295 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
6297 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299}
6300
6301
6302#ifndef MODULE
6303
6304/*
6305 * Searches all registered partitions for autorun RAID arrays
6306 * at boot time.
6307 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006308
6309static LIST_HEAD(all_detected_devices);
6310struct detected_devices_node {
6311 struct list_head list;
6312 dev_t dev;
6313};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
6315void md_autodetect_dev(dev_t dev)
6316{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006317 struct detected_devices_node *node_detected_dev;
6318
6319 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6320 if (node_detected_dev) {
6321 node_detected_dev->dev = dev;
6322 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6323 } else {
6324 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6325 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327}
6328
6329
6330static void autostart_arrays(int part)
6331{
6332 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006333 struct detected_devices_node *node_detected_dev;
6334 dev_t dev;
6335 int i_scanned, i_passed;
6336
6337 i_scanned = 0;
6338 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
6340 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6341
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006342 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6343 i_scanned++;
6344 node_detected_dev = list_entry(all_detected_devices.next,
6345 struct detected_devices_node, list);
6346 list_del(&node_detected_dev->list);
6347 dev = node_detected_dev->dev;
6348 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006349 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 if (IS_ERR(rdev))
6351 continue;
6352
NeilBrownb2d444d2005-11-08 21:39:31 -08006353 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 MD_BUG();
6355 continue;
6356 }
NeilBrownd0fae182008-03-04 14:29:31 -08006357 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006359 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006361
6362 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6363 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
6365 autorun_devices(part);
6366}
6367
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006368#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
6370static __exit void md_exit(void)
6371{
6372 mddev_t *mddev;
6373 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006374
NeilBrowne8703fe2006-10-03 01:15:59 -07006375 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6376 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 unregister_blkdev(MAJOR_NR,"md");
6379 unregister_blkdev(mdp_major, "mdp");
6380 unregister_reboot_notifier(&md_notifier);
6381 unregister_sysctl_table(raid_table_header);
6382 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006383 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 struct gendisk *disk = mddev->gendisk;
6385 if (!disk)
6386 continue;
6387 export_array(mddev);
6388 del_gendisk(disk);
6389 put_disk(disk);
6390 mddev->gendisk = NULL;
6391 mddev_put(mddev);
6392 }
6393}
6394
Dan Williams685784a2007-07-09 11:56:42 -07006395subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396module_exit(md_exit)
6397
NeilBrownf91de922005-11-08 21:39:36 -08006398static int get_ro(char *buffer, struct kernel_param *kp)
6399{
6400 return sprintf(buffer, "%d", start_readonly);
6401}
6402static int set_ro(const char *val, struct kernel_param *kp)
6403{
6404 char *e;
6405 int num = simple_strtoul(val, &e, 10);
6406 if (*val && (*e == '\0' || *e == '\n')) {
6407 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006408 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006409 }
6410 return -EINVAL;
6411}
6412
NeilBrown80ca3a42006-07-10 04:44:18 -07006413module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6414module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08006415
NeilBrownf91de922005-11-08 21:39:36 -08006416
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417EXPORT_SYMBOL(register_md_personality);
6418EXPORT_SYMBOL(unregister_md_personality);
6419EXPORT_SYMBOL(md_error);
6420EXPORT_SYMBOL(md_done_sync);
6421EXPORT_SYMBOL(md_write_start);
6422EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423EXPORT_SYMBOL(md_register_thread);
6424EXPORT_SYMBOL(md_unregister_thread);
6425EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426EXPORT_SYMBOL(md_check_recovery);
6427MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006428MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006429MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);