Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | 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 | |
| 35 | #include <linux/module.h> |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 36 | #include <linux/kernel.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 37 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/linkage.h> |
| 39 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 40 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 43 | #include <linux/poll.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 44 | #include <linux/mutex.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 45 | #include <linux/ctype.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 46 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | #include <linux/init.h> |
| 49 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 50 | #include <linux/file.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_KMOD |
| 53 | #include <linux/kmod.h> |
| 54 | #endif |
| 55 | |
| 56 | #include <asm/unaligned.h> |
| 57 | |
| 58 | #define MAJOR_NR MD_MAJOR |
| 59 | #define MD_DRIVER |
| 60 | |
| 61 | /* 63 partitions with the alternate major number (mdp) */ |
| 62 | #define MdpMinorShift 6 |
| 63 | |
| 64 | #define DEBUG 0 |
| 65 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 66 | |
| 67 | |
| 68 | #ifndef MODULE |
| 69 | static void autostart_arrays (int part); |
| 70 | #endif |
| 71 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 72 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static DEFINE_SPINLOCK(pers_lock); |
| 74 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 75 | static void md_print_devices(void); |
| 76 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 77 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 78 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 79 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
| 112 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 113 | .procname = "speed_limit_min", |
| 114 | .data = &sysctl_speed_limit_min, |
| 115 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 116 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .proc_handler = &proc_dointvec, |
| 118 | }, |
| 119 | { |
| 120 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 121 | .procname = "speed_limit_max", |
| 122 | .data = &sysctl_speed_limit_max, |
| 123 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .proc_handler = &proc_dointvec, |
| 126 | }, |
| 127 | { .ctl_name = 0 } |
| 128 | }; |
| 129 | |
| 130 | static ctl_table raid_dir_table[] = { |
| 131 | { |
| 132 | .ctl_name = DEV_RAID, |
| 133 | .procname = "raid", |
| 134 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 135 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .child = raid_table, |
| 137 | }, |
| 138 | { .ctl_name = 0 } |
| 139 | }; |
| 140 | |
| 141 | static ctl_table raid_root_table[] = { |
| 142 | { |
| 143 | .ctl_name = CTL_DEV, |
| 144 | .procname = "dev", |
| 145 | .maxlen = 0, |
| 146 | .mode = 0555, |
| 147 | .child = raid_dir_table, |
| 148 | }, |
| 149 | { .ctl_name = 0 } |
| 150 | }; |
| 151 | |
| 152 | static struct block_device_operations md_fops; |
| 153 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 154 | static int start_readonly; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 157 | * We have a system wide 'event count' that is incremented |
| 158 | * on any 'interesting' event, and readers of /proc/mdstat |
| 159 | * can use 'poll' or 'select' to find out when the event |
| 160 | * count increases. |
| 161 | * |
| 162 | * Events are: |
| 163 | * start array, stop array, error, add device, remove device, |
| 164 | * start build, activate spare |
| 165 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 166 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 167 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 168 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 169 | { |
| 170 | atomic_inc(&md_event_count); |
| 171 | wake_up(&md_event_waiters); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 172 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 173 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 174 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 175 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 176 | /* Alternate version that can be called from interrupts |
| 177 | * when calling sysfs_notify isn't needed. |
| 178 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 179 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 180 | { |
| 181 | atomic_inc(&md_event_count); |
| 182 | wake_up(&md_event_waiters); |
| 183 | } |
| 184 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 185 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | * Enables to iterate over all existing md arrays |
| 187 | * all_mddevs_lock protects this list. |
| 188 | */ |
| 189 | static LIST_HEAD(all_mddevs); |
| 190 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 191 | |
| 192 | |
| 193 | /* |
| 194 | * iterates through all used mddevs in the system. |
| 195 | * We take care to grab the all_mddevs_lock whenever navigating |
| 196 | * the list, and to always hold a refcount when unlocked. |
| 197 | * Any code which breaks out of this loop while own |
| 198 | * a reference to the current mddev and must mddev_put it. |
| 199 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 200 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | \ |
| 202 | for (({ spin_lock(&all_mddevs_lock); \ |
| 203 | tmp = all_mddevs.next; \ |
| 204 | mddev = NULL;}); \ |
| 205 | ({ if (tmp != &all_mddevs) \ |
| 206 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 207 | spin_unlock(&all_mddevs_lock); \ |
| 208 | if (mddev) mddev_put(mddev); \ |
| 209 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 210 | tmp != &all_mddevs;}); \ |
| 211 | ({ spin_lock(&all_mddevs_lock); \ |
| 212 | tmp = tmp->next;}) \ |
| 213 | ) |
| 214 | |
| 215 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 216 | static int md_fail_request (struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 218 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 223 | { |
| 224 | atomic_inc(&mddev->active); |
| 225 | return mddev; |
| 226 | } |
| 227 | |
| 228 | static void mddev_put(mddev_t *mddev) |
| 229 | { |
| 230 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 231 | return; |
| 232 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 233 | list_del(&mddev->all_mddevs); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 234 | spin_unlock(&all_mddevs_lock); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 235 | blk_cleanup_queue(mddev->queue); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 236 | kobject_put(&mddev->kobj); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 237 | } else |
| 238 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | static mddev_t * mddev_find(dev_t unit) |
| 242 | { |
| 243 | mddev_t *mddev, *new = NULL; |
| 244 | |
| 245 | retry: |
| 246 | spin_lock(&all_mddevs_lock); |
| 247 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 248 | if (mddev->unit == unit) { |
| 249 | mddev_get(mddev); |
| 250 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 251 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return mddev; |
| 253 | } |
| 254 | |
| 255 | if (new) { |
| 256 | list_add(&new->all_mddevs, &all_mddevs); |
| 257 | spin_unlock(&all_mddevs_lock); |
| 258 | return new; |
| 259 | } |
| 260 | spin_unlock(&all_mddevs_lock); |
| 261 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 262 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (!new) |
| 264 | return NULL; |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | new->unit = unit; |
| 267 | if (MAJOR(unit) == MD_MAJOR) |
| 268 | new->md_minor = MINOR(unit); |
| 269 | else |
| 270 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 271 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 272 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | INIT_LIST_HEAD(&new->disks); |
| 274 | INIT_LIST_HEAD(&new->all_mddevs); |
| 275 | init_timer(&new->safemode_timer); |
| 276 | atomic_set(&new->active, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 277 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 278 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 279 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 280 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 281 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 282 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 283 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | new->queue = blk_alloc_queue(GFP_KERNEL); |
| 286 | if (!new->queue) { |
| 287 | kfree(new); |
| 288 | return NULL; |
| 289 | } |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 290 | /* Can be unlocked because the queue is new: no concurrency */ |
| 291 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | blk_queue_make_request(new->queue, md_fail_request); |
| 294 | |
| 295 | goto retry; |
| 296 | } |
| 297 | |
| 298 | static inline int mddev_lock(mddev_t * mddev) |
| 299 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 300 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | static inline int mddev_trylock(mddev_t * mddev) |
| 304 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 305 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static inline void mddev_unlock(mddev_t * mddev) |
| 309 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 310 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 312 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 315 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
| 317 | mdk_rdev_t * rdev; |
| 318 | struct list_head *tmp; |
| 319 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 320 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | if (rdev->desc_nr == nr) |
| 322 | return rdev; |
| 323 | } |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 328 | { |
| 329 | struct list_head *tmp; |
| 330 | mdk_rdev_t *rdev; |
| 331 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 332 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (rdev->bdev->bd_dev == dev) |
| 334 | return rdev; |
| 335 | } |
| 336 | return NULL; |
| 337 | } |
| 338 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 339 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 340 | { |
| 341 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 342 | list_for_each_entry(pers, &pers_list, list) { |
| 343 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 344 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 345 | if (strcmp(pers->name, clevel)==0) |
| 346 | return pers; |
| 347 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 348 | return NULL; |
| 349 | } |
| 350 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 351 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
| 353 | sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 354 | return MD_NEW_SIZE_BLOCKS(size); |
| 355 | } |
| 356 | |
| 357 | static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size) |
| 358 | { |
| 359 | sector_t size; |
| 360 | |
| 361 | size = rdev->sb_offset; |
| 362 | |
| 363 | if (chunk_size) |
| 364 | size &= ~((sector_t)chunk_size/1024 - 1); |
| 365 | return size; |
| 366 | } |
| 367 | |
| 368 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 369 | { |
| 370 | if (rdev->sb_page) |
| 371 | MD_BUG(); |
| 372 | |
| 373 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 374 | if (!rdev->sb_page) { |
| 375 | printk(KERN_ALERT "md: out of memory.\n"); |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 383 | { |
| 384 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 385 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | rdev->sb_loaded = 0; |
| 387 | rdev->sb_page = NULL; |
| 388 | rdev->sb_offset = 0; |
| 389 | rdev->size = 0; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 394 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 395 | { |
| 396 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 397 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 398 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 399 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 400 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 401 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 402 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 403 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 404 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 405 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 406 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 407 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 408 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 409 | } |
| 410 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 411 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 412 | { |
| 413 | struct bio *bio2 = bio->bi_private; |
| 414 | mdk_rdev_t *rdev = bio2->bi_private; |
| 415 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 416 | |
| 417 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 418 | error == -EOPNOTSUPP) { |
| 419 | unsigned long flags; |
| 420 | /* barriers don't appear to be supported :-( */ |
| 421 | set_bit(BarriersNotsupp, &rdev->flags); |
| 422 | mddev->barriers_work = 0; |
| 423 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 424 | bio2->bi_next = mddev->biolist; |
| 425 | mddev->biolist = bio2; |
| 426 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 427 | wake_up(&mddev->sb_wait); |
| 428 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 429 | } else { |
| 430 | bio_put(bio2); |
| 431 | bio->bi_private = rdev; |
| 432 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 433 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 434 | } |
| 435 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 436 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 437 | sector_t sector, int size, struct page *page) |
| 438 | { |
| 439 | /* write first size bytes of page to sector of rdev |
| 440 | * Increment mddev->pending_writes before returning |
| 441 | * and decrement it on completion, waking up sb_wait |
| 442 | * if zero is reached. |
| 443 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 444 | * |
| 445 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 446 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 447 | */ |
| 448 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 449 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 450 | |
| 451 | bio->bi_bdev = rdev->bdev; |
| 452 | bio->bi_sector = sector; |
| 453 | bio_add_page(bio, page, size, 0); |
| 454 | bio->bi_private = rdev; |
| 455 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 456 | bio->bi_rw = rw; |
| 457 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 458 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 459 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 460 | struct bio *rbio; |
| 461 | rw |= (1<<BIO_RW_BARRIER); |
| 462 | rbio = bio_clone(bio, GFP_NOIO); |
| 463 | rbio->bi_private = bio; |
| 464 | rbio->bi_end_io = super_written_barrier; |
| 465 | submit_bio(rw, rbio); |
| 466 | } else |
| 467 | submit_bio(rw, bio); |
| 468 | } |
| 469 | |
| 470 | void md_super_wait(mddev_t *mddev) |
| 471 | { |
| 472 | /* wait for all superblock writes that were scheduled to complete. |
| 473 | * if any had to be retried (due to BARRIER problems), retry them |
| 474 | */ |
| 475 | DEFINE_WAIT(wq); |
| 476 | for(;;) { |
| 477 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 478 | if (atomic_read(&mddev->pending_writes)==0) |
| 479 | break; |
| 480 | while (mddev->biolist) { |
| 481 | struct bio *bio; |
| 482 | spin_lock_irq(&mddev->write_lock); |
| 483 | bio = mddev->biolist; |
| 484 | mddev->biolist = bio->bi_next ; |
| 485 | bio->bi_next = NULL; |
| 486 | spin_unlock_irq(&mddev->write_lock); |
| 487 | submit_bio(bio->bi_rw, bio); |
| 488 | } |
| 489 | schedule(); |
| 490 | } |
| 491 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 492 | } |
| 493 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 494 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 499 | int sync_page_io(struct block_device *bdev, sector_t sector, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | struct page *page, int rw) |
| 501 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 502 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | struct completion event; |
| 504 | int ret; |
| 505 | |
| 506 | rw |= (1 << BIO_RW_SYNC); |
| 507 | |
| 508 | bio->bi_bdev = bdev; |
| 509 | bio->bi_sector = sector; |
| 510 | bio_add_page(bio, page, size, 0); |
| 511 | init_completion(&event); |
| 512 | bio->bi_private = &event; |
| 513 | bio->bi_end_io = bi_complete; |
| 514 | submit_bio(rw, bio); |
| 515 | wait_for_completion(&event); |
| 516 | |
| 517 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 518 | bio_put(bio); |
| 519 | return ret; |
| 520 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 521 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 523 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
| 525 | char b[BDEVNAME_SIZE]; |
| 526 | if (!rdev->sb_page) { |
| 527 | MD_BUG(); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | if (rdev->sb_loaded) |
| 531 | return 0; |
| 532 | |
| 533 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 534 | if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | goto fail; |
| 536 | rdev->sb_loaded = 1; |
| 537 | return 0; |
| 538 | |
| 539 | fail: |
| 540 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 541 | bdevname(rdev->bdev,b)); |
| 542 | return -EINVAL; |
| 543 | } |
| 544 | |
| 545 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 546 | { |
| 547 | if ( (sb1->set_uuid0 == sb2->set_uuid0) && |
| 548 | (sb1->set_uuid1 == sb2->set_uuid1) && |
| 549 | (sb1->set_uuid2 == sb2->set_uuid2) && |
| 550 | (sb1->set_uuid3 == sb2->set_uuid3)) |
| 551 | |
| 552 | return 1; |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | |
| 558 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 559 | { |
| 560 | int ret; |
| 561 | mdp_super_t *tmp1, *tmp2; |
| 562 | |
| 563 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 564 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 565 | |
| 566 | if (!tmp1 || !tmp2) { |
| 567 | ret = 0; |
| 568 | printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n"); |
| 569 | goto abort; |
| 570 | } |
| 571 | |
| 572 | *tmp1 = *sb1; |
| 573 | *tmp2 = *sb2; |
| 574 | |
| 575 | /* |
| 576 | * nr_disks is not constant |
| 577 | */ |
| 578 | tmp1->nr_disks = 0; |
| 579 | tmp2->nr_disks = 0; |
| 580 | |
| 581 | if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4)) |
| 582 | ret = 0; |
| 583 | else |
| 584 | ret = 1; |
| 585 | |
| 586 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 587 | kfree(tmp1); |
| 588 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | return ret; |
| 590 | } |
| 591 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 592 | |
| 593 | static u32 md_csum_fold(u32 csum) |
| 594 | { |
| 595 | csum = (csum & 0xffff) + (csum >> 16); |
| 596 | return (csum & 0xffff) + (csum >> 16); |
| 597 | } |
| 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 600 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 601 | u64 newcsum = 0; |
| 602 | u32 *sb32 = (u32*)sb; |
| 603 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | unsigned int disk_csum, csum; |
| 605 | |
| 606 | disk_csum = sb->sb_csum; |
| 607 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 608 | |
| 609 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 610 | newcsum += sb32[i]; |
| 611 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 612 | |
| 613 | |
| 614 | #ifdef CONFIG_ALPHA |
| 615 | /* This used to use csum_partial, which was wrong for several |
| 616 | * reasons including that different results are returned on |
| 617 | * different architectures. It isn't critical that we get exactly |
| 618 | * the same return value as before (we always csum_fold before |
| 619 | * testing, and that removes any differences). However as we |
| 620 | * know that csum_partial always returned a 16bit value on |
| 621 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 622 | */ |
| 623 | sb->sb_csum = md_csum_fold(disk_csum); |
| 624 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 626 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | return csum; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /* |
| 632 | * Handle superblock details. |
| 633 | * We want to be able to handle multiple superblock formats |
| 634 | * so we have a common interface to them all, and an array of |
| 635 | * different handlers. |
| 636 | * We rely on user-space to write the initial superblock, and support |
| 637 | * reading and updating of superblocks. |
| 638 | * Interface methods are: |
| 639 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 640 | * loads and validates a superblock on dev. |
| 641 | * if refdev != NULL, compare superblocks on both devices |
| 642 | * Return: |
| 643 | * 0 - dev has a superblock that is compatible with refdev |
| 644 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 645 | * so dev should be used as the refdev in future |
| 646 | * -EINVAL superblock incompatible or invalid |
| 647 | * -othererror e.g. -EIO |
| 648 | * |
| 649 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 650 | * Verify that dev is acceptable into mddev. |
| 651 | * The first time, mddev->raid_disks will be 0, and data from |
| 652 | * dev should be merged in. Subsequent calls check that dev |
| 653 | * is new enough. Return 0 or -EINVAL |
| 654 | * |
| 655 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 656 | * Update the superblock for rdev with data in mddev |
| 657 | * This does not write to disc. |
| 658 | * |
| 659 | */ |
| 660 | |
| 661 | struct super_type { |
| 662 | char *name; |
| 663 | struct module *owner; |
| 664 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version); |
| 665 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 666 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 667 | }; |
| 668 | |
| 669 | /* |
| 670 | * load_super for 0.90.0 |
| 671 | */ |
| 672 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 673 | { |
| 674 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 675 | mdp_super_t *sb; |
| 676 | int ret; |
| 677 | sector_t sb_offset; |
| 678 | |
| 679 | /* |
| 680 | * Calculate the position of the superblock, |
| 681 | * it's at the end of the disk. |
| 682 | * |
| 683 | * It also happens to be a multiple of 4Kb. |
| 684 | */ |
| 685 | sb_offset = calc_dev_sboffset(rdev->bdev); |
| 686 | rdev->sb_offset = sb_offset; |
| 687 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 688 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | if (ret) return ret; |
| 690 | |
| 691 | ret = -EINVAL; |
| 692 | |
| 693 | bdevname(rdev->bdev, b); |
| 694 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 695 | |
| 696 | if (sb->md_magic != MD_SB_MAGIC) { |
| 697 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 698 | b); |
| 699 | goto abort; |
| 700 | } |
| 701 | |
| 702 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 703 | sb->minor_version < 90 || |
| 704 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 706 | sb->major_version, sb->minor_version, |
| 707 | b); |
| 708 | goto abort; |
| 709 | } |
| 710 | |
| 711 | if (sb->raid_disks <= 0) |
| 712 | goto abort; |
| 713 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 714 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 716 | b); |
| 717 | goto abort; |
| 718 | } |
| 719 | |
| 720 | rdev->preferred_minor = sb->md_minor; |
| 721 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 722 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 724 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 725 | if (sb->level != 1 && sb->level != 4 |
| 726 | && sb->level != 5 && sb->level != 6 |
| 727 | && sb->level != 10) { |
| 728 | /* FIXME use a better test */ |
| 729 | printk(KERN_WARNING |
| 730 | "md: bitmaps not supported for this level.\n"); |
| 731 | goto abort; |
| 732 | } |
| 733 | } |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | if (sb->level == LEVEL_MULTIPATH) |
| 736 | rdev->desc_nr = -1; |
| 737 | else |
| 738 | rdev->desc_nr = sb->this_disk.number; |
| 739 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 740 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 742 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | __u64 ev1, ev2; |
| 744 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 745 | if (!uuid_equal(refsb, sb)) { |
| 746 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 747 | b, bdevname(refdev->bdev,b2)); |
| 748 | goto abort; |
| 749 | } |
| 750 | if (!sb_equal(refsb, sb)) { |
| 751 | printk(KERN_WARNING "md: %s has same UUID" |
| 752 | " but different superblock to %s\n", |
| 753 | b, bdevname(refdev->bdev, b2)); |
| 754 | goto abort; |
| 755 | } |
| 756 | ev1 = md_event(sb); |
| 757 | ev2 = md_event(refsb); |
| 758 | if (ev1 > ev2) |
| 759 | ret = 1; |
| 760 | else |
| 761 | ret = 0; |
| 762 | } |
| 763 | rdev->size = calc_dev_size(rdev, sb->chunk_size); |
| 764 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 765 | if (rdev->size < sb->size && sb->level > 1) |
| 766 | /* "this cannot possibly happen" ... */ |
| 767 | ret = -EINVAL; |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | abort: |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * validate_super for 0.90.0 |
| 775 | */ |
| 776 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 777 | { |
| 778 | mdp_disk_t *desc; |
| 779 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 780 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 782 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 783 | clear_bit(Faulty, &rdev->flags); |
| 784 | clear_bit(In_sync, &rdev->flags); |
| 785 | clear_bit(WriteMostly, &rdev->flags); |
| 786 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | if (mddev->raid_disks == 0) { |
| 789 | mddev->major_version = 0; |
| 790 | mddev->minor_version = sb->minor_version; |
| 791 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 792 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | mddev->chunk_size = sb->chunk_size; |
| 794 | mddev->ctime = sb->ctime; |
| 795 | mddev->utime = sb->utime; |
| 796 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 797 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | mddev->layout = sb->layout; |
| 799 | mddev->raid_disks = sb->raid_disks; |
| 800 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 801 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 802 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 803 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 805 | if (mddev->minor_version >= 91) { |
| 806 | mddev->reshape_position = sb->reshape_position; |
| 807 | mddev->delta_disks = sb->delta_disks; |
| 808 | mddev->new_level = sb->new_level; |
| 809 | mddev->new_layout = sb->new_layout; |
| 810 | mddev->new_chunk = sb->new_chunk; |
| 811 | } else { |
| 812 | mddev->reshape_position = MaxSector; |
| 813 | mddev->delta_disks = 0; |
| 814 | mddev->new_level = mddev->level; |
| 815 | mddev->new_layout = mddev->layout; |
| 816 | mddev->new_chunk = mddev->chunk_size; |
| 817 | } |
| 818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 820 | mddev->recovery_cp = MaxSector; |
| 821 | else { |
| 822 | if (sb->events_hi == sb->cp_events_hi && |
| 823 | sb->events_lo == sb->cp_events_lo) { |
| 824 | mddev->recovery_cp = sb->recovery_cp; |
| 825 | } else |
| 826 | mddev->recovery_cp = 0; |
| 827 | } |
| 828 | |
| 829 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 830 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 831 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 832 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 833 | |
| 834 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 835 | |
| 836 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 837 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 838 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 839 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 840 | } else if (mddev->pers == NULL) { |
| 841 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | ++ev1; |
| 843 | if (ev1 < mddev->events) |
| 844 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 845 | } else if (mddev->bitmap) { |
| 846 | /* if adding to array with a bitmap, then we can accept an |
| 847 | * older device ... but not too old. |
| 848 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 849 | if (ev1 < mddev->bitmap->events_cleared) |
| 850 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 851 | } else { |
| 852 | if (ev1 < mddev->events) |
| 853 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 854 | return 0; |
| 855 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | desc = sb->disks + rdev->desc_nr; |
| 859 | |
| 860 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 861 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 862 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 863 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 864 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | rdev->raid_disk = desc->raid_disk; |
| 866 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 867 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 868 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 869 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 870 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * sync_super for 0.90.0 |
| 876 | */ |
| 877 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 878 | { |
| 879 | mdp_super_t *sb; |
| 880 | struct list_head *tmp; |
| 881 | mdk_rdev_t *rdev2; |
| 882 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | /* make rdev->sb match mddev data.. |
| 886 | * |
| 887 | * 1/ zero out disks |
| 888 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 889 | * 3/ any empty disks < next_spare become removed |
| 890 | * |
| 891 | * disks[0] gets initialised to REMOVED because |
| 892 | * we cannot be sure from other fields if it has |
| 893 | * been initialised or not. |
| 894 | */ |
| 895 | int i; |
| 896 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 897 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 898 | rdev->sb_size = MD_SB_BYTES; |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 901 | |
| 902 | memset(sb, 0, sizeof(*sb)); |
| 903 | |
| 904 | sb->md_magic = MD_SB_MAGIC; |
| 905 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | sb->patch_version = mddev->patch_version; |
| 907 | sb->gvalid_words = 0; /* ignored */ |
| 908 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 909 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 910 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 911 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 912 | |
| 913 | sb->ctime = mddev->ctime; |
| 914 | sb->level = mddev->level; |
| 915 | sb->size = mddev->size; |
| 916 | sb->raid_disks = mddev->raid_disks; |
| 917 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 918 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | sb->utime = mddev->utime; |
| 920 | sb->state = 0; |
| 921 | sb->events_hi = (mddev->events>>32); |
| 922 | sb->events_lo = (u32)mddev->events; |
| 923 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 924 | if (mddev->reshape_position == MaxSector) |
| 925 | sb->minor_version = 90; |
| 926 | else { |
| 927 | sb->minor_version = 91; |
| 928 | sb->reshape_position = mddev->reshape_position; |
| 929 | sb->new_level = mddev->new_level; |
| 930 | sb->delta_disks = mddev->delta_disks; |
| 931 | sb->new_layout = mddev->new_layout; |
| 932 | sb->new_chunk = mddev->new_chunk; |
| 933 | } |
| 934 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | if (mddev->in_sync) |
| 936 | { |
| 937 | sb->recovery_cp = mddev->recovery_cp; |
| 938 | sb->cp_events_hi = (mddev->events>>32); |
| 939 | sb->cp_events_lo = (u32)mddev->events; |
| 940 | if (mddev->recovery_cp == MaxSector) |
| 941 | sb->state = (1<< MD_SB_CLEAN); |
| 942 | } else |
| 943 | sb->recovery_cp = 0; |
| 944 | |
| 945 | sb->layout = mddev->layout; |
| 946 | sb->chunk_size = mddev->chunk_size; |
| 947 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 948 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 949 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 950 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 952 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 954 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 955 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 956 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 957 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 959 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 960 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | d = &sb->disks[rdev2->desc_nr]; |
| 962 | nr_disks++; |
| 963 | d->number = rdev2->desc_nr; |
| 964 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 965 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 966 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 967 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | d->raid_disk = rdev2->raid_disk; |
| 969 | else |
| 970 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 971 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 973 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | d->state = (1<<MD_DISK_ACTIVE); |
| 975 | d->state |= (1<<MD_DISK_SYNC); |
| 976 | active++; |
| 977 | working++; |
| 978 | } else { |
| 979 | d->state = 0; |
| 980 | spare++; |
| 981 | working++; |
| 982 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 983 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 984 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 987 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 988 | mdp_disk_t *d = &sb->disks[i]; |
| 989 | if (d->state == 0 && d->number == 0) { |
| 990 | d->number = i; |
| 991 | d->raid_disk = i; |
| 992 | d->state = (1<<MD_DISK_REMOVED); |
| 993 | d->state |= (1<<MD_DISK_FAULTY); |
| 994 | failed++; |
| 995 | } |
| 996 | } |
| 997 | sb->nr_disks = nr_disks; |
| 998 | sb->active_disks = active; |
| 999 | sb->working_disks = working; |
| 1000 | sb->failed_disks = failed; |
| 1001 | sb->spare_disks = spare; |
| 1002 | |
| 1003 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1004 | sb->sb_csum = calc_sb_csum(sb); |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * version 1 superblock |
| 1009 | */ |
| 1010 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1011 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1013 | __le32 disk_csum; |
| 1014 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | unsigned long long newcsum; |
| 1016 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1017 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | 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) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1027 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
| 1029 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1030 | sb->sb_csum = disk_csum; |
| 1031 | return cpu_to_le32(csum); |
| 1032 | } |
| 1033 | |
| 1034 | static 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; |
| 1038 | sector_t sb_offset; |
| 1039 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1040 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | /* |
| 1043 | * Calculate the position of the superblock. |
| 1044 | * 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: |
| 1052 | sb_offset = rdev->bdev->bd_inode->i_size >> 9; |
| 1053 | sb_offset -= 8*2; |
NeilBrown | 3973096 | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1054 | sb_offset &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | /* convert from sectors to K */ |
| 1056 | sb_offset /= 2; |
| 1057 | break; |
| 1058 | case 1: |
| 1059 | sb_offset = 0; |
| 1060 | break; |
| 1061 | case 2: |
| 1062 | sb_offset = 4; |
| 1063 | break; |
| 1064 | default: |
| 1065 | return -EINVAL; |
| 1066 | } |
| 1067 | rdev->sb_offset = sb_offset; |
| 1068 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1069 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1070 | * and it is safe to read 4k, so we do that |
| 1071 | */ |
| 1072 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | if (ret) return ret; |
| 1074 | |
| 1075 | |
| 1076 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1077 | |
| 1078 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1079 | sb->major_version != cpu_to_le32(1) || |
| 1080 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
| 1081 | le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1082 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | return -EINVAL; |
| 1084 | |
| 1085 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1086 | printk("md: invalid superblock checksum on %s\n", |
| 1087 | bdevname(rdev->bdev,b)); |
| 1088 | return -EINVAL; |
| 1089 | } |
| 1090 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1091 | printk("md: data_size too small on %s\n", |
| 1092 | bdevname(rdev->bdev,b)); |
| 1093 | return -EINVAL; |
| 1094 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1095 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1096 | if (sb->level != cpu_to_le32(1) && |
| 1097 | sb->level != cpu_to_le32(4) && |
| 1098 | sb->level != cpu_to_le32(5) && |
| 1099 | sb->level != cpu_to_le32(6) && |
| 1100 | sb->level != cpu_to_le32(10)) { |
| 1101 | printk(KERN_WARNING |
| 1102 | "md: bitmaps not supported for this level.\n"); |
| 1103 | return -EINVAL; |
| 1104 | } |
| 1105 | } |
| 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | rdev->preferred_minor = 0xffff; |
| 1108 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1109 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1111 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1112 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1113 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1114 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1115 | |
| 1116 | if (minor_version |
| 1117 | && rdev->data_offset < sb_offset + (rdev->sb_size/512)) |
| 1118 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1119 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1120 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1121 | rdev->desc_nr = -1; |
| 1122 | else |
| 1123 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1124 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1125 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1126 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1127 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | __u64 ev1, ev2; |
| 1129 | struct mdp_superblock_1 *refsb = |
| 1130 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1131 | |
| 1132 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1133 | sb->level != refsb->level || |
| 1134 | sb->layout != refsb->layout || |
| 1135 | sb->chunksize != refsb->chunksize) { |
| 1136 | printk(KERN_WARNING "md: %s has strangely different" |
| 1137 | " superblock to %s\n", |
| 1138 | bdevname(rdev->bdev,b), |
| 1139 | bdevname(refdev->bdev,b2)); |
| 1140 | return -EINVAL; |
| 1141 | } |
| 1142 | ev1 = le64_to_cpu(sb->events); |
| 1143 | ev2 = le64_to_cpu(refsb->events); |
| 1144 | |
| 1145 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1146 | ret = 1; |
| 1147 | else |
| 1148 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1150 | if (minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1152 | else |
| 1153 | rdev->size = rdev->sb_offset; |
| 1154 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1155 | return -EINVAL; |
| 1156 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1157 | if (le32_to_cpu(sb->chunksize)) |
| 1158 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1159 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1160 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1161 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1162 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1166 | { |
| 1167 | struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1168 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1170 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1171 | clear_bit(Faulty, &rdev->flags); |
| 1172 | clear_bit(In_sync, &rdev->flags); |
| 1173 | clear_bit(WriteMostly, &rdev->flags); |
| 1174 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | if (mddev->raid_disks == 0) { |
| 1177 | mddev->major_version = 1; |
| 1178 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1179 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1181 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1182 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1183 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1184 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | mddev->layout = le32_to_cpu(sb->layout); |
| 1186 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1187 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1188 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1189 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1190 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | |
| 1192 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1193 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1194 | |
| 1195 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1196 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1197 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1198 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1199 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1200 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1201 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1202 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1203 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1204 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1205 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1206 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1207 | } else { |
| 1208 | mddev->reshape_position = MaxSector; |
| 1209 | mddev->delta_disks = 0; |
| 1210 | mddev->new_level = mddev->level; |
| 1211 | mddev->new_layout = mddev->layout; |
| 1212 | mddev->new_chunk = mddev->chunk_size; |
| 1213 | } |
| 1214 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1215 | } else if (mddev->pers == NULL) { |
| 1216 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | ++ev1; |
| 1218 | if (ev1 < mddev->events) |
| 1219 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1220 | } else if (mddev->bitmap) { |
| 1221 | /* If adding to array with a bitmap, then we can accept an |
| 1222 | * older device, but not too old. |
| 1223 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1224 | if (ev1 < mddev->bitmap->events_cleared) |
| 1225 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1226 | } else { |
| 1227 | if (ev1 < mddev->events) |
| 1228 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1229 | return 0; |
| 1230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1232 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1234 | switch(role) { |
| 1235 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | break; |
| 1237 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1238 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | break; |
| 1240 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1241 | if ((le32_to_cpu(sb->feature_map) & |
| 1242 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1243 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1244 | else |
| 1245 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | rdev->raid_disk = role; |
| 1247 | break; |
| 1248 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1249 | if (sb->devflags & WriteMostly1) |
| 1250 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1251 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1252 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1258 | { |
| 1259 | struct mdp_superblock_1 *sb; |
| 1260 | struct list_head *tmp; |
| 1261 | mdk_rdev_t *rdev2; |
| 1262 | int max_dev, i; |
| 1263 | /* make rdev->sb match mddev and rdev data. */ |
| 1264 | |
| 1265 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1266 | |
| 1267 | sb->feature_map = 0; |
| 1268 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1269 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1271 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1272 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1273 | |
| 1274 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1275 | sb->events = cpu_to_le64(mddev->events); |
| 1276 | if (mddev->in_sync) |
| 1277 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1278 | else |
| 1279 | sb->resync_offset = cpu_to_le64(0); |
| 1280 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1281 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1282 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1283 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1284 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1285 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1286 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1287 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1288 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1289 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1290 | |
| 1291 | if (rdev->raid_disk >= 0 && |
| 1292 | !test_bit(In_sync, &rdev->flags) && |
| 1293 | rdev->recovery_offset > 0) { |
| 1294 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1295 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1296 | } |
| 1297 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1298 | if (mddev->reshape_position != MaxSector) { |
| 1299 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1300 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1301 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1302 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1303 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1304 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1305 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | max_dev = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1308 | rdev_for_each(rdev2, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | if (rdev2->desc_nr+1 > max_dev) |
| 1310 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1311 | |
| 1312 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1313 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | for (i=0; i<max_dev;i++) |
| 1315 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1316 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1317 | rdev_for_each(rdev2, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1319 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1321 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1323 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1324 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | else |
| 1326 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1327 | } |
| 1328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1330 | } |
| 1331 | |
| 1332 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1333 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | [0] = { |
| 1335 | .name = "0.90.0", |
| 1336 | .owner = THIS_MODULE, |
| 1337 | .load_super = super_90_load, |
| 1338 | .validate_super = super_90_validate, |
| 1339 | .sync_super = super_90_sync, |
| 1340 | }, |
| 1341 | [1] = { |
| 1342 | .name = "md-1", |
| 1343 | .owner = THIS_MODULE, |
| 1344 | .load_super = super_1_load, |
| 1345 | .validate_super = super_1_validate, |
| 1346 | .sync_super = super_1_sync, |
| 1347 | }, |
| 1348 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | |
| 1350 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1351 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1352 | struct list_head *tmp, *tmp2; |
| 1353 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1355 | rdev_for_each(rdev, tmp, mddev1) |
| 1356 | rdev_for_each(rdev2, tmp2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1357 | if (rdev->bdev->bd_contains == |
| 1358 | rdev2->bdev->bd_contains) |
| 1359 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
| 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | static LIST_HEAD(pending_raid_disks); |
| 1365 | |
| 1366 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1367 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1368 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1369 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1370 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1371 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (rdev->mddev) { |
| 1374 | MD_BUG(); |
| 1375 | return -EINVAL; |
| 1376 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1377 | |
| 1378 | /* prevent duplicates */ |
| 1379 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1380 | return -EEXIST; |
| 1381 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1382 | /* make sure rdev->size exceeds mddev->size */ |
| 1383 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1384 | if (mddev->pers) { |
| 1385 | /* Cannot change size, so fail |
| 1386 | * If mddev->level <= 0, then we don't care |
| 1387 | * about aligning sizes (e.g. linear) |
| 1388 | */ |
| 1389 | if (mddev->level > 0) |
| 1390 | return -ENOSPC; |
| 1391 | } else |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1392 | mddev->size = rdev->size; |
| 1393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
| 1395 | /* Verify rdev->desc_nr is unique. |
| 1396 | * If it is -1, assign a free number, else |
| 1397 | * check number is not in use |
| 1398 | */ |
| 1399 | if (rdev->desc_nr < 0) { |
| 1400 | int choice = 0; |
| 1401 | if (mddev->pers) choice = mddev->raid_disks; |
| 1402 | while (find_rdev_nr(mddev, choice)) |
| 1403 | choice++; |
| 1404 | rdev->desc_nr = choice; |
| 1405 | } else { |
| 1406 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1407 | return -EBUSY; |
| 1408 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1409 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1410 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1411 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1414 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1415 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1416 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1417 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1418 | |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1419 | if (rdev->bdev->bd_part) |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1420 | ko = &rdev->bdev->bd_part->dev.kobj; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1421 | else |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 1422 | ko = &rdev->bdev->bd_disk->dev.kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1423 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1424 | kobject_del(&rdev->kobj); |
| 1425 | goto fail; |
| 1426 | } |
| 1427 | list_add(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1428 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1430 | |
| 1431 | fail: |
| 1432 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1433 | b, mdname(mddev)); |
| 1434 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1437 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1438 | { |
| 1439 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1440 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1441 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1445 | { |
| 1446 | char b[BDEVNAME_SIZE]; |
| 1447 | if (!rdev->mddev) { |
| 1448 | MD_BUG(); |
| 1449 | return; |
| 1450 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1451 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | list_del_init(&rdev->same_set); |
| 1453 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1454 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1455 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1456 | |
| 1457 | /* We need to delay this, otherwise we can deadlock when |
| 1458 | * writing to 'remove' to "dev/state" |
| 1459 | */ |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1460 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1461 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1462 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * prevent the device from being mounted, repartitioned or |
| 1467 | * otherwise reused by a RAID array (or any other kernel |
| 1468 | * subsystem), by bd_claiming the device. |
| 1469 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1470 | static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | { |
| 1472 | int err = 0; |
| 1473 | struct block_device *bdev; |
| 1474 | char b[BDEVNAME_SIZE]; |
| 1475 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1476 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | if (IS_ERR(bdev)) { |
| 1478 | printk(KERN_ERR "md: could not open %s.\n", |
| 1479 | __bdevname(dev, b)); |
| 1480 | return PTR_ERR(bdev); |
| 1481 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1482 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | if (err) { |
| 1484 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1485 | bdevname(bdev, b)); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1486 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | return err; |
| 1488 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1489 | if (!shared) |
| 1490 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | rdev->bdev = bdev; |
| 1492 | return err; |
| 1493 | } |
| 1494 | |
| 1495 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1496 | { |
| 1497 | struct block_device *bdev = rdev->bdev; |
| 1498 | rdev->bdev = NULL; |
| 1499 | if (!bdev) |
| 1500 | MD_BUG(); |
| 1501 | bd_release(bdev); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1502 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | void md_autodetect_dev(dev_t dev); |
| 1506 | |
| 1507 | static void export_rdev(mdk_rdev_t * rdev) |
| 1508 | { |
| 1509 | char b[BDEVNAME_SIZE]; |
| 1510 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1511 | bdevname(rdev->bdev,b)); |
| 1512 | if (rdev->mddev) |
| 1513 | MD_BUG(); |
| 1514 | free_disk_sb(rdev); |
| 1515 | list_del_init(&rdev->same_set); |
| 1516 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1517 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1518 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | #endif |
| 1520 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1521 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1525 | { |
| 1526 | unbind_rdev_from_array(rdev); |
| 1527 | export_rdev(rdev); |
| 1528 | } |
| 1529 | |
| 1530 | static void export_array(mddev_t *mddev) |
| 1531 | { |
| 1532 | struct list_head *tmp; |
| 1533 | mdk_rdev_t *rdev; |
| 1534 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1535 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | if (!rdev->mddev) { |
| 1537 | MD_BUG(); |
| 1538 | continue; |
| 1539 | } |
| 1540 | kick_rdev_from_array(rdev); |
| 1541 | } |
| 1542 | if (!list_empty(&mddev->disks)) |
| 1543 | MD_BUG(); |
| 1544 | mddev->raid_disks = 0; |
| 1545 | mddev->major_version = 0; |
| 1546 | } |
| 1547 | |
| 1548 | static void print_desc(mdp_disk_t *desc) |
| 1549 | { |
| 1550 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1551 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1552 | } |
| 1553 | |
| 1554 | static void print_sb(mdp_super_t *sb) |
| 1555 | { |
| 1556 | int i; |
| 1557 | |
| 1558 | printk(KERN_INFO |
| 1559 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1560 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1561 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1562 | sb->ctime); |
| 1563 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1564 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1565 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1566 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1567 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1568 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1569 | sb->failed_disks, sb->spare_disks, |
| 1570 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1571 | |
| 1572 | printk(KERN_INFO); |
| 1573 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1574 | mdp_disk_t *desc; |
| 1575 | |
| 1576 | desc = sb->disks + i; |
| 1577 | if (desc->number || desc->major || desc->minor || |
| 1578 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1579 | printk(" D %2d: ", i); |
| 1580 | print_desc(desc); |
| 1581 | } |
| 1582 | } |
| 1583 | printk(KERN_INFO "md: THIS: "); |
| 1584 | print_desc(&sb->this_disk); |
| 1585 | |
| 1586 | } |
| 1587 | |
| 1588 | static void print_rdev(mdk_rdev_t *rdev) |
| 1589 | { |
| 1590 | char b[BDEVNAME_SIZE]; |
| 1591 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1592 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1593 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1594 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | if (rdev->sb_loaded) { |
| 1596 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1597 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1598 | } else |
| 1599 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1600 | } |
| 1601 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1602 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | { |
| 1604 | struct list_head *tmp, *tmp2; |
| 1605 | mdk_rdev_t *rdev; |
| 1606 | mddev_t *mddev; |
| 1607 | char b[BDEVNAME_SIZE]; |
| 1608 | |
| 1609 | printk("\n"); |
| 1610 | printk("md: **********************************\n"); |
| 1611 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1612 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1613 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1615 | if (mddev->bitmap) |
| 1616 | bitmap_print_sb(mddev->bitmap); |
| 1617 | else |
| 1618 | printk("%s: ", mdname(mddev)); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1619 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1621 | printk("\n"); |
| 1622 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1623 | rdev_for_each(rdev, tmp2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | print_rdev(rdev); |
| 1625 | } |
| 1626 | printk("md: **********************************\n"); |
| 1627 | printk("\n"); |
| 1628 | } |
| 1629 | |
| 1630 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1631 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1633 | /* Update each superblock (in-memory image), but |
| 1634 | * if we are allowed to, skip spares which already |
| 1635 | * have the right event counter, or have one earlier |
| 1636 | * (which would mean they aren't being marked as dirty |
| 1637 | * with the rest of the array) |
| 1638 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | mdk_rdev_t *rdev; |
| 1640 | struct list_head *tmp; |
| 1641 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1642 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1643 | if (rdev->sb_events == mddev->events || |
| 1644 | (nospares && |
| 1645 | rdev->raid_disk < 0 && |
| 1646 | (rdev->sb_events&1)==0 && |
| 1647 | rdev->sb_events+1 == mddev->events)) { |
| 1648 | /* Don't update this superblock */ |
| 1649 | rdev->sb_loaded = 2; |
| 1650 | } else { |
| 1651 | super_types[mddev->major_version]. |
| 1652 | sync_super(mddev, rdev); |
| 1653 | rdev->sb_loaded = 1; |
| 1654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1658 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | struct list_head *tmp; |
| 1661 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1662 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1663 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1665 | if (mddev->external) |
| 1666 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1668 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1669 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1670 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1671 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1672 | force_change = 1; |
| 1673 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1674 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1675 | * though if events isn't the right even/odd, we will have to do |
| 1676 | * spares after all |
| 1677 | */ |
| 1678 | nospares = 1; |
| 1679 | if (force_change) |
| 1680 | nospares = 0; |
| 1681 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1682 | /* If the array is degraded, then skipping spares is both |
| 1683 | * dangerous and fairly pointless. |
| 1684 | * Dangerous because a device that was removed from the array |
| 1685 | * might have a event_count that still looks up-to-date, |
| 1686 | * so it can be re-added without a resync. |
| 1687 | * Pointless because if there are any spares to skip, |
| 1688 | * then a recovery will happen and soon that array won't |
| 1689 | * be degraded any more and the spare can go back to sleep then. |
| 1690 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1691 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1692 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1693 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1695 | |
| 1696 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1697 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1698 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1699 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1700 | && (mddev->events & 1) |
| 1701 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1702 | mddev->events--; |
| 1703 | else { |
| 1704 | /* otherwise we have to go forward and ... */ |
| 1705 | mddev->events ++; |
| 1706 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1707 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1708 | if ((mddev->events&1)==0) { |
| 1709 | mddev->events++; |
| 1710 | nospares = 0; |
| 1711 | } |
| 1712 | } else { |
| 1713 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1714 | if ((mddev->events&1)) { |
| 1715 | mddev->events++; |
| 1716 | nospares = 0; |
| 1717 | } |
| 1718 | } |
| 1719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
| 1721 | if (!mddev->events) { |
| 1722 | /* |
| 1723 | * oops, this 64-bit counter should never wrap. |
| 1724 | * Either we are in around ~1 trillion A.C., assuming |
| 1725 | * 1 reboot per second, or we have a bug: |
| 1726 | */ |
| 1727 | MD_BUG(); |
| 1728 | mddev->events --; |
| 1729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* |
| 1732 | * do not write anything to disk if using |
| 1733 | * nonpersistent superblocks |
| 1734 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1735 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1736 | if (!mddev->external) |
| 1737 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1738 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1739 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1740 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1742 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1743 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1744 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
| 1746 | dprintk(KERN_INFO |
| 1747 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1748 | mdname(mddev),mddev->in_sync); |
| 1749 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1750 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1751 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | char b[BDEVNAME_SIZE]; |
| 1753 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1754 | if (rdev->sb_loaded != 1) |
| 1755 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1756 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | dprintk("(skipping faulty "); |
| 1758 | |
| 1759 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1760 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1761 | md_super_write(mddev,rdev, |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1762 | rdev->sb_offset<<1, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1763 | rdev->sb_page); |
| 1764 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1765 | bdevname(rdev->bdev,b), |
| 1766 | (unsigned long long)rdev->sb_offset); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1767 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | } else |
| 1770 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1771 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | /* only need to write one superblock... */ |
| 1773 | break; |
| 1774 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1775 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1776 | /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1777 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1778 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1779 | if (mddev->in_sync != sync_req || |
| 1780 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1781 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1782 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1783 | goto repeat; |
| 1784 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1785 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1786 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1787 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1791 | /* words written to sysfs files may, or my not, be \n terminated. |
| 1792 | * We want to accept with case. For this we use cmd_match. |
| 1793 | */ |
| 1794 | static int cmd_match(const char *cmd, const char *str) |
| 1795 | { |
| 1796 | /* See if cmd, written into a sysfs file, matches |
| 1797 | * str. They must either be the same, or cmd can |
| 1798 | * have a trailing newline |
| 1799 | */ |
| 1800 | while (*cmd && *str && *cmd == *str) { |
| 1801 | cmd++; |
| 1802 | str++; |
| 1803 | } |
| 1804 | if (*cmd == '\n') |
| 1805 | cmd++; |
| 1806 | if (*str || *cmd) |
| 1807 | return 0; |
| 1808 | return 1; |
| 1809 | } |
| 1810 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1811 | struct rdev_sysfs_entry { |
| 1812 | struct attribute attr; |
| 1813 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1814 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1815 | }; |
| 1816 | |
| 1817 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1818 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1819 | { |
| 1820 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 1821 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1822 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1823 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1824 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1825 | sep = ","; |
| 1826 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1827 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1828 | len += sprintf(page+len, "%sin_sync",sep); |
| 1829 | sep = ","; |
| 1830 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1831 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1832 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1833 | sep = ","; |
| 1834 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1835 | if (test_bit(Blocked, &rdev->flags)) { |
| 1836 | len += sprintf(page+len, "%sblocked", sep); |
| 1837 | sep = ","; |
| 1838 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1839 | if (!test_bit(Faulty, &rdev->flags) && |
| 1840 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1841 | len += sprintf(page+len, "%sspare", sep); |
| 1842 | sep = ","; |
| 1843 | } |
| 1844 | return len+sprintf(page+len, "\n"); |
| 1845 | } |
| 1846 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1847 | static ssize_t |
| 1848 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1849 | { |
| 1850 | /* can write |
| 1851 | * faulty - simulates and error |
| 1852 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1853 | * writemostly - sets write_mostly |
| 1854 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1855 | * blocked - sets the Blocked flag |
| 1856 | * -blocked - clears the Blocked flag |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1857 | */ |
| 1858 | int err = -EINVAL; |
| 1859 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1860 | md_error(rdev->mddev, rdev); |
| 1861 | err = 0; |
| 1862 | } else if (cmd_match(buf, "remove")) { |
| 1863 | if (rdev->raid_disk >= 0) |
| 1864 | err = -EBUSY; |
| 1865 | else { |
| 1866 | mddev_t *mddev = rdev->mddev; |
| 1867 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 1868 | if (mddev->pers) |
| 1869 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1870 | md_new_event(mddev); |
| 1871 | err = 0; |
| 1872 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1873 | } else if (cmd_match(buf, "writemostly")) { |
| 1874 | set_bit(WriteMostly, &rdev->flags); |
| 1875 | err = 0; |
| 1876 | } else if (cmd_match(buf, "-writemostly")) { |
| 1877 | clear_bit(WriteMostly, &rdev->flags); |
| 1878 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1879 | } else if (cmd_match(buf, "blocked")) { |
| 1880 | set_bit(Blocked, &rdev->flags); |
| 1881 | err = 0; |
| 1882 | } else if (cmd_match(buf, "-blocked")) { |
| 1883 | clear_bit(Blocked, &rdev->flags); |
| 1884 | wake_up(&rdev->blocked_wait); |
| 1885 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1886 | md_wakeup_thread(rdev->mddev->thread); |
| 1887 | |
| 1888 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1889 | } |
| 1890 | return err ? err : len; |
| 1891 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1892 | static struct rdev_sysfs_entry rdev_state = |
| 1893 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1894 | |
| 1895 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1896 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1897 | { |
| 1898 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1899 | } |
| 1900 | |
| 1901 | static ssize_t |
| 1902 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1903 | { |
| 1904 | char *e; |
| 1905 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1906 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1907 | atomic_set(&rdev->corrected_errors, n); |
| 1908 | return len; |
| 1909 | } |
| 1910 | return -EINVAL; |
| 1911 | } |
| 1912 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1913 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1914 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1915 | static ssize_t |
| 1916 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1917 | { |
| 1918 | if (rdev->raid_disk < 0) |
| 1919 | return sprintf(page, "none\n"); |
| 1920 | else |
| 1921 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1922 | } |
| 1923 | |
| 1924 | static ssize_t |
| 1925 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1926 | { |
| 1927 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1928 | int err; |
| 1929 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1930 | int slot = simple_strtoul(buf, &e, 10); |
| 1931 | if (strncmp(buf, "none", 4)==0) |
| 1932 | slot = -1; |
| 1933 | else if (e==buf || (*e && *e!= '\n')) |
| 1934 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame^] | 1935 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1936 | /* Setting 'slot' on an active array requires also |
| 1937 | * updating the 'rd%d' link, and communicating |
| 1938 | * with the personality with ->hot_*_disk. |
| 1939 | * For now we only support removing |
| 1940 | * failed/spare devices. This normally happens automatically, |
| 1941 | * but not when the metadata is externally managed. |
| 1942 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1943 | if (rdev->raid_disk == -1) |
| 1944 | return -EEXIST; |
| 1945 | /* personality does all needed checks */ |
| 1946 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 1947 | return -EINVAL; |
| 1948 | err = rdev->mddev->pers-> |
| 1949 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 1950 | if (err) |
| 1951 | return err; |
| 1952 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 1953 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 1954 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 1955 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame^] | 1956 | } else if (rdev->mddev->pers) { |
| 1957 | mdk_rdev_t *rdev2; |
| 1958 | struct list_head *tmp; |
| 1959 | /* Activating a spare .. or possibly reactivating |
| 1960 | * if we every get bitmaps working here. |
| 1961 | */ |
| 1962 | |
| 1963 | if (rdev->raid_disk != -1) |
| 1964 | return -EBUSY; |
| 1965 | |
| 1966 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 1967 | return -EINVAL; |
| 1968 | |
| 1969 | rdev_for_each(rdev2, tmp, rdev->mddev) |
| 1970 | if (rdev2->raid_disk == slot) |
| 1971 | return -EEXIST; |
| 1972 | |
| 1973 | rdev->raid_disk = slot; |
| 1974 | if (test_bit(In_sync, &rdev->flags)) |
| 1975 | rdev->saved_raid_disk = slot; |
| 1976 | else |
| 1977 | rdev->saved_raid_disk = -1; |
| 1978 | err = rdev->mddev->pers-> |
| 1979 | hot_add_disk(rdev->mddev, rdev); |
| 1980 | if (err != 1) { |
| 1981 | rdev->raid_disk = -1; |
| 1982 | if (err == 0) |
| 1983 | return -EEXIST; |
| 1984 | return err; |
| 1985 | } |
| 1986 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 1987 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 1988 | printk(KERN_WARNING |
| 1989 | "md: cannot register " |
| 1990 | "%s for %s\n", |
| 1991 | nm, mdname(rdev->mddev)); |
| 1992 | |
| 1993 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1994 | } else { |
| 1995 | if (slot >= rdev->mddev->raid_disks) |
| 1996 | return -ENOSPC; |
| 1997 | rdev->raid_disk = slot; |
| 1998 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1999 | clear_bit(Faulty, &rdev->flags); |
| 2000 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2001 | set_bit(In_sync, &rdev->flags); |
| 2002 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2003 | return len; |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2008 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2009 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2010 | static ssize_t |
| 2011 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2012 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2013 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | static ssize_t |
| 2017 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2018 | { |
| 2019 | char *e; |
| 2020 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2021 | if (e==buf || (*e && *e != '\n')) |
| 2022 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2023 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2024 | return -EBUSY; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2025 | if (rdev->size && rdev->mddev->external) |
| 2026 | /* Must set offset before size, so overlap checks |
| 2027 | * can be sane */ |
| 2028 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2029 | rdev->data_offset = offset; |
| 2030 | return len; |
| 2031 | } |
| 2032 | |
| 2033 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2034 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2035 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2036 | static ssize_t |
| 2037 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2038 | { |
| 2039 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 2040 | } |
| 2041 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2042 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2043 | { |
| 2044 | /* check if two start/length pairs overlap */ |
| 2045 | if (s1+l1 <= s2) |
| 2046 | return 0; |
| 2047 | if (s2+l2 <= s1) |
| 2048 | return 0; |
| 2049 | return 1; |
| 2050 | } |
| 2051 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2052 | static ssize_t |
| 2053 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2054 | { |
| 2055 | char *e; |
| 2056 | unsigned long long size = simple_strtoull(buf, &e, 10); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2057 | unsigned long long oldsize = rdev->size; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2058 | mddev_t *my_mddev = rdev->mddev; |
| 2059 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2060 | if (e==buf || (*e && *e != '\n')) |
| 2061 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2062 | if (my_mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2063 | return -EBUSY; |
| 2064 | rdev->size = size; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2065 | if (size > oldsize && rdev->mddev->external) { |
| 2066 | /* need to check that all other rdevs with the same ->bdev |
| 2067 | * do not overlap. We need to unlock the mddev to avoid |
| 2068 | * a deadlock. We have already changed rdev->size, and if |
| 2069 | * we have to change it back, we will have the lock again. |
| 2070 | */ |
| 2071 | mddev_t *mddev; |
| 2072 | int overlap = 0; |
| 2073 | struct list_head *tmp, *tmp2; |
| 2074 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2075 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2076 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2077 | mdk_rdev_t *rdev2; |
| 2078 | |
| 2079 | mddev_lock(mddev); |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2080 | rdev_for_each(rdev2, tmp2, mddev) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2081 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2082 | (rdev->bdev == rdev2->bdev && |
| 2083 | rdev != rdev2 && |
| 2084 | overlaps(rdev->data_offset, rdev->size, |
| 2085 | rdev2->data_offset, rdev2->size))) { |
| 2086 | overlap = 1; |
| 2087 | break; |
| 2088 | } |
| 2089 | mddev_unlock(mddev); |
| 2090 | if (overlap) { |
| 2091 | mddev_put(mddev); |
| 2092 | break; |
| 2093 | } |
| 2094 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2095 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2096 | if (overlap) { |
| 2097 | /* Someone else could have slipped in a size |
| 2098 | * change here, but doing so is just silly. |
| 2099 | * We put oldsize back because we *know* it is |
| 2100 | * safe, and trust userspace not to race with |
| 2101 | * itself |
| 2102 | */ |
| 2103 | rdev->size = oldsize; |
| 2104 | return -EBUSY; |
| 2105 | } |
| 2106 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2107 | if (size < my_mddev->size || my_mddev->size == 0) |
| 2108 | my_mddev->size = size; |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2109 | return len; |
| 2110 | } |
| 2111 | |
| 2112 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2113 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2114 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2115 | static struct attribute *rdev_default_attrs[] = { |
| 2116 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2117 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2118 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2119 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2120 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2121 | NULL, |
| 2122 | }; |
| 2123 | static ssize_t |
| 2124 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2125 | { |
| 2126 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2127 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2128 | mddev_t *mddev = rdev->mddev; |
| 2129 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2130 | |
| 2131 | if (!entry->show) |
| 2132 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2133 | |
| 2134 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2135 | if (!rv) { |
| 2136 | if (rdev->mddev == NULL) |
| 2137 | rv = -EBUSY; |
| 2138 | else |
| 2139 | rv = entry->show(rdev, page); |
| 2140 | mddev_unlock(mddev); |
| 2141 | } |
| 2142 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | static ssize_t |
| 2146 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2147 | const char *page, size_t length) |
| 2148 | { |
| 2149 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2150 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2151 | ssize_t rv; |
| 2152 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2153 | |
| 2154 | if (!entry->store) |
| 2155 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2156 | if (!capable(CAP_SYS_ADMIN)) |
| 2157 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2158 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2159 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2160 | if (rdev->mddev == NULL) |
| 2161 | rv = -EBUSY; |
| 2162 | else |
| 2163 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2164 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2165 | } |
| 2166 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | static void rdev_free(struct kobject *ko) |
| 2170 | { |
| 2171 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2172 | kfree(rdev); |
| 2173 | } |
| 2174 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2175 | .show = rdev_attr_show, |
| 2176 | .store = rdev_attr_store, |
| 2177 | }; |
| 2178 | static struct kobj_type rdev_ktype = { |
| 2179 | .release = rdev_free, |
| 2180 | .sysfs_ops = &rdev_sysfs_ops, |
| 2181 | .default_attrs = rdev_default_attrs, |
| 2182 | }; |
| 2183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | /* |
| 2185 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2186 | * |
| 2187 | * mark the device faulty if: |
| 2188 | * |
| 2189 | * - the device is nonexistent (zero size) |
| 2190 | * - the device has no valid superblock |
| 2191 | * |
| 2192 | * a faulty rdev _never_ has rdev->sb set. |
| 2193 | */ |
| 2194 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2195 | { |
| 2196 | char b[BDEVNAME_SIZE]; |
| 2197 | int err; |
| 2198 | mdk_rdev_t *rdev; |
| 2199 | sector_t size; |
| 2200 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2201 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | if (!rdev) { |
| 2203 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2204 | return ERR_PTR(-ENOMEM); |
| 2205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
| 2207 | if ((err = alloc_disk_sb(rdev))) |
| 2208 | goto abort_free; |
| 2209 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2210 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | if (err) |
| 2212 | goto abort_free; |
| 2213 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2214 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2217 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2218 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2219 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2221 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2223 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2224 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | |
| 2226 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2227 | if (!size) { |
| 2228 | printk(KERN_WARNING |
| 2229 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2230 | bdevname(rdev->bdev,b)); |
| 2231 | err = -EINVAL; |
| 2232 | goto abort_free; |
| 2233 | } |
| 2234 | |
| 2235 | if (super_format >= 0) { |
| 2236 | err = super_types[super_format]. |
| 2237 | load_super(rdev, NULL, super_minor); |
| 2238 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2239 | printk(KERN_WARNING |
| 2240 | "md: %s does not have a valid v%d.%d " |
| 2241 | "superblock, not importing!\n", |
| 2242 | bdevname(rdev->bdev,b), |
| 2243 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | goto abort_free; |
| 2245 | } |
| 2246 | if (err < 0) { |
| 2247 | printk(KERN_WARNING |
| 2248 | "md: could not read %s's sb, not importing!\n", |
| 2249 | bdevname(rdev->bdev,b)); |
| 2250 | goto abort_free; |
| 2251 | } |
| 2252 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2255 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | |
| 2257 | return rdev; |
| 2258 | |
| 2259 | abort_free: |
| 2260 | if (rdev->sb_page) { |
| 2261 | if (rdev->bdev) |
| 2262 | unlock_rdev(rdev); |
| 2263 | free_disk_sb(rdev); |
| 2264 | } |
| 2265 | kfree(rdev); |
| 2266 | return ERR_PTR(err); |
| 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * Check a full RAID array for plausibility |
| 2271 | */ |
| 2272 | |
| 2273 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2274 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | { |
| 2276 | int i; |
| 2277 | struct list_head *tmp; |
| 2278 | mdk_rdev_t *rdev, *freshest; |
| 2279 | char b[BDEVNAME_SIZE]; |
| 2280 | |
| 2281 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2282 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | switch (super_types[mddev->major_version]. |
| 2284 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2285 | case 1: |
| 2286 | freshest = rdev; |
| 2287 | break; |
| 2288 | case 0: |
| 2289 | break; |
| 2290 | default: |
| 2291 | printk( KERN_ERR \ |
| 2292 | "md: fatal superblock inconsistency in %s" |
| 2293 | " -- removing from array\n", |
| 2294 | bdevname(rdev->bdev,b)); |
| 2295 | kick_rdev_from_array(rdev); |
| 2296 | } |
| 2297 | |
| 2298 | |
| 2299 | super_types[mddev->major_version]. |
| 2300 | validate_super(mddev, freshest); |
| 2301 | |
| 2302 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2303 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | if (rdev != freshest) |
| 2305 | if (super_types[mddev->major_version]. |
| 2306 | validate_super(mddev, rdev)) { |
| 2307 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2308 | " from array!\n", |
| 2309 | bdevname(rdev->bdev,b)); |
| 2310 | kick_rdev_from_array(rdev); |
| 2311 | continue; |
| 2312 | } |
| 2313 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2314 | rdev->desc_nr = i++; |
| 2315 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2316 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2317 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2318 | rdev->raid_disk = -1; |
| 2319 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | |
| 2324 | |
| 2325 | if (mddev->recovery_cp != MaxSector && |
| 2326 | mddev->level >= 1) |
| 2327 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2328 | " -- starting background reconstruction\n", |
| 2329 | mdname(mddev)); |
| 2330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | } |
| 2332 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2333 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2334 | safe_delay_show(mddev_t *mddev, char *page) |
| 2335 | { |
| 2336 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2337 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2338 | } |
| 2339 | static ssize_t |
| 2340 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2341 | { |
| 2342 | int scale=1; |
| 2343 | int dot=0; |
| 2344 | int i; |
| 2345 | unsigned long msec; |
| 2346 | char buf[30]; |
| 2347 | char *e; |
| 2348 | /* remove a period, and count digits after it */ |
| 2349 | if (len >= sizeof(buf)) |
| 2350 | return -EINVAL; |
| 2351 | strlcpy(buf, cbuf, len); |
| 2352 | buf[len] = 0; |
| 2353 | for (i=0; i<len; i++) { |
| 2354 | if (dot) { |
| 2355 | if (isdigit(buf[i])) { |
| 2356 | buf[i-1] = buf[i]; |
| 2357 | scale *= 10; |
| 2358 | } |
| 2359 | buf[i] = 0; |
| 2360 | } else if (buf[i] == '.') { |
| 2361 | dot=1; |
| 2362 | buf[i] = 0; |
| 2363 | } |
| 2364 | } |
| 2365 | msec = simple_strtoul(buf, &e, 10); |
| 2366 | if (e == buf || (*e && *e != '\n')) |
| 2367 | return -EINVAL; |
| 2368 | msec = (msec * 1000) / scale; |
| 2369 | if (msec == 0) |
| 2370 | mddev->safemode_delay = 0; |
| 2371 | else { |
| 2372 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2373 | if (mddev->safemode_delay == 0) |
| 2374 | mddev->safemode_delay = 1; |
| 2375 | } |
| 2376 | return len; |
| 2377 | } |
| 2378 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2379 | __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2380 | |
| 2381 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2382 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2383 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2384 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2385 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2386 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2387 | else if (mddev->clevel[0]) |
| 2388 | return sprintf(page, "%s\n", mddev->clevel); |
| 2389 | else if (mddev->level != LEVEL_NONE) |
| 2390 | return sprintf(page, "%d\n", mddev->level); |
| 2391 | else |
| 2392 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2393 | } |
| 2394 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2395 | static ssize_t |
| 2396 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2397 | { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2398 | ssize_t rv = len; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2399 | if (mddev->pers) |
| 2400 | return -EBUSY; |
| 2401 | if (len == 0) |
| 2402 | return 0; |
| 2403 | if (len >= sizeof(mddev->clevel)) |
| 2404 | return -ENOSPC; |
| 2405 | strncpy(mddev->clevel, buf, len); |
| 2406 | if (mddev->clevel[len-1] == '\n') |
| 2407 | len--; |
| 2408 | mddev->clevel[len] = 0; |
| 2409 | mddev->level = LEVEL_NONE; |
| 2410 | return rv; |
| 2411 | } |
| 2412 | |
| 2413 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2414 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2415 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2416 | |
| 2417 | static ssize_t |
| 2418 | layout_show(mddev_t *mddev, char *page) |
| 2419 | { |
| 2420 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2421 | if (mddev->reshape_position != MaxSector && |
| 2422 | mddev->layout != mddev->new_layout) |
| 2423 | return sprintf(page, "%d (%d)\n", |
| 2424 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2425 | return sprintf(page, "%d\n", mddev->layout); |
| 2426 | } |
| 2427 | |
| 2428 | static ssize_t |
| 2429 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2430 | { |
| 2431 | char *e; |
| 2432 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2433 | |
| 2434 | if (!*buf || (*e && *e != '\n')) |
| 2435 | return -EINVAL; |
| 2436 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2437 | if (mddev->pers) |
| 2438 | return -EBUSY; |
| 2439 | if (mddev->reshape_position != MaxSector) |
| 2440 | mddev->new_layout = n; |
| 2441 | else |
| 2442 | mddev->layout = n; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2443 | return len; |
| 2444 | } |
| 2445 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2446 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2447 | |
| 2448 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2449 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2450 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2451 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2452 | if (mddev->raid_disks == 0) |
| 2453 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2454 | if (mddev->reshape_position != MaxSector && |
| 2455 | mddev->delta_disks != 0) |
| 2456 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2457 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2458 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2459 | } |
| 2460 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2461 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2462 | |
| 2463 | static ssize_t |
| 2464 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2465 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2466 | char *e; |
| 2467 | int rv = 0; |
| 2468 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2469 | |
| 2470 | if (!*buf || (*e && *e != '\n')) |
| 2471 | return -EINVAL; |
| 2472 | |
| 2473 | if (mddev->pers) |
| 2474 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2475 | else if (mddev->reshape_position != MaxSector) { |
| 2476 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2477 | mddev->delta_disks = n - olddisks; |
| 2478 | mddev->raid_disks = n; |
| 2479 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2480 | mddev->raid_disks = n; |
| 2481 | return rv ? rv : len; |
| 2482 | } |
| 2483 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2484 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2485 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2486 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2487 | chunk_size_show(mddev_t *mddev, char *page) |
| 2488 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2489 | if (mddev->reshape_position != MaxSector && |
| 2490 | mddev->chunk_size != mddev->new_chunk) |
| 2491 | return sprintf(page, "%d (%d)\n", mddev->new_chunk, |
| 2492 | mddev->chunk_size); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2493 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2494 | } |
| 2495 | |
| 2496 | static ssize_t |
| 2497 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2498 | { |
| 2499 | /* can only set chunk_size if array is not yet active */ |
| 2500 | char *e; |
| 2501 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2502 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2503 | if (!*buf || (*e && *e != '\n')) |
| 2504 | return -EINVAL; |
| 2505 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2506 | if (mddev->pers) |
| 2507 | return -EBUSY; |
| 2508 | else if (mddev->reshape_position != MaxSector) |
| 2509 | mddev->new_chunk = n; |
| 2510 | else |
| 2511 | mddev->chunk_size = n; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2512 | return len; |
| 2513 | } |
| 2514 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2515 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2516 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2517 | static ssize_t |
| 2518 | resync_start_show(mddev_t *mddev, char *page) |
| 2519 | { |
| 2520 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2521 | } |
| 2522 | |
| 2523 | static ssize_t |
| 2524 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2525 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2526 | char *e; |
| 2527 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2528 | |
| 2529 | if (mddev->pers) |
| 2530 | return -EBUSY; |
| 2531 | if (!*buf || (*e && *e != '\n')) |
| 2532 | return -EINVAL; |
| 2533 | |
| 2534 | mddev->recovery_cp = n; |
| 2535 | return len; |
| 2536 | } |
| 2537 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2538 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2539 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2540 | /* |
| 2541 | * The array state can be: |
| 2542 | * |
| 2543 | * clear |
| 2544 | * No devices, no size, no level |
| 2545 | * Equivalent to STOP_ARRAY ioctl |
| 2546 | * inactive |
| 2547 | * May have some settings, but array is not active |
| 2548 | * all IO results in error |
| 2549 | * When written, doesn't tear down array, but just stops it |
| 2550 | * suspended (not supported yet) |
| 2551 | * All IO requests will block. The array can be reconfigured. |
| 2552 | * Writing this, if accepted, will block until array is quiessent |
| 2553 | * readonly |
| 2554 | * no resync can happen. no superblocks get written. |
| 2555 | * write requests fail |
| 2556 | * read-auto |
| 2557 | * like readonly, but behaves like 'clean' on a write request. |
| 2558 | * |
| 2559 | * clean - no pending writes, but otherwise active. |
| 2560 | * When written to inactive array, starts without resync |
| 2561 | * If a write request arrives then |
| 2562 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2563 | * if not known, block and switch to write-pending |
| 2564 | * If written to an active array that has pending writes, then fails. |
| 2565 | * active |
| 2566 | * fully active: IO and resync can be happening. |
| 2567 | * When written to inactive array, starts with resync |
| 2568 | * |
| 2569 | * write-pending |
| 2570 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2571 | * |
| 2572 | * active-idle |
| 2573 | * like active, but no writes have been seen for a while (100msec). |
| 2574 | * |
| 2575 | */ |
| 2576 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2577 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2578 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2579 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2580 | "write-pending", "active-idle", NULL }; |
| 2581 | |
| 2582 | static int match_word(const char *word, char **list) |
| 2583 | { |
| 2584 | int n; |
| 2585 | for (n=0; list[n]; n++) |
| 2586 | if (cmd_match(word, list[n])) |
| 2587 | break; |
| 2588 | return n; |
| 2589 | } |
| 2590 | |
| 2591 | static ssize_t |
| 2592 | array_state_show(mddev_t *mddev, char *page) |
| 2593 | { |
| 2594 | enum array_state st = inactive; |
| 2595 | |
| 2596 | if (mddev->pers) |
| 2597 | switch(mddev->ro) { |
| 2598 | case 1: |
| 2599 | st = readonly; |
| 2600 | break; |
| 2601 | case 2: |
| 2602 | st = read_auto; |
| 2603 | break; |
| 2604 | case 0: |
| 2605 | if (mddev->in_sync) |
| 2606 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2607 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2608 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2609 | else if (mddev->safemode) |
| 2610 | st = active_idle; |
| 2611 | else |
| 2612 | st = active; |
| 2613 | } |
| 2614 | else { |
| 2615 | if (list_empty(&mddev->disks) && |
| 2616 | mddev->raid_disks == 0 && |
| 2617 | mddev->size == 0) |
| 2618 | st = clear; |
| 2619 | else |
| 2620 | st = inactive; |
| 2621 | } |
| 2622 | return sprintf(page, "%s\n", array_states[st]); |
| 2623 | } |
| 2624 | |
| 2625 | static int do_md_stop(mddev_t * mddev, int ro); |
| 2626 | static int do_md_run(mddev_t * mddev); |
| 2627 | static int restart_array(mddev_t *mddev); |
| 2628 | |
| 2629 | static ssize_t |
| 2630 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2631 | { |
| 2632 | int err = -EINVAL; |
| 2633 | enum array_state st = match_word(buf, array_states); |
| 2634 | switch(st) { |
| 2635 | case bad_word: |
| 2636 | break; |
| 2637 | case clear: |
| 2638 | /* stopping an active array */ |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2639 | if (atomic_read(&mddev->active) > 1) |
| 2640 | return -EBUSY; |
| 2641 | err = do_md_stop(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2642 | break; |
| 2643 | case inactive: |
| 2644 | /* stopping an active array */ |
| 2645 | if (mddev->pers) { |
| 2646 | if (atomic_read(&mddev->active) > 1) |
| 2647 | return -EBUSY; |
| 2648 | err = do_md_stop(mddev, 2); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2649 | } else |
| 2650 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2651 | break; |
| 2652 | case suspended: |
| 2653 | break; /* not supported yet */ |
| 2654 | case readonly: |
| 2655 | if (mddev->pers) |
| 2656 | err = do_md_stop(mddev, 1); |
| 2657 | else { |
| 2658 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2659 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2660 | err = do_md_run(mddev); |
| 2661 | } |
| 2662 | break; |
| 2663 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2664 | if (mddev->pers) { |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2665 | if (mddev->ro != 1) |
| 2666 | err = do_md_stop(mddev, 1); |
| 2667 | else |
| 2668 | err = restart_array(mddev); |
| 2669 | if (err == 0) { |
| 2670 | mddev->ro = 2; |
| 2671 | set_disk_ro(mddev->gendisk, 0); |
| 2672 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2673 | } else { |
| 2674 | mddev->ro = 2; |
| 2675 | err = do_md_run(mddev); |
| 2676 | } |
| 2677 | break; |
| 2678 | case clean: |
| 2679 | if (mddev->pers) { |
| 2680 | restart_array(mddev); |
| 2681 | spin_lock_irq(&mddev->write_lock); |
| 2682 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2683 | if (mddev->in_sync == 0) { |
| 2684 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2685 | if (mddev->safemode == 1) |
| 2686 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2687 | if (mddev->persistent) |
| 2688 | set_bit(MD_CHANGE_CLEAN, |
| 2689 | &mddev->flags); |
| 2690 | } |
| 2691 | err = 0; |
| 2692 | } else |
| 2693 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2694 | spin_unlock_irq(&mddev->write_lock); |
| 2695 | } else { |
| 2696 | mddev->ro = 0; |
| 2697 | mddev->recovery_cp = MaxSector; |
| 2698 | err = do_md_run(mddev); |
| 2699 | } |
| 2700 | break; |
| 2701 | case active: |
| 2702 | if (mddev->pers) { |
| 2703 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2704 | if (mddev->external) |
| 2705 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2706 | wake_up(&mddev->sb_wait); |
| 2707 | err = 0; |
| 2708 | } else { |
| 2709 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 2710 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2711 | err = do_md_run(mddev); |
| 2712 | } |
| 2713 | break; |
| 2714 | case write_pending: |
| 2715 | case active_idle: |
| 2716 | /* these cannot be set */ |
| 2717 | break; |
| 2718 | } |
| 2719 | if (err) |
| 2720 | return err; |
| 2721 | else |
| 2722 | return len; |
| 2723 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2724 | static struct md_sysfs_entry md_array_state = |
| 2725 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2726 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2727 | static ssize_t |
| 2728 | null_show(mddev_t *mddev, char *page) |
| 2729 | { |
| 2730 | return -EINVAL; |
| 2731 | } |
| 2732 | |
| 2733 | static ssize_t |
| 2734 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2735 | { |
| 2736 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2737 | /* The new device is added to the array. |
| 2738 | * If the array has a persistent superblock, we read the |
| 2739 | * superblock to initialise info and check validity. |
| 2740 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2741 | * which mainly checks size. |
| 2742 | */ |
| 2743 | char *e; |
| 2744 | int major = simple_strtoul(buf, &e, 10); |
| 2745 | int minor; |
| 2746 | dev_t dev; |
| 2747 | mdk_rdev_t *rdev; |
| 2748 | int err; |
| 2749 | |
| 2750 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2751 | return -EINVAL; |
| 2752 | minor = simple_strtoul(e+1, &e, 10); |
| 2753 | if (*e && *e != '\n') |
| 2754 | return -EINVAL; |
| 2755 | dev = MKDEV(major, minor); |
| 2756 | if (major != MAJOR(dev) || |
| 2757 | minor != MINOR(dev)) |
| 2758 | return -EOVERFLOW; |
| 2759 | |
| 2760 | |
| 2761 | if (mddev->persistent) { |
| 2762 | rdev = md_import_device(dev, mddev->major_version, |
| 2763 | mddev->minor_version); |
| 2764 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2765 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2766 | mdk_rdev_t, same_set); |
| 2767 | err = super_types[mddev->major_version] |
| 2768 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2769 | if (err < 0) |
| 2770 | goto out; |
| 2771 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2772 | } else if (mddev->external) |
| 2773 | rdev = md_import_device(dev, -2, -1); |
| 2774 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2775 | rdev = md_import_device(dev, -1, -1); |
| 2776 | |
| 2777 | if (IS_ERR(rdev)) |
| 2778 | return PTR_ERR(rdev); |
| 2779 | err = bind_rdev_to_array(rdev, mddev); |
| 2780 | out: |
| 2781 | if (err) |
| 2782 | export_rdev(rdev); |
| 2783 | return err ? err : len; |
| 2784 | } |
| 2785 | |
| 2786 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2787 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2788 | |
| 2789 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2790 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2791 | { |
| 2792 | char *end; |
| 2793 | unsigned long chunk, end_chunk; |
| 2794 | |
| 2795 | if (!mddev->bitmap) |
| 2796 | goto out; |
| 2797 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2798 | while (*buf) { |
| 2799 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2800 | if (buf == end) break; |
| 2801 | if (*end == '-') { /* range */ |
| 2802 | buf = end + 1; |
| 2803 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2804 | if (buf == end) break; |
| 2805 | } |
| 2806 | if (*end && !isspace(*end)) break; |
| 2807 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2808 | buf = end; |
| 2809 | while (isspace(*buf)) buf++; |
| 2810 | } |
| 2811 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2812 | out: |
| 2813 | return len; |
| 2814 | } |
| 2815 | |
| 2816 | static struct md_sysfs_entry md_bitmap = |
| 2817 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2818 | |
| 2819 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2820 | size_show(mddev_t *mddev, char *page) |
| 2821 | { |
| 2822 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2823 | } |
| 2824 | |
| 2825 | static int update_size(mddev_t *mddev, unsigned long size); |
| 2826 | |
| 2827 | static ssize_t |
| 2828 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2829 | { |
| 2830 | /* If array is inactive, we can reduce the component size, but |
| 2831 | * not increase it (except from 0). |
| 2832 | * If array is active, we can try an on-line resize |
| 2833 | */ |
| 2834 | char *e; |
| 2835 | int err = 0; |
| 2836 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2837 | if (!*buf || *buf == '\n' || |
| 2838 | (*e && *e != '\n')) |
| 2839 | return -EINVAL; |
| 2840 | |
| 2841 | if (mddev->pers) { |
| 2842 | err = update_size(mddev, size); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2843 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2844 | } else { |
| 2845 | if (mddev->size == 0 || |
| 2846 | mddev->size > size) |
| 2847 | mddev->size = size; |
| 2848 | else |
| 2849 | err = -ENOSPC; |
| 2850 | } |
| 2851 | return err ? err : len; |
| 2852 | } |
| 2853 | |
| 2854 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2855 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2856 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2857 | |
| 2858 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2859 | * This is one of |
| 2860 | * 'none' for arrays with no metadata (good luck...) |
| 2861 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2862 | * or N.M for internally known formats |
| 2863 | */ |
| 2864 | static ssize_t |
| 2865 | metadata_show(mddev_t *mddev, char *page) |
| 2866 | { |
| 2867 | if (mddev->persistent) |
| 2868 | return sprintf(page, "%d.%d\n", |
| 2869 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2870 | else if (mddev->external) |
| 2871 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2872 | else |
| 2873 | return sprintf(page, "none\n"); |
| 2874 | } |
| 2875 | |
| 2876 | static ssize_t |
| 2877 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2878 | { |
| 2879 | int major, minor; |
| 2880 | char *e; |
| 2881 | if (!list_empty(&mddev->disks)) |
| 2882 | return -EBUSY; |
| 2883 | |
| 2884 | if (cmd_match(buf, "none")) { |
| 2885 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2886 | mddev->external = 0; |
| 2887 | mddev->major_version = 0; |
| 2888 | mddev->minor_version = 90; |
| 2889 | return len; |
| 2890 | } |
| 2891 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2892 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2893 | if (namelen >= sizeof(mddev->metadata_type)) |
| 2894 | namelen = sizeof(mddev->metadata_type)-1; |
| 2895 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 2896 | mddev->metadata_type[namelen] = 0; |
| 2897 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 2898 | mddev->metadata_type[--namelen] = 0; |
| 2899 | mddev->persistent = 0; |
| 2900 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2901 | mddev->major_version = 0; |
| 2902 | mddev->minor_version = 90; |
| 2903 | return len; |
| 2904 | } |
| 2905 | major = simple_strtoul(buf, &e, 10); |
| 2906 | if (e==buf || *e != '.') |
| 2907 | return -EINVAL; |
| 2908 | buf = e+1; |
| 2909 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2910 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2911 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 2912 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2913 | return -ENOENT; |
| 2914 | mddev->major_version = major; |
| 2915 | mddev->minor_version = minor; |
| 2916 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2917 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2918 | return len; |
| 2919 | } |
| 2920 | |
| 2921 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2922 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2923 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2924 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2925 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2926 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2927 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2928 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 2929 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2930 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2931 | type = "reshape"; |
| 2932 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2933 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2934 | type = "resync"; |
| 2935 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2936 | type = "check"; |
| 2937 | else |
| 2938 | type = "repair"; |
| 2939 | } else |
| 2940 | type = "recover"; |
| 2941 | } |
| 2942 | return sprintf(page, "%s\n", type); |
| 2943 | } |
| 2944 | |
| 2945 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2946 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2947 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2948 | if (!mddev->pers || !mddev->pers->sync_request) |
| 2949 | return -EINVAL; |
| 2950 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2951 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2952 | if (mddev->sync_thread) { |
| 2953 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2954 | md_unregister_thread(mddev->sync_thread); |
| 2955 | mddev->sync_thread = NULL; |
| 2956 | mddev->recovery = 0; |
| 2957 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2958 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2959 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2960 | return -EBUSY; |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2961 | else if (cmd_match(page, "resync") || cmd_match(page, "recover")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2962 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2963 | else if (cmd_match(page, "reshape")) { |
| 2964 | int err; |
| 2965 | if (mddev->pers->start_reshape == NULL) |
| 2966 | return -EINVAL; |
| 2967 | err = mddev->pers->start_reshape(mddev); |
| 2968 | if (err) |
| 2969 | return err; |
| 2970 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2971 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2972 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 2973 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2974 | return -EINVAL; |
| 2975 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 2976 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2977 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2978 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2979 | md_wakeup_thread(mddev->thread); |
| 2980 | return len; |
| 2981 | } |
| 2982 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2983 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2984 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2985 | { |
| 2986 | return sprintf(page, "%llu\n", |
| 2987 | (unsigned long long) mddev->resync_mismatches); |
| 2988 | } |
| 2989 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2990 | static struct md_sysfs_entry md_scan_mode = |
| 2991 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2992 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2993 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2994 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2995 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2996 | static ssize_t |
| 2997 | sync_min_show(mddev_t *mddev, char *page) |
| 2998 | { |
| 2999 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3000 | mddev->sync_speed_min ? "local": "system"); |
| 3001 | } |
| 3002 | |
| 3003 | static ssize_t |
| 3004 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3005 | { |
| 3006 | int min; |
| 3007 | char *e; |
| 3008 | if (strncmp(buf, "system", 6)==0) { |
| 3009 | mddev->sync_speed_min = 0; |
| 3010 | return len; |
| 3011 | } |
| 3012 | min = simple_strtoul(buf, &e, 10); |
| 3013 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3014 | return -EINVAL; |
| 3015 | mddev->sync_speed_min = min; |
| 3016 | return len; |
| 3017 | } |
| 3018 | |
| 3019 | static struct md_sysfs_entry md_sync_min = |
| 3020 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3021 | |
| 3022 | static ssize_t |
| 3023 | sync_max_show(mddev_t *mddev, char *page) |
| 3024 | { |
| 3025 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3026 | mddev->sync_speed_max ? "local": "system"); |
| 3027 | } |
| 3028 | |
| 3029 | static ssize_t |
| 3030 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3031 | { |
| 3032 | int max; |
| 3033 | char *e; |
| 3034 | if (strncmp(buf, "system", 6)==0) { |
| 3035 | mddev->sync_speed_max = 0; |
| 3036 | return len; |
| 3037 | } |
| 3038 | max = simple_strtoul(buf, &e, 10); |
| 3039 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3040 | return -EINVAL; |
| 3041 | mddev->sync_speed_max = max; |
| 3042 | return len; |
| 3043 | } |
| 3044 | |
| 3045 | static struct md_sysfs_entry md_sync_max = |
| 3046 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3047 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3048 | static ssize_t |
| 3049 | degraded_show(mddev_t *mddev, char *page) |
| 3050 | { |
| 3051 | return sprintf(page, "%d\n", mddev->degraded); |
| 3052 | } |
| 3053 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3054 | |
| 3055 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3056 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3057 | { |
| 3058 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3059 | } |
| 3060 | |
| 3061 | static ssize_t |
| 3062 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3063 | { |
| 3064 | long n; |
| 3065 | |
| 3066 | if (strict_strtol(buf, 10, &n)) |
| 3067 | return -EINVAL; |
| 3068 | |
| 3069 | if (n != 0 && n != 1) |
| 3070 | return -EINVAL; |
| 3071 | |
| 3072 | mddev->parallel_resync = n; |
| 3073 | |
| 3074 | if (mddev->sync_thread) |
| 3075 | wake_up(&resync_wait); |
| 3076 | |
| 3077 | return len; |
| 3078 | } |
| 3079 | |
| 3080 | /* force parallel resync, even with shared block devices */ |
| 3081 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3082 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3083 | sync_force_parallel_show, sync_force_parallel_store); |
| 3084 | |
| 3085 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3086 | sync_speed_show(mddev_t *mddev, char *page) |
| 3087 | { |
| 3088 | unsigned long resync, dt, db; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 3089 | resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3090 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 3091 | if (!dt) dt++; |
| 3092 | db = resync - (mddev->resync_mark_cnt); |
| 3093 | return sprintf(page, "%ld\n", db/dt/2); /* K/sec */ |
| 3094 | } |
| 3095 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3096 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3097 | |
| 3098 | static ssize_t |
| 3099 | sync_completed_show(mddev_t *mddev, char *page) |
| 3100 | { |
| 3101 | unsigned long max_blocks, resync; |
| 3102 | |
| 3103 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 3104 | max_blocks = mddev->resync_max_sectors; |
| 3105 | else |
| 3106 | max_blocks = mddev->size << 1; |
| 3107 | |
| 3108 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 3109 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 3110 | } |
| 3111 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3112 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3113 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3114 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3115 | min_sync_show(mddev_t *mddev, char *page) |
| 3116 | { |
| 3117 | return sprintf(page, "%llu\n", |
| 3118 | (unsigned long long)mddev->resync_min); |
| 3119 | } |
| 3120 | static ssize_t |
| 3121 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3122 | { |
| 3123 | unsigned long long min; |
| 3124 | if (strict_strtoull(buf, 10, &min)) |
| 3125 | return -EINVAL; |
| 3126 | if (min > mddev->resync_max) |
| 3127 | return -EINVAL; |
| 3128 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3129 | return -EBUSY; |
| 3130 | |
| 3131 | /* Must be a multiple of chunk_size */ |
| 3132 | if (mddev->chunk_size) { |
| 3133 | if (min & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3134 | return -EINVAL; |
| 3135 | } |
| 3136 | mddev->resync_min = min; |
| 3137 | |
| 3138 | return len; |
| 3139 | } |
| 3140 | |
| 3141 | static struct md_sysfs_entry md_min_sync = |
| 3142 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3143 | |
| 3144 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3145 | max_sync_show(mddev_t *mddev, char *page) |
| 3146 | { |
| 3147 | if (mddev->resync_max == MaxSector) |
| 3148 | return sprintf(page, "max\n"); |
| 3149 | else |
| 3150 | return sprintf(page, "%llu\n", |
| 3151 | (unsigned long long)mddev->resync_max); |
| 3152 | } |
| 3153 | static ssize_t |
| 3154 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3155 | { |
| 3156 | if (strncmp(buf, "max", 3) == 0) |
| 3157 | mddev->resync_max = MaxSector; |
| 3158 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3159 | unsigned long long max; |
| 3160 | if (strict_strtoull(buf, 10, &max)) |
| 3161 | return -EINVAL; |
| 3162 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3163 | return -EINVAL; |
| 3164 | if (max < mddev->resync_max && |
| 3165 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3166 | return -EBUSY; |
| 3167 | |
| 3168 | /* Must be a multiple of chunk_size */ |
| 3169 | if (mddev->chunk_size) { |
| 3170 | if (max & (sector_t)((mddev->chunk_size>>9)-1)) |
| 3171 | return -EINVAL; |
| 3172 | } |
| 3173 | mddev->resync_max = max; |
| 3174 | } |
| 3175 | wake_up(&mddev->recovery_wait); |
| 3176 | return len; |
| 3177 | } |
| 3178 | |
| 3179 | static struct md_sysfs_entry md_max_sync = |
| 3180 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3181 | |
| 3182 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3183 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3184 | { |
| 3185 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3186 | } |
| 3187 | |
| 3188 | static ssize_t |
| 3189 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3190 | { |
| 3191 | char *e; |
| 3192 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3193 | |
| 3194 | if (mddev->pers->quiesce == NULL) |
| 3195 | return -EINVAL; |
| 3196 | if (buf == e || (*e && *e != '\n')) |
| 3197 | return -EINVAL; |
| 3198 | if (new >= mddev->suspend_hi || |
| 3199 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3200 | mddev->suspend_lo = new; |
| 3201 | mddev->pers->quiesce(mddev, 2); |
| 3202 | return len; |
| 3203 | } else |
| 3204 | return -EINVAL; |
| 3205 | } |
| 3206 | static struct md_sysfs_entry md_suspend_lo = |
| 3207 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3208 | |
| 3209 | |
| 3210 | static ssize_t |
| 3211 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3212 | { |
| 3213 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3214 | } |
| 3215 | |
| 3216 | static ssize_t |
| 3217 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3218 | { |
| 3219 | char *e; |
| 3220 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3221 | |
| 3222 | if (mddev->pers->quiesce == NULL) |
| 3223 | return -EINVAL; |
| 3224 | if (buf == e || (*e && *e != '\n')) |
| 3225 | return -EINVAL; |
| 3226 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3227 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3228 | mddev->suspend_hi = new; |
| 3229 | mddev->pers->quiesce(mddev, 1); |
| 3230 | mddev->pers->quiesce(mddev, 0); |
| 3231 | return len; |
| 3232 | } else |
| 3233 | return -EINVAL; |
| 3234 | } |
| 3235 | static struct md_sysfs_entry md_suspend_hi = |
| 3236 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3237 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3238 | static ssize_t |
| 3239 | reshape_position_show(mddev_t *mddev, char *page) |
| 3240 | { |
| 3241 | if (mddev->reshape_position != MaxSector) |
| 3242 | return sprintf(page, "%llu\n", |
| 3243 | (unsigned long long)mddev->reshape_position); |
| 3244 | strcpy(page, "none\n"); |
| 3245 | return 5; |
| 3246 | } |
| 3247 | |
| 3248 | static ssize_t |
| 3249 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3250 | { |
| 3251 | char *e; |
| 3252 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3253 | if (mddev->pers) |
| 3254 | return -EBUSY; |
| 3255 | if (buf == e || (*e && *e != '\n')) |
| 3256 | return -EINVAL; |
| 3257 | mddev->reshape_position = new; |
| 3258 | mddev->delta_disks = 0; |
| 3259 | mddev->new_level = mddev->level; |
| 3260 | mddev->new_layout = mddev->layout; |
| 3261 | mddev->new_chunk = mddev->chunk_size; |
| 3262 | return len; |
| 3263 | } |
| 3264 | |
| 3265 | static struct md_sysfs_entry md_reshape_position = |
| 3266 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3267 | reshape_position_store); |
| 3268 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3269 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3270 | static struct attribute *md_default_attrs[] = { |
| 3271 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3272 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3273 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3274 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3275 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3276 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3277 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3278 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3279 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3280 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3281 | &md_reshape_position.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3282 | NULL, |
| 3283 | }; |
| 3284 | |
| 3285 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3286 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3287 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3288 | &md_sync_min.attr, |
| 3289 | &md_sync_max.attr, |
| 3290 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3291 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3292 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3293 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3294 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3295 | &md_suspend_lo.attr, |
| 3296 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3297 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3298 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3299 | NULL, |
| 3300 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3301 | static struct attribute_group md_redundancy_group = { |
| 3302 | .name = NULL, |
| 3303 | .attrs = md_redundancy_attrs, |
| 3304 | }; |
| 3305 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3306 | |
| 3307 | static ssize_t |
| 3308 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3309 | { |
| 3310 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3311 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3312 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3313 | |
| 3314 | if (!entry->show) |
| 3315 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3316 | rv = mddev_lock(mddev); |
| 3317 | if (!rv) { |
| 3318 | rv = entry->show(mddev, page); |
| 3319 | mddev_unlock(mddev); |
| 3320 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3321 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | static ssize_t |
| 3325 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3326 | const char *page, size_t length) |
| 3327 | { |
| 3328 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3329 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3330 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3331 | |
| 3332 | if (!entry->store) |
| 3333 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3334 | if (!capable(CAP_SYS_ADMIN)) |
| 3335 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3336 | rv = mddev_lock(mddev); |
| 3337 | if (!rv) { |
| 3338 | rv = entry->store(mddev, page, length); |
| 3339 | mddev_unlock(mddev); |
| 3340 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3341 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | static void md_free(struct kobject *ko) |
| 3345 | { |
| 3346 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 3347 | kfree(mddev); |
| 3348 | } |
| 3349 | |
| 3350 | static struct sysfs_ops md_sysfs_ops = { |
| 3351 | .show = md_attr_show, |
| 3352 | .store = md_attr_store, |
| 3353 | }; |
| 3354 | static struct kobj_type md_ktype = { |
| 3355 | .release = md_free, |
| 3356 | .sysfs_ops = &md_sysfs_ops, |
| 3357 | .default_attrs = md_default_attrs, |
| 3358 | }; |
| 3359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | int mdp_major = 0; |
| 3361 | |
| 3362 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3363 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3364 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3365 | mddev_t *mddev = mddev_find(dev); |
| 3366 | struct gendisk *disk; |
| 3367 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 3368 | int shift = partitioned ? MdpMinorShift : 0; |
| 3369 | int unit = MINOR(dev) >> shift; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3370 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | |
| 3372 | if (!mddev) |
| 3373 | return NULL; |
| 3374 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3375 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3376 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3377 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | mddev_put(mddev); |
| 3379 | return NULL; |
| 3380 | } |
| 3381 | disk = alloc_disk(1 << shift); |
| 3382 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3383 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | mddev_put(mddev); |
| 3385 | return NULL; |
| 3386 | } |
| 3387 | disk->major = MAJOR(dev); |
| 3388 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3389 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3391 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | disk->fops = &md_fops; |
| 3394 | disk->private_data = mddev; |
| 3395 | disk->queue = mddev->queue; |
| 3396 | add_disk(disk); |
| 3397 | mddev->gendisk = disk; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3398 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj, |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3399 | "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3400 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3401 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3402 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3403 | disk->disk_name); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3404 | else |
| 3405 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | return NULL; |
| 3407 | } |
| 3408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | static void md_safemode_timeout(unsigned long data) |
| 3410 | { |
| 3411 | mddev_t *mddev = (mddev_t *) data; |
| 3412 | |
| 3413 | mddev->safemode = 1; |
| 3414 | md_wakeup_thread(mddev->thread); |
| 3415 | } |
| 3416 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3417 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | |
| 3419 | static int do_md_run(mddev_t * mddev) |
| 3420 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3421 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3422 | int chunk_size; |
| 3423 | struct list_head *tmp; |
| 3424 | mdk_rdev_t *rdev; |
| 3425 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3426 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | char b[BDEVNAME_SIZE]; |
| 3428 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3429 | if (list_empty(&mddev->disks)) |
| 3430 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | |
| 3433 | if (mddev->pers) |
| 3434 | return -EBUSY; |
| 3435 | |
| 3436 | /* |
| 3437 | * Analyze all RAID superblock(s) |
| 3438 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3439 | if (!mddev->raid_disks) { |
| 3440 | if (!mddev->persistent) |
| 3441 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3442 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | |
| 3445 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3447 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3449 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3450 | chunk_size, MAX_CHUNK_SIZE); |
| 3451 | return -EINVAL; |
| 3452 | } |
| 3453 | /* |
| 3454 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3455 | */ |
| 3456 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3457 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | return -EINVAL; |
| 3459 | } |
| 3460 | if (chunk_size < PAGE_SIZE) { |
| 3461 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3462 | chunk_size, PAGE_SIZE); |
| 3463 | return -EINVAL; |
| 3464 | } |
| 3465 | |
| 3466 | /* devices must have minimum size of one chunk */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3467 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3468 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | continue; |
| 3470 | if (rdev->size < chunk_size / 1024) { |
| 3471 | printk(KERN_WARNING |
| 3472 | "md: Dev %s smaller than chunk_size:" |
| 3473 | " %lluk < %dk\n", |
| 3474 | bdevname(rdev->bdev,b), |
| 3475 | (unsigned long long)rdev->size, |
| 3476 | chunk_size / 1024); |
| 3477 | return -EINVAL; |
| 3478 | } |
| 3479 | } |
| 3480 | } |
| 3481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3483 | if (mddev->level != LEVEL_NONE) |
| 3484 | request_module("md-level-%d", mddev->level); |
| 3485 | else if (mddev->clevel[0]) |
| 3486 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | #endif |
| 3488 | |
| 3489 | /* |
| 3490 | * Drop all container device buffers, from now on |
| 3491 | * the only valid external interface is through the md |
| 3492 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | */ |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3494 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3495 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | continue; |
| 3497 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 3498 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 3499 | |
| 3500 | /* perform some consistency tests on the device. |
| 3501 | * We don't want the data to overlap the metadata, |
| 3502 | * Internal Bitmap issues has handled elsewhere. |
| 3503 | */ |
| 3504 | if (rdev->data_offset < rdev->sb_offset) { |
| 3505 | if (mddev->size && |
| 3506 | rdev->data_offset + mddev->size*2 |
| 3507 | > rdev->sb_offset*2) { |
| 3508 | printk("md: %s: data overlaps metadata\n", |
| 3509 | mdname(mddev)); |
| 3510 | return -EINVAL; |
| 3511 | } |
| 3512 | } else { |
| 3513 | if (rdev->sb_offset*2 + rdev->sb_size/512 |
| 3514 | > rdev->data_offset) { |
| 3515 | printk("md: %s: metadata overlaps data\n", |
| 3516 | mdname(mddev)); |
| 3517 | return -EINVAL; |
| 3518 | } |
| 3519 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | md_probe(mddev->unit, NULL, NULL); |
| 3523 | disk = mddev->gendisk; |
| 3524 | if (!disk) |
| 3525 | return -ENOMEM; |
| 3526 | |
| 3527 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3528 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3529 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3531 | if (mddev->level != LEVEL_NONE) |
| 3532 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3533 | mddev->level); |
| 3534 | else |
| 3535 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3536 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | return -EINVAL; |
| 3538 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3539 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3541 | mddev->level = pers->level; |
| 3542 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3544 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3545 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3546 | /* This personality cannot handle reshaping... */ |
| 3547 | mddev->pers = NULL; |
| 3548 | module_put(pers->owner); |
| 3549 | return -EINVAL; |
| 3550 | } |
| 3551 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3552 | if (pers->sync_request) { |
| 3553 | /* Warn if this is a potentially silly |
| 3554 | * configuration. |
| 3555 | */ |
| 3556 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3557 | mdk_rdev_t *rdev2; |
| 3558 | struct list_head *tmp2; |
| 3559 | int warned = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3560 | rdev_for_each(rdev, tmp, mddev) { |
| 3561 | rdev_for_each(rdev2, tmp2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3562 | if (rdev < rdev2 && |
| 3563 | rdev->bdev->bd_contains == |
| 3564 | rdev2->bdev->bd_contains) { |
| 3565 | printk(KERN_WARNING |
| 3566 | "%s: WARNING: %s appears to be" |
| 3567 | " on the same physical disk as" |
| 3568 | " %s.\n", |
| 3569 | mdname(mddev), |
| 3570 | bdevname(rdev->bdev,b), |
| 3571 | bdevname(rdev2->bdev,b2)); |
| 3572 | warned = 1; |
| 3573 | } |
| 3574 | } |
| 3575 | } |
| 3576 | if (warned) |
| 3577 | printk(KERN_WARNING |
| 3578 | "True protection against single-disk" |
| 3579 | " failure might be compromised.\n"); |
| 3580 | } |
| 3581 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3582 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3584 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3585 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3587 | if (start_readonly) |
| 3588 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3589 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3590 | err = mddev->pers->run(mddev); |
| 3591 | if (!err && mddev->pers->sync_request) { |
| 3592 | err = bitmap_create(mddev); |
| 3593 | if (err) { |
| 3594 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3595 | mdname(mddev), err); |
| 3596 | mddev->pers->stop(mddev); |
| 3597 | } |
| 3598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | if (err) { |
| 3600 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3601 | module_put(mddev->pers->owner); |
| 3602 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3603 | bitmap_destroy(mddev); |
| 3604 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3605 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3606 | if (mddev->pers->sync_request) { |
| 3607 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3608 | printk(KERN_WARNING |
| 3609 | "md: cannot register extra attributes for %s\n", |
| 3610 | mdname(mddev)); |
| 3611 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3612 | mddev->ro = 0; |
| 3613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | atomic_set(&mddev->writes_pending,0); |
| 3615 | mddev->safemode = 0; |
| 3616 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3617 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3618 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3620 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3621 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3622 | if (rdev->raid_disk >= 0) { |
| 3623 | char nm[20]; |
| 3624 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3625 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3626 | printk("md: cannot register %s for %s\n", |
| 3627 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3629 | |
| 3630 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3631 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3632 | if (mddev->flags) |
| 3633 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | |
| 3635 | set_capacity(disk, mddev->array_size<<1); |
| 3636 | |
| 3637 | /* If we call blk_queue_make_request here, it will |
| 3638 | * re-initialise max_sectors etc which may have been |
| 3639 | * refined inside -> run. So just set the bits we need to set. |
| 3640 | * Most initialisation happended when we called |
| 3641 | * blk_queue_make_request(..., md_fail_request) |
| 3642 | * earlier. |
| 3643 | */ |
| 3644 | mddev->queue->queuedata = mddev; |
| 3645 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3646 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3647 | /* If there is a partially-recovered drive we need to |
| 3648 | * start recovery here. If we leave it to md_check_recovery, |
| 3649 | * it will remove the drives and not do the right thing |
| 3650 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3651 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3652 | struct list_head *rtmp; |
| 3653 | int spares = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3654 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3655 | if (rdev->raid_disk >= 0 && |
| 3656 | !test_bit(In_sync, &rdev->flags) && |
| 3657 | !test_bit(Faulty, &rdev->flags)) |
| 3658 | /* complete an interrupted recovery */ |
| 3659 | spares++; |
| 3660 | if (spares && mddev->pers->sync_request) { |
| 3661 | mddev->recovery = 0; |
| 3662 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3663 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3664 | mddev, |
| 3665 | "%s_resync"); |
| 3666 | if (!mddev->sync_thread) { |
| 3667 | printk(KERN_ERR "%s: could not start resync" |
| 3668 | " thread...\n", |
| 3669 | mdname(mddev)); |
| 3670 | /* leave the spares where they are, it shouldn't hurt */ |
| 3671 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3672 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3673 | } |
| 3674 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3675 | md_wakeup_thread(mddev->thread); |
| 3676 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3677 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3678 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3679 | md_new_event(mddev); |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 3680 | kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | return 0; |
| 3682 | } |
| 3683 | |
| 3684 | static int restart_array(mddev_t *mddev) |
| 3685 | { |
| 3686 | struct gendisk *disk = mddev->gendisk; |
| 3687 | int err; |
| 3688 | |
| 3689 | /* |
| 3690 | * Complain if it has no devices |
| 3691 | */ |
| 3692 | err = -ENXIO; |
| 3693 | if (list_empty(&mddev->disks)) |
| 3694 | goto out; |
| 3695 | |
| 3696 | if (mddev->pers) { |
| 3697 | err = -EBUSY; |
| 3698 | if (!mddev->ro) |
| 3699 | goto out; |
| 3700 | |
| 3701 | mddev->safemode = 0; |
| 3702 | mddev->ro = 0; |
| 3703 | set_disk_ro(disk, 0); |
| 3704 | |
| 3705 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3706 | mdname(mddev)); |
| 3707 | /* |
| 3708 | * Kick recovery or resync if necessary |
| 3709 | */ |
| 3710 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3711 | md_wakeup_thread(mddev->thread); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3712 | md_wakeup_thread(mddev->sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3714 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3715 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | |
| 3717 | out: |
| 3718 | return err; |
| 3719 | } |
| 3720 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3721 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3722 | * to the file ourselves */ |
| 3723 | static int deny_bitmap_write_access(struct file * file) |
| 3724 | { |
| 3725 | struct inode *inode = file->f_mapping->host; |
| 3726 | |
| 3727 | spin_lock(&inode->i_lock); |
| 3728 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3729 | spin_unlock(&inode->i_lock); |
| 3730 | return -ETXTBSY; |
| 3731 | } |
| 3732 | atomic_set(&inode->i_writecount, -1); |
| 3733 | spin_unlock(&inode->i_lock); |
| 3734 | |
| 3735 | return 0; |
| 3736 | } |
| 3737 | |
| 3738 | static void restore_bitmap_write_access(struct file *file) |
| 3739 | { |
| 3740 | struct inode *inode = file->f_mapping->host; |
| 3741 | |
| 3742 | spin_lock(&inode->i_lock); |
| 3743 | atomic_set(&inode->i_writecount, 1); |
| 3744 | spin_unlock(&inode->i_lock); |
| 3745 | } |
| 3746 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3747 | /* mode: |
| 3748 | * 0 - completely stop and dis-assemble array |
| 3749 | * 1 - switch to readonly |
| 3750 | * 2 - stop but do not disassemble array |
| 3751 | */ |
| 3752 | static int do_md_stop(mddev_t * mddev, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3753 | { |
| 3754 | int err = 0; |
| 3755 | struct gendisk *disk = mddev->gendisk; |
| 3756 | |
| 3757 | if (mddev->pers) { |
| 3758 | if (atomic_read(&mddev->active)>2) { |
| 3759 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3760 | return -EBUSY; |
| 3761 | } |
| 3762 | |
| 3763 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3764 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3766 | md_unregister_thread(mddev->sync_thread); |
| 3767 | mddev->sync_thread = NULL; |
| 3768 | } |
| 3769 | |
| 3770 | del_timer_sync(&mddev->safemode_timer); |
| 3771 | |
| 3772 | invalidate_partition(disk, 0); |
| 3773 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3774 | switch(mode) { |
| 3775 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3777 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | goto out; |
| 3779 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3780 | break; |
| 3781 | case 0: /* disassemble */ |
| 3782 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3783 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3784 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3785 | if (mddev->ro) |
| 3786 | set_disk_ro(disk, 0); |
| 3787 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3788 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3789 | mddev->queue->merge_bvec_fn = NULL; |
| 3790 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3791 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3792 | if (mddev->pers->sync_request) |
| 3793 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | module_put(mddev->pers->owner); |
| 3796 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 3797 | /* tell userspace to handle 'inactive' */ |
| 3798 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3799 | |
| 3800 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 3801 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | if (mddev->ro) |
| 3804 | mddev->ro = 0; |
| 3805 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3806 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3807 | /* mark array as shutdown cleanly */ |
| 3808 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3809 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3810 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3811 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3813 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | /* |
| 3817 | * Free resources if final stop |
| 3818 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3819 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3820 | mdk_rdev_t *rdev; |
| 3821 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3824 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3825 | bitmap_destroy(mddev); |
| 3826 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3827 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3828 | fput(mddev->bitmap_file); |
| 3829 | mddev->bitmap_file = NULL; |
| 3830 | } |
| 3831 | mddev->bitmap_offset = 0; |
| 3832 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3833 | rdev_for_each(rdev, tmp, mddev) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3834 | if (rdev->raid_disk >= 0) { |
| 3835 | char nm[20]; |
| 3836 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3837 | sysfs_remove_link(&mddev->kobj, nm); |
| 3838 | } |
| 3839 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 3840 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3841 | flush_scheduled_work(); |
| 3842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | export_array(mddev); |
| 3844 | |
| 3845 | mddev->array_size = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3846 | mddev->size = 0; |
| 3847 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3848 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3849 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3850 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3851 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3852 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3853 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 3854 | mddev->level = LEVEL_NONE; |
| 3855 | mddev->clevel[0] = 0; |
| 3856 | mddev->flags = 0; |
| 3857 | mddev->ro = 0; |
| 3858 | mddev->metadata_type[0] = 0; |
| 3859 | mddev->chunk_size = 0; |
| 3860 | mddev->ctime = mddev->utime = 0; |
| 3861 | mddev->layout = 0; |
| 3862 | mddev->max_disks = 0; |
| 3863 | mddev->events = 0; |
| 3864 | mddev->delta_disks = 0; |
| 3865 | mddev->new_level = LEVEL_NONE; |
| 3866 | mddev->new_layout = 0; |
| 3867 | mddev->new_chunk = 0; |
| 3868 | mddev->curr_resync = 0; |
| 3869 | mddev->resync_mismatches = 0; |
| 3870 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 3871 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 3872 | mddev->recovery = 0; |
| 3873 | mddev->in_sync = 0; |
| 3874 | mddev->changed = 0; |
| 3875 | mddev->degraded = 0; |
| 3876 | mddev->barriers_work = 0; |
| 3877 | mddev->safemode = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3878 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3879 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3880 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3881 | mdname(mddev)); |
| 3882 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3883 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | out: |
| 3885 | return err; |
| 3886 | } |
| 3887 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3888 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | static void autorun_array(mddev_t *mddev) |
| 3890 | { |
| 3891 | mdk_rdev_t *rdev; |
| 3892 | struct list_head *tmp; |
| 3893 | int err; |
| 3894 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3895 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | |
| 3898 | printk(KERN_INFO "md: running: "); |
| 3899 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3900 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | char b[BDEVNAME_SIZE]; |
| 3902 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3903 | } |
| 3904 | printk("\n"); |
| 3905 | |
| 3906 | err = do_md_run (mddev); |
| 3907 | if (err) { |
| 3908 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 3909 | do_md_stop (mddev, 0); |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | /* |
| 3914 | * lets try to run arrays based on all disks that have arrived |
| 3915 | * until now. (those are in pending_raid_disks) |
| 3916 | * |
| 3917 | * the method: pick the first pending disk, collect all disks with |
| 3918 | * the same UUID, remove all from the pending list and put them into |
| 3919 | * the 'same_array' list. Then order this list based on superblock |
| 3920 | * update time (freshest comes first), kick out 'old' disks and |
| 3921 | * compare superblocks. If everything's fine then run it. |
| 3922 | * |
| 3923 | * If "unit" is allocated, then bump its reference count |
| 3924 | */ |
| 3925 | static void autorun_devices(int part) |
| 3926 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | struct list_head *tmp; |
| 3928 | mdk_rdev_t *rdev0, *rdev; |
| 3929 | mddev_t *mddev; |
| 3930 | char b[BDEVNAME_SIZE]; |
| 3931 | |
| 3932 | printk(KERN_INFO "md: autorun ...\n"); |
| 3933 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3934 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 3936 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | rdev0 = list_entry(pending_raid_disks.next, |
| 3938 | mdk_rdev_t, same_set); |
| 3939 | |
| 3940 | printk(KERN_INFO "md: considering %s ...\n", |
| 3941 | bdevname(rdev0->bdev,b)); |
| 3942 | INIT_LIST_HEAD(&candidates); |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3943 | rdev_for_each_list(rdev, tmp, pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 3945 | printk(KERN_INFO "md: adding %s ...\n", |
| 3946 | bdevname(rdev->bdev,b)); |
| 3947 | list_move(&rdev->same_set, &candidates); |
| 3948 | } |
| 3949 | /* |
| 3950 | * now we have a set of devices, with all of them having |
| 3951 | * mostly sane superblocks. It's time to allocate the |
| 3952 | * mddev. |
| 3953 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3954 | if (part) { |
| 3955 | dev = MKDEV(mdp_major, |
| 3956 | rdev0->preferred_minor << MdpMinorShift); |
| 3957 | unit = MINOR(dev) >> MdpMinorShift; |
| 3958 | } else { |
| 3959 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 3960 | unit = MINOR(dev); |
| 3961 | } |
| 3962 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 3964 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 3965 | break; |
| 3966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | |
| 3968 | md_probe(dev, NULL, NULL); |
| 3969 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 3970 | if (!mddev || !mddev->gendisk) { |
| 3971 | if (mddev) |
| 3972 | mddev_put(mddev); |
| 3973 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | "md: cannot allocate memory for md drive.\n"); |
| 3975 | break; |
| 3976 | } |
| 3977 | if (mddev_lock(mddev)) |
| 3978 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 3979 | mdname(mddev)); |
| 3980 | else if (mddev->raid_disks || mddev->major_version |
| 3981 | || !list_empty(&mddev->disks)) { |
| 3982 | printk(KERN_WARNING |
| 3983 | "md: %s already running, cannot run %s\n", |
| 3984 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 3985 | mddev_unlock(mddev); |
| 3986 | } else { |
| 3987 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3988 | mddev->persistent = 1; |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 3989 | rdev_for_each_list(rdev, tmp, candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | list_del_init(&rdev->same_set); |
| 3991 | if (bind_rdev_to_array(rdev, mddev)) |
| 3992 | export_rdev(rdev); |
| 3993 | } |
| 3994 | autorun_array(mddev); |
| 3995 | mddev_unlock(mddev); |
| 3996 | } |
| 3997 | /* on success, candidates will be empty, on error |
| 3998 | * it won't... |
| 3999 | */ |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4000 | rdev_for_each_list(rdev, tmp, candidates) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | export_rdev(rdev); |
| 4002 | mddev_put(mddev); |
| 4003 | } |
| 4004 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4005 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4006 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | static int get_version(void __user * arg) |
| 4009 | { |
| 4010 | mdu_version_t ver; |
| 4011 | |
| 4012 | ver.major = MD_MAJOR_VERSION; |
| 4013 | ver.minor = MD_MINOR_VERSION; |
| 4014 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4015 | |
| 4016 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4017 | return -EFAULT; |
| 4018 | |
| 4019 | return 0; |
| 4020 | } |
| 4021 | |
| 4022 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4023 | { |
| 4024 | mdu_array_info_t info; |
| 4025 | int nr,working,active,failed,spare; |
| 4026 | mdk_rdev_t *rdev; |
| 4027 | struct list_head *tmp; |
| 4028 | |
| 4029 | nr=working=active=failed=spare=0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4030 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4032 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | failed++; |
| 4034 | else { |
| 4035 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4036 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | active++; |
| 4038 | else |
| 4039 | spare++; |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | info.major_version = mddev->major_version; |
| 4044 | info.minor_version = mddev->minor_version; |
| 4045 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4046 | info.ctime = mddev->ctime; |
| 4047 | info.level = mddev->level; |
| 4048 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4049 | if (info.size != mddev->size) /* overflow */ |
| 4050 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | info.nr_disks = nr; |
| 4052 | info.raid_disks = mddev->raid_disks; |
| 4053 | info.md_minor = mddev->md_minor; |
| 4054 | info.not_persistent= !mddev->persistent; |
| 4055 | |
| 4056 | info.utime = mddev->utime; |
| 4057 | info.state = 0; |
| 4058 | if (mddev->in_sync) |
| 4059 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4060 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4061 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | info.active_disks = active; |
| 4063 | info.working_disks = working; |
| 4064 | info.failed_disks = failed; |
| 4065 | info.spare_disks = spare; |
| 4066 | |
| 4067 | info.layout = mddev->layout; |
| 4068 | info.chunk_size = mddev->chunk_size; |
| 4069 | |
| 4070 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4071 | return -EFAULT; |
| 4072 | |
| 4073 | return 0; |
| 4074 | } |
| 4075 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4076 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4077 | { |
| 4078 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4079 | char *ptr, *buf = NULL; |
| 4080 | int err = -ENOMEM; |
| 4081 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4082 | md_allow_write(mddev); |
| 4083 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4084 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
| 4085 | if (!file) |
| 4086 | goto out; |
| 4087 | |
| 4088 | /* bitmap disabled, zero the first byte and copy out */ |
| 4089 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4090 | file->pathname[0] = '\0'; |
| 4091 | goto copy_out; |
| 4092 | } |
| 4093 | |
| 4094 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4095 | if (!buf) |
| 4096 | goto out; |
| 4097 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4098 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4099 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4100 | goto out; |
| 4101 | |
| 4102 | strcpy(file->pathname, ptr); |
| 4103 | |
| 4104 | copy_out: |
| 4105 | err = 0; |
| 4106 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4107 | err = -EFAULT; |
| 4108 | out: |
| 4109 | kfree(buf); |
| 4110 | kfree(file); |
| 4111 | return err; |
| 4112 | } |
| 4113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4115 | { |
| 4116 | mdu_disk_info_t info; |
| 4117 | unsigned int nr; |
| 4118 | mdk_rdev_t *rdev; |
| 4119 | |
| 4120 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4121 | return -EFAULT; |
| 4122 | |
| 4123 | nr = info.number; |
| 4124 | |
| 4125 | rdev = find_rdev_nr(mddev, nr); |
| 4126 | if (rdev) { |
| 4127 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4128 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4129 | info.raid_disk = rdev->raid_disk; |
| 4130 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4131 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4133 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4135 | info.state |= (1<<MD_DISK_SYNC); |
| 4136 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4137 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4138 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | } else { |
| 4140 | info.major = info.minor = 0; |
| 4141 | info.raid_disk = -1; |
| 4142 | info.state = (1<<MD_DISK_REMOVED); |
| 4143 | } |
| 4144 | |
| 4145 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4146 | return -EFAULT; |
| 4147 | |
| 4148 | return 0; |
| 4149 | } |
| 4150 | |
| 4151 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4152 | { |
| 4153 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4154 | mdk_rdev_t *rdev; |
| 4155 | dev_t dev = MKDEV(info->major,info->minor); |
| 4156 | |
| 4157 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4158 | return -EOVERFLOW; |
| 4159 | |
| 4160 | if (!mddev->raid_disks) { |
| 4161 | int err; |
| 4162 | /* expecting a device which has a superblock */ |
| 4163 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4164 | if (IS_ERR(rdev)) { |
| 4165 | printk(KERN_WARNING |
| 4166 | "md: md_import_device returned %ld\n", |
| 4167 | PTR_ERR(rdev)); |
| 4168 | return PTR_ERR(rdev); |
| 4169 | } |
| 4170 | if (!list_empty(&mddev->disks)) { |
| 4171 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4172 | mdk_rdev_t, same_set); |
| 4173 | int err = super_types[mddev->major_version] |
| 4174 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4175 | if (err < 0) { |
| 4176 | printk(KERN_WARNING |
| 4177 | "md: %s has different UUID to %s\n", |
| 4178 | bdevname(rdev->bdev,b), |
| 4179 | bdevname(rdev0->bdev,b2)); |
| 4180 | export_rdev(rdev); |
| 4181 | return -EINVAL; |
| 4182 | } |
| 4183 | } |
| 4184 | err = bind_rdev_to_array(rdev, mddev); |
| 4185 | if (err) |
| 4186 | export_rdev(rdev); |
| 4187 | return err; |
| 4188 | } |
| 4189 | |
| 4190 | /* |
| 4191 | * add_new_disk can be used once the array is assembled |
| 4192 | * to add "hot spares". They must already have a superblock |
| 4193 | * written |
| 4194 | */ |
| 4195 | if (mddev->pers) { |
| 4196 | int err; |
| 4197 | if (!mddev->pers->hot_add_disk) { |
| 4198 | printk(KERN_WARNING |
| 4199 | "%s: personality does not support diskops!\n", |
| 4200 | mdname(mddev)); |
| 4201 | return -EINVAL; |
| 4202 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4203 | if (mddev->persistent) |
| 4204 | rdev = md_import_device(dev, mddev->major_version, |
| 4205 | mddev->minor_version); |
| 4206 | else |
| 4207 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | if (IS_ERR(rdev)) { |
| 4209 | printk(KERN_WARNING |
| 4210 | "md: md_import_device returned %ld\n", |
| 4211 | PTR_ERR(rdev)); |
| 4212 | return PTR_ERR(rdev); |
| 4213 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4214 | /* set save_raid_disk if appropriate */ |
| 4215 | if (!mddev->persistent) { |
| 4216 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4217 | info->raid_disk < mddev->raid_disks) |
| 4218 | rdev->raid_disk = info->raid_disk; |
| 4219 | else |
| 4220 | rdev->raid_disk = -1; |
| 4221 | } else |
| 4222 | super_types[mddev->major_version]. |
| 4223 | validate_super(mddev, rdev); |
| 4224 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4225 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4226 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4227 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4228 | set_bit(WriteMostly, &rdev->flags); |
| 4229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | rdev->raid_disk = -1; |
| 4231 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4232 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4233 | /* If there is hot_add_disk but no hot_remove_disk |
| 4234 | * then added disks for geometry changes, |
| 4235 | * and should be added immediately. |
| 4236 | */ |
| 4237 | super_types[mddev->major_version]. |
| 4238 | validate_super(mddev, rdev); |
| 4239 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4240 | if (err) |
| 4241 | unbind_rdev_from_array(rdev); |
| 4242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | if (err) |
| 4244 | export_rdev(rdev); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4245 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4246 | md_update_sb(mddev, 1); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4247 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4248 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | return err; |
| 4250 | } |
| 4251 | |
| 4252 | /* otherwise, add_new_disk is only allowed |
| 4253 | * for major_version==0 superblocks |
| 4254 | */ |
| 4255 | if (mddev->major_version != 0) { |
| 4256 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4257 | mdname(mddev)); |
| 4258 | return -EINVAL; |
| 4259 | } |
| 4260 | |
| 4261 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4262 | int err; |
| 4263 | rdev = md_import_device (dev, -1, 0); |
| 4264 | if (IS_ERR(rdev)) { |
| 4265 | printk(KERN_WARNING |
| 4266 | "md: error, md_import_device() returned %ld\n", |
| 4267 | PTR_ERR(rdev)); |
| 4268 | return PTR_ERR(rdev); |
| 4269 | } |
| 4270 | rdev->desc_nr = info->number; |
| 4271 | if (info->raid_disk < mddev->raid_disks) |
| 4272 | rdev->raid_disk = info->raid_disk; |
| 4273 | else |
| 4274 | rdev->raid_disk = -1; |
| 4275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4277 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4278 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4280 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4281 | set_bit(WriteMostly, &rdev->flags); |
| 4282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | if (!mddev->persistent) { |
| 4284 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 4285 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4286 | } else |
| 4287 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4288 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 4289 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4290 | err = bind_rdev_to_array(rdev, mddev); |
| 4291 | if (err) { |
| 4292 | export_rdev(rdev); |
| 4293 | return err; |
| 4294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | return 0; |
| 4298 | } |
| 4299 | |
| 4300 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4301 | { |
| 4302 | char b[BDEVNAME_SIZE]; |
| 4303 | mdk_rdev_t *rdev; |
| 4304 | |
| 4305 | if (!mddev->pers) |
| 4306 | return -ENODEV; |
| 4307 | |
| 4308 | rdev = find_rdev(mddev, dev); |
| 4309 | if (!rdev) |
| 4310 | return -ENXIO; |
| 4311 | |
| 4312 | if (rdev->raid_disk >= 0) |
| 4313 | goto busy; |
| 4314 | |
| 4315 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4316 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4317 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | |
| 4319 | return 0; |
| 4320 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4321 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4323 | return -EBUSY; |
| 4324 | } |
| 4325 | |
| 4326 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4327 | { |
| 4328 | char b[BDEVNAME_SIZE]; |
| 4329 | int err; |
| 4330 | unsigned int size; |
| 4331 | mdk_rdev_t *rdev; |
| 4332 | |
| 4333 | if (!mddev->pers) |
| 4334 | return -ENODEV; |
| 4335 | |
| 4336 | if (mddev->major_version != 0) { |
| 4337 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4338 | " version-0 superblocks.\n", |
| 4339 | mdname(mddev)); |
| 4340 | return -EINVAL; |
| 4341 | } |
| 4342 | if (!mddev->pers->hot_add_disk) { |
| 4343 | printk(KERN_WARNING |
| 4344 | "%s: personality does not support diskops!\n", |
| 4345 | mdname(mddev)); |
| 4346 | return -EINVAL; |
| 4347 | } |
| 4348 | |
| 4349 | rdev = md_import_device (dev, -1, 0); |
| 4350 | if (IS_ERR(rdev)) { |
| 4351 | printk(KERN_WARNING |
| 4352 | "md: error, md_import_device() returned %ld\n", |
| 4353 | PTR_ERR(rdev)); |
| 4354 | return -EINVAL; |
| 4355 | } |
| 4356 | |
| 4357 | if (mddev->persistent) |
| 4358 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 4359 | else |
| 4360 | rdev->sb_offset = |
| 4361 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 4362 | |
| 4363 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 4364 | rdev->size = size; |
| 4365 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4366 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | printk(KERN_WARNING |
| 4368 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4369 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4370 | err = -EINVAL; |
| 4371 | goto abort_export; |
| 4372 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4373 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4375 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4376 | err = bind_rdev_to_array(rdev, mddev); |
| 4377 | if (err) |
| 4378 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | |
| 4380 | /* |
| 4381 | * The rest should better be atomic, we can have disk failures |
| 4382 | * noticed in interrupt contexts ... |
| 4383 | */ |
| 4384 | |
| 4385 | if (rdev->desc_nr == mddev->max_disks) { |
| 4386 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 4387 | mdname(mddev)); |
| 4388 | err = -EBUSY; |
| 4389 | goto abort_unbind_export; |
| 4390 | } |
| 4391 | |
| 4392 | rdev->raid_disk = -1; |
| 4393 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4394 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | |
| 4396 | /* |
| 4397 | * Kick recovery, maybe this spare has to be added to the |
| 4398 | * array immediately. |
| 4399 | */ |
| 4400 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4401 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4402 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | return 0; |
| 4404 | |
| 4405 | abort_unbind_export: |
| 4406 | unbind_rdev_from_array(rdev); |
| 4407 | |
| 4408 | abort_export: |
| 4409 | export_rdev(rdev); |
| 4410 | return err; |
| 4411 | } |
| 4412 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4413 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4414 | { |
| 4415 | int err; |
| 4416 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4417 | if (mddev->pers) { |
| 4418 | if (!mddev->pers->quiesce) |
| 4419 | return -EBUSY; |
| 4420 | if (mddev->recovery || mddev->sync_thread) |
| 4421 | return -EBUSY; |
| 4422 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4423 | } |
| 4424 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4425 | |
| 4426 | if (fd >= 0) { |
| 4427 | if (mddev->bitmap) |
| 4428 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4429 | mddev->bitmap_file = fget(fd); |
| 4430 | |
| 4431 | if (mddev->bitmap_file == NULL) { |
| 4432 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4433 | mdname(mddev)); |
| 4434 | return -EBADF; |
| 4435 | } |
| 4436 | |
| 4437 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4438 | if (err) { |
| 4439 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4440 | mdname(mddev)); |
| 4441 | fput(mddev->bitmap_file); |
| 4442 | mddev->bitmap_file = NULL; |
| 4443 | return err; |
| 4444 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4445 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4446 | } else if (mddev->bitmap == NULL) |
| 4447 | return -ENOENT; /* cannot remove what isn't there */ |
| 4448 | err = 0; |
| 4449 | if (mddev->pers) { |
| 4450 | mddev->pers->quiesce(mddev, 1); |
| 4451 | if (fd >= 0) |
| 4452 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4453 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4454 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4455 | fd = -1; /* make sure to put the file */ |
| 4456 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4457 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4458 | } |
| 4459 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4460 | if (mddev->bitmap_file) { |
| 4461 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4462 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4463 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4464 | mddev->bitmap_file = NULL; |
| 4465 | } |
| 4466 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4467 | return err; |
| 4468 | } |
| 4469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | /* |
| 4471 | * set_array_info is used two different ways |
| 4472 | * The original usage is when creating a new array. |
| 4473 | * In this usage, raid_disks is > 0 and it together with |
| 4474 | * level, size, not_persistent,layout,chunksize determine the |
| 4475 | * shape of the array. |
| 4476 | * This will always create an array with a type-0.90.0 superblock. |
| 4477 | * The newer usage is when assembling an array. |
| 4478 | * In this case raid_disks will be 0, and the major_version field is |
| 4479 | * use to determine which style super-blocks are to be found on the devices. |
| 4480 | * The minor and patch _version numbers are also kept incase the |
| 4481 | * super_block handler wishes to interpret them. |
| 4482 | */ |
| 4483 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4484 | { |
| 4485 | |
| 4486 | if (info->raid_disks == 0) { |
| 4487 | /* just setting version number for superblock loading */ |
| 4488 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4489 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | super_types[info->major_version].name == NULL) { |
| 4491 | /* maybe try to auto-load a module? */ |
| 4492 | printk(KERN_INFO |
| 4493 | "md: superblock version %d not known\n", |
| 4494 | info->major_version); |
| 4495 | return -EINVAL; |
| 4496 | } |
| 4497 | mddev->major_version = info->major_version; |
| 4498 | mddev->minor_version = info->minor_version; |
| 4499 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4500 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | mddev->major_version = MD_MAJOR_VERSION; |
| 4504 | mddev->minor_version = MD_MINOR_VERSION; |
| 4505 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4506 | mddev->ctime = get_seconds(); |
| 4507 | |
| 4508 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4509 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | mddev->size = info->size; |
| 4511 | mddev->raid_disks = info->raid_disks; |
| 4512 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4513 | * openned |
| 4514 | */ |
| 4515 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4516 | mddev->recovery_cp = MaxSector; |
| 4517 | else |
| 4518 | mddev->recovery_cp = 0; |
| 4519 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4520 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4521 | |
| 4522 | mddev->layout = info->layout; |
| 4523 | mddev->chunk_size = info->chunk_size; |
| 4524 | |
| 4525 | mddev->max_disks = MD_SB_DISKS; |
| 4526 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4527 | if (mddev->persistent) |
| 4528 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4529 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4531 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4532 | mddev->bitmap_offset = 0; |
| 4533 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4534 | mddev->reshape_position = MaxSector; |
| 4535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | /* |
| 4537 | * Generate a 128 bit UUID |
| 4538 | */ |
| 4539 | get_random_bytes(mddev->uuid, 16); |
| 4540 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4541 | mddev->new_level = mddev->level; |
| 4542 | mddev->new_chunk = mddev->chunk_size; |
| 4543 | mddev->new_layout = mddev->layout; |
| 4544 | mddev->delta_disks = 0; |
| 4545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | return 0; |
| 4547 | } |
| 4548 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4549 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4550 | { |
| 4551 | mdk_rdev_t * rdev; |
| 4552 | int rv; |
| 4553 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4554 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4555 | |
| 4556 | if (mddev->pers->resize == NULL) |
| 4557 | return -EINVAL; |
| 4558 | /* The "size" is the amount of each device that is used. |
| 4559 | * This can only make sense for arrays with redundancy. |
| 4560 | * linear and raid0 always use whatever space is available |
| 4561 | * We can only consider changing the size if no resync |
| 4562 | * or reconstruction is happening, and if the new size |
| 4563 | * is acceptable. It must fit before the sb_offset or, |
| 4564 | * if that is <data_offset, it must fit before the |
| 4565 | * size of each device. |
| 4566 | * If size is zero, we find the largest size that fits. |
| 4567 | */ |
| 4568 | if (mddev->sync_thread) |
| 4569 | return -EBUSY; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 4570 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4571 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4572 | avail = rdev->size * 2; |
| 4573 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4574 | if (fit && (size == 0 || size > avail/2)) |
| 4575 | size = avail/2; |
| 4576 | if (avail < ((sector_t)size << 1)) |
| 4577 | return -ENOSPC; |
| 4578 | } |
| 4579 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4580 | if (!rv) { |
| 4581 | struct block_device *bdev; |
| 4582 | |
| 4583 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4584 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4585 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4586 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4587 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4588 | bdput(bdev); |
| 4589 | } |
| 4590 | } |
| 4591 | return rv; |
| 4592 | } |
| 4593 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4594 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4595 | { |
| 4596 | int rv; |
| 4597 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4598 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4599 | return -EINVAL; |
| 4600 | if (raid_disks <= 0 || |
| 4601 | raid_disks >= mddev->max_disks) |
| 4602 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4603 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4604 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4605 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4606 | |
| 4607 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4608 | return rv; |
| 4609 | } |
| 4610 | |
| 4611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | /* |
| 4613 | * update_array_info is used to change the configuration of an |
| 4614 | * on-line array. |
| 4615 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4616 | * fields in the info are checked against the array. |
| 4617 | * Any differences that cannot be handled will cause an error. |
| 4618 | * Normally, only one change can be managed at a time. |
| 4619 | */ |
| 4620 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4621 | { |
| 4622 | int rv = 0; |
| 4623 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4624 | int state = 0; |
| 4625 | |
| 4626 | /* calculate expected state,ignoring low bits */ |
| 4627 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4628 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4629 | |
| 4630 | if (mddev->major_version != info->major_version || |
| 4631 | mddev->minor_version != info->minor_version || |
| 4632 | /* mddev->patch_version != info->patch_version || */ |
| 4633 | mddev->ctime != info->ctime || |
| 4634 | mddev->level != info->level || |
| 4635 | /* mddev->layout != info->layout || */ |
| 4636 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4637 | mddev->chunk_size != info->chunk_size || |
| 4638 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4639 | ((state^info->state) & 0xfffffe00) |
| 4640 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | return -EINVAL; |
| 4642 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4643 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4645 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4646 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | if (cnt == 0) return 0; |
| 4648 | if (cnt > 1) return -EINVAL; |
| 4649 | |
| 4650 | if (mddev->layout != info->layout) { |
| 4651 | /* Change layout |
| 4652 | * we don't need to do anything at the md level, the |
| 4653 | * personality will take care of it all. |
| 4654 | */ |
| 4655 | if (mddev->pers->reconfig == NULL) |
| 4656 | return -EINVAL; |
| 4657 | else |
| 4658 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4659 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4660 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4661 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4663 | if (mddev->raid_disks != info->raid_disks) |
| 4664 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4666 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4667 | if (mddev->pers->quiesce == NULL) |
| 4668 | return -EINVAL; |
| 4669 | if (mddev->recovery || mddev->sync_thread) |
| 4670 | return -EBUSY; |
| 4671 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4672 | /* add the bitmap */ |
| 4673 | if (mddev->bitmap) |
| 4674 | return -EEXIST; |
| 4675 | if (mddev->default_bitmap_offset == 0) |
| 4676 | return -EINVAL; |
| 4677 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4678 | mddev->pers->quiesce(mddev, 1); |
| 4679 | rv = bitmap_create(mddev); |
| 4680 | if (rv) |
| 4681 | bitmap_destroy(mddev); |
| 4682 | mddev->pers->quiesce(mddev, 0); |
| 4683 | } else { |
| 4684 | /* remove the bitmap */ |
| 4685 | if (!mddev->bitmap) |
| 4686 | return -ENOENT; |
| 4687 | if (mddev->bitmap->file) |
| 4688 | return -EINVAL; |
| 4689 | mddev->pers->quiesce(mddev, 1); |
| 4690 | bitmap_destroy(mddev); |
| 4691 | mddev->pers->quiesce(mddev, 0); |
| 4692 | mddev->bitmap_offset = 0; |
| 4693 | } |
| 4694 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4695 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | return rv; |
| 4697 | } |
| 4698 | |
| 4699 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4700 | { |
| 4701 | mdk_rdev_t *rdev; |
| 4702 | |
| 4703 | if (mddev->pers == NULL) |
| 4704 | return -ENODEV; |
| 4705 | |
| 4706 | rdev = find_rdev(mddev, dev); |
| 4707 | if (!rdev) |
| 4708 | return -ENODEV; |
| 4709 | |
| 4710 | md_error(mddev, rdev); |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4714 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4715 | { |
| 4716 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4717 | |
| 4718 | geo->heads = 2; |
| 4719 | geo->sectors = 4; |
| 4720 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4721 | return 0; |
| 4722 | } |
| 4723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4725 | unsigned int cmd, unsigned long arg) |
| 4726 | { |
| 4727 | int err = 0; |
| 4728 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | mddev_t *mddev = NULL; |
| 4730 | |
| 4731 | if (!capable(CAP_SYS_ADMIN)) |
| 4732 | return -EACCES; |
| 4733 | |
| 4734 | /* |
| 4735 | * Commands dealing with the RAID driver but not any |
| 4736 | * particular array: |
| 4737 | */ |
| 4738 | switch (cmd) |
| 4739 | { |
| 4740 | case RAID_VERSION: |
| 4741 | err = get_version(argp); |
| 4742 | goto done; |
| 4743 | |
| 4744 | case PRINT_RAID_DEBUG: |
| 4745 | err = 0; |
| 4746 | md_print_devices(); |
| 4747 | goto done; |
| 4748 | |
| 4749 | #ifndef MODULE |
| 4750 | case RAID_AUTORUN: |
| 4751 | err = 0; |
| 4752 | autostart_arrays(arg); |
| 4753 | goto done; |
| 4754 | #endif |
| 4755 | default:; |
| 4756 | } |
| 4757 | |
| 4758 | /* |
| 4759 | * Commands creating/starting a new array: |
| 4760 | */ |
| 4761 | |
| 4762 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4763 | |
| 4764 | if (!mddev) { |
| 4765 | BUG(); |
| 4766 | goto abort; |
| 4767 | } |
| 4768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | err = mddev_lock(mddev); |
| 4770 | if (err) { |
| 4771 | printk(KERN_INFO |
| 4772 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4773 | err, cmd); |
| 4774 | goto abort; |
| 4775 | } |
| 4776 | |
| 4777 | switch (cmd) |
| 4778 | { |
| 4779 | case SET_ARRAY_INFO: |
| 4780 | { |
| 4781 | mdu_array_info_t info; |
| 4782 | if (!arg) |
| 4783 | memset(&info, 0, sizeof(info)); |
| 4784 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4785 | err = -EFAULT; |
| 4786 | goto abort_unlock; |
| 4787 | } |
| 4788 | if (mddev->pers) { |
| 4789 | err = update_array_info(mddev, &info); |
| 4790 | if (err) { |
| 4791 | printk(KERN_WARNING "md: couldn't update" |
| 4792 | " array info. %d\n", err); |
| 4793 | goto abort_unlock; |
| 4794 | } |
| 4795 | goto done_unlock; |
| 4796 | } |
| 4797 | if (!list_empty(&mddev->disks)) { |
| 4798 | printk(KERN_WARNING |
| 4799 | "md: array %s already has disks!\n", |
| 4800 | mdname(mddev)); |
| 4801 | err = -EBUSY; |
| 4802 | goto abort_unlock; |
| 4803 | } |
| 4804 | if (mddev->raid_disks) { |
| 4805 | printk(KERN_WARNING |
| 4806 | "md: array %s already initialised!\n", |
| 4807 | mdname(mddev)); |
| 4808 | err = -EBUSY; |
| 4809 | goto abort_unlock; |
| 4810 | } |
| 4811 | err = set_array_info(mddev, &info); |
| 4812 | if (err) { |
| 4813 | printk(KERN_WARNING "md: couldn't set" |
| 4814 | " array info. %d\n", err); |
| 4815 | goto abort_unlock; |
| 4816 | } |
| 4817 | } |
| 4818 | goto done_unlock; |
| 4819 | |
| 4820 | default:; |
| 4821 | } |
| 4822 | |
| 4823 | /* |
| 4824 | * Commands querying/configuring an existing array: |
| 4825 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4826 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4827 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 4828 | if ((!mddev->raid_disks && !mddev->external) |
| 4829 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4830 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4831 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | err = -ENODEV; |
| 4833 | goto abort_unlock; |
| 4834 | } |
| 4835 | |
| 4836 | /* |
| 4837 | * Commands even a read-only array can execute: |
| 4838 | */ |
| 4839 | switch (cmd) |
| 4840 | { |
| 4841 | case GET_ARRAY_INFO: |
| 4842 | err = get_array_info(mddev, argp); |
| 4843 | goto done_unlock; |
| 4844 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4845 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4846 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4847 | goto done_unlock; |
| 4848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | case GET_DISK_INFO: |
| 4850 | err = get_disk_info(mddev, argp); |
| 4851 | goto done_unlock; |
| 4852 | |
| 4853 | case RESTART_ARRAY_RW: |
| 4854 | err = restart_array(mddev); |
| 4855 | goto done_unlock; |
| 4856 | |
| 4857 | case STOP_ARRAY: |
| 4858 | err = do_md_stop (mddev, 0); |
| 4859 | goto done_unlock; |
| 4860 | |
| 4861 | case STOP_ARRAY_RO: |
| 4862 | err = do_md_stop (mddev, 1); |
| 4863 | goto done_unlock; |
| 4864 | |
| 4865 | /* |
| 4866 | * We have a problem here : there is no easy way to give a CHS |
| 4867 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4868 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4869 | * dosfs just mad... ;-) |
| 4870 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | /* |
| 4874 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4875 | * superblock, so we do not allow them on read-only arrays. |
| 4876 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4877 | * here and hit the 'default' below, so only disallow |
| 4878 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | */ |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4880 | if (_IOC_TYPE(cmd) == MD_MAJOR && |
| 4881 | mddev->ro && mddev->pers) { |
| 4882 | if (mddev->ro == 2) { |
| 4883 | mddev->ro = 0; |
| 4884 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4885 | md_wakeup_thread(mddev->thread); |
| 4886 | |
| 4887 | } else { |
| 4888 | err = -EROFS; |
| 4889 | goto abort_unlock; |
| 4890 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | switch (cmd) |
| 4894 | { |
| 4895 | case ADD_NEW_DISK: |
| 4896 | { |
| 4897 | mdu_disk_info_t info; |
| 4898 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4899 | err = -EFAULT; |
| 4900 | else |
| 4901 | err = add_new_disk(mddev, &info); |
| 4902 | goto done_unlock; |
| 4903 | } |
| 4904 | |
| 4905 | case HOT_REMOVE_DISK: |
| 4906 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4907 | goto done_unlock; |
| 4908 | |
| 4909 | case HOT_ADD_DISK: |
| 4910 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4911 | goto done_unlock; |
| 4912 | |
| 4913 | case SET_DISK_FAULTY: |
| 4914 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4915 | goto done_unlock; |
| 4916 | |
| 4917 | case RUN_ARRAY: |
| 4918 | err = do_md_run (mddev); |
| 4919 | goto done_unlock; |
| 4920 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4921 | case SET_BITMAP_FILE: |
| 4922 | err = set_bitmap_file(mddev, (int)arg); |
| 4923 | goto done_unlock; |
| 4924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | err = -EINVAL; |
| 4927 | goto abort_unlock; |
| 4928 | } |
| 4929 | |
| 4930 | done_unlock: |
| 4931 | abort_unlock: |
| 4932 | mddev_unlock(mddev); |
| 4933 | |
| 4934 | return err; |
| 4935 | done: |
| 4936 | if (err) |
| 4937 | MD_BUG(); |
| 4938 | abort: |
| 4939 | return err; |
| 4940 | } |
| 4941 | |
| 4942 | static int md_open(struct inode *inode, struct file *file) |
| 4943 | { |
| 4944 | /* |
| 4945 | * Succeed if we can lock the mddev, which confirms that |
| 4946 | * it isn't being stopped right now. |
| 4947 | */ |
| 4948 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4949 | int err; |
| 4950 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 4951 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | goto out; |
| 4953 | |
| 4954 | err = 0; |
| 4955 | mddev_get(mddev); |
| 4956 | mddev_unlock(mddev); |
| 4957 | |
| 4958 | check_disk_change(inode->i_bdev); |
| 4959 | out: |
| 4960 | return err; |
| 4961 | } |
| 4962 | |
| 4963 | static int md_release(struct inode *inode, struct file * file) |
| 4964 | { |
| 4965 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4966 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 4967 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4968 | mddev_put(mddev); |
| 4969 | |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4973 | static int md_media_changed(struct gendisk *disk) |
| 4974 | { |
| 4975 | mddev_t *mddev = disk->private_data; |
| 4976 | |
| 4977 | return mddev->changed; |
| 4978 | } |
| 4979 | |
| 4980 | static int md_revalidate(struct gendisk *disk) |
| 4981 | { |
| 4982 | mddev_t *mddev = disk->private_data; |
| 4983 | |
| 4984 | mddev->changed = 0; |
| 4985 | return 0; |
| 4986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | static struct block_device_operations md_fops = |
| 4988 | { |
| 4989 | .owner = THIS_MODULE, |
| 4990 | .open = md_open, |
| 4991 | .release = md_release, |
| 4992 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4993 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4994 | .media_changed = md_media_changed, |
| 4995 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | }; |
| 4997 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4998 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4999 | { |
| 5000 | mdk_thread_t *thread = arg; |
| 5001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | /* |
| 5003 | * md_thread is a 'system-thread', it's priority should be very |
| 5004 | * high. We avoid resource deadlocks individually in each |
| 5005 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5006 | * the very same RT priority as kswapd, thus we will never get |
| 5007 | * into a priority inversion deadlock. |
| 5008 | * |
| 5009 | * we definitely have to have equal or higher priority than |
| 5010 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5011 | * many dirty RAID5 blocks. |
| 5012 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5014 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5015 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5017 | /* We need to wait INTERRUPTIBLE so that |
| 5018 | * we don't add to the load-average. |
| 5019 | * That means we need to be sure no signals are |
| 5020 | * pending |
| 5021 | */ |
| 5022 | if (signal_pending(current)) |
| 5023 | flush_signals(current); |
| 5024 | |
| 5025 | wait_event_interruptible_timeout |
| 5026 | (thread->wqueue, |
| 5027 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5028 | || kthread_should_stop(), |
| 5029 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
| 5031 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5032 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5033 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | return 0; |
| 5037 | } |
| 5038 | |
| 5039 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5040 | { |
| 5041 | if (thread) { |
| 5042 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5043 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5044 | wake_up(&thread->wqueue); |
| 5045 | } |
| 5046 | } |
| 5047 | |
| 5048 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5049 | const char *name) |
| 5050 | { |
| 5051 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5052 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5053 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | if (!thread) |
| 5055 | return NULL; |
| 5056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | init_waitqueue_head(&thread->wqueue); |
| 5058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | thread->run = run; |
| 5060 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5061 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5062 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5063 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | kfree(thread); |
| 5065 | return NULL; |
| 5066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | return thread; |
| 5068 | } |
| 5069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | void md_unregister_thread(mdk_thread_t *thread) |
| 5071 | { |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5072 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5073 | |
| 5074 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5075 | kfree(thread); |
| 5076 | } |
| 5077 | |
| 5078 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5079 | { |
| 5080 | if (!mddev) { |
| 5081 | MD_BUG(); |
| 5082 | return; |
| 5083 | } |
| 5084 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5085 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5087 | |
| 5088 | if (mddev->external) |
| 5089 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5090 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5092 | mdname(mddev), |
| 5093 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5094 | __builtin_return_address(0),__builtin_return_address(1), |
| 5095 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5096 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5097 | if (!mddev->pers) |
| 5098 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | if (!mddev->pers->error_handler) |
| 5100 | return; |
| 5101 | mddev->pers->error_handler(mddev,rdev); |
| 5102 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5103 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5104 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5105 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | /* seq_file implementation /proc/mdstat */ |
| 5109 | |
| 5110 | static void status_unused(struct seq_file *seq) |
| 5111 | { |
| 5112 | int i = 0; |
| 5113 | mdk_rdev_t *rdev; |
| 5114 | struct list_head *tmp; |
| 5115 | |
| 5116 | seq_printf(seq, "unused devices: "); |
| 5117 | |
NeilBrown | 73c3443 | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5118 | rdev_for_each_list(rdev, tmp, pending_raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | char b[BDEVNAME_SIZE]; |
| 5120 | i++; |
| 5121 | seq_printf(seq, "%s ", |
| 5122 | bdevname(rdev->bdev,b)); |
| 5123 | } |
| 5124 | if (!i) |
| 5125 | seq_printf(seq, "<none>"); |
| 5126 | |
| 5127 | seq_printf(seq, "\n"); |
| 5128 | } |
| 5129 | |
| 5130 | |
| 5131 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5132 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5133 | sector_t max_blocks, resync, res; |
| 5134 | unsigned long dt, db, rt; |
| 5135 | int scale; |
| 5136 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | |
| 5138 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 5139 | |
| 5140 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5141 | max_blocks = mddev->resync_max_sectors >> 1; |
| 5142 | else |
| 5143 | max_blocks = mddev->size; |
| 5144 | |
| 5145 | /* |
| 5146 | * Should not happen. |
| 5147 | */ |
| 5148 | if (!max_blocks) { |
| 5149 | MD_BUG(); |
| 5150 | return; |
| 5151 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5152 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 5153 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 5154 | * u32, as those are the requirements for sector_div. |
| 5155 | * Thus 'scale' must be at least 10 |
| 5156 | */ |
| 5157 | scale = 10; |
| 5158 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 5159 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 5160 | scale++; |
| 5161 | } |
| 5162 | res = (resync>>scale)*1000; |
| 5163 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 5164 | |
| 5165 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5167 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | seq_printf(seq, "["); |
| 5169 | for (i = 0; i < x; i++) |
| 5170 | seq_printf(seq, "="); |
| 5171 | seq_printf(seq, ">"); |
| 5172 | for (i = 0; i < y; i++) |
| 5173 | seq_printf(seq, "."); |
| 5174 | seq_printf(seq, "] "); |
| 5175 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5176 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5177 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5178 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5179 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5180 | "check" : |
| 5181 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5182 | "resync" : "recovery"))), |
| 5183 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5184 | (unsigned long long) resync, |
| 5185 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | |
| 5187 | /* |
| 5188 | * We do not want to overflow, so the order of operands and |
| 5189 | * the * 100 / 100 trick are important. We do a +1 to be |
| 5190 | * safe against division by zero. We only estimate anyway. |
| 5191 | * |
| 5192 | * dt: time from mark until now |
| 5193 | * db: blocks written from mark until now |
| 5194 | * rt: remaining time |
| 5195 | */ |
| 5196 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5197 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5198 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5199 | - mddev->resync_mark_cnt; |
| 5200 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | |
| 5202 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 5203 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5204 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5208 | { |
| 5209 | struct list_head *tmp; |
| 5210 | loff_t l = *pos; |
| 5211 | mddev_t *mddev; |
| 5212 | |
| 5213 | if (l >= 0x10000) |
| 5214 | return NULL; |
| 5215 | if (!l--) |
| 5216 | /* header */ |
| 5217 | return (void*)1; |
| 5218 | |
| 5219 | spin_lock(&all_mddevs_lock); |
| 5220 | list_for_each(tmp,&all_mddevs) |
| 5221 | if (!l--) { |
| 5222 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5223 | mddev_get(mddev); |
| 5224 | spin_unlock(&all_mddevs_lock); |
| 5225 | return mddev; |
| 5226 | } |
| 5227 | spin_unlock(&all_mddevs_lock); |
| 5228 | if (!l--) |
| 5229 | return (void*)2;/* tail */ |
| 5230 | return NULL; |
| 5231 | } |
| 5232 | |
| 5233 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5234 | { |
| 5235 | struct list_head *tmp; |
| 5236 | mddev_t *next_mddev, *mddev = v; |
| 5237 | |
| 5238 | ++*pos; |
| 5239 | if (v == (void*)2) |
| 5240 | return NULL; |
| 5241 | |
| 5242 | spin_lock(&all_mddevs_lock); |
| 5243 | if (v == (void*)1) |
| 5244 | tmp = all_mddevs.next; |
| 5245 | else |
| 5246 | tmp = mddev->all_mddevs.next; |
| 5247 | if (tmp != &all_mddevs) |
| 5248 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5249 | else { |
| 5250 | next_mddev = (void*)2; |
| 5251 | *pos = 0x10000; |
| 5252 | } |
| 5253 | spin_unlock(&all_mddevs_lock); |
| 5254 | |
| 5255 | if (v != (void*)1) |
| 5256 | mddev_put(mddev); |
| 5257 | return next_mddev; |
| 5258 | |
| 5259 | } |
| 5260 | |
| 5261 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5262 | { |
| 5263 | mddev_t *mddev = v; |
| 5264 | |
| 5265 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5266 | mddev_put(mddev); |
| 5267 | } |
| 5268 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5269 | struct mdstat_info { |
| 5270 | int event; |
| 5271 | }; |
| 5272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5274 | { |
| 5275 | mddev_t *mddev = v; |
| 5276 | sector_t size; |
| 5277 | struct list_head *tmp2; |
| 5278 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5279 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5280 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | |
| 5282 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5283 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | seq_printf(seq, "Personalities : "); |
| 5285 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5286 | list_for_each_entry(pers, &pers_list, list) |
| 5287 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | |
| 5289 | spin_unlock(&pers_lock); |
| 5290 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5291 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | return 0; |
| 5293 | } |
| 5294 | if (v == (void*)2) { |
| 5295 | status_unused(seq); |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5299 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5303 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5304 | mddev->pers ? "" : "in"); |
| 5305 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5306 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5308 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5309 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5310 | seq_printf(seq, " %s", mddev->pers->name); |
| 5311 | } |
| 5312 | |
| 5313 | size = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5314 | rdev_for_each(rdev, tmp2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | char b[BDEVNAME_SIZE]; |
| 5316 | seq_printf(seq, " %s[%d]", |
| 5317 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5318 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5319 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5320 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | seq_printf(seq, "(F)"); |
| 5322 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5323 | } else if (rdev->raid_disk < 0) |
| 5324 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | size += rdev->size; |
| 5326 | } |
| 5327 | |
| 5328 | if (!list_empty(&mddev->disks)) { |
| 5329 | if (mddev->pers) |
| 5330 | seq_printf(seq, "\n %llu blocks", |
| 5331 | (unsigned long long)mddev->array_size); |
| 5332 | else |
| 5333 | seq_printf(seq, "\n %llu blocks", |
| 5334 | (unsigned long long)size); |
| 5335 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5336 | if (mddev->persistent) { |
| 5337 | if (mddev->major_version != 0 || |
| 5338 | mddev->minor_version != 90) { |
| 5339 | seq_printf(seq," super %d.%d", |
| 5340 | mddev->major_version, |
| 5341 | mddev->minor_version); |
| 5342 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5343 | } else if (mddev->external) |
| 5344 | seq_printf(seq, " super external:%s", |
| 5345 | mddev->metadata_type); |
| 5346 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5347 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | |
| 5349 | if (mddev->pers) { |
| 5350 | mddev->pers->status (seq, mddev); |
| 5351 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5352 | if (mddev->pers->sync_request) { |
| 5353 | if (mddev->curr_resync > 2) { |
| 5354 | status_resync (seq, mddev); |
| 5355 | seq_printf(seq, "\n "); |
| 5356 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5357 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5358 | else if (mddev->recovery_cp < MaxSector) |
| 5359 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5360 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5361 | } else |
| 5362 | seq_printf(seq, "\n "); |
| 5363 | |
| 5364 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5365 | unsigned long chunk_kb; |
| 5366 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5367 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5368 | chunk_kb = bitmap->chunksize >> 10; |
| 5369 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5370 | "%lu%s chunk", |
| 5371 | bitmap->pages - bitmap->missing_pages, |
| 5372 | bitmap->pages, |
| 5373 | (bitmap->pages - bitmap->missing_pages) |
| 5374 | << (PAGE_SHIFT - 10), |
| 5375 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5376 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5377 | if (bitmap->file) { |
| 5378 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5379 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5380 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5381 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5382 | seq_printf(seq, "\n"); |
| 5383 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | seq_printf(seq, "\n"); |
| 5387 | } |
| 5388 | mddev_unlock(mddev); |
| 5389 | |
| 5390 | return 0; |
| 5391 | } |
| 5392 | |
| 5393 | static struct seq_operations md_seq_ops = { |
| 5394 | .start = md_seq_start, |
| 5395 | .next = md_seq_next, |
| 5396 | .stop = md_seq_stop, |
| 5397 | .show = md_seq_show, |
| 5398 | }; |
| 5399 | |
| 5400 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5401 | { |
| 5402 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5403 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5404 | if (mi == NULL) |
| 5405 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | |
| 5407 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5408 | if (error) |
| 5409 | kfree(mi); |
| 5410 | else { |
| 5411 | struct seq_file *p = file->private_data; |
| 5412 | p->private = mi; |
| 5413 | mi->event = atomic_read(&md_event_count); |
| 5414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | return error; |
| 5416 | } |
| 5417 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5418 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5419 | { |
| 5420 | struct seq_file *m = filp->private_data; |
| 5421 | struct mdstat_info *mi = m->private; |
| 5422 | int mask; |
| 5423 | |
| 5424 | poll_wait(filp, &md_event_waiters, wait); |
| 5425 | |
| 5426 | /* always allow read */ |
| 5427 | mask = POLLIN | POLLRDNORM; |
| 5428 | |
| 5429 | if (mi->event != atomic_read(&md_event_count)) |
| 5430 | mask |= POLLERR | POLLPRI; |
| 5431 | return mask; |
| 5432 | } |
| 5433 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5434 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5435 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | .open = md_seq_open, |
| 5437 | .read = seq_read, |
| 5438 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5439 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5440 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | }; |
| 5442 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5443 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5446 | list_add_tail(&p->list, &pers_list); |
| 5447 | printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | spin_unlock(&pers_lock); |
| 5449 | return 0; |
| 5450 | } |
| 5451 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5452 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5454 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5456 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | spin_unlock(&pers_lock); |
| 5458 | return 0; |
| 5459 | } |
| 5460 | |
| 5461 | static int is_mddev_idle(mddev_t *mddev) |
| 5462 | { |
| 5463 | mdk_rdev_t * rdev; |
| 5464 | struct list_head *tmp; |
| 5465 | int idle; |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5466 | long curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | |
| 5468 | idle = 1; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5469 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5471 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5472 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5474 | /* sync IO will cause sync_io to increase before the disk_stats |
| 5475 | * as sync_io is counted when a request starts, and |
| 5476 | * disk_stats is counted when it completes. |
| 5477 | * So resync activity will cause curr_events to be smaller than |
| 5478 | * when there was no such activity. |
| 5479 | * non-sync IO will cause disk_stat to increase without |
| 5480 | * increasing sync_io so curr_events will (eventually) |
| 5481 | * be larger than it was before. Once it becomes |
| 5482 | * substantially larger, the test below will cause |
| 5483 | * the array to appear non-idle, and resync will slow |
| 5484 | * down. |
| 5485 | * If there is a lot of outstanding resync activity when |
| 5486 | * we set last_event to curr_events, then all that activity |
| 5487 | * completing might cause the array to appear non-idle |
| 5488 | * and resync will be slowed down even though there might |
| 5489 | * not have been non-resync activity. This will only |
| 5490 | * happen once though. 'last_events' will soon reflect |
| 5491 | * the state where there is little or no outstanding |
| 5492 | * resync requests, and further resync activity will |
| 5493 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5494 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | */ |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5496 | if (curr_events - rdev->last_events > 4096) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | rdev->last_events = curr_events; |
| 5498 | idle = 0; |
| 5499 | } |
| 5500 | } |
| 5501 | return idle; |
| 5502 | } |
| 5503 | |
| 5504 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5505 | { |
| 5506 | /* another "blocks" (512byte) blocks have been synced */ |
| 5507 | atomic_sub(blocks, &mddev->recovery_active); |
| 5508 | wake_up(&mddev->recovery_wait); |
| 5509 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5510 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5511 | md_wakeup_thread(mddev->thread); |
| 5512 | // stop recovery, signal do_sync .... |
| 5513 | } |
| 5514 | } |
| 5515 | |
| 5516 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5517 | /* md_write_start(mddev, bi) |
| 5518 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5519 | * in superblock) before writing, schedule a superblock update |
| 5520 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5521 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5522 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5524 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5525 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5526 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5527 | BUG_ON(mddev->ro == 1); |
| 5528 | if (mddev->ro == 2) { |
| 5529 | /* need to switch to read/write */ |
| 5530 | mddev->ro = 0; |
| 5531 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5532 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 5533 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5534 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5535 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5536 | if (mddev->safemode == 1) |
| 5537 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5538 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5539 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5540 | if (mddev->in_sync) { |
| 5541 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5542 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5543 | md_wakeup_thread(mddev->thread); |
| 5544 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5545 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5546 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5547 | } |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5548 | wait_event(mddev->sb_wait, |
| 5549 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5550 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | void md_write_end(mddev_t *mddev) |
| 5554 | { |
| 5555 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5556 | if (mddev->safemode == 2) |
| 5557 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5558 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5560 | } |
| 5561 | } |
| 5562 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5563 | /* md_allow_write(mddev) |
| 5564 | * Calling this ensures that the array is marked 'active' so that writes |
| 5565 | * may proceed without blocking. It is important to call this before |
| 5566 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5567 | * Must be called with mddev_lock held. |
| 5568 | */ |
| 5569 | void md_allow_write(mddev_t *mddev) |
| 5570 | { |
| 5571 | if (!mddev->pers) |
| 5572 | return; |
| 5573 | if (mddev->ro) |
| 5574 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 5575 | if (!mddev->pers->sync_request) |
| 5576 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5577 | |
| 5578 | spin_lock_irq(&mddev->write_lock); |
| 5579 | if (mddev->in_sync) { |
| 5580 | mddev->in_sync = 0; |
| 5581 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5582 | if (mddev->safemode_delay && |
| 5583 | mddev->safemode == 0) |
| 5584 | mddev->safemode = 1; |
| 5585 | spin_unlock_irq(&mddev->write_lock); |
| 5586 | md_update_sb(mddev, 0); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 5587 | |
| 5588 | sysfs_notify(&mddev->kobj, NULL, "array_state"); |
| 5589 | /* wait for the dirty state to be recorded in the metadata */ |
| 5590 | wait_event(mddev->sb_wait, |
| 5591 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 5592 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5593 | } else |
| 5594 | spin_unlock_irq(&mddev->write_lock); |
| 5595 | } |
| 5596 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5598 | #define SYNC_MARKS 10 |
| 5599 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5600 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | { |
| 5602 | mddev_t *mddev2; |
| 5603 | unsigned int currspeed = 0, |
| 5604 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5605 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | unsigned long mark[SYNC_MARKS]; |
| 5607 | sector_t mark_cnt[SYNC_MARKS]; |
| 5608 | int last_mark,m; |
| 5609 | struct list_head *tmp; |
| 5610 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5611 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5612 | struct list_head *rtmp; |
| 5613 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5614 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5615 | |
| 5616 | /* just incase thread restarts... */ |
| 5617 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5618 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5619 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5620 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5622 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5623 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5624 | desc = "data-check"; |
| 5625 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5626 | desc = "requested-resync"; |
| 5627 | else |
| 5628 | desc = "resync"; |
| 5629 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5630 | desc = "reshape"; |
| 5631 | else |
| 5632 | desc = "recovery"; |
| 5633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | /* we overload curr_resync somewhat here. |
| 5635 | * 0 == not engaged in resync at all |
| 5636 | * 2 == checking that there is no conflict with another sync |
| 5637 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5638 | * commense |
| 5639 | * other == active in resync - this many blocks |
| 5640 | * |
| 5641 | * Before starting a resync we must have set curr_resync to |
| 5642 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5643 | * less than ours. When we find one that is the same or higher |
| 5644 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5645 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5646 | * This will mean we have to start checking from the beginning again. |
| 5647 | * |
| 5648 | */ |
| 5649 | |
| 5650 | do { |
| 5651 | mddev->curr_resync = 2; |
| 5652 | |
| 5653 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5654 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5655 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | goto skip; |
| 5657 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 5658 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | if (mddev2 == mddev) |
| 5660 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5661 | if (!mddev->parallel_resync |
| 5662 | && mddev2->curr_resync |
| 5663 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | DEFINE_WAIT(wq); |
| 5665 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5666 | /* arbitrarily yield */ |
| 5667 | mddev->curr_resync = 1; |
| 5668 | wake_up(&resync_wait); |
| 5669 | } |
| 5670 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5671 | /* no need to wait here, we can wait the next |
| 5672 | * time 'round when curr_resync == 2 |
| 5673 | */ |
| 5674 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5675 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5676 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5677 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5678 | printk(KERN_INFO "md: delaying %s of %s" |
| 5679 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5681 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | mddev_put(mddev2); |
| 5683 | schedule(); |
| 5684 | finish_wait(&resync_wait, &wq); |
| 5685 | goto try_again; |
| 5686 | } |
| 5687 | finish_wait(&resync_wait, &wq); |
| 5688 | } |
| 5689 | } |
| 5690 | } while (mddev->curr_resync < 2); |
| 5691 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5692 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5693 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5695 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | */ |
| 5697 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5698 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5699 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5700 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5701 | j = mddev->resync_min; |
| 5702 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5703 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5704 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5705 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5706 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5707 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | /* recovery follows the physical size of devices */ |
| 5709 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5710 | j = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5711 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5712 | if (rdev->raid_disk >= 0 && |
| 5713 | !test_bit(Faulty, &rdev->flags) && |
| 5714 | !test_bit(In_sync, &rdev->flags) && |
| 5715 | rdev->recovery_offset < j) |
| 5716 | j = rdev->recovery_offset; |
| 5717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5719 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5720 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5721 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5722 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5723 | "(but not more than %d KB/sec) for %s.\n", |
| 5724 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | |
| 5726 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5727 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5728 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5730 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5731 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | } |
| 5733 | last_mark = 0; |
| 5734 | mddev->resync_mark = mark[last_mark]; |
| 5735 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5736 | |
| 5737 | /* |
| 5738 | * Tune reconstruction: |
| 5739 | */ |
| 5740 | window = 32*(PAGE_SIZE/512); |
| 5741 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5742 | window/2,(unsigned long long) max_sectors/2); |
| 5743 | |
| 5744 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | last_check = 0; |
| 5746 | |
| 5747 | if (j>2) { |
| 5748 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5749 | "md: resuming %s of %s from checkpoint.\n", |
| 5750 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | mddev->curr_resync = j; |
| 5752 | } |
| 5753 | |
| 5754 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5755 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5757 | skipped = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5758 | if (j >= mddev->resync_max) { |
| 5759 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 5760 | wait_event(mddev->recovery_wait, |
| 5761 | mddev->resync_max > j |
| 5762 | || kthread_should_stop()); |
| 5763 | } |
| 5764 | if (kthread_should_stop()) |
| 5765 | goto interrupted; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5766 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5767 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5768 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5769 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | goto out; |
| 5771 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5772 | |
| 5773 | if (!skipped) { /* actual IO requested */ |
| 5774 | io_sectors += sectors; |
| 5775 | atomic_add(sectors, &mddev->recovery_active); |
| 5776 | } |
| 5777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | j += sectors; |
| 5779 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5780 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5781 | if (last_check == 0) |
| 5782 | /* this is the earliers that rebuilt will be |
| 5783 | * visible in /proc/mdstat |
| 5784 | */ |
| 5785 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5786 | |
| 5787 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | continue; |
| 5789 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5790 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5792 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | break; |
| 5794 | |
| 5795 | repeat: |
| 5796 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5797 | /* step marks */ |
| 5798 | int next = (last_mark+1) % SYNC_MARKS; |
| 5799 | |
| 5800 | mddev->resync_mark = mark[next]; |
| 5801 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5802 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5803 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | last_mark = next; |
| 5805 | } |
| 5806 | |
| 5807 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5808 | if (kthread_should_stop()) |
| 5809 | goto interrupted; |
| 5810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | |
| 5812 | /* |
| 5813 | * this loop exits only if either when we are slower than |
| 5814 | * the 'hard' speed limit, or the system was IO-idle for |
| 5815 | * a jiffy. |
| 5816 | * the system might be non-idle CPU-wise, but we only care |
| 5817 | * about not overloading the IO subsystem. (things like an |
| 5818 | * e2fsck being done on the RAID array should execute fast) |
| 5819 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5820 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | cond_resched(); |
| 5822 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5823 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5824 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5826 | if (currspeed > speed_min(mddev)) { |
| 5827 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5829 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | goto repeat; |
| 5831 | } |
| 5832 | } |
| 5833 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5834 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | /* |
| 5836 | * this also signals 'finished resyncing' to md_stop |
| 5837 | */ |
| 5838 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 5839 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5840 | |
| 5841 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5842 | |
| 5843 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5844 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5846 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5847 | mddev->curr_resync > 2) { |
| 5848 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5849 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5850 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5851 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5852 | "md: checkpointing %s of %s.\n", |
| 5853 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5854 | mddev->recovery_cp = mddev->curr_resync; |
| 5855 | } |
| 5856 | } else |
| 5857 | mddev->recovery_cp = MaxSector; |
| 5858 | } else { |
| 5859 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5860 | mddev->curr_resync = MaxSector; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5861 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5862 | if (rdev->raid_disk >= 0 && |
| 5863 | !test_bit(Faulty, &rdev->flags) && |
| 5864 | !test_bit(In_sync, &rdev->flags) && |
| 5865 | rdev->recovery_offset < mddev->curr_resync) |
| 5866 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5868 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5869 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | skip: |
| 5872 | mddev->curr_resync = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5873 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5874 | mddev->resync_max = MaxSector; |
| 5875 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | wake_up(&resync_wait); |
| 5877 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5878 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5879 | return; |
| 5880 | |
| 5881 | interrupted: |
| 5882 | /* |
| 5883 | * got a signal, exit. |
| 5884 | */ |
| 5885 | printk(KERN_INFO |
| 5886 | "md: md_do_sync() got signal ... exiting\n"); |
| 5887 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5888 | goto out; |
| 5889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5891 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | |
| 5893 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5894 | static int remove_and_add_spares(mddev_t *mddev) |
| 5895 | { |
| 5896 | mdk_rdev_t *rdev; |
| 5897 | struct list_head *rtmp; |
| 5898 | int spares = 0; |
| 5899 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 5900 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5901 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5902 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5903 | (test_bit(Faulty, &rdev->flags) || |
| 5904 | ! test_bit(In_sync, &rdev->flags)) && |
| 5905 | atomic_read(&rdev->nr_pending)==0) { |
| 5906 | if (mddev->pers->hot_remove_disk( |
| 5907 | mddev, rdev->raid_disk)==0) { |
| 5908 | char nm[20]; |
| 5909 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5910 | sysfs_remove_link(&mddev->kobj, nm); |
| 5911 | rdev->raid_disk = -1; |
| 5912 | } |
| 5913 | } |
| 5914 | |
| 5915 | if (mddev->degraded) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5916 | rdev_for_each(rdev, rtmp, mddev) { |
| 5917 | if (rdev->raid_disk >= 0 && |
| 5918 | !test_bit(In_sync, &rdev->flags)) |
| 5919 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5920 | if (rdev->raid_disk < 0 |
| 5921 | && !test_bit(Faulty, &rdev->flags)) { |
| 5922 | rdev->recovery_offset = 0; |
| 5923 | if (mddev->pers->hot_add_disk(mddev,rdev)) { |
| 5924 | char nm[20]; |
| 5925 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5926 | if (sysfs_create_link(&mddev->kobj, |
| 5927 | &rdev->kobj, nm)) |
| 5928 | printk(KERN_WARNING |
| 5929 | "md: cannot register " |
| 5930 | "%s for %s\n", |
| 5931 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5932 | spares++; |
| 5933 | md_new_event(mddev); |
| 5934 | } else |
| 5935 | break; |
| 5936 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5937 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5938 | } |
| 5939 | return spares; |
| 5940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | /* |
| 5942 | * This routine is regularly called by all per-raid-array threads to |
| 5943 | * deal with generic issues like resync and super-block update. |
| 5944 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 5945 | * need this as they never do any recovery or update the superblock. |
| 5946 | * |
| 5947 | * It does not do any resync itself, but rather "forks" off other threads |
| 5948 | * to do that as needed. |
| 5949 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 5950 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 5951 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | * and wakeups up this thread which will reap the thread and finish up. |
| 5953 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 5954 | * |
| 5955 | * The overall approach is: |
| 5956 | * 1/ if the superblock needs updating, update it. |
| 5957 | * 2/ If a recovery thread is running, don't do anything else. |
| 5958 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 5959 | * 4/ If there are any faulty devices, remove them. |
| 5960 | * 5/ If array is degraded, try to add spares devices |
| 5961 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 5962 | */ |
| 5963 | void md_check_recovery(mddev_t *mddev) |
| 5964 | { |
| 5965 | mdk_rdev_t *rdev; |
| 5966 | struct list_head *rtmp; |
| 5967 | |
| 5968 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 5969 | if (mddev->bitmap) |
| 5970 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | |
| 5972 | if (mddev->ro) |
| 5973 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5974 | |
| 5975 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5976 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5977 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 5978 | mdname(mddev)); |
| 5979 | mddev->safemode = 2; |
| 5980 | } |
| 5981 | flush_signals(current); |
| 5982 | } |
| 5983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5984 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5985 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5987 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5988 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5989 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 5990 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | )) |
| 5992 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5993 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5994 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5995 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5996 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 5997 | if (!mddev->external) { |
| 5998 | spin_lock_irq(&mddev->write_lock); |
| 5999 | if (mddev->safemode && |
| 6000 | !atomic_read(&mddev->writes_pending) && |
| 6001 | !mddev->in_sync && |
| 6002 | mddev->recovery_cp == MaxSector) { |
| 6003 | mddev->in_sync = 1; |
| 6004 | if (mddev->persistent) |
| 6005 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6006 | } |
| 6007 | if (mddev->safemode == 1) |
| 6008 | mddev->safemode = 0; |
| 6009 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6010 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6011 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6012 | if (mddev->flags) |
| 6013 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6014 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6017 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6018 | /* resync/recovery still happening */ |
| 6019 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6020 | goto unlock; |
| 6021 | } |
| 6022 | if (mddev->sync_thread) { |
| 6023 | /* resync has finished, collect result */ |
| 6024 | md_unregister_thread(mddev->sync_thread); |
| 6025 | mddev->sync_thread = NULL; |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6026 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6027 | /* success...*/ |
| 6028 | /* activate any spares */ |
| 6029 | mddev->pers->spare_active(mddev); |
| 6030 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6031 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6032 | |
| 6033 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6034 | * information must be scrapped |
| 6035 | */ |
| 6036 | if (!mddev->degraded) |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 6037 | rdev_for_each(rdev, rtmp, mddev) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6038 | rdev->saved_raid_disk = -1; |
| 6039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | mddev->recovery = 0; |
| 6041 | /* flag recovery needed just to double check */ |
| 6042 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6043 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | goto unlock; |
| 6045 | } |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6046 | /* Clear some bits that don't mean anything, but |
| 6047 | * might be left set |
| 6048 | */ |
| 6049 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6050 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6051 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6053 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6054 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | /* no recovery is running. |
| 6056 | * remove any failed drives, then |
| 6057 | * add spares if possible. |
| 6058 | * Spare are also removed and re-added, to allow |
| 6059 | * the personality to fail the re-add. |
| 6060 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6062 | if (mddev->reshape_position != MaxSector) { |
| 6063 | if (mddev->pers->check_reshape(mddev) != 0) |
| 6064 | /* Cannot proceed */ |
| 6065 | goto unlock; |
| 6066 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 6067 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6068 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6069 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 6070 | } else if (mddev->recovery_cp < MaxSector) { |
| 6071 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6072 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6073 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | if (mddev->pers->sync_request) { |
| 6077 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6078 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6079 | /* We are adding a device or devices to an array |
| 6080 | * which has the bitmap stored on all devices. |
| 6081 | * So make sure all bitmap pages get written |
| 6082 | */ |
| 6083 | bitmap_write_all(mddev->bitmap); |
| 6084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6086 | mddev, |
| 6087 | "%s_resync"); |
| 6088 | if (!mddev->sync_thread) { |
| 6089 | printk(KERN_ERR "%s: could not start resync" |
| 6090 | " thread...\n", |
| 6091 | mdname(mddev)); |
| 6092 | /* leave the spares where they are, it shouldn't hurt */ |
| 6093 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6094 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6096 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | } |
| 6098 | unlock: |
| 6099 | mddev_unlock(mddev); |
| 6100 | } |
| 6101 | } |
| 6102 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6103 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6104 | { |
| 6105 | sysfs_notify(&rdev->kobj, NULL, "state"); |
| 6106 | wait_event_timeout(rdev->blocked_wait, |
| 6107 | !test_bit(Blocked, &rdev->flags), |
| 6108 | msecs_to_jiffies(5000)); |
| 6109 | rdev_dec_pending(rdev, mddev); |
| 6110 | } |
| 6111 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6112 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6113 | static int md_notify_reboot(struct notifier_block *this, |
| 6114 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | { |
| 6116 | struct list_head *tmp; |
| 6117 | mddev_t *mddev; |
| 6118 | |
| 6119 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6120 | |
| 6121 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6122 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6123 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6124 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | do_md_stop (mddev, 1); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6126 | mddev_unlock(mddev); |
| 6127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | /* |
| 6129 | * certain more exotic SCSI devices are known to be |
| 6130 | * volatile wrt too early system reboots. While the |
| 6131 | * right place to handle this issue is the given |
| 6132 | * driver, we do want to have a safe RAID driver ... |
| 6133 | */ |
| 6134 | mdelay(1000*1); |
| 6135 | } |
| 6136 | return NOTIFY_DONE; |
| 6137 | } |
| 6138 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6139 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | .notifier_call = md_notify_reboot, |
| 6141 | .next = NULL, |
| 6142 | .priority = INT_MAX, /* before any real devices */ |
| 6143 | }; |
| 6144 | |
| 6145 | static void md_geninit(void) |
| 6146 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6148 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6149 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | } |
| 6151 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6152 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6153 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | if (register_blkdev(MAJOR_NR, "md")) |
| 6155 | return -1; |
| 6156 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 6157 | unregister_blkdev(MAJOR_NR, "md"); |
| 6158 | return -1; |
| 6159 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6160 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 6161 | md_probe, NULL, NULL); |
| 6162 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | md_probe, NULL, NULL); |
| 6164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6166 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | |
| 6168 | md_geninit(); |
| 6169 | return (0); |
| 6170 | } |
| 6171 | |
| 6172 | |
| 6173 | #ifndef MODULE |
| 6174 | |
| 6175 | /* |
| 6176 | * Searches all registered partitions for autorun RAID arrays |
| 6177 | * at boot time. |
| 6178 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6179 | |
| 6180 | static LIST_HEAD(all_detected_devices); |
| 6181 | struct detected_devices_node { |
| 6182 | struct list_head list; |
| 6183 | dev_t dev; |
| 6184 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | |
| 6186 | void md_autodetect_dev(dev_t dev) |
| 6187 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6188 | struct detected_devices_node *node_detected_dev; |
| 6189 | |
| 6190 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6191 | if (node_detected_dev) { |
| 6192 | node_detected_dev->dev = dev; |
| 6193 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6194 | } else { |
| 6195 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6196 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | |
| 6201 | static void autostart_arrays(int part) |
| 6202 | { |
| 6203 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6204 | struct detected_devices_node *node_detected_dev; |
| 6205 | dev_t dev; |
| 6206 | int i_scanned, i_passed; |
| 6207 | |
| 6208 | i_scanned = 0; |
| 6209 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | |
| 6211 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6212 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6213 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6214 | i_scanned++; |
| 6215 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6216 | struct detected_devices_node, list); |
| 6217 | list_del(&node_detected_dev->list); |
| 6218 | dev = node_detected_dev->dev; |
| 6219 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6220 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6221 | if (IS_ERR(rdev)) |
| 6222 | continue; |
| 6223 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6224 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | MD_BUG(); |
| 6226 | continue; |
| 6227 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6228 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6230 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6232 | |
| 6233 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6234 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | |
| 6236 | autorun_devices(part); |
| 6237 | } |
| 6238 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6239 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | |
| 6241 | static __exit void md_exit(void) |
| 6242 | { |
| 6243 | mddev_t *mddev; |
| 6244 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6245 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6246 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 6247 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | |
| 6249 | unregister_blkdev(MAJOR_NR,"md"); |
| 6250 | unregister_blkdev(mdp_major, "mdp"); |
| 6251 | unregister_reboot_notifier(&md_notifier); |
| 6252 | unregister_sysctl_table(raid_table_header); |
| 6253 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6254 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6255 | struct gendisk *disk = mddev->gendisk; |
| 6256 | if (!disk) |
| 6257 | continue; |
| 6258 | export_array(mddev); |
| 6259 | del_gendisk(disk); |
| 6260 | put_disk(disk); |
| 6261 | mddev->gendisk = NULL; |
| 6262 | mddev_put(mddev); |
| 6263 | } |
| 6264 | } |
| 6265 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6266 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | module_exit(md_exit) |
| 6268 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6269 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6270 | { |
| 6271 | return sprintf(buffer, "%d", start_readonly); |
| 6272 | } |
| 6273 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6274 | { |
| 6275 | char *e; |
| 6276 | int num = simple_strtoul(val, &e, 10); |
| 6277 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6278 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6279 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6280 | } |
| 6281 | return -EINVAL; |
| 6282 | } |
| 6283 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6284 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6285 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6286 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | EXPORT_SYMBOL(register_md_personality); |
| 6289 | EXPORT_SYMBOL(unregister_md_personality); |
| 6290 | EXPORT_SYMBOL(md_error); |
| 6291 | EXPORT_SYMBOL(md_done_sync); |
| 6292 | EXPORT_SYMBOL(md_write_start); |
| 6293 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | EXPORT_SYMBOL(md_register_thread); |
| 6295 | EXPORT_SYMBOL(md_unregister_thread); |
| 6296 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | EXPORT_SYMBOL(md_check_recovery); |
| 6298 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6299 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6300 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |