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> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 36 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/linkage.h> |
| 38 | #include <linux/raid/md.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 39 | #include <linux/raid/bitmap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/sysctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
| 42 | #include <linux/suspend.h> |
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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #include <linux/init.h> |
| 48 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 49 | #include <linux/file.h> |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_KMOD |
| 52 | #include <linux/kmod.h> |
| 53 | #endif |
| 54 | |
| 55 | #include <asm/unaligned.h> |
| 56 | |
| 57 | #define MAJOR_NR MD_MAJOR |
| 58 | #define MD_DRIVER |
| 59 | |
| 60 | /* 63 partitions with the alternate major number (mdp) */ |
| 61 | #define MdpMinorShift 6 |
| 62 | |
| 63 | #define DEBUG 0 |
| 64 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 65 | |
| 66 | |
| 67 | #ifndef MODULE |
| 68 | static void autostart_arrays (int part); |
| 69 | #endif |
| 70 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 71 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static DEFINE_SPINLOCK(pers_lock); |
| 73 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 74 | static void md_print_devices(void); |
| 75 | |
| 76 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 80 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 81 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 82 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 84 | * speed limit - in case reconstruction slows down your system despite |
| 85 | * idle IO detection. |
| 86 | * |
| 87 | * 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] | 88 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | */ |
| 90 | |
| 91 | static int sysctl_speed_limit_min = 1000; |
| 92 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 93 | static inline int speed_min(mddev_t *mddev) |
| 94 | { |
| 95 | return mddev->sync_speed_min ? |
| 96 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 97 | } |
| 98 | |
| 99 | static inline int speed_max(mddev_t *mddev) |
| 100 | { |
| 101 | return mddev->sync_speed_max ? |
| 102 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | static struct ctl_table_header *raid_table_header; |
| 106 | |
| 107 | static ctl_table raid_table[] = { |
| 108 | { |
| 109 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 110 | .procname = "speed_limit_min", |
| 111 | .data = &sysctl_speed_limit_min, |
| 112 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 113 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .proc_handler = &proc_dointvec, |
| 115 | }, |
| 116 | { |
| 117 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 118 | .procname = "speed_limit_max", |
| 119 | .data = &sysctl_speed_limit_max, |
| 120 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 121 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | .proc_handler = &proc_dointvec, |
| 123 | }, |
| 124 | { .ctl_name = 0 } |
| 125 | }; |
| 126 | |
| 127 | static ctl_table raid_dir_table[] = { |
| 128 | { |
| 129 | .ctl_name = DEV_RAID, |
| 130 | .procname = "raid", |
| 131 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 132 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .child = raid_table, |
| 134 | }, |
| 135 | { .ctl_name = 0 } |
| 136 | }; |
| 137 | |
| 138 | static ctl_table raid_root_table[] = { |
| 139 | { |
| 140 | .ctl_name = CTL_DEV, |
| 141 | .procname = "dev", |
| 142 | .maxlen = 0, |
| 143 | .mode = 0555, |
| 144 | .child = raid_dir_table, |
| 145 | }, |
| 146 | { .ctl_name = 0 } |
| 147 | }; |
| 148 | |
| 149 | static struct block_device_operations md_fops; |
| 150 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 151 | static int start_readonly; |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 154 | * We have a system wide 'event count' that is incremented |
| 155 | * on any 'interesting' event, and readers of /proc/mdstat |
| 156 | * can use 'poll' or 'select' to find out when the event |
| 157 | * count increases. |
| 158 | * |
| 159 | * Events are: |
| 160 | * start array, stop array, error, add device, remove device, |
| 161 | * start build, activate spare |
| 162 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 163 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 164 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 165 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 166 | { |
| 167 | atomic_inc(&md_event_count); |
| 168 | wake_up(&md_event_waiters); |
NeilBrown | 4508a7a | 2006-03-20 17:53:53 +1100 | [diff] [blame] | 169 | sysfs_notify(&mddev->kobj, NULL, "sync_action"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 170 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 171 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 172 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 173 | /* Alternate version that can be called from interrupts |
| 174 | * when calling sysfs_notify isn't needed. |
| 175 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 176 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 177 | { |
| 178 | atomic_inc(&md_event_count); |
| 179 | wake_up(&md_event_waiters); |
| 180 | } |
| 181 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 182 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * Enables to iterate over all existing md arrays |
| 184 | * all_mddevs_lock protects this list. |
| 185 | */ |
| 186 | static LIST_HEAD(all_mddevs); |
| 187 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 188 | |
| 189 | |
| 190 | /* |
| 191 | * iterates through all used mddevs in the system. |
| 192 | * We take care to grab the all_mddevs_lock whenever navigating |
| 193 | * the list, and to always hold a refcount when unlocked. |
| 194 | * Any code which breaks out of this loop while own |
| 195 | * a reference to the current mddev and must mddev_put it. |
| 196 | */ |
| 197 | #define ITERATE_MDDEV(mddev,tmp) \ |
| 198 | \ |
| 199 | for (({ spin_lock(&all_mddevs_lock); \ |
| 200 | tmp = all_mddevs.next; \ |
| 201 | mddev = NULL;}); \ |
| 202 | ({ if (tmp != &all_mddevs) \ |
| 203 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 204 | spin_unlock(&all_mddevs_lock); \ |
| 205 | if (mddev) mddev_put(mddev); \ |
| 206 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 207 | tmp != &all_mddevs;}); \ |
| 208 | ({ spin_lock(&all_mddevs_lock); \ |
| 209 | tmp = tmp->next;}) \ |
| 210 | ) |
| 211 | |
| 212 | |
| 213 | static int md_fail_request (request_queue_t *q, struct bio *bio) |
| 214 | { |
| 215 | bio_io_error(bio, bio->bi_size); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 220 | { |
| 221 | atomic_inc(&mddev->active); |
| 222 | return mddev; |
| 223 | } |
| 224 | |
| 225 | static void mddev_put(mddev_t *mddev) |
| 226 | { |
| 227 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 228 | return; |
| 229 | if (!mddev->raid_disks && list_empty(&mddev->disks)) { |
| 230 | list_del(&mddev->all_mddevs); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 231 | spin_unlock(&all_mddevs_lock); |
Al Viro | 1312f40 | 2006-03-12 11:02:03 -0500 | [diff] [blame] | 232 | blk_cleanup_queue(mddev->queue); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 233 | kobject_unregister(&mddev->kobj); |
NeilBrown | 926ce2d | 2006-03-31 02:32:02 -0800 | [diff] [blame] | 234 | } else |
| 235 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static mddev_t * mddev_find(dev_t unit) |
| 239 | { |
| 240 | mddev_t *mddev, *new = NULL; |
| 241 | |
| 242 | retry: |
| 243 | spin_lock(&all_mddevs_lock); |
| 244 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 245 | if (mddev->unit == unit) { |
| 246 | mddev_get(mddev); |
| 247 | spin_unlock(&all_mddevs_lock); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 248 | kfree(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return mddev; |
| 250 | } |
| 251 | |
| 252 | if (new) { |
| 253 | list_add(&new->all_mddevs, &all_mddevs); |
| 254 | spin_unlock(&all_mddevs_lock); |
| 255 | return new; |
| 256 | } |
| 257 | spin_unlock(&all_mddevs_lock); |
| 258 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 259 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if (!new) |
| 261 | return NULL; |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | new->unit = unit; |
| 264 | if (MAJOR(unit) == MD_MAJOR) |
| 265 | new->md_minor = MINOR(unit); |
| 266 | else |
| 267 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 268 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 269 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | INIT_LIST_HEAD(&new->disks); |
| 271 | INIT_LIST_HEAD(&new->all_mddevs); |
| 272 | init_timer(&new->safemode_timer); |
| 273 | atomic_set(&new->active, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 274 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 275 | init_waitqueue_head(&new->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | new->queue = blk_alloc_queue(GFP_KERNEL); |
| 278 | if (!new->queue) { |
| 279 | kfree(new); |
| 280 | return NULL; |
| 281 | } |
NeilBrown | 89e5c8b | 2006-03-27 01:18:02 -0800 | [diff] [blame] | 282 | set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | blk_queue_make_request(new->queue, md_fail_request); |
| 285 | |
| 286 | goto retry; |
| 287 | } |
| 288 | |
| 289 | static inline int mddev_lock(mddev_t * mddev) |
| 290 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 291 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | static inline int mddev_trylock(mddev_t * mddev) |
| 295 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 296 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static inline void mddev_unlock(mddev_t * mddev) |
| 300 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 301 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 303 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 306 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
| 308 | mdk_rdev_t * rdev; |
| 309 | struct list_head *tmp; |
| 310 | |
| 311 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 312 | if (rdev->desc_nr == nr) |
| 313 | return rdev; |
| 314 | } |
| 315 | return NULL; |
| 316 | } |
| 317 | |
| 318 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 319 | { |
| 320 | struct list_head *tmp; |
| 321 | mdk_rdev_t *rdev; |
| 322 | |
| 323 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 324 | if (rdev->bdev->bd_dev == dev) |
| 325 | return rdev; |
| 326 | } |
| 327 | return NULL; |
| 328 | } |
| 329 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 330 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 331 | { |
| 332 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 333 | list_for_each_entry(pers, &pers_list, list) { |
| 334 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 335 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 336 | if (strcmp(pers->name, clevel)==0) |
| 337 | return pers; |
| 338 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 339 | return NULL; |
| 340 | } |
| 341 | |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 342 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 345 | return MD_NEW_SIZE_BLOCKS(size); |
| 346 | } |
| 347 | |
| 348 | static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size) |
| 349 | { |
| 350 | sector_t size; |
| 351 | |
| 352 | size = rdev->sb_offset; |
| 353 | |
| 354 | if (chunk_size) |
| 355 | size &= ~((sector_t)chunk_size/1024 - 1); |
| 356 | return size; |
| 357 | } |
| 358 | |
| 359 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 360 | { |
| 361 | if (rdev->sb_page) |
| 362 | MD_BUG(); |
| 363 | |
| 364 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 365 | if (!rdev->sb_page) { |
| 366 | printk(KERN_ALERT "md: out of memory.\n"); |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 374 | { |
| 375 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 376 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | rdev->sb_loaded = 0; |
| 378 | rdev->sb_page = NULL; |
| 379 | rdev->sb_offset = 0; |
| 380 | rdev->size = 0; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 385 | static int super_written(struct bio *bio, unsigned int bytes_done, int error) |
| 386 | { |
| 387 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 388 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 389 | if (bio->bi_size) |
| 390 | return 1; |
| 391 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 392 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 393 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 394 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 395 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 396 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 397 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 398 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 399 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 400 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 401 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 405 | static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error) |
| 406 | { |
| 407 | struct bio *bio2 = bio->bi_private; |
| 408 | mdk_rdev_t *rdev = bio2->bi_private; |
| 409 | mddev_t *mddev = rdev->mddev; |
| 410 | if (bio->bi_size) |
| 411 | return 1; |
| 412 | |
| 413 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 414 | error == -EOPNOTSUPP) { |
| 415 | unsigned long flags; |
| 416 | /* barriers don't appear to be supported :-( */ |
| 417 | set_bit(BarriersNotsupp, &rdev->flags); |
| 418 | mddev->barriers_work = 0; |
| 419 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 420 | bio2->bi_next = mddev->biolist; |
| 421 | mddev->biolist = bio2; |
| 422 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 423 | wake_up(&mddev->sb_wait); |
| 424 | bio_put(bio); |
| 425 | return 0; |
| 426 | } |
| 427 | bio_put(bio2); |
| 428 | bio->bi_private = rdev; |
| 429 | return super_written(bio, bytes_done, error); |
| 430 | } |
| 431 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 432 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 433 | sector_t sector, int size, struct page *page) |
| 434 | { |
| 435 | /* write first size bytes of page to sector of rdev |
| 436 | * Increment mddev->pending_writes before returning |
| 437 | * and decrement it on completion, waking up sb_wait |
| 438 | * if zero is reached. |
| 439 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 440 | * |
| 441 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 442 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 443 | */ |
| 444 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 445 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 446 | |
| 447 | bio->bi_bdev = rdev->bdev; |
| 448 | bio->bi_sector = sector; |
| 449 | bio_add_page(bio, page, size, 0); |
| 450 | bio->bi_private = rdev; |
| 451 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 452 | bio->bi_rw = rw; |
| 453 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 454 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 455 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 456 | struct bio *rbio; |
| 457 | rw |= (1<<BIO_RW_BARRIER); |
| 458 | rbio = bio_clone(bio, GFP_NOIO); |
| 459 | rbio->bi_private = bio; |
| 460 | rbio->bi_end_io = super_written_barrier; |
| 461 | submit_bio(rw, rbio); |
| 462 | } else |
| 463 | submit_bio(rw, bio); |
| 464 | } |
| 465 | |
| 466 | void md_super_wait(mddev_t *mddev) |
| 467 | { |
| 468 | /* wait for all superblock writes that were scheduled to complete. |
| 469 | * if any had to be retried (due to BARRIER problems), retry them |
| 470 | */ |
| 471 | DEFINE_WAIT(wq); |
| 472 | for(;;) { |
| 473 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 474 | if (atomic_read(&mddev->pending_writes)==0) |
| 475 | break; |
| 476 | while (mddev->biolist) { |
| 477 | struct bio *bio; |
| 478 | spin_lock_irq(&mddev->write_lock); |
| 479 | bio = mddev->biolist; |
| 480 | mddev->biolist = bio->bi_next ; |
| 481 | bio->bi_next = NULL; |
| 482 | spin_unlock_irq(&mddev->write_lock); |
| 483 | submit_bio(bio->bi_rw, bio); |
| 484 | } |
| 485 | schedule(); |
| 486 | } |
| 487 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | static int bi_complete(struct bio *bio, unsigned int bytes_done, int error) |
| 491 | { |
| 492 | if (bio->bi_size) |
| 493 | return 1; |
| 494 | |
| 495 | complete((struct completion*)bio->bi_private); |
| 496 | return 0; |
| 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 | |
| 592 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 593 | { |
| 594 | unsigned int disk_csum, csum; |
| 595 | |
| 596 | disk_csum = sb->sb_csum; |
| 597 | sb->sb_csum = 0; |
| 598 | csum = csum_partial((void *)sb, MD_SB_BYTES, 0); |
| 599 | sb->sb_csum = disk_csum; |
| 600 | return csum; |
| 601 | } |
| 602 | |
| 603 | |
| 604 | /* |
| 605 | * Handle superblock details. |
| 606 | * We want to be able to handle multiple superblock formats |
| 607 | * so we have a common interface to them all, and an array of |
| 608 | * different handlers. |
| 609 | * We rely on user-space to write the initial superblock, and support |
| 610 | * reading and updating of superblocks. |
| 611 | * Interface methods are: |
| 612 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 613 | * loads and validates a superblock on dev. |
| 614 | * if refdev != NULL, compare superblocks on both devices |
| 615 | * Return: |
| 616 | * 0 - dev has a superblock that is compatible with refdev |
| 617 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 618 | * so dev should be used as the refdev in future |
| 619 | * -EINVAL superblock incompatible or invalid |
| 620 | * -othererror e.g. -EIO |
| 621 | * |
| 622 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 623 | * Verify that dev is acceptable into mddev. |
| 624 | * The first time, mddev->raid_disks will be 0, and data from |
| 625 | * dev should be merged in. Subsequent calls check that dev |
| 626 | * is new enough. Return 0 or -EINVAL |
| 627 | * |
| 628 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 629 | * Update the superblock for rdev with data in mddev |
| 630 | * This does not write to disc. |
| 631 | * |
| 632 | */ |
| 633 | |
| 634 | struct super_type { |
| 635 | char *name; |
| 636 | struct module *owner; |
| 637 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version); |
| 638 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 639 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 640 | }; |
| 641 | |
| 642 | /* |
| 643 | * load_super for 0.90.0 |
| 644 | */ |
| 645 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 646 | { |
| 647 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 648 | mdp_super_t *sb; |
| 649 | int ret; |
| 650 | sector_t sb_offset; |
| 651 | |
| 652 | /* |
| 653 | * Calculate the position of the superblock, |
| 654 | * it's at the end of the disk. |
| 655 | * |
| 656 | * It also happens to be a multiple of 4Kb. |
| 657 | */ |
| 658 | sb_offset = calc_dev_sboffset(rdev->bdev); |
| 659 | rdev->sb_offset = sb_offset; |
| 660 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 661 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (ret) return ret; |
| 663 | |
| 664 | ret = -EINVAL; |
| 665 | |
| 666 | bdevname(rdev->bdev, b); |
| 667 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 668 | |
| 669 | if (sb->md_magic != MD_SB_MAGIC) { |
| 670 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 671 | b); |
| 672 | goto abort; |
| 673 | } |
| 674 | |
| 675 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 676 | sb->minor_version < 90 || |
| 677 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 679 | sb->major_version, sb->minor_version, |
| 680 | b); |
| 681 | goto abort; |
| 682 | } |
| 683 | |
| 684 | if (sb->raid_disks <= 0) |
| 685 | goto abort; |
| 686 | |
| 687 | if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) { |
| 688 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 689 | b); |
| 690 | goto abort; |
| 691 | } |
| 692 | |
| 693 | rdev->preferred_minor = sb->md_minor; |
| 694 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 695 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | if (sb->level == LEVEL_MULTIPATH) |
| 698 | rdev->desc_nr = -1; |
| 699 | else |
| 700 | rdev->desc_nr = sb->this_disk.number; |
| 701 | |
| 702 | if (refdev == 0) |
| 703 | ret = 1; |
| 704 | else { |
| 705 | __u64 ev1, ev2; |
| 706 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 707 | if (!uuid_equal(refsb, sb)) { |
| 708 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 709 | b, bdevname(refdev->bdev,b2)); |
| 710 | goto abort; |
| 711 | } |
| 712 | if (!sb_equal(refsb, sb)) { |
| 713 | printk(KERN_WARNING "md: %s has same UUID" |
| 714 | " but different superblock to %s\n", |
| 715 | b, bdevname(refdev->bdev, b2)); |
| 716 | goto abort; |
| 717 | } |
| 718 | ev1 = md_event(sb); |
| 719 | ev2 = md_event(refsb); |
| 720 | if (ev1 > ev2) |
| 721 | ret = 1; |
| 722 | else |
| 723 | ret = 0; |
| 724 | } |
| 725 | rdev->size = calc_dev_size(rdev, sb->chunk_size); |
| 726 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 727 | if (rdev->size < sb->size && sb->level > 1) |
| 728 | /* "this cannot possibly happen" ... */ |
| 729 | ret = -EINVAL; |
| 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | abort: |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * validate_super for 0.90.0 |
| 737 | */ |
| 738 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 739 | { |
| 740 | mdp_disk_t *desc; |
| 741 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 742 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 744 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 745 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | if (mddev->raid_disks == 0) { |
| 747 | mddev->major_version = 0; |
| 748 | mddev->minor_version = sb->minor_version; |
| 749 | mddev->patch_version = sb->patch_version; |
| 750 | mddev->persistent = ! sb->not_persistent; |
| 751 | mddev->chunk_size = sb->chunk_size; |
| 752 | mddev->ctime = sb->ctime; |
| 753 | mddev->utime = sb->utime; |
| 754 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 755 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | mddev->layout = sb->layout; |
| 757 | mddev->raid_disks = sb->raid_disks; |
| 758 | mddev->size = sb->size; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 759 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 760 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 761 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 763 | if (mddev->minor_version >= 91) { |
| 764 | mddev->reshape_position = sb->reshape_position; |
| 765 | mddev->delta_disks = sb->delta_disks; |
| 766 | mddev->new_level = sb->new_level; |
| 767 | mddev->new_layout = sb->new_layout; |
| 768 | mddev->new_chunk = sb->new_chunk; |
| 769 | } else { |
| 770 | mddev->reshape_position = MaxSector; |
| 771 | mddev->delta_disks = 0; |
| 772 | mddev->new_level = mddev->level; |
| 773 | mddev->new_layout = mddev->layout; |
| 774 | mddev->new_chunk = mddev->chunk_size; |
| 775 | } |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 778 | mddev->recovery_cp = MaxSector; |
| 779 | else { |
| 780 | if (sb->events_hi == sb->cp_events_hi && |
| 781 | sb->events_lo == sb->cp_events_lo) { |
| 782 | mddev->recovery_cp = sb->recovery_cp; |
| 783 | } else |
| 784 | mddev->recovery_cp = 0; |
| 785 | } |
| 786 | |
| 787 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 788 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 789 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 790 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 791 | |
| 792 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 793 | |
| 794 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
| 795 | mddev->bitmap_file == NULL) { |
NeilBrown | c5a10f6 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 796 | if (mddev->level != 1 && mddev->level != 4 |
| 797 | && mddev->level != 5 && mddev->level != 6 |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 798 | && mddev->level != 10) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 799 | /* FIXME use a better test */ |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 800 | printk(KERN_WARNING "md: bitmaps not supported for this level.\n"); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 801 | return -EINVAL; |
| 802 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 803 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 804 | } |
| 805 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 806 | } else if (mddev->pers == NULL) { |
| 807 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | ++ev1; |
| 809 | if (ev1 < mddev->events) |
| 810 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 811 | } else if (mddev->bitmap) { |
| 812 | /* if adding to array with a bitmap, then we can accept an |
| 813 | * older device ... but not too old. |
| 814 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 815 | if (ev1 < mddev->bitmap->events_cleared) |
| 816 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 817 | } else { |
| 818 | if (ev1 < mddev->events) |
| 819 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 820 | return 0; |
| 821 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | desc = sb->disks + rdev->desc_nr; |
| 825 | |
| 826 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 827 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 828 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 829 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 830 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | rdev->raid_disk = desc->raid_disk; |
| 832 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 833 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 834 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 835 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 836 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * sync_super for 0.90.0 |
| 842 | */ |
| 843 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 844 | { |
| 845 | mdp_super_t *sb; |
| 846 | struct list_head *tmp; |
| 847 | mdk_rdev_t *rdev2; |
| 848 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
| 851 | /* make rdev->sb match mddev data.. |
| 852 | * |
| 853 | * 1/ zero out disks |
| 854 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 855 | * 3/ any empty disks < next_spare become removed |
| 856 | * |
| 857 | * disks[0] gets initialised to REMOVED because |
| 858 | * we cannot be sure from other fields if it has |
| 859 | * been initialised or not. |
| 860 | */ |
| 861 | int i; |
| 862 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 863 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 864 | rdev->sb_size = MD_SB_BYTES; |
| 865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 867 | |
| 868 | memset(sb, 0, sizeof(*sb)); |
| 869 | |
| 870 | sb->md_magic = MD_SB_MAGIC; |
| 871 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | sb->patch_version = mddev->patch_version; |
| 873 | sb->gvalid_words = 0; /* ignored */ |
| 874 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 875 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 876 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 877 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 878 | |
| 879 | sb->ctime = mddev->ctime; |
| 880 | sb->level = mddev->level; |
| 881 | sb->size = mddev->size; |
| 882 | sb->raid_disks = mddev->raid_disks; |
| 883 | sb->md_minor = mddev->md_minor; |
| 884 | sb->not_persistent = !mddev->persistent; |
| 885 | sb->utime = mddev->utime; |
| 886 | sb->state = 0; |
| 887 | sb->events_hi = (mddev->events>>32); |
| 888 | sb->events_lo = (u32)mddev->events; |
| 889 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 890 | if (mddev->reshape_position == MaxSector) |
| 891 | sb->minor_version = 90; |
| 892 | else { |
| 893 | sb->minor_version = 91; |
| 894 | sb->reshape_position = mddev->reshape_position; |
| 895 | sb->new_level = mddev->new_level; |
| 896 | sb->delta_disks = mddev->delta_disks; |
| 897 | sb->new_layout = mddev->new_layout; |
| 898 | sb->new_chunk = mddev->new_chunk; |
| 899 | } |
| 900 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | if (mddev->in_sync) |
| 902 | { |
| 903 | sb->recovery_cp = mddev->recovery_cp; |
| 904 | sb->cp_events_hi = (mddev->events>>32); |
| 905 | sb->cp_events_lo = (u32)mddev->events; |
| 906 | if (mddev->recovery_cp == MaxSector) |
| 907 | sb->state = (1<< MD_SB_CLEAN); |
| 908 | } else |
| 909 | sb->recovery_cp = 0; |
| 910 | |
| 911 | sb->layout = mddev->layout; |
| 912 | sb->chunk_size = mddev->chunk_size; |
| 913 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 914 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 915 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
| 918 | ITERATE_RDEV(mddev,rdev2,tmp) { |
| 919 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 920 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 921 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 922 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 923 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 925 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 926 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | d = &sb->disks[rdev2->desc_nr]; |
| 928 | nr_disks++; |
| 929 | d->number = rdev2->desc_nr; |
| 930 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 931 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 932 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 933 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | d->raid_disk = rdev2->raid_disk; |
| 935 | else |
| 936 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 937 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 939 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | d->state = (1<<MD_DISK_ACTIVE); |
| 941 | d->state |= (1<<MD_DISK_SYNC); |
| 942 | active++; |
| 943 | working++; |
| 944 | } else { |
| 945 | d->state = 0; |
| 946 | spare++; |
| 947 | working++; |
| 948 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 949 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 950 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 953 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 954 | mdp_disk_t *d = &sb->disks[i]; |
| 955 | if (d->state == 0 && d->number == 0) { |
| 956 | d->number = i; |
| 957 | d->raid_disk = i; |
| 958 | d->state = (1<<MD_DISK_REMOVED); |
| 959 | d->state |= (1<<MD_DISK_FAULTY); |
| 960 | failed++; |
| 961 | } |
| 962 | } |
| 963 | sb->nr_disks = nr_disks; |
| 964 | sb->active_disks = active; |
| 965 | sb->working_disks = working; |
| 966 | sb->failed_disks = failed; |
| 967 | sb->spare_disks = spare; |
| 968 | |
| 969 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 970 | sb->sb_csum = calc_sb_csum(sb); |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * version 1 superblock |
| 975 | */ |
| 976 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame^] | 977 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame^] | 979 | __le32 disk_csum; |
| 980 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | unsigned long long newcsum; |
| 982 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame^] | 983 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | int i; |
| 985 | |
| 986 | disk_csum = sb->sb_csum; |
| 987 | sb->sb_csum = 0; |
| 988 | newcsum = 0; |
| 989 | for (i=0; size>=4; size -= 4 ) |
| 990 | newcsum += le32_to_cpu(*isuper++); |
| 991 | |
| 992 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame^] | 993 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 996 | sb->sb_csum = disk_csum; |
| 997 | return cpu_to_le32(csum); |
| 998 | } |
| 999 | |
| 1000 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1001 | { |
| 1002 | struct mdp_superblock_1 *sb; |
| 1003 | int ret; |
| 1004 | sector_t sb_offset; |
| 1005 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1006 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
| 1008 | /* |
| 1009 | * Calculate the position of the superblock. |
| 1010 | * It is always aligned to a 4K boundary and |
| 1011 | * depeding on minor_version, it can be: |
| 1012 | * 0: At least 8K, but less than 12K, from end of device |
| 1013 | * 1: At start of device |
| 1014 | * 2: 4K from start of device. |
| 1015 | */ |
| 1016 | switch(minor_version) { |
| 1017 | case 0: |
| 1018 | sb_offset = rdev->bdev->bd_inode->i_size >> 9; |
| 1019 | sb_offset -= 8*2; |
NeilBrown | 3973096 | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1020 | sb_offset &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | /* convert from sectors to K */ |
| 1022 | sb_offset /= 2; |
| 1023 | break; |
| 1024 | case 1: |
| 1025 | sb_offset = 0; |
| 1026 | break; |
| 1027 | case 2: |
| 1028 | sb_offset = 4; |
| 1029 | break; |
| 1030 | default: |
| 1031 | return -EINVAL; |
| 1032 | } |
| 1033 | rdev->sb_offset = sb_offset; |
| 1034 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1035 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1036 | * and it is safe to read 4k, so we do that |
| 1037 | */ |
| 1038 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | if (ret) return ret; |
| 1040 | |
| 1041 | |
| 1042 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1043 | |
| 1044 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1045 | sb->major_version != cpu_to_le32(1) || |
| 1046 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
| 1047 | le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1048 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | return -EINVAL; |
| 1050 | |
| 1051 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1052 | printk("md: invalid superblock checksum on %s\n", |
| 1053 | bdevname(rdev->bdev,b)); |
| 1054 | return -EINVAL; |
| 1055 | } |
| 1056 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1057 | printk("md: data_size too small on %s\n", |
| 1058 | bdevname(rdev->bdev,b)); |
| 1059 | return -EINVAL; |
| 1060 | } |
| 1061 | rdev->preferred_minor = 0xffff; |
| 1062 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1063 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1065 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
NeilBrown | 720a3dc | 2005-09-09 16:23:59 -0700 | [diff] [blame] | 1066 | bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1067 | if (rdev->sb_size & bmask) |
| 1068 | rdev-> sb_size = (rdev->sb_size | bmask)+1; |
| 1069 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1070 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1071 | rdev->desc_nr = -1; |
| 1072 | else |
| 1073 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | if (refdev == 0) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1076 | ret = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | else { |
| 1078 | __u64 ev1, ev2; |
| 1079 | struct mdp_superblock_1 *refsb = |
| 1080 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1081 | |
| 1082 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1083 | sb->level != refsb->level || |
| 1084 | sb->layout != refsb->layout || |
| 1085 | sb->chunksize != refsb->chunksize) { |
| 1086 | printk(KERN_WARNING "md: %s has strangely different" |
| 1087 | " superblock to %s\n", |
| 1088 | bdevname(rdev->bdev,b), |
| 1089 | bdevname(refdev->bdev,b2)); |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | ev1 = le64_to_cpu(sb->events); |
| 1093 | ev2 = le64_to_cpu(refsb->events); |
| 1094 | |
| 1095 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1096 | ret = 1; |
| 1097 | else |
| 1098 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | if (minor_version) |
| 1101 | rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2; |
| 1102 | else |
| 1103 | rdev->size = rdev->sb_offset; |
| 1104 | if (rdev->size < le64_to_cpu(sb->data_size)/2) |
| 1105 | return -EINVAL; |
| 1106 | rdev->size = le64_to_cpu(sb->data_size)/2; |
| 1107 | if (le32_to_cpu(sb->chunksize)) |
| 1108 | rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1); |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1109 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame^] | 1110 | if (le64_to_cpu(sb->size) > rdev->size*2) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1111 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1112 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1116 | { |
| 1117 | 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] | 1118 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1120 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1121 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | if (mddev->raid_disks == 0) { |
| 1123 | mddev->major_version = 1; |
| 1124 | mddev->patch_version = 0; |
| 1125 | mddev->persistent = 1; |
| 1126 | mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9; |
| 1127 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1128 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1129 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1130 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | mddev->layout = le32_to_cpu(sb->layout); |
| 1132 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
| 1133 | mddev->size = le64_to_cpu(sb->size)/2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1134 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1135 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1136 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
| 1138 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1139 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1140 | |
| 1141 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1142 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1143 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1144 | mddev->bitmap_file == NULL ) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1145 | if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6 |
| 1146 | && mddev->level != 10) { |
| 1147 | printk(KERN_WARNING "md: bitmaps not supported for this level.\n"); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1148 | return -EINVAL; |
| 1149 | } |
| 1150 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
| 1151 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1152 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1153 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1154 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1155 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1156 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
| 1157 | mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9; |
| 1158 | } else { |
| 1159 | mddev->reshape_position = MaxSector; |
| 1160 | mddev->delta_disks = 0; |
| 1161 | mddev->new_level = mddev->level; |
| 1162 | mddev->new_layout = mddev->layout; |
| 1163 | mddev->new_chunk = mddev->chunk_size; |
| 1164 | } |
| 1165 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1166 | } else if (mddev->pers == NULL) { |
| 1167 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | ++ev1; |
| 1169 | if (ev1 < mddev->events) |
| 1170 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1171 | } else if (mddev->bitmap) { |
| 1172 | /* If adding to array with a bitmap, then we can accept an |
| 1173 | * older device, but not too old. |
| 1174 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1175 | if (ev1 < mddev->bitmap->events_cleared) |
| 1176 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1177 | } else { |
| 1178 | if (ev1 < mddev->events) |
| 1179 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1180 | return 0; |
| 1181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1183 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1185 | switch(role) { |
| 1186 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | break; |
| 1188 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1189 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | break; |
| 1191 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1192 | if ((le32_to_cpu(sb->feature_map) & |
| 1193 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1194 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1195 | else |
| 1196 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | rdev->raid_disk = role; |
| 1198 | break; |
| 1199 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1200 | if (sb->devflags & WriteMostly1) |
| 1201 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1202 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1203 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1209 | { |
| 1210 | struct mdp_superblock_1 *sb; |
| 1211 | struct list_head *tmp; |
| 1212 | mdk_rdev_t *rdev2; |
| 1213 | int max_dev, i; |
| 1214 | /* make rdev->sb match mddev and rdev data. */ |
| 1215 | |
| 1216 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1217 | |
| 1218 | sb->feature_map = 0; |
| 1219 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1220 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1222 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1223 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1224 | |
| 1225 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1226 | sb->events = cpu_to_le64(mddev->events); |
| 1227 | if (mddev->in_sync) |
| 1228 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1229 | else |
| 1230 | sb->resync_offset = cpu_to_le64(0); |
| 1231 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame^] | 1232 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1233 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1234 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1235 | sb->size = cpu_to_le64(mddev->size<<1); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1236 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1237 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1238 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1239 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1240 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1241 | |
| 1242 | if (rdev->raid_disk >= 0 && |
| 1243 | !test_bit(In_sync, &rdev->flags) && |
| 1244 | rdev->recovery_offset > 0) { |
| 1245 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1246 | sb->recovery_offset = cpu_to_le64(rdev->recovery_offset); |
| 1247 | } |
| 1248 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1249 | if (mddev->reshape_position != MaxSector) { |
| 1250 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1251 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1252 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1253 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1254 | sb->new_level = cpu_to_le32(mddev->new_level); |
| 1255 | sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9); |
| 1256 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | max_dev = 0; |
| 1259 | ITERATE_RDEV(mddev,rdev2,tmp) |
| 1260 | if (rdev2->desc_nr+1 > max_dev) |
| 1261 | max_dev = rdev2->desc_nr+1; |
| 1262 | |
| 1263 | sb->max_dev = cpu_to_le32(max_dev); |
| 1264 | for (i=0; i<max_dev;i++) |
| 1265 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1266 | |
| 1267 | ITERATE_RDEV(mddev,rdev2,tmp) { |
| 1268 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1269 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1271 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1273 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1274 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | else |
| 1276 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1277 | } |
| 1278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1280 | } |
| 1281 | |
| 1282 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1283 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | [0] = { |
| 1285 | .name = "0.90.0", |
| 1286 | .owner = THIS_MODULE, |
| 1287 | .load_super = super_90_load, |
| 1288 | .validate_super = super_90_validate, |
| 1289 | .sync_super = super_90_sync, |
| 1290 | }, |
| 1291 | [1] = { |
| 1292 | .name = "md-1", |
| 1293 | .owner = THIS_MODULE, |
| 1294 | .load_super = super_1_load, |
| 1295 | .validate_super = super_1_validate, |
| 1296 | .sync_super = super_1_sync, |
| 1297 | }, |
| 1298 | }; |
| 1299 | |
| 1300 | static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev) |
| 1301 | { |
| 1302 | struct list_head *tmp; |
| 1303 | mdk_rdev_t *rdev; |
| 1304 | |
| 1305 | ITERATE_RDEV(mddev,rdev,tmp) |
| 1306 | if (rdev->bdev->bd_contains == dev->bdev->bd_contains) |
| 1307 | return rdev; |
| 1308 | |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
| 1312 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1313 | { |
| 1314 | struct list_head *tmp; |
| 1315 | mdk_rdev_t *rdev; |
| 1316 | |
| 1317 | ITERATE_RDEV(mddev1,rdev,tmp) |
| 1318 | if (match_dev_unit(mddev2, rdev)) |
| 1319 | return 1; |
| 1320 | |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | static LIST_HEAD(pending_raid_disks); |
| 1325 | |
| 1326 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1327 | { |
| 1328 | mdk_rdev_t *same_pdev; |
| 1329 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1330 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1331 | char *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
| 1333 | if (rdev->mddev) { |
| 1334 | MD_BUG(); |
| 1335 | return -EINVAL; |
| 1336 | } |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1337 | /* make sure rdev->size exceeds mddev->size */ |
| 1338 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
| 1339 | if (mddev->pers) |
| 1340 | /* Cannot change size, so fail */ |
| 1341 | return -ENOSPC; |
| 1342 | else |
| 1343 | mddev->size = rdev->size; |
| 1344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | same_pdev = match_dev_unit(mddev, rdev); |
| 1346 | if (same_pdev) |
| 1347 | printk(KERN_WARNING |
| 1348 | "%s: WARNING: %s appears to be on the same physical" |
| 1349 | " disk as %s. True\n protection against single-disk" |
| 1350 | " failure might be compromised.\n", |
| 1351 | mdname(mddev), bdevname(rdev->bdev,b), |
| 1352 | bdevname(same_pdev->bdev,b2)); |
| 1353 | |
| 1354 | /* Verify rdev->desc_nr is unique. |
| 1355 | * If it is -1, assign a free number, else |
| 1356 | * check number is not in use |
| 1357 | */ |
| 1358 | if (rdev->desc_nr < 0) { |
| 1359 | int choice = 0; |
| 1360 | if (mddev->pers) choice = mddev->raid_disks; |
| 1361 | while (find_rdev_nr(mddev, choice)) |
| 1362 | choice++; |
| 1363 | rdev->desc_nr = choice; |
| 1364 | } else { |
| 1365 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1366 | return -EBUSY; |
| 1367 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1368 | bdevname(rdev->bdev,b); |
| 1369 | if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0) |
| 1370 | return -ENOMEM; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1371 | while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL) |
| 1372 | *s = '!'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
| 1374 | list_add(&rdev->same_set, &mddev->disks); |
| 1375 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1376 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1377 | |
NeilBrown | 9c79197 | 2005-11-08 21:39:28 -0800 | [diff] [blame] | 1378 | rdev->kobj.parent = &mddev->kobj; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1379 | kobject_add(&rdev->kobj); |
| 1380 | |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1381 | if (rdev->bdev->bd_part) |
| 1382 | ko = &rdev->bdev->bd_part->kobj; |
| 1383 | else |
| 1384 | ko = &rdev->bdev->bd_disk->kobj; |
| 1385 | sysfs_create_link(&rdev->kobj, ko, "block"); |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1386 | bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1391 | { |
| 1392 | char b[BDEVNAME_SIZE]; |
| 1393 | if (!rdev->mddev) { |
| 1394 | MD_BUG(); |
| 1395 | return; |
| 1396 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1397 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | list_del_init(&rdev->same_set); |
| 1399 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1400 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1401 | sysfs_remove_link(&rdev->kobj, "block"); |
| 1402 | kobject_del(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | /* |
| 1406 | * prevent the device from being mounted, repartitioned or |
| 1407 | * otherwise reused by a RAID array (or any other kernel |
| 1408 | * subsystem), by bd_claiming the device. |
| 1409 | */ |
| 1410 | static int lock_rdev(mdk_rdev_t *rdev, dev_t dev) |
| 1411 | { |
| 1412 | int err = 0; |
| 1413 | struct block_device *bdev; |
| 1414 | char b[BDEVNAME_SIZE]; |
| 1415 | |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1416 | bdev = open_partition_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | if (IS_ERR(bdev)) { |
| 1418 | printk(KERN_ERR "md: could not open %s.\n", |
| 1419 | __bdevname(dev, b)); |
| 1420 | return PTR_ERR(bdev); |
| 1421 | } |
| 1422 | err = bd_claim(bdev, rdev); |
| 1423 | if (err) { |
| 1424 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1425 | bdevname(bdev, b)); |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1426 | blkdev_put_partition(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | return err; |
| 1428 | } |
| 1429 | rdev->bdev = bdev; |
| 1430 | return err; |
| 1431 | } |
| 1432 | |
| 1433 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1434 | { |
| 1435 | struct block_device *bdev = rdev->bdev; |
| 1436 | rdev->bdev = NULL; |
| 1437 | if (!bdev) |
| 1438 | MD_BUG(); |
| 1439 | bd_release(bdev); |
Ingo Molnar | 663d440 | 2006-07-03 00:25:33 -0700 | [diff] [blame] | 1440 | blkdev_put_partition(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | void md_autodetect_dev(dev_t dev); |
| 1444 | |
| 1445 | static void export_rdev(mdk_rdev_t * rdev) |
| 1446 | { |
| 1447 | char b[BDEVNAME_SIZE]; |
| 1448 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1449 | bdevname(rdev->bdev,b)); |
| 1450 | if (rdev->mddev) |
| 1451 | MD_BUG(); |
| 1452 | free_disk_sb(rdev); |
| 1453 | list_del_init(&rdev->same_set); |
| 1454 | #ifndef MODULE |
| 1455 | md_autodetect_dev(rdev->bdev->bd_dev); |
| 1456 | #endif |
| 1457 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1458 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1462 | { |
| 1463 | unbind_rdev_from_array(rdev); |
| 1464 | export_rdev(rdev); |
| 1465 | } |
| 1466 | |
| 1467 | static void export_array(mddev_t *mddev) |
| 1468 | { |
| 1469 | struct list_head *tmp; |
| 1470 | mdk_rdev_t *rdev; |
| 1471 | |
| 1472 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1473 | if (!rdev->mddev) { |
| 1474 | MD_BUG(); |
| 1475 | continue; |
| 1476 | } |
| 1477 | kick_rdev_from_array(rdev); |
| 1478 | } |
| 1479 | if (!list_empty(&mddev->disks)) |
| 1480 | MD_BUG(); |
| 1481 | mddev->raid_disks = 0; |
| 1482 | mddev->major_version = 0; |
| 1483 | } |
| 1484 | |
| 1485 | static void print_desc(mdp_disk_t *desc) |
| 1486 | { |
| 1487 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1488 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1489 | } |
| 1490 | |
| 1491 | static void print_sb(mdp_super_t *sb) |
| 1492 | { |
| 1493 | int i; |
| 1494 | |
| 1495 | printk(KERN_INFO |
| 1496 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1497 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1498 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1499 | sb->ctime); |
| 1500 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1501 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1502 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1503 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1504 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1505 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1506 | sb->failed_disks, sb->spare_disks, |
| 1507 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1508 | |
| 1509 | printk(KERN_INFO); |
| 1510 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1511 | mdp_disk_t *desc; |
| 1512 | |
| 1513 | desc = sb->disks + i; |
| 1514 | if (desc->number || desc->major || desc->minor || |
| 1515 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1516 | printk(" D %2d: ", i); |
| 1517 | print_desc(desc); |
| 1518 | } |
| 1519 | } |
| 1520 | printk(KERN_INFO "md: THIS: "); |
| 1521 | print_desc(&sb->this_disk); |
| 1522 | |
| 1523 | } |
| 1524 | |
| 1525 | static void print_rdev(mdk_rdev_t *rdev) |
| 1526 | { |
| 1527 | char b[BDEVNAME_SIZE]; |
| 1528 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1529 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1530 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1531 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | if (rdev->sb_loaded) { |
| 1533 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1534 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1535 | } else |
| 1536 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1537 | } |
| 1538 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1539 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | { |
| 1541 | struct list_head *tmp, *tmp2; |
| 1542 | mdk_rdev_t *rdev; |
| 1543 | mddev_t *mddev; |
| 1544 | char b[BDEVNAME_SIZE]; |
| 1545 | |
| 1546 | printk("\n"); |
| 1547 | printk("md: **********************************\n"); |
| 1548 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1549 | printk("md: **********************************\n"); |
| 1550 | ITERATE_MDDEV(mddev,tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1552 | if (mddev->bitmap) |
| 1553 | bitmap_print_sb(mddev->bitmap); |
| 1554 | else |
| 1555 | printk("%s: ", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | ITERATE_RDEV(mddev,rdev,tmp2) |
| 1557 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1558 | printk("\n"); |
| 1559 | |
| 1560 | ITERATE_RDEV(mddev,rdev,tmp2) |
| 1561 | print_rdev(rdev); |
| 1562 | } |
| 1563 | printk("md: **********************************\n"); |
| 1564 | printk("\n"); |
| 1565 | } |
| 1566 | |
| 1567 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1568 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1570 | /* Update each superblock (in-memory image), but |
| 1571 | * if we are allowed to, skip spares which already |
| 1572 | * have the right event counter, or have one earlier |
| 1573 | * (which would mean they aren't being marked as dirty |
| 1574 | * with the rest of the array) |
| 1575 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | mdk_rdev_t *rdev; |
| 1577 | struct list_head *tmp; |
| 1578 | |
| 1579 | ITERATE_RDEV(mddev,rdev,tmp) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1580 | if (rdev->sb_events == mddev->events || |
| 1581 | (nospares && |
| 1582 | rdev->raid_disk < 0 && |
| 1583 | (rdev->sb_events&1)==0 && |
| 1584 | rdev->sb_events+1 == mddev->events)) { |
| 1585 | /* Don't update this superblock */ |
| 1586 | rdev->sb_loaded = 2; |
| 1587 | } else { |
| 1588 | super_types[mddev->major_version]. |
| 1589 | sync_super(mddev, rdev); |
| 1590 | rdev->sb_loaded = 1; |
| 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1595 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1597 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | struct list_head *tmp; |
| 1599 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1600 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1601 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1604 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1605 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1606 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1607 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1608 | force_change = 1; |
| 1609 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1610 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1611 | * though if events isn't the right even/odd, we will have to do |
| 1612 | * spares after all |
| 1613 | */ |
| 1614 | nospares = 1; |
| 1615 | if (force_change) |
| 1616 | nospares = 0; |
| 1617 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1618 | /* If the array is degraded, then skipping spares is both |
| 1619 | * dangerous and fairly pointless. |
| 1620 | * Dangerous because a device that was removed from the array |
| 1621 | * might have a event_count that still looks up-to-date, |
| 1622 | * so it can be re-added without a resync. |
| 1623 | * Pointless because if there are any spares to skip, |
| 1624 | * then a recovery will happen and soon that array won't |
| 1625 | * be degraded any more and the spare can go back to sleep then. |
| 1626 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1627 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1628 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1629 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1631 | |
| 1632 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1633 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1634 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1635 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
| 1636 | && (mddev->events & 1)) |
| 1637 | mddev->events--; |
| 1638 | else { |
| 1639 | /* otherwise we have to go forward and ... */ |
| 1640 | mddev->events ++; |
| 1641 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1642 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1643 | if ((mddev->events&1)==0) { |
| 1644 | mddev->events++; |
| 1645 | nospares = 0; |
| 1646 | } |
| 1647 | } else { |
| 1648 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1649 | if ((mddev->events&1)) { |
| 1650 | mddev->events++; |
| 1651 | nospares = 0; |
| 1652 | } |
| 1653 | } |
| 1654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
| 1656 | if (!mddev->events) { |
| 1657 | /* |
| 1658 | * oops, this 64-bit counter should never wrap. |
| 1659 | * Either we are in around ~1 trillion A.C., assuming |
| 1660 | * 1 reboot per second, or we have a bug: |
| 1661 | */ |
| 1662 | MD_BUG(); |
| 1663 | mddev->events --; |
| 1664 | } |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1665 | sync_sbs(mddev, nospares); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
| 1667 | /* |
| 1668 | * do not write anything to disk if using |
| 1669 | * nonpersistent superblocks |
| 1670 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1671 | if (!mddev->persistent) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1672 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1673 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1674 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1676 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1677 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
| 1679 | dprintk(KERN_INFO |
| 1680 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1681 | mdname(mddev),mddev->in_sync); |
| 1682 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1683 | err = bitmap_update_sb(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1685 | char b[BDEVNAME_SIZE]; |
| 1686 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1687 | if (rdev->sb_loaded != 1) |
| 1688 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1689 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | dprintk("(skipping faulty "); |
| 1691 | |
| 1692 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1693 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1694 | md_super_write(mddev,rdev, |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1695 | rdev->sb_offset<<1, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1696 | rdev->sb_page); |
| 1697 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1698 | bdevname(rdev->bdev,b), |
| 1699 | (unsigned long long)rdev->sb_offset); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1700 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | } else |
| 1703 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1704 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | /* only need to write one superblock... */ |
| 1706 | break; |
| 1707 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1708 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1709 | /* 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] | 1710 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1711 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1712 | if (mddev->in_sync != sync_req || |
| 1713 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1714 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1715 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1716 | goto repeat; |
| 1717 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1718 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1719 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1720 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1724 | /* words written to sysfs files may, or my not, be \n terminated. |
| 1725 | * We want to accept with case. For this we use cmd_match. |
| 1726 | */ |
| 1727 | static int cmd_match(const char *cmd, const char *str) |
| 1728 | { |
| 1729 | /* See if cmd, written into a sysfs file, matches |
| 1730 | * str. They must either be the same, or cmd can |
| 1731 | * have a trailing newline |
| 1732 | */ |
| 1733 | while (*cmd && *str && *cmd == *str) { |
| 1734 | cmd++; |
| 1735 | str++; |
| 1736 | } |
| 1737 | if (*cmd == '\n') |
| 1738 | cmd++; |
| 1739 | if (*str || *cmd) |
| 1740 | return 0; |
| 1741 | return 1; |
| 1742 | } |
| 1743 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1744 | struct rdev_sysfs_entry { |
| 1745 | struct attribute attr; |
| 1746 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1747 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1748 | }; |
| 1749 | |
| 1750 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1751 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1752 | { |
| 1753 | char *sep = ""; |
| 1754 | int len=0; |
| 1755 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1756 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1757 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1758 | sep = ","; |
| 1759 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1760 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1761 | len += sprintf(page+len, "%sin_sync",sep); |
| 1762 | sep = ","; |
| 1763 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1764 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1765 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1766 | sep = ","; |
| 1767 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1768 | if (!test_bit(Faulty, &rdev->flags) && |
| 1769 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1770 | len += sprintf(page+len, "%sspare", sep); |
| 1771 | sep = ","; |
| 1772 | } |
| 1773 | return len+sprintf(page+len, "\n"); |
| 1774 | } |
| 1775 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1776 | static ssize_t |
| 1777 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1778 | { |
| 1779 | /* can write |
| 1780 | * faulty - simulates and error |
| 1781 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1782 | * writemostly - sets write_mostly |
| 1783 | * -writemostly - clears write_mostly |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1784 | */ |
| 1785 | int err = -EINVAL; |
| 1786 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1787 | md_error(rdev->mddev, rdev); |
| 1788 | err = 0; |
| 1789 | } else if (cmd_match(buf, "remove")) { |
| 1790 | if (rdev->raid_disk >= 0) |
| 1791 | err = -EBUSY; |
| 1792 | else { |
| 1793 | mddev_t *mddev = rdev->mddev; |
| 1794 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1795 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1796 | md_new_event(mddev); |
| 1797 | err = 0; |
| 1798 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1799 | } else if (cmd_match(buf, "writemostly")) { |
| 1800 | set_bit(WriteMostly, &rdev->flags); |
| 1801 | err = 0; |
| 1802 | } else if (cmd_match(buf, "-writemostly")) { |
| 1803 | clear_bit(WriteMostly, &rdev->flags); |
| 1804 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1805 | } |
| 1806 | return err ? err : len; |
| 1807 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1808 | static struct rdev_sysfs_entry rdev_state = |
| 1809 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1810 | |
| 1811 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1812 | super_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1813 | { |
| 1814 | if (rdev->sb_loaded && rdev->sb_size) { |
| 1815 | memcpy(page, page_address(rdev->sb_page), rdev->sb_size); |
| 1816 | return rdev->sb_size; |
| 1817 | } else |
| 1818 | return 0; |
| 1819 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1820 | static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super); |
| 1821 | |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1822 | static ssize_t |
| 1823 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1824 | { |
| 1825 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1826 | } |
| 1827 | |
| 1828 | static ssize_t |
| 1829 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1830 | { |
| 1831 | char *e; |
| 1832 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1833 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1834 | atomic_set(&rdev->corrected_errors, n); |
| 1835 | return len; |
| 1836 | } |
| 1837 | return -EINVAL; |
| 1838 | } |
| 1839 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1840 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1841 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1842 | static ssize_t |
| 1843 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1844 | { |
| 1845 | if (rdev->raid_disk < 0) |
| 1846 | return sprintf(page, "none\n"); |
| 1847 | else |
| 1848 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1849 | } |
| 1850 | |
| 1851 | static ssize_t |
| 1852 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1853 | { |
| 1854 | char *e; |
| 1855 | int slot = simple_strtoul(buf, &e, 10); |
| 1856 | if (strncmp(buf, "none", 4)==0) |
| 1857 | slot = -1; |
| 1858 | else if (e==buf || (*e && *e!= '\n')) |
| 1859 | return -EINVAL; |
| 1860 | if (rdev->mddev->pers) |
| 1861 | /* Cannot set slot in active array (yet) */ |
| 1862 | return -EBUSY; |
| 1863 | if (slot >= rdev->mddev->raid_disks) |
| 1864 | return -ENOSPC; |
| 1865 | rdev->raid_disk = slot; |
| 1866 | /* assume it is working */ |
| 1867 | rdev->flags = 0; |
| 1868 | set_bit(In_sync, &rdev->flags); |
| 1869 | return len; |
| 1870 | } |
| 1871 | |
| 1872 | |
| 1873 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1874 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1875 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1876 | static ssize_t |
| 1877 | offset_show(mdk_rdev_t *rdev, char *page) |
| 1878 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 1879 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | static ssize_t |
| 1883 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1884 | { |
| 1885 | char *e; |
| 1886 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 1887 | if (e==buf || (*e && *e != '\n')) |
| 1888 | return -EINVAL; |
| 1889 | if (rdev->mddev->pers) |
| 1890 | return -EBUSY; |
| 1891 | rdev->data_offset = offset; |
| 1892 | return len; |
| 1893 | } |
| 1894 | |
| 1895 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1896 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1897 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 1898 | static ssize_t |
| 1899 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 1900 | { |
| 1901 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 1902 | } |
| 1903 | |
| 1904 | static ssize_t |
| 1905 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1906 | { |
| 1907 | char *e; |
| 1908 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 1909 | if (e==buf || (*e && *e != '\n')) |
| 1910 | return -EINVAL; |
| 1911 | if (rdev->mddev->pers) |
| 1912 | return -EBUSY; |
| 1913 | rdev->size = size; |
| 1914 | if (size < rdev->mddev->size || rdev->mddev->size == 0) |
| 1915 | rdev->mddev->size = size; |
| 1916 | return len; |
| 1917 | } |
| 1918 | |
| 1919 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1920 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 1921 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1922 | static struct attribute *rdev_default_attrs[] = { |
| 1923 | &rdev_state.attr, |
| 1924 | &rdev_super.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1925 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1926 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1927 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 1928 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1929 | NULL, |
| 1930 | }; |
| 1931 | static ssize_t |
| 1932 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 1933 | { |
| 1934 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 1935 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
| 1936 | |
| 1937 | if (!entry->show) |
| 1938 | return -EIO; |
| 1939 | return entry->show(rdev, page); |
| 1940 | } |
| 1941 | |
| 1942 | static ssize_t |
| 1943 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 1944 | const char *page, size_t length) |
| 1945 | { |
| 1946 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 1947 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
| 1948 | |
| 1949 | if (!entry->store) |
| 1950 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 1951 | if (!capable(CAP_SYS_ADMIN)) |
| 1952 | return -EACCES; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1953 | return entry->store(rdev, page, length); |
| 1954 | } |
| 1955 | |
| 1956 | static void rdev_free(struct kobject *ko) |
| 1957 | { |
| 1958 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 1959 | kfree(rdev); |
| 1960 | } |
| 1961 | static struct sysfs_ops rdev_sysfs_ops = { |
| 1962 | .show = rdev_attr_show, |
| 1963 | .store = rdev_attr_store, |
| 1964 | }; |
| 1965 | static struct kobj_type rdev_ktype = { |
| 1966 | .release = rdev_free, |
| 1967 | .sysfs_ops = &rdev_sysfs_ops, |
| 1968 | .default_attrs = rdev_default_attrs, |
| 1969 | }; |
| 1970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | /* |
| 1972 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 1973 | * |
| 1974 | * mark the device faulty if: |
| 1975 | * |
| 1976 | * - the device is nonexistent (zero size) |
| 1977 | * - the device has no valid superblock |
| 1978 | * |
| 1979 | * a faulty rdev _never_ has rdev->sb set. |
| 1980 | */ |
| 1981 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 1982 | { |
| 1983 | char b[BDEVNAME_SIZE]; |
| 1984 | int err; |
| 1985 | mdk_rdev_t *rdev; |
| 1986 | sector_t size; |
| 1987 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 1988 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | if (!rdev) { |
| 1990 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 1991 | return ERR_PTR(-ENOMEM); |
| 1992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
| 1994 | if ((err = alloc_disk_sb(rdev))) |
| 1995 | goto abort_free; |
| 1996 | |
| 1997 | err = lock_rdev(rdev, newdev); |
| 1998 | if (err) |
| 1999 | goto abort_free; |
| 2000 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2001 | rdev->kobj.parent = NULL; |
| 2002 | rdev->kobj.ktype = &rdev_ktype; |
| 2003 | kobject_init(&rdev->kobj); |
| 2004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | rdev->desc_nr = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2006 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2008 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2010 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2011 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
| 2013 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2014 | if (!size) { |
| 2015 | printk(KERN_WARNING |
| 2016 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2017 | bdevname(rdev->bdev,b)); |
| 2018 | err = -EINVAL; |
| 2019 | goto abort_free; |
| 2020 | } |
| 2021 | |
| 2022 | if (super_format >= 0) { |
| 2023 | err = super_types[super_format]. |
| 2024 | load_super(rdev, NULL, super_minor); |
| 2025 | if (err == -EINVAL) { |
| 2026 | printk(KERN_WARNING |
| 2027 | "md: %s has invalid sb, not importing!\n", |
| 2028 | bdevname(rdev->bdev,b)); |
| 2029 | goto abort_free; |
| 2030 | } |
| 2031 | if (err < 0) { |
| 2032 | printk(KERN_WARNING |
| 2033 | "md: could not read %s's sb, not importing!\n", |
| 2034 | bdevname(rdev->bdev,b)); |
| 2035 | goto abort_free; |
| 2036 | } |
| 2037 | } |
| 2038 | INIT_LIST_HEAD(&rdev->same_set); |
| 2039 | |
| 2040 | return rdev; |
| 2041 | |
| 2042 | abort_free: |
| 2043 | if (rdev->sb_page) { |
| 2044 | if (rdev->bdev) |
| 2045 | unlock_rdev(rdev); |
| 2046 | free_disk_sb(rdev); |
| 2047 | } |
| 2048 | kfree(rdev); |
| 2049 | return ERR_PTR(err); |
| 2050 | } |
| 2051 | |
| 2052 | /* |
| 2053 | * Check a full RAID array for plausibility |
| 2054 | */ |
| 2055 | |
| 2056 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2057 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | { |
| 2059 | int i; |
| 2060 | struct list_head *tmp; |
| 2061 | mdk_rdev_t *rdev, *freshest; |
| 2062 | char b[BDEVNAME_SIZE]; |
| 2063 | |
| 2064 | freshest = NULL; |
| 2065 | ITERATE_RDEV(mddev,rdev,tmp) |
| 2066 | switch (super_types[mddev->major_version]. |
| 2067 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2068 | case 1: |
| 2069 | freshest = rdev; |
| 2070 | break; |
| 2071 | case 0: |
| 2072 | break; |
| 2073 | default: |
| 2074 | printk( KERN_ERR \ |
| 2075 | "md: fatal superblock inconsistency in %s" |
| 2076 | " -- removing from array\n", |
| 2077 | bdevname(rdev->bdev,b)); |
| 2078 | kick_rdev_from_array(rdev); |
| 2079 | } |
| 2080 | |
| 2081 | |
| 2082 | super_types[mddev->major_version]. |
| 2083 | validate_super(mddev, freshest); |
| 2084 | |
| 2085 | i = 0; |
| 2086 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 2087 | if (rdev != freshest) |
| 2088 | if (super_types[mddev->major_version]. |
| 2089 | validate_super(mddev, rdev)) { |
| 2090 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2091 | " from array!\n", |
| 2092 | bdevname(rdev->bdev,b)); |
| 2093 | kick_rdev_from_array(rdev); |
| 2094 | continue; |
| 2095 | } |
| 2096 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2097 | rdev->desc_nr = i++; |
| 2098 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2099 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | |
| 2104 | |
| 2105 | if (mddev->recovery_cp != MaxSector && |
| 2106 | mddev->level >= 1) |
| 2107 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2108 | " -- starting background reconstruction\n", |
| 2109 | mdname(mddev)); |
| 2110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2113 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2114 | safe_delay_show(mddev_t *mddev, char *page) |
| 2115 | { |
| 2116 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2117 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2118 | } |
| 2119 | static ssize_t |
| 2120 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2121 | { |
| 2122 | int scale=1; |
| 2123 | int dot=0; |
| 2124 | int i; |
| 2125 | unsigned long msec; |
| 2126 | char buf[30]; |
| 2127 | char *e; |
| 2128 | /* remove a period, and count digits after it */ |
| 2129 | if (len >= sizeof(buf)) |
| 2130 | return -EINVAL; |
| 2131 | strlcpy(buf, cbuf, len); |
| 2132 | buf[len] = 0; |
| 2133 | for (i=0; i<len; i++) { |
| 2134 | if (dot) { |
| 2135 | if (isdigit(buf[i])) { |
| 2136 | buf[i-1] = buf[i]; |
| 2137 | scale *= 10; |
| 2138 | } |
| 2139 | buf[i] = 0; |
| 2140 | } else if (buf[i] == '.') { |
| 2141 | dot=1; |
| 2142 | buf[i] = 0; |
| 2143 | } |
| 2144 | } |
| 2145 | msec = simple_strtoul(buf, &e, 10); |
| 2146 | if (e == buf || (*e && *e != '\n')) |
| 2147 | return -EINVAL; |
| 2148 | msec = (msec * 1000) / scale; |
| 2149 | if (msec == 0) |
| 2150 | mddev->safemode_delay = 0; |
| 2151 | else { |
| 2152 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2153 | if (mddev->safemode_delay == 0) |
| 2154 | mddev->safemode_delay = 1; |
| 2155 | } |
| 2156 | return len; |
| 2157 | } |
| 2158 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2159 | __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] | 2160 | |
| 2161 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2162 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2163 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2164 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2165 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2166 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2167 | else if (mddev->clevel[0]) |
| 2168 | return sprintf(page, "%s\n", mddev->clevel); |
| 2169 | else if (mddev->level != LEVEL_NONE) |
| 2170 | return sprintf(page, "%d\n", mddev->level); |
| 2171 | else |
| 2172 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2175 | static ssize_t |
| 2176 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2177 | { |
| 2178 | int rv = len; |
| 2179 | if (mddev->pers) |
| 2180 | return -EBUSY; |
| 2181 | if (len == 0) |
| 2182 | return 0; |
| 2183 | if (len >= sizeof(mddev->clevel)) |
| 2184 | return -ENOSPC; |
| 2185 | strncpy(mddev->clevel, buf, len); |
| 2186 | if (mddev->clevel[len-1] == '\n') |
| 2187 | len--; |
| 2188 | mddev->clevel[len] = 0; |
| 2189 | mddev->level = LEVEL_NONE; |
| 2190 | return rv; |
| 2191 | } |
| 2192 | |
| 2193 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2194 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2195 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2196 | |
| 2197 | static ssize_t |
| 2198 | layout_show(mddev_t *mddev, char *page) |
| 2199 | { |
| 2200 | /* just a number, not meaningful for all levels */ |
| 2201 | return sprintf(page, "%d\n", mddev->layout); |
| 2202 | } |
| 2203 | |
| 2204 | static ssize_t |
| 2205 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2206 | { |
| 2207 | char *e; |
| 2208 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2209 | if (mddev->pers) |
| 2210 | return -EBUSY; |
| 2211 | |
| 2212 | if (!*buf || (*e && *e != '\n')) |
| 2213 | return -EINVAL; |
| 2214 | |
| 2215 | mddev->layout = n; |
| 2216 | return len; |
| 2217 | } |
| 2218 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2219 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2220 | |
| 2221 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2222 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2223 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2224 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2225 | if (mddev->raid_disks == 0) |
| 2226 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2227 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2228 | } |
| 2229 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2230 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2231 | |
| 2232 | static ssize_t |
| 2233 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2234 | { |
| 2235 | /* can only set raid_disks if array is not yet active */ |
| 2236 | char *e; |
| 2237 | int rv = 0; |
| 2238 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2239 | |
| 2240 | if (!*buf || (*e && *e != '\n')) |
| 2241 | return -EINVAL; |
| 2242 | |
| 2243 | if (mddev->pers) |
| 2244 | rv = update_raid_disks(mddev, n); |
| 2245 | else |
| 2246 | mddev->raid_disks = n; |
| 2247 | return rv ? rv : len; |
| 2248 | } |
| 2249 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2250 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2251 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2252 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2253 | chunk_size_show(mddev_t *mddev, char *page) |
| 2254 | { |
| 2255 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2256 | } |
| 2257 | |
| 2258 | static ssize_t |
| 2259 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2260 | { |
| 2261 | /* can only set chunk_size if array is not yet active */ |
| 2262 | char *e; |
| 2263 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2264 | |
| 2265 | if (mddev->pers) |
| 2266 | return -EBUSY; |
| 2267 | if (!*buf || (*e && *e != '\n')) |
| 2268 | return -EINVAL; |
| 2269 | |
| 2270 | mddev->chunk_size = n; |
| 2271 | return len; |
| 2272 | } |
| 2273 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2274 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2275 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2276 | static ssize_t |
| 2277 | resync_start_show(mddev_t *mddev, char *page) |
| 2278 | { |
| 2279 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2280 | } |
| 2281 | |
| 2282 | static ssize_t |
| 2283 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2284 | { |
| 2285 | /* can only set chunk_size if array is not yet active */ |
| 2286 | char *e; |
| 2287 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2288 | |
| 2289 | if (mddev->pers) |
| 2290 | return -EBUSY; |
| 2291 | if (!*buf || (*e && *e != '\n')) |
| 2292 | return -EINVAL; |
| 2293 | |
| 2294 | mddev->recovery_cp = n; |
| 2295 | return len; |
| 2296 | } |
| 2297 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2298 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2299 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2300 | /* |
| 2301 | * The array state can be: |
| 2302 | * |
| 2303 | * clear |
| 2304 | * No devices, no size, no level |
| 2305 | * Equivalent to STOP_ARRAY ioctl |
| 2306 | * inactive |
| 2307 | * May have some settings, but array is not active |
| 2308 | * all IO results in error |
| 2309 | * When written, doesn't tear down array, but just stops it |
| 2310 | * suspended (not supported yet) |
| 2311 | * All IO requests will block. The array can be reconfigured. |
| 2312 | * Writing this, if accepted, will block until array is quiessent |
| 2313 | * readonly |
| 2314 | * no resync can happen. no superblocks get written. |
| 2315 | * write requests fail |
| 2316 | * read-auto |
| 2317 | * like readonly, but behaves like 'clean' on a write request. |
| 2318 | * |
| 2319 | * clean - no pending writes, but otherwise active. |
| 2320 | * When written to inactive array, starts without resync |
| 2321 | * If a write request arrives then |
| 2322 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2323 | * if not known, block and switch to write-pending |
| 2324 | * If written to an active array that has pending writes, then fails. |
| 2325 | * active |
| 2326 | * fully active: IO and resync can be happening. |
| 2327 | * When written to inactive array, starts with resync |
| 2328 | * |
| 2329 | * write-pending |
| 2330 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2331 | * |
| 2332 | * active-idle |
| 2333 | * like active, but no writes have been seen for a while (100msec). |
| 2334 | * |
| 2335 | */ |
| 2336 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2337 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2338 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2339 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2340 | "write-pending", "active-idle", NULL }; |
| 2341 | |
| 2342 | static int match_word(const char *word, char **list) |
| 2343 | { |
| 2344 | int n; |
| 2345 | for (n=0; list[n]; n++) |
| 2346 | if (cmd_match(word, list[n])) |
| 2347 | break; |
| 2348 | return n; |
| 2349 | } |
| 2350 | |
| 2351 | static ssize_t |
| 2352 | array_state_show(mddev_t *mddev, char *page) |
| 2353 | { |
| 2354 | enum array_state st = inactive; |
| 2355 | |
| 2356 | if (mddev->pers) |
| 2357 | switch(mddev->ro) { |
| 2358 | case 1: |
| 2359 | st = readonly; |
| 2360 | break; |
| 2361 | case 2: |
| 2362 | st = read_auto; |
| 2363 | break; |
| 2364 | case 0: |
| 2365 | if (mddev->in_sync) |
| 2366 | st = clean; |
| 2367 | else if (mddev->safemode) |
| 2368 | st = active_idle; |
| 2369 | else |
| 2370 | st = active; |
| 2371 | } |
| 2372 | else { |
| 2373 | if (list_empty(&mddev->disks) && |
| 2374 | mddev->raid_disks == 0 && |
| 2375 | mddev->size == 0) |
| 2376 | st = clear; |
| 2377 | else |
| 2378 | st = inactive; |
| 2379 | } |
| 2380 | return sprintf(page, "%s\n", array_states[st]); |
| 2381 | } |
| 2382 | |
| 2383 | static int do_md_stop(mddev_t * mddev, int ro); |
| 2384 | static int do_md_run(mddev_t * mddev); |
| 2385 | static int restart_array(mddev_t *mddev); |
| 2386 | |
| 2387 | static ssize_t |
| 2388 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2389 | { |
| 2390 | int err = -EINVAL; |
| 2391 | enum array_state st = match_word(buf, array_states); |
| 2392 | switch(st) { |
| 2393 | case bad_word: |
| 2394 | break; |
| 2395 | case clear: |
| 2396 | /* stopping an active array */ |
| 2397 | if (mddev->pers) { |
| 2398 | if (atomic_read(&mddev->active) > 1) |
| 2399 | return -EBUSY; |
| 2400 | err = do_md_stop(mddev, 0); |
| 2401 | } |
| 2402 | break; |
| 2403 | case inactive: |
| 2404 | /* stopping an active array */ |
| 2405 | if (mddev->pers) { |
| 2406 | if (atomic_read(&mddev->active) > 1) |
| 2407 | return -EBUSY; |
| 2408 | err = do_md_stop(mddev, 2); |
| 2409 | } |
| 2410 | break; |
| 2411 | case suspended: |
| 2412 | break; /* not supported yet */ |
| 2413 | case readonly: |
| 2414 | if (mddev->pers) |
| 2415 | err = do_md_stop(mddev, 1); |
| 2416 | else { |
| 2417 | mddev->ro = 1; |
| 2418 | err = do_md_run(mddev); |
| 2419 | } |
| 2420 | break; |
| 2421 | case read_auto: |
| 2422 | /* stopping an active array */ |
| 2423 | if (mddev->pers) { |
| 2424 | err = do_md_stop(mddev, 1); |
| 2425 | if (err == 0) |
| 2426 | mddev->ro = 2; /* FIXME mark devices writable */ |
| 2427 | } else { |
| 2428 | mddev->ro = 2; |
| 2429 | err = do_md_run(mddev); |
| 2430 | } |
| 2431 | break; |
| 2432 | case clean: |
| 2433 | if (mddev->pers) { |
| 2434 | restart_array(mddev); |
| 2435 | spin_lock_irq(&mddev->write_lock); |
| 2436 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 2437 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2438 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2439 | } |
| 2440 | spin_unlock_irq(&mddev->write_lock); |
| 2441 | } else { |
| 2442 | mddev->ro = 0; |
| 2443 | mddev->recovery_cp = MaxSector; |
| 2444 | err = do_md_run(mddev); |
| 2445 | } |
| 2446 | break; |
| 2447 | case active: |
| 2448 | if (mddev->pers) { |
| 2449 | restart_array(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2450 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2451 | wake_up(&mddev->sb_wait); |
| 2452 | err = 0; |
| 2453 | } else { |
| 2454 | mddev->ro = 0; |
| 2455 | err = do_md_run(mddev); |
| 2456 | } |
| 2457 | break; |
| 2458 | case write_pending: |
| 2459 | case active_idle: |
| 2460 | /* these cannot be set */ |
| 2461 | break; |
| 2462 | } |
| 2463 | if (err) |
| 2464 | return err; |
| 2465 | else |
| 2466 | return len; |
| 2467 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2468 | static struct md_sysfs_entry md_array_state = |
| 2469 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2470 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2471 | static ssize_t |
| 2472 | null_show(mddev_t *mddev, char *page) |
| 2473 | { |
| 2474 | return -EINVAL; |
| 2475 | } |
| 2476 | |
| 2477 | static ssize_t |
| 2478 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2479 | { |
| 2480 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2481 | /* The new device is added to the array. |
| 2482 | * If the array has a persistent superblock, we read the |
| 2483 | * superblock to initialise info and check validity. |
| 2484 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2485 | * which mainly checks size. |
| 2486 | */ |
| 2487 | char *e; |
| 2488 | int major = simple_strtoul(buf, &e, 10); |
| 2489 | int minor; |
| 2490 | dev_t dev; |
| 2491 | mdk_rdev_t *rdev; |
| 2492 | int err; |
| 2493 | |
| 2494 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2495 | return -EINVAL; |
| 2496 | minor = simple_strtoul(e+1, &e, 10); |
| 2497 | if (*e && *e != '\n') |
| 2498 | return -EINVAL; |
| 2499 | dev = MKDEV(major, minor); |
| 2500 | if (major != MAJOR(dev) || |
| 2501 | minor != MINOR(dev)) |
| 2502 | return -EOVERFLOW; |
| 2503 | |
| 2504 | |
| 2505 | if (mddev->persistent) { |
| 2506 | rdev = md_import_device(dev, mddev->major_version, |
| 2507 | mddev->minor_version); |
| 2508 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2509 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2510 | mdk_rdev_t, same_set); |
| 2511 | err = super_types[mddev->major_version] |
| 2512 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2513 | if (err < 0) |
| 2514 | goto out; |
| 2515 | } |
| 2516 | } else |
| 2517 | rdev = md_import_device(dev, -1, -1); |
| 2518 | |
| 2519 | if (IS_ERR(rdev)) |
| 2520 | return PTR_ERR(rdev); |
| 2521 | err = bind_rdev_to_array(rdev, mddev); |
| 2522 | out: |
| 2523 | if (err) |
| 2524 | export_rdev(rdev); |
| 2525 | return err ? err : len; |
| 2526 | } |
| 2527 | |
| 2528 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2529 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2530 | |
| 2531 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2532 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2533 | { |
| 2534 | char *end; |
| 2535 | unsigned long chunk, end_chunk; |
| 2536 | |
| 2537 | if (!mddev->bitmap) |
| 2538 | goto out; |
| 2539 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2540 | while (*buf) { |
| 2541 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2542 | if (buf == end) break; |
| 2543 | if (*end == '-') { /* range */ |
| 2544 | buf = end + 1; |
| 2545 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2546 | if (buf == end) break; |
| 2547 | } |
| 2548 | if (*end && !isspace(*end)) break; |
| 2549 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2550 | buf = end; |
| 2551 | while (isspace(*buf)) buf++; |
| 2552 | } |
| 2553 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2554 | out: |
| 2555 | return len; |
| 2556 | } |
| 2557 | |
| 2558 | static struct md_sysfs_entry md_bitmap = |
| 2559 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2560 | |
| 2561 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2562 | size_show(mddev_t *mddev, char *page) |
| 2563 | { |
| 2564 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2565 | } |
| 2566 | |
| 2567 | static int update_size(mddev_t *mddev, unsigned long size); |
| 2568 | |
| 2569 | static ssize_t |
| 2570 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2571 | { |
| 2572 | /* If array is inactive, we can reduce the component size, but |
| 2573 | * not increase it (except from 0). |
| 2574 | * If array is active, we can try an on-line resize |
| 2575 | */ |
| 2576 | char *e; |
| 2577 | int err = 0; |
| 2578 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2579 | if (!*buf || *buf == '\n' || |
| 2580 | (*e && *e != '\n')) |
| 2581 | return -EINVAL; |
| 2582 | |
| 2583 | if (mddev->pers) { |
| 2584 | err = update_size(mddev, size); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2585 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2586 | } else { |
| 2587 | if (mddev->size == 0 || |
| 2588 | mddev->size > size) |
| 2589 | mddev->size = size; |
| 2590 | else |
| 2591 | err = -ENOSPC; |
| 2592 | } |
| 2593 | return err ? err : len; |
| 2594 | } |
| 2595 | |
| 2596 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2597 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2598 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2599 | |
| 2600 | /* Metdata version. |
| 2601 | * This is either 'none' for arrays with externally managed metadata, |
| 2602 | * or N.M for internally known formats |
| 2603 | */ |
| 2604 | static ssize_t |
| 2605 | metadata_show(mddev_t *mddev, char *page) |
| 2606 | { |
| 2607 | if (mddev->persistent) |
| 2608 | return sprintf(page, "%d.%d\n", |
| 2609 | mddev->major_version, mddev->minor_version); |
| 2610 | else |
| 2611 | return sprintf(page, "none\n"); |
| 2612 | } |
| 2613 | |
| 2614 | static ssize_t |
| 2615 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2616 | { |
| 2617 | int major, minor; |
| 2618 | char *e; |
| 2619 | if (!list_empty(&mddev->disks)) |
| 2620 | return -EBUSY; |
| 2621 | |
| 2622 | if (cmd_match(buf, "none")) { |
| 2623 | mddev->persistent = 0; |
| 2624 | mddev->major_version = 0; |
| 2625 | mddev->minor_version = 90; |
| 2626 | return len; |
| 2627 | } |
| 2628 | major = simple_strtoul(buf, &e, 10); |
| 2629 | if (e==buf || *e != '.') |
| 2630 | return -EINVAL; |
| 2631 | buf = e+1; |
| 2632 | minor = simple_strtoul(buf, &e, 10); |
| 2633 | if (e==buf || *e != '\n') |
| 2634 | return -EINVAL; |
| 2635 | if (major >= sizeof(super_types)/sizeof(super_types[0]) || |
| 2636 | super_types[major].name == NULL) |
| 2637 | return -ENOENT; |
| 2638 | mddev->major_version = major; |
| 2639 | mddev->minor_version = minor; |
| 2640 | mddev->persistent = 1; |
| 2641 | return len; |
| 2642 | } |
| 2643 | |
| 2644 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2645 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2646 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2647 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2648 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2649 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2650 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2651 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2652 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2653 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2654 | type = "reshape"; |
| 2655 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2656 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2657 | type = "resync"; |
| 2658 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2659 | type = "check"; |
| 2660 | else |
| 2661 | type = "repair"; |
| 2662 | } else |
| 2663 | type = "recover"; |
| 2664 | } |
| 2665 | return sprintf(page, "%s\n", type); |
| 2666 | } |
| 2667 | |
| 2668 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2669 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2670 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2671 | if (!mddev->pers || !mddev->pers->sync_request) |
| 2672 | return -EINVAL; |
| 2673 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2674 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2675 | if (mddev->sync_thread) { |
| 2676 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2677 | md_unregister_thread(mddev->sync_thread); |
| 2678 | mddev->sync_thread = NULL; |
| 2679 | mddev->recovery = 0; |
| 2680 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2681 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2682 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2683 | return -EBUSY; |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2684 | else if (cmd_match(page, "resync") || cmd_match(page, "recover")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2685 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2686 | else if (cmd_match(page, "reshape")) { |
| 2687 | int err; |
| 2688 | if (mddev->pers->start_reshape == NULL) |
| 2689 | return -EINVAL; |
| 2690 | err = mddev->pers->start_reshape(mddev); |
| 2691 | if (err) |
| 2692 | return err; |
| 2693 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2694 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2695 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 2696 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2697 | return -EINVAL; |
| 2698 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 2699 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2700 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2701 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2702 | md_wakeup_thread(mddev->thread); |
| 2703 | return len; |
| 2704 | } |
| 2705 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2706 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2707 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2708 | { |
| 2709 | return sprintf(page, "%llu\n", |
| 2710 | (unsigned long long) mddev->resync_mismatches); |
| 2711 | } |
| 2712 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2713 | static struct md_sysfs_entry md_scan_mode = |
| 2714 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2715 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2716 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2717 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2718 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2719 | static ssize_t |
| 2720 | sync_min_show(mddev_t *mddev, char *page) |
| 2721 | { |
| 2722 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 2723 | mddev->sync_speed_min ? "local": "system"); |
| 2724 | } |
| 2725 | |
| 2726 | static ssize_t |
| 2727 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 2728 | { |
| 2729 | int min; |
| 2730 | char *e; |
| 2731 | if (strncmp(buf, "system", 6)==0) { |
| 2732 | mddev->sync_speed_min = 0; |
| 2733 | return len; |
| 2734 | } |
| 2735 | min = simple_strtoul(buf, &e, 10); |
| 2736 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 2737 | return -EINVAL; |
| 2738 | mddev->sync_speed_min = min; |
| 2739 | return len; |
| 2740 | } |
| 2741 | |
| 2742 | static struct md_sysfs_entry md_sync_min = |
| 2743 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 2744 | |
| 2745 | static ssize_t |
| 2746 | sync_max_show(mddev_t *mddev, char *page) |
| 2747 | { |
| 2748 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 2749 | mddev->sync_speed_max ? "local": "system"); |
| 2750 | } |
| 2751 | |
| 2752 | static ssize_t |
| 2753 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 2754 | { |
| 2755 | int max; |
| 2756 | char *e; |
| 2757 | if (strncmp(buf, "system", 6)==0) { |
| 2758 | mddev->sync_speed_max = 0; |
| 2759 | return len; |
| 2760 | } |
| 2761 | max = simple_strtoul(buf, &e, 10); |
| 2762 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 2763 | return -EINVAL; |
| 2764 | mddev->sync_speed_max = max; |
| 2765 | return len; |
| 2766 | } |
| 2767 | |
| 2768 | static struct md_sysfs_entry md_sync_max = |
| 2769 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 2770 | |
| 2771 | |
| 2772 | static ssize_t |
| 2773 | sync_speed_show(mddev_t *mddev, char *page) |
| 2774 | { |
| 2775 | unsigned long resync, dt, db; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 2776 | resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2777 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 2778 | if (!dt) dt++; |
| 2779 | db = resync - (mddev->resync_mark_cnt); |
| 2780 | return sprintf(page, "%ld\n", db/dt/2); /* K/sec */ |
| 2781 | } |
| 2782 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2783 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2784 | |
| 2785 | static ssize_t |
| 2786 | sync_completed_show(mddev_t *mddev, char *page) |
| 2787 | { |
| 2788 | unsigned long max_blocks, resync; |
| 2789 | |
| 2790 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 2791 | max_blocks = mddev->resync_max_sectors; |
| 2792 | else |
| 2793 | max_blocks = mddev->size << 1; |
| 2794 | |
| 2795 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 2796 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 2797 | } |
| 2798 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2799 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2800 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 2801 | static ssize_t |
| 2802 | suspend_lo_show(mddev_t *mddev, char *page) |
| 2803 | { |
| 2804 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 2805 | } |
| 2806 | |
| 2807 | static ssize_t |
| 2808 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 2809 | { |
| 2810 | char *e; |
| 2811 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 2812 | |
| 2813 | if (mddev->pers->quiesce == NULL) |
| 2814 | return -EINVAL; |
| 2815 | if (buf == e || (*e && *e != '\n')) |
| 2816 | return -EINVAL; |
| 2817 | if (new >= mddev->suspend_hi || |
| 2818 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 2819 | mddev->suspend_lo = new; |
| 2820 | mddev->pers->quiesce(mddev, 2); |
| 2821 | return len; |
| 2822 | } else |
| 2823 | return -EINVAL; |
| 2824 | } |
| 2825 | static struct md_sysfs_entry md_suspend_lo = |
| 2826 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 2827 | |
| 2828 | |
| 2829 | static ssize_t |
| 2830 | suspend_hi_show(mddev_t *mddev, char *page) |
| 2831 | { |
| 2832 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 2833 | } |
| 2834 | |
| 2835 | static ssize_t |
| 2836 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 2837 | { |
| 2838 | char *e; |
| 2839 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 2840 | |
| 2841 | if (mddev->pers->quiesce == NULL) |
| 2842 | return -EINVAL; |
| 2843 | if (buf == e || (*e && *e != '\n')) |
| 2844 | return -EINVAL; |
| 2845 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 2846 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 2847 | mddev->suspend_hi = new; |
| 2848 | mddev->pers->quiesce(mddev, 1); |
| 2849 | mddev->pers->quiesce(mddev, 0); |
| 2850 | return len; |
| 2851 | } else |
| 2852 | return -EINVAL; |
| 2853 | } |
| 2854 | static struct md_sysfs_entry md_suspend_hi = |
| 2855 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 2856 | |
| 2857 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2858 | static struct attribute *md_default_attrs[] = { |
| 2859 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2860 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2861 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2862 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2863 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2864 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2865 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2866 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2867 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2868 | &md_array_state.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 2869 | NULL, |
| 2870 | }; |
| 2871 | |
| 2872 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2873 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2874 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2875 | &md_sync_min.attr, |
| 2876 | &md_sync_max.attr, |
| 2877 | &md_sync_speed.attr, |
| 2878 | &md_sync_completed.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 2879 | &md_suspend_lo.attr, |
| 2880 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2881 | &md_bitmap.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2882 | NULL, |
| 2883 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 2884 | static struct attribute_group md_redundancy_group = { |
| 2885 | .name = NULL, |
| 2886 | .attrs = md_redundancy_attrs, |
| 2887 | }; |
| 2888 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2889 | |
| 2890 | static ssize_t |
| 2891 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2892 | { |
| 2893 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 2894 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2895 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2896 | |
| 2897 | if (!entry->show) |
| 2898 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 2899 | rv = mddev_lock(mddev); |
| 2900 | if (!rv) { |
| 2901 | rv = entry->show(mddev, page); |
| 2902 | mddev_unlock(mddev); |
| 2903 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2904 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | static ssize_t |
| 2908 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2909 | const char *page, size_t length) |
| 2910 | { |
| 2911 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 2912 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2913 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2914 | |
| 2915 | if (!entry->store) |
| 2916 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2917 | if (!capable(CAP_SYS_ADMIN)) |
| 2918 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 2919 | rv = mddev_lock(mddev); |
| 2920 | if (!rv) { |
| 2921 | rv = entry->store(mddev, page, length); |
| 2922 | mddev_unlock(mddev); |
| 2923 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2924 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | static void md_free(struct kobject *ko) |
| 2928 | { |
| 2929 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 2930 | kfree(mddev); |
| 2931 | } |
| 2932 | |
| 2933 | static struct sysfs_ops md_sysfs_ops = { |
| 2934 | .show = md_attr_show, |
| 2935 | .store = md_attr_store, |
| 2936 | }; |
| 2937 | static struct kobj_type md_ktype = { |
| 2938 | .release = md_free, |
| 2939 | .sysfs_ops = &md_sysfs_ops, |
| 2940 | .default_attrs = md_default_attrs, |
| 2941 | }; |
| 2942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | int mdp_major = 0; |
| 2944 | |
| 2945 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 2946 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2947 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | mddev_t *mddev = mddev_find(dev); |
| 2949 | struct gendisk *disk; |
| 2950 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 2951 | int shift = partitioned ? MdpMinorShift : 0; |
| 2952 | int unit = MINOR(dev) >> shift; |
| 2953 | |
| 2954 | if (!mddev) |
| 2955 | return NULL; |
| 2956 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2957 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2959 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | mddev_put(mddev); |
| 2961 | return NULL; |
| 2962 | } |
| 2963 | disk = alloc_disk(1 << shift); |
| 2964 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2965 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | mddev_put(mddev); |
| 2967 | return NULL; |
| 2968 | } |
| 2969 | disk->major = MAJOR(dev); |
| 2970 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2971 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2973 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | disk->fops = &md_fops; |
| 2976 | disk->private_data = mddev; |
| 2977 | disk->queue = mddev->queue; |
| 2978 | add_disk(disk); |
| 2979 | mddev->gendisk = disk; |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2980 | mutex_unlock(&disks_mutex); |
NeilBrown | 9c79197 | 2005-11-08 21:39:28 -0800 | [diff] [blame] | 2981 | mddev->kobj.parent = &disk->kobj; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2982 | mddev->kobj.k_name = NULL; |
| 2983 | snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md"); |
| 2984 | mddev->kobj.ktype = &md_ktype; |
| 2985 | kobject_register(&mddev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | return NULL; |
| 2987 | } |
| 2988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | static void md_safemode_timeout(unsigned long data) |
| 2990 | { |
| 2991 | mddev_t *mddev = (mddev_t *) data; |
| 2992 | |
| 2993 | mddev->safemode = 1; |
| 2994 | md_wakeup_thread(mddev->thread); |
| 2995 | } |
| 2996 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 2997 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | |
| 2999 | static int do_md_run(mddev_t * mddev) |
| 3000 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3001 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | int chunk_size; |
| 3003 | struct list_head *tmp; |
| 3004 | mdk_rdev_t *rdev; |
| 3005 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3006 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3007 | char b[BDEVNAME_SIZE]; |
| 3008 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3009 | if (list_empty(&mddev->disks)) |
| 3010 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | |
| 3013 | if (mddev->pers) |
| 3014 | return -EBUSY; |
| 3015 | |
| 3016 | /* |
| 3017 | * Analyze all RAID superblock(s) |
| 3018 | */ |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3019 | if (!mddev->raid_disks) |
| 3020 | analyze_sbs(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | |
| 3022 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3024 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3026 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3027 | chunk_size, MAX_CHUNK_SIZE); |
| 3028 | return -EINVAL; |
| 3029 | } |
| 3030 | /* |
| 3031 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3032 | */ |
| 3033 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3034 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | return -EINVAL; |
| 3036 | } |
| 3037 | if (chunk_size < PAGE_SIZE) { |
| 3038 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3039 | chunk_size, PAGE_SIZE); |
| 3040 | return -EINVAL; |
| 3041 | } |
| 3042 | |
| 3043 | /* devices must have minimum size of one chunk */ |
| 3044 | ITERATE_RDEV(mddev,rdev,tmp) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3045 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | continue; |
| 3047 | if (rdev->size < chunk_size / 1024) { |
| 3048 | printk(KERN_WARNING |
| 3049 | "md: Dev %s smaller than chunk_size:" |
| 3050 | " %lluk < %dk\n", |
| 3051 | bdevname(rdev->bdev,b), |
| 3052 | (unsigned long long)rdev->size, |
| 3053 | chunk_size / 1024); |
| 3054 | return -EINVAL; |
| 3055 | } |
| 3056 | } |
| 3057 | } |
| 3058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3060 | if (mddev->level != LEVEL_NONE) |
| 3061 | request_module("md-level-%d", mddev->level); |
| 3062 | else if (mddev->clevel[0]) |
| 3063 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | #endif |
| 3065 | |
| 3066 | /* |
| 3067 | * Drop all container device buffers, from now on |
| 3068 | * the only valid external interface is through the md |
| 3069 | * device. |
| 3070 | * Also find largest hardsector size |
| 3071 | */ |
| 3072 | ITERATE_RDEV(mddev,rdev,tmp) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3073 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3074 | continue; |
| 3075 | sync_blockdev(rdev->bdev); |
| 3076 | invalidate_bdev(rdev->bdev, 0); |
| 3077 | } |
| 3078 | |
| 3079 | md_probe(mddev->unit, NULL, NULL); |
| 3080 | disk = mddev->gendisk; |
| 3081 | if (!disk) |
| 3082 | return -ENOMEM; |
| 3083 | |
| 3084 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3085 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3086 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3088 | if (mddev->level != LEVEL_NONE) |
| 3089 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3090 | mddev->level); |
| 3091 | else |
| 3092 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3093 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | return -EINVAL; |
| 3095 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3096 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3098 | mddev->level = pers->level; |
| 3099 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3101 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3102 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3103 | /* This personality cannot handle reshaping... */ |
| 3104 | mddev->pers = NULL; |
| 3105 | module_put(pers->owner); |
| 3106 | return -EINVAL; |
| 3107 | } |
| 3108 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3109 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3111 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3112 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3114 | if (start_readonly) |
| 3115 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3116 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3117 | err = mddev->pers->run(mddev); |
| 3118 | if (!err && mddev->pers->sync_request) { |
| 3119 | err = bitmap_create(mddev); |
| 3120 | if (err) { |
| 3121 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3122 | mdname(mddev), err); |
| 3123 | mddev->pers->stop(mddev); |
| 3124 | } |
| 3125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | if (err) { |
| 3127 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3128 | module_put(mddev->pers->owner); |
| 3129 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3130 | bitmap_destroy(mddev); |
| 3131 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3132 | } |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3133 | if (mddev->pers->sync_request) |
| 3134 | sysfs_create_group(&mddev->kobj, &md_redundancy_group); |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3135 | else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
| 3136 | mddev->ro = 0; |
| 3137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | atomic_set(&mddev->writes_pending,0); |
| 3139 | mddev->safemode = 0; |
| 3140 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3141 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3142 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3144 | |
| 3145 | ITERATE_RDEV(mddev,rdev,tmp) |
| 3146 | if (rdev->raid_disk >= 0) { |
| 3147 | char nm[20]; |
| 3148 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3149 | sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); |
| 3150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | |
| 3152 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3153 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3154 | if (mddev->flags) |
| 3155 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | |
| 3157 | set_capacity(disk, mddev->array_size<<1); |
| 3158 | |
| 3159 | /* If we call blk_queue_make_request here, it will |
| 3160 | * re-initialise max_sectors etc which may have been |
| 3161 | * refined inside -> run. So just set the bits we need to set. |
| 3162 | * Most initialisation happended when we called |
| 3163 | * blk_queue_make_request(..., md_fail_request) |
| 3164 | * earlier. |
| 3165 | */ |
| 3166 | mddev->queue->queuedata = mddev; |
| 3167 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3168 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3169 | /* If there is a partially-recovered drive we need to |
| 3170 | * start recovery here. If we leave it to md_check_recovery, |
| 3171 | * it will remove the drives and not do the right thing |
| 3172 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3173 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3174 | struct list_head *rtmp; |
| 3175 | int spares = 0; |
| 3176 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 3177 | if (rdev->raid_disk >= 0 && |
| 3178 | !test_bit(In_sync, &rdev->flags) && |
| 3179 | !test_bit(Faulty, &rdev->flags)) |
| 3180 | /* complete an interrupted recovery */ |
| 3181 | spares++; |
| 3182 | if (spares && mddev->pers->sync_request) { |
| 3183 | mddev->recovery = 0; |
| 3184 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3185 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3186 | mddev, |
| 3187 | "%s_resync"); |
| 3188 | if (!mddev->sync_thread) { |
| 3189 | printk(KERN_ERR "%s: could not start resync" |
| 3190 | " thread...\n", |
| 3191 | mdname(mddev)); |
| 3192 | /* leave the spares where they are, it shouldn't hurt */ |
| 3193 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3194 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3195 | } |
| 3196 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3197 | md_wakeup_thread(mddev->thread); |
| 3198 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3201 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | return 0; |
| 3203 | } |
| 3204 | |
| 3205 | static int restart_array(mddev_t *mddev) |
| 3206 | { |
| 3207 | struct gendisk *disk = mddev->gendisk; |
| 3208 | int err; |
| 3209 | |
| 3210 | /* |
| 3211 | * Complain if it has no devices |
| 3212 | */ |
| 3213 | err = -ENXIO; |
| 3214 | if (list_empty(&mddev->disks)) |
| 3215 | goto out; |
| 3216 | |
| 3217 | if (mddev->pers) { |
| 3218 | err = -EBUSY; |
| 3219 | if (!mddev->ro) |
| 3220 | goto out; |
| 3221 | |
| 3222 | mddev->safemode = 0; |
| 3223 | mddev->ro = 0; |
| 3224 | set_disk_ro(disk, 0); |
| 3225 | |
| 3226 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3227 | mdname(mddev)); |
| 3228 | /* |
| 3229 | * Kick recovery or resync if necessary |
| 3230 | */ |
| 3231 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3232 | md_wakeup_thread(mddev->thread); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3233 | md_wakeup_thread(mddev->sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3235 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | |
| 3238 | out: |
| 3239 | return err; |
| 3240 | } |
| 3241 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3242 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3243 | * to the file ourselves */ |
| 3244 | static int deny_bitmap_write_access(struct file * file) |
| 3245 | { |
| 3246 | struct inode *inode = file->f_mapping->host; |
| 3247 | |
| 3248 | spin_lock(&inode->i_lock); |
| 3249 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3250 | spin_unlock(&inode->i_lock); |
| 3251 | return -ETXTBSY; |
| 3252 | } |
| 3253 | atomic_set(&inode->i_writecount, -1); |
| 3254 | spin_unlock(&inode->i_lock); |
| 3255 | |
| 3256 | return 0; |
| 3257 | } |
| 3258 | |
| 3259 | static void restore_bitmap_write_access(struct file *file) |
| 3260 | { |
| 3261 | struct inode *inode = file->f_mapping->host; |
| 3262 | |
| 3263 | spin_lock(&inode->i_lock); |
| 3264 | atomic_set(&inode->i_writecount, 1); |
| 3265 | spin_unlock(&inode->i_lock); |
| 3266 | } |
| 3267 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3268 | /* mode: |
| 3269 | * 0 - completely stop and dis-assemble array |
| 3270 | * 1 - switch to readonly |
| 3271 | * 2 - stop but do not disassemble array |
| 3272 | */ |
| 3273 | static int do_md_stop(mddev_t * mddev, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | { |
| 3275 | int err = 0; |
| 3276 | struct gendisk *disk = mddev->gendisk; |
| 3277 | |
| 3278 | if (mddev->pers) { |
| 3279 | if (atomic_read(&mddev->active)>2) { |
| 3280 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3281 | return -EBUSY; |
| 3282 | } |
| 3283 | |
| 3284 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3285 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3287 | md_unregister_thread(mddev->sync_thread); |
| 3288 | mddev->sync_thread = NULL; |
| 3289 | } |
| 3290 | |
| 3291 | del_timer_sync(&mddev->safemode_timer); |
| 3292 | |
| 3293 | invalidate_partition(disk, 0); |
| 3294 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3295 | switch(mode) { |
| 3296 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3298 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3299 | goto out; |
| 3300 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3301 | break; |
| 3302 | case 0: /* disassemble */ |
| 3303 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3304 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3305 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | if (mddev->ro) |
| 3307 | set_disk_ro(disk, 0); |
| 3308 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3309 | mddev->pers->stop(mddev); |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3310 | if (mddev->pers->sync_request) |
| 3311 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | module_put(mddev->pers->owner); |
| 3314 | mddev->pers = NULL; |
| 3315 | if (mddev->ro) |
| 3316 | mddev->ro = 0; |
| 3317 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3318 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | /* mark array as shutdown cleanly */ |
| 3320 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3321 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3323 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3325 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | /* |
| 3329 | * Free resources if final stop |
| 3330 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3331 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3332 | mdk_rdev_t *rdev; |
| 3333 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | struct gendisk *disk; |
| 3335 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3336 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3337 | bitmap_destroy(mddev); |
| 3338 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3339 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3340 | fput(mddev->bitmap_file); |
| 3341 | mddev->bitmap_file = NULL; |
| 3342 | } |
| 3343 | mddev->bitmap_offset = 0; |
| 3344 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3345 | ITERATE_RDEV(mddev,rdev,tmp) |
| 3346 | if (rdev->raid_disk >= 0) { |
| 3347 | char nm[20]; |
| 3348 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3349 | sysfs_remove_link(&mddev->kobj, nm); |
| 3350 | } |
| 3351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | export_array(mddev); |
| 3353 | |
| 3354 | mddev->array_size = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3355 | mddev->size = 0; |
| 3356 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3357 | mddev->recovery_cp = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | disk = mddev->gendisk; |
| 3360 | if (disk) |
| 3361 | set_capacity(disk, 0); |
| 3362 | mddev->changed = 1; |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3363 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3364 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3365 | mdname(mddev)); |
| 3366 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3367 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3368 | out: |
| 3369 | return err; |
| 3370 | } |
| 3371 | |
| 3372 | static void autorun_array(mddev_t *mddev) |
| 3373 | { |
| 3374 | mdk_rdev_t *rdev; |
| 3375 | struct list_head *tmp; |
| 3376 | int err; |
| 3377 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3378 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | |
| 3381 | printk(KERN_INFO "md: running: "); |
| 3382 | |
| 3383 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 3384 | char b[BDEVNAME_SIZE]; |
| 3385 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3386 | } |
| 3387 | printk("\n"); |
| 3388 | |
| 3389 | err = do_md_run (mddev); |
| 3390 | if (err) { |
| 3391 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 3392 | do_md_stop (mddev, 0); |
| 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | /* |
| 3397 | * lets try to run arrays based on all disks that have arrived |
| 3398 | * until now. (those are in pending_raid_disks) |
| 3399 | * |
| 3400 | * the method: pick the first pending disk, collect all disks with |
| 3401 | * the same UUID, remove all from the pending list and put them into |
| 3402 | * the 'same_array' list. Then order this list based on superblock |
| 3403 | * update time (freshest comes first), kick out 'old' disks and |
| 3404 | * compare superblocks. If everything's fine then run it. |
| 3405 | * |
| 3406 | * If "unit" is allocated, then bump its reference count |
| 3407 | */ |
| 3408 | static void autorun_devices(int part) |
| 3409 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | struct list_head *tmp; |
| 3411 | mdk_rdev_t *rdev0, *rdev; |
| 3412 | mddev_t *mddev; |
| 3413 | char b[BDEVNAME_SIZE]; |
| 3414 | |
| 3415 | printk(KERN_INFO "md: autorun ...\n"); |
| 3416 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3417 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 3419 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | rdev0 = list_entry(pending_raid_disks.next, |
| 3421 | mdk_rdev_t, same_set); |
| 3422 | |
| 3423 | printk(KERN_INFO "md: considering %s ...\n", |
| 3424 | bdevname(rdev0->bdev,b)); |
| 3425 | INIT_LIST_HEAD(&candidates); |
| 3426 | ITERATE_RDEV_PENDING(rdev,tmp) |
| 3427 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 3428 | printk(KERN_INFO "md: adding %s ...\n", |
| 3429 | bdevname(rdev->bdev,b)); |
| 3430 | list_move(&rdev->same_set, &candidates); |
| 3431 | } |
| 3432 | /* |
| 3433 | * now we have a set of devices, with all of them having |
| 3434 | * mostly sane superblocks. It's time to allocate the |
| 3435 | * mddev. |
| 3436 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3437 | if (part) { |
| 3438 | dev = MKDEV(mdp_major, |
| 3439 | rdev0->preferred_minor << MdpMinorShift); |
| 3440 | unit = MINOR(dev) >> MdpMinorShift; |
| 3441 | } else { |
| 3442 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 3443 | unit = MINOR(dev); |
| 3444 | } |
| 3445 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 3447 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 3448 | break; |
| 3449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | |
| 3451 | md_probe(dev, NULL, NULL); |
| 3452 | mddev = mddev_find(dev); |
| 3453 | if (!mddev) { |
| 3454 | printk(KERN_ERR |
| 3455 | "md: cannot allocate memory for md drive.\n"); |
| 3456 | break; |
| 3457 | } |
| 3458 | if (mddev_lock(mddev)) |
| 3459 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 3460 | mdname(mddev)); |
| 3461 | else if (mddev->raid_disks || mddev->major_version |
| 3462 | || !list_empty(&mddev->disks)) { |
| 3463 | printk(KERN_WARNING |
| 3464 | "md: %s already running, cannot run %s\n", |
| 3465 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 3466 | mddev_unlock(mddev); |
| 3467 | } else { |
| 3468 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
| 3469 | ITERATE_RDEV_GENERIC(candidates,rdev,tmp) { |
| 3470 | list_del_init(&rdev->same_set); |
| 3471 | if (bind_rdev_to_array(rdev, mddev)) |
| 3472 | export_rdev(rdev); |
| 3473 | } |
| 3474 | autorun_array(mddev); |
| 3475 | mddev_unlock(mddev); |
| 3476 | } |
| 3477 | /* on success, candidates will be empty, on error |
| 3478 | * it won't... |
| 3479 | */ |
| 3480 | ITERATE_RDEV_GENERIC(candidates,rdev,tmp) |
| 3481 | export_rdev(rdev); |
| 3482 | mddev_put(mddev); |
| 3483 | } |
| 3484 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 3485 | } |
| 3486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | static int get_version(void __user * arg) |
| 3488 | { |
| 3489 | mdu_version_t ver; |
| 3490 | |
| 3491 | ver.major = MD_MAJOR_VERSION; |
| 3492 | ver.minor = MD_MINOR_VERSION; |
| 3493 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 3494 | |
| 3495 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 3496 | return -EFAULT; |
| 3497 | |
| 3498 | return 0; |
| 3499 | } |
| 3500 | |
| 3501 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 3502 | { |
| 3503 | mdu_array_info_t info; |
| 3504 | int nr,working,active,failed,spare; |
| 3505 | mdk_rdev_t *rdev; |
| 3506 | struct list_head *tmp; |
| 3507 | |
| 3508 | nr=working=active=failed=spare=0; |
| 3509 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 3510 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3511 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3512 | failed++; |
| 3513 | else { |
| 3514 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3515 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | active++; |
| 3517 | else |
| 3518 | spare++; |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | info.major_version = mddev->major_version; |
| 3523 | info.minor_version = mddev->minor_version; |
| 3524 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 3525 | info.ctime = mddev->ctime; |
| 3526 | info.level = mddev->level; |
| 3527 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 3528 | if (info.size != mddev->size) /* overflow */ |
| 3529 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | info.nr_disks = nr; |
| 3531 | info.raid_disks = mddev->raid_disks; |
| 3532 | info.md_minor = mddev->md_minor; |
| 3533 | info.not_persistent= !mddev->persistent; |
| 3534 | |
| 3535 | info.utime = mddev->utime; |
| 3536 | info.state = 0; |
| 3537 | if (mddev->in_sync) |
| 3538 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3539 | if (mddev->bitmap && mddev->bitmap_offset) |
| 3540 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | info.active_disks = active; |
| 3542 | info.working_disks = working; |
| 3543 | info.failed_disks = failed; |
| 3544 | info.spare_disks = spare; |
| 3545 | |
| 3546 | info.layout = mddev->layout; |
| 3547 | info.chunk_size = mddev->chunk_size; |
| 3548 | |
| 3549 | if (copy_to_user(arg, &info, sizeof(info))) |
| 3550 | return -EFAULT; |
| 3551 | |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 3555 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3556 | { |
| 3557 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 3558 | char *ptr, *buf = NULL; |
| 3559 | int err = -ENOMEM; |
| 3560 | |
| 3561 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
| 3562 | if (!file) |
| 3563 | goto out; |
| 3564 | |
| 3565 | /* bitmap disabled, zero the first byte and copy out */ |
| 3566 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 3567 | file->pathname[0] = '\0'; |
| 3568 | goto copy_out; |
| 3569 | } |
| 3570 | |
| 3571 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 3572 | if (!buf) |
| 3573 | goto out; |
| 3574 | |
| 3575 | ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname)); |
| 3576 | if (!ptr) |
| 3577 | goto out; |
| 3578 | |
| 3579 | strcpy(file->pathname, ptr); |
| 3580 | |
| 3581 | copy_out: |
| 3582 | err = 0; |
| 3583 | if (copy_to_user(arg, file, sizeof(*file))) |
| 3584 | err = -EFAULT; |
| 3585 | out: |
| 3586 | kfree(buf); |
| 3587 | kfree(file); |
| 3588 | return err; |
| 3589 | } |
| 3590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 3592 | { |
| 3593 | mdu_disk_info_t info; |
| 3594 | unsigned int nr; |
| 3595 | mdk_rdev_t *rdev; |
| 3596 | |
| 3597 | if (copy_from_user(&info, arg, sizeof(info))) |
| 3598 | return -EFAULT; |
| 3599 | |
| 3600 | nr = info.number; |
| 3601 | |
| 3602 | rdev = find_rdev_nr(mddev, nr); |
| 3603 | if (rdev) { |
| 3604 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 3605 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 3606 | info.raid_disk = rdev->raid_disk; |
| 3607 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3608 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3610 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | info.state |= (1<<MD_DISK_ACTIVE); |
| 3612 | info.state |= (1<<MD_DISK_SYNC); |
| 3613 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3614 | if (test_bit(WriteMostly, &rdev->flags)) |
| 3615 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | } else { |
| 3617 | info.major = info.minor = 0; |
| 3618 | info.raid_disk = -1; |
| 3619 | info.state = (1<<MD_DISK_REMOVED); |
| 3620 | } |
| 3621 | |
| 3622 | if (copy_to_user(arg, &info, sizeof(info))) |
| 3623 | return -EFAULT; |
| 3624 | |
| 3625 | return 0; |
| 3626 | } |
| 3627 | |
| 3628 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 3629 | { |
| 3630 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3631 | mdk_rdev_t *rdev; |
| 3632 | dev_t dev = MKDEV(info->major,info->minor); |
| 3633 | |
| 3634 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 3635 | return -EOVERFLOW; |
| 3636 | |
| 3637 | if (!mddev->raid_disks) { |
| 3638 | int err; |
| 3639 | /* expecting a device which has a superblock */ |
| 3640 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 3641 | if (IS_ERR(rdev)) { |
| 3642 | printk(KERN_WARNING |
| 3643 | "md: md_import_device returned %ld\n", |
| 3644 | PTR_ERR(rdev)); |
| 3645 | return PTR_ERR(rdev); |
| 3646 | } |
| 3647 | if (!list_empty(&mddev->disks)) { |
| 3648 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3649 | mdk_rdev_t, same_set); |
| 3650 | int err = super_types[mddev->major_version] |
| 3651 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3652 | if (err < 0) { |
| 3653 | printk(KERN_WARNING |
| 3654 | "md: %s has different UUID to %s\n", |
| 3655 | bdevname(rdev->bdev,b), |
| 3656 | bdevname(rdev0->bdev,b2)); |
| 3657 | export_rdev(rdev); |
| 3658 | return -EINVAL; |
| 3659 | } |
| 3660 | } |
| 3661 | err = bind_rdev_to_array(rdev, mddev); |
| 3662 | if (err) |
| 3663 | export_rdev(rdev); |
| 3664 | return err; |
| 3665 | } |
| 3666 | |
| 3667 | /* |
| 3668 | * add_new_disk can be used once the array is assembled |
| 3669 | * to add "hot spares". They must already have a superblock |
| 3670 | * written |
| 3671 | */ |
| 3672 | if (mddev->pers) { |
| 3673 | int err; |
| 3674 | if (!mddev->pers->hot_add_disk) { |
| 3675 | printk(KERN_WARNING |
| 3676 | "%s: personality does not support diskops!\n", |
| 3677 | mdname(mddev)); |
| 3678 | return -EINVAL; |
| 3679 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 3680 | if (mddev->persistent) |
| 3681 | rdev = md_import_device(dev, mddev->major_version, |
| 3682 | mddev->minor_version); |
| 3683 | else |
| 3684 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | if (IS_ERR(rdev)) { |
| 3686 | printk(KERN_WARNING |
| 3687 | "md: md_import_device returned %ld\n", |
| 3688 | PTR_ERR(rdev)); |
| 3689 | return PTR_ERR(rdev); |
| 3690 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 3691 | /* set save_raid_disk if appropriate */ |
| 3692 | if (!mddev->persistent) { |
| 3693 | if (info->state & (1<<MD_DISK_SYNC) && |
| 3694 | info->raid_disk < mddev->raid_disks) |
| 3695 | rdev->raid_disk = info->raid_disk; |
| 3696 | else |
| 3697 | rdev->raid_disk = -1; |
| 3698 | } else |
| 3699 | super_types[mddev->major_version]. |
| 3700 | validate_super(mddev, rdev); |
| 3701 | rdev->saved_raid_disk = rdev->raid_disk; |
| 3702 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3703 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3704 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 3705 | set_bit(WriteMostly, &rdev->flags); |
| 3706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3707 | rdev->raid_disk = -1; |
| 3708 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 3709 | if (!err && !mddev->pers->hot_remove_disk) { |
| 3710 | /* If there is hot_add_disk but no hot_remove_disk |
| 3711 | * then added disks for geometry changes, |
| 3712 | * and should be added immediately. |
| 3713 | */ |
| 3714 | super_types[mddev->major_version]. |
| 3715 | validate_super(mddev, rdev); |
| 3716 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 3717 | if (err) |
| 3718 | unbind_rdev_from_array(rdev); |
| 3719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | if (err) |
| 3721 | export_rdev(rdev); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 3722 | |
| 3723 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 3724 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | return err; |
| 3726 | } |
| 3727 | |
| 3728 | /* otherwise, add_new_disk is only allowed |
| 3729 | * for major_version==0 superblocks |
| 3730 | */ |
| 3731 | if (mddev->major_version != 0) { |
| 3732 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 3733 | mdname(mddev)); |
| 3734 | return -EINVAL; |
| 3735 | } |
| 3736 | |
| 3737 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 3738 | int err; |
| 3739 | rdev = md_import_device (dev, -1, 0); |
| 3740 | if (IS_ERR(rdev)) { |
| 3741 | printk(KERN_WARNING |
| 3742 | "md: error, md_import_device() returned %ld\n", |
| 3743 | PTR_ERR(rdev)); |
| 3744 | return PTR_ERR(rdev); |
| 3745 | } |
| 3746 | rdev->desc_nr = info->number; |
| 3747 | if (info->raid_disk < mddev->raid_disks) |
| 3748 | rdev->raid_disk = info->raid_disk; |
| 3749 | else |
| 3750 | rdev->raid_disk = -1; |
| 3751 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3752 | rdev->flags = 0; |
| 3753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3755 | if (info->state & (1<<MD_DISK_SYNC)) |
| 3756 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3758 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 3759 | set_bit(WriteMostly, &rdev->flags); |
| 3760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | if (!mddev->persistent) { |
| 3762 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 3763 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 3764 | } else |
| 3765 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 3766 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 3767 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3768 | err = bind_rdev_to_array(rdev, mddev); |
| 3769 | if (err) { |
| 3770 | export_rdev(rdev); |
| 3771 | return err; |
| 3772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
| 3775 | return 0; |
| 3776 | } |
| 3777 | |
| 3778 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 3779 | { |
| 3780 | char b[BDEVNAME_SIZE]; |
| 3781 | mdk_rdev_t *rdev; |
| 3782 | |
| 3783 | if (!mddev->pers) |
| 3784 | return -ENODEV; |
| 3785 | |
| 3786 | rdev = find_rdev(mddev, dev); |
| 3787 | if (!rdev) |
| 3788 | return -ENXIO; |
| 3789 | |
| 3790 | if (rdev->raid_disk >= 0) |
| 3791 | goto busy; |
| 3792 | |
| 3793 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3794 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3795 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | |
| 3797 | return 0; |
| 3798 | busy: |
| 3799 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n", |
| 3800 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 3801 | return -EBUSY; |
| 3802 | } |
| 3803 | |
| 3804 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 3805 | { |
| 3806 | char b[BDEVNAME_SIZE]; |
| 3807 | int err; |
| 3808 | unsigned int size; |
| 3809 | mdk_rdev_t *rdev; |
| 3810 | |
| 3811 | if (!mddev->pers) |
| 3812 | return -ENODEV; |
| 3813 | |
| 3814 | if (mddev->major_version != 0) { |
| 3815 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 3816 | " version-0 superblocks.\n", |
| 3817 | mdname(mddev)); |
| 3818 | return -EINVAL; |
| 3819 | } |
| 3820 | if (!mddev->pers->hot_add_disk) { |
| 3821 | printk(KERN_WARNING |
| 3822 | "%s: personality does not support diskops!\n", |
| 3823 | mdname(mddev)); |
| 3824 | return -EINVAL; |
| 3825 | } |
| 3826 | |
| 3827 | rdev = md_import_device (dev, -1, 0); |
| 3828 | if (IS_ERR(rdev)) { |
| 3829 | printk(KERN_WARNING |
| 3830 | "md: error, md_import_device() returned %ld\n", |
| 3831 | PTR_ERR(rdev)); |
| 3832 | return -EINVAL; |
| 3833 | } |
| 3834 | |
| 3835 | if (mddev->persistent) |
| 3836 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 3837 | else |
| 3838 | rdev->sb_offset = |
| 3839 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 3840 | |
| 3841 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 3842 | rdev->size = size; |
| 3843 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3844 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | printk(KERN_WARNING |
| 3846 | "md: can not hot-add faulty %s disk to %s!\n", |
| 3847 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 3848 | err = -EINVAL; |
| 3849 | goto abort_export; |
| 3850 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3851 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 3853 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3854 | err = bind_rdev_to_array(rdev, mddev); |
| 3855 | if (err) |
| 3856 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | |
| 3858 | /* |
| 3859 | * The rest should better be atomic, we can have disk failures |
| 3860 | * noticed in interrupt contexts ... |
| 3861 | */ |
| 3862 | |
| 3863 | if (rdev->desc_nr == mddev->max_disks) { |
| 3864 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 3865 | mdname(mddev)); |
| 3866 | err = -EBUSY; |
| 3867 | goto abort_unbind_export; |
| 3868 | } |
| 3869 | |
| 3870 | rdev->raid_disk = -1; |
| 3871 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3872 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | |
| 3874 | /* |
| 3875 | * Kick recovery, maybe this spare has to be added to the |
| 3876 | * array immediately. |
| 3877 | */ |
| 3878 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3879 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3880 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | return 0; |
| 3882 | |
| 3883 | abort_unbind_export: |
| 3884 | unbind_rdev_from_array(rdev); |
| 3885 | |
| 3886 | abort_export: |
| 3887 | export_rdev(rdev); |
| 3888 | return err; |
| 3889 | } |
| 3890 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3891 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 3892 | { |
| 3893 | int err; |
| 3894 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3895 | if (mddev->pers) { |
| 3896 | if (!mddev->pers->quiesce) |
| 3897 | return -EBUSY; |
| 3898 | if (mddev->recovery || mddev->sync_thread) |
| 3899 | return -EBUSY; |
| 3900 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3901 | } |
| 3902 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3903 | |
| 3904 | if (fd >= 0) { |
| 3905 | if (mddev->bitmap) |
| 3906 | return -EEXIST; /* cannot add when bitmap is present */ |
| 3907 | mddev->bitmap_file = fget(fd); |
| 3908 | |
| 3909 | if (mddev->bitmap_file == NULL) { |
| 3910 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 3911 | mdname(mddev)); |
| 3912 | return -EBADF; |
| 3913 | } |
| 3914 | |
| 3915 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 3916 | if (err) { |
| 3917 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 3918 | mdname(mddev)); |
| 3919 | fput(mddev->bitmap_file); |
| 3920 | mddev->bitmap_file = NULL; |
| 3921 | return err; |
| 3922 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 3923 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3924 | } else if (mddev->bitmap == NULL) |
| 3925 | return -ENOENT; /* cannot remove what isn't there */ |
| 3926 | err = 0; |
| 3927 | if (mddev->pers) { |
| 3928 | mddev->pers->quiesce(mddev, 1); |
| 3929 | if (fd >= 0) |
| 3930 | err = bitmap_create(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3931 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3932 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3933 | fd = -1; /* make sure to put the file */ |
| 3934 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3935 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3936 | } |
| 3937 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3938 | if (mddev->bitmap_file) { |
| 3939 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3940 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3941 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3942 | mddev->bitmap_file = NULL; |
| 3943 | } |
| 3944 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3945 | return err; |
| 3946 | } |
| 3947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | /* |
| 3949 | * set_array_info is used two different ways |
| 3950 | * The original usage is when creating a new array. |
| 3951 | * In this usage, raid_disks is > 0 and it together with |
| 3952 | * level, size, not_persistent,layout,chunksize determine the |
| 3953 | * shape of the array. |
| 3954 | * This will always create an array with a type-0.90.0 superblock. |
| 3955 | * The newer usage is when assembling an array. |
| 3956 | * In this case raid_disks will be 0, and the major_version field is |
| 3957 | * use to determine which style super-blocks are to be found on the devices. |
| 3958 | * The minor and patch _version numbers are also kept incase the |
| 3959 | * super_block handler wishes to interpret them. |
| 3960 | */ |
| 3961 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 3962 | { |
| 3963 | |
| 3964 | if (info->raid_disks == 0) { |
| 3965 | /* just setting version number for superblock loading */ |
| 3966 | if (info->major_version < 0 || |
| 3967 | info->major_version >= sizeof(super_types)/sizeof(super_types[0]) || |
| 3968 | super_types[info->major_version].name == NULL) { |
| 3969 | /* maybe try to auto-load a module? */ |
| 3970 | printk(KERN_INFO |
| 3971 | "md: superblock version %d not known\n", |
| 3972 | info->major_version); |
| 3973 | return -EINVAL; |
| 3974 | } |
| 3975 | mddev->major_version = info->major_version; |
| 3976 | mddev->minor_version = info->minor_version; |
| 3977 | mddev->patch_version = info->patch_version; |
| 3978 | return 0; |
| 3979 | } |
| 3980 | mddev->major_version = MD_MAJOR_VERSION; |
| 3981 | mddev->minor_version = MD_MINOR_VERSION; |
| 3982 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 3983 | mddev->ctime = get_seconds(); |
| 3984 | |
| 3985 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 3986 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | mddev->size = info->size; |
| 3988 | mddev->raid_disks = info->raid_disks; |
| 3989 | /* don't set md_minor, it is determined by which /dev/md* was |
| 3990 | * openned |
| 3991 | */ |
| 3992 | if (info->state & (1<<MD_SB_CLEAN)) |
| 3993 | mddev->recovery_cp = MaxSector; |
| 3994 | else |
| 3995 | mddev->recovery_cp = 0; |
| 3996 | mddev->persistent = ! info->not_persistent; |
| 3997 | |
| 3998 | mddev->layout = info->layout; |
| 3999 | mddev->chunk_size = info->chunk_size; |
| 4000 | |
| 4001 | mddev->max_disks = MD_SB_DISKS; |
| 4002 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4003 | mddev->flags = 0; |
| 4004 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4006 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4007 | mddev->bitmap_offset = 0; |
| 4008 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4009 | mddev->reshape_position = MaxSector; |
| 4010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | /* |
| 4012 | * Generate a 128 bit UUID |
| 4013 | */ |
| 4014 | get_random_bytes(mddev->uuid, 16); |
| 4015 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4016 | mddev->new_level = mddev->level; |
| 4017 | mddev->new_chunk = mddev->chunk_size; |
| 4018 | mddev->new_layout = mddev->layout; |
| 4019 | mddev->delta_disks = 0; |
| 4020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | return 0; |
| 4022 | } |
| 4023 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4024 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4025 | { |
| 4026 | mdk_rdev_t * rdev; |
| 4027 | int rv; |
| 4028 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4029 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4030 | |
| 4031 | if (mddev->pers->resize == NULL) |
| 4032 | return -EINVAL; |
| 4033 | /* The "size" is the amount of each device that is used. |
| 4034 | * This can only make sense for arrays with redundancy. |
| 4035 | * linear and raid0 always use whatever space is available |
| 4036 | * We can only consider changing the size if no resync |
| 4037 | * or reconstruction is happening, and if the new size |
| 4038 | * is acceptable. It must fit before the sb_offset or, |
| 4039 | * if that is <data_offset, it must fit before the |
| 4040 | * size of each device. |
| 4041 | * If size is zero, we find the largest size that fits. |
| 4042 | */ |
| 4043 | if (mddev->sync_thread) |
| 4044 | return -EBUSY; |
| 4045 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 4046 | sector_t avail; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4047 | if (rdev->sb_offset > rdev->data_offset) |
| 4048 | avail = (rdev->sb_offset*2) - rdev->data_offset; |
| 4049 | else |
| 4050 | avail = get_capacity(rdev->bdev->bd_disk) |
| 4051 | - rdev->data_offset; |
| 4052 | if (fit && (size == 0 || size > avail/2)) |
| 4053 | size = avail/2; |
| 4054 | if (avail < ((sector_t)size << 1)) |
| 4055 | return -ENOSPC; |
| 4056 | } |
| 4057 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4058 | if (!rv) { |
| 4059 | struct block_device *bdev; |
| 4060 | |
| 4061 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4062 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4063 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4064 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4065 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4066 | bdput(bdev); |
| 4067 | } |
| 4068 | } |
| 4069 | return rv; |
| 4070 | } |
| 4071 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4072 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4073 | { |
| 4074 | int rv; |
| 4075 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4076 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4077 | return -EINVAL; |
| 4078 | if (raid_disks <= 0 || |
| 4079 | raid_disks >= mddev->max_disks) |
| 4080 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4081 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4082 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4083 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4084 | |
| 4085 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4086 | return rv; |
| 4087 | } |
| 4088 | |
| 4089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | /* |
| 4091 | * update_array_info is used to change the configuration of an |
| 4092 | * on-line array. |
| 4093 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4094 | * fields in the info are checked against the array. |
| 4095 | * Any differences that cannot be handled will cause an error. |
| 4096 | * Normally, only one change can be managed at a time. |
| 4097 | */ |
| 4098 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4099 | { |
| 4100 | int rv = 0; |
| 4101 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4102 | int state = 0; |
| 4103 | |
| 4104 | /* calculate expected state,ignoring low bits */ |
| 4105 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4106 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | |
| 4108 | if (mddev->major_version != info->major_version || |
| 4109 | mddev->minor_version != info->minor_version || |
| 4110 | /* mddev->patch_version != info->patch_version || */ |
| 4111 | mddev->ctime != info->ctime || |
| 4112 | mddev->level != info->level || |
| 4113 | /* mddev->layout != info->layout || */ |
| 4114 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4115 | mddev->chunk_size != info->chunk_size || |
| 4116 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4117 | ((state^info->state) & 0xfffffe00) |
| 4118 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | return -EINVAL; |
| 4120 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4121 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4122 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4123 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4124 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | if (cnt == 0) return 0; |
| 4126 | if (cnt > 1) return -EINVAL; |
| 4127 | |
| 4128 | if (mddev->layout != info->layout) { |
| 4129 | /* Change layout |
| 4130 | * we don't need to do anything at the md level, the |
| 4131 | * personality will take care of it all. |
| 4132 | */ |
| 4133 | if (mddev->pers->reconfig == NULL) |
| 4134 | return -EINVAL; |
| 4135 | else |
| 4136 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4137 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4138 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4139 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4141 | if (mddev->raid_disks != info->raid_disks) |
| 4142 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4144 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4145 | if (mddev->pers->quiesce == NULL) |
| 4146 | return -EINVAL; |
| 4147 | if (mddev->recovery || mddev->sync_thread) |
| 4148 | return -EBUSY; |
| 4149 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4150 | /* add the bitmap */ |
| 4151 | if (mddev->bitmap) |
| 4152 | return -EEXIST; |
| 4153 | if (mddev->default_bitmap_offset == 0) |
| 4154 | return -EINVAL; |
| 4155 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4156 | mddev->pers->quiesce(mddev, 1); |
| 4157 | rv = bitmap_create(mddev); |
| 4158 | if (rv) |
| 4159 | bitmap_destroy(mddev); |
| 4160 | mddev->pers->quiesce(mddev, 0); |
| 4161 | } else { |
| 4162 | /* remove the bitmap */ |
| 4163 | if (!mddev->bitmap) |
| 4164 | return -ENOENT; |
| 4165 | if (mddev->bitmap->file) |
| 4166 | return -EINVAL; |
| 4167 | mddev->pers->quiesce(mddev, 1); |
| 4168 | bitmap_destroy(mddev); |
| 4169 | mddev->pers->quiesce(mddev, 0); |
| 4170 | mddev->bitmap_offset = 0; |
| 4171 | } |
| 4172 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4173 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | return rv; |
| 4175 | } |
| 4176 | |
| 4177 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4178 | { |
| 4179 | mdk_rdev_t *rdev; |
| 4180 | |
| 4181 | if (mddev->pers == NULL) |
| 4182 | return -ENODEV; |
| 4183 | |
| 4184 | rdev = find_rdev(mddev, dev); |
| 4185 | if (!rdev) |
| 4186 | return -ENODEV; |
| 4187 | |
| 4188 | md_error(mddev, rdev); |
| 4189 | return 0; |
| 4190 | } |
| 4191 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4192 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4193 | { |
| 4194 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4195 | |
| 4196 | geo->heads = 2; |
| 4197 | geo->sectors = 4; |
| 4198 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4199 | return 0; |
| 4200 | } |
| 4201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4203 | unsigned int cmd, unsigned long arg) |
| 4204 | { |
| 4205 | int err = 0; |
| 4206 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | mddev_t *mddev = NULL; |
| 4208 | |
| 4209 | if (!capable(CAP_SYS_ADMIN)) |
| 4210 | return -EACCES; |
| 4211 | |
| 4212 | /* |
| 4213 | * Commands dealing with the RAID driver but not any |
| 4214 | * particular array: |
| 4215 | */ |
| 4216 | switch (cmd) |
| 4217 | { |
| 4218 | case RAID_VERSION: |
| 4219 | err = get_version(argp); |
| 4220 | goto done; |
| 4221 | |
| 4222 | case PRINT_RAID_DEBUG: |
| 4223 | err = 0; |
| 4224 | md_print_devices(); |
| 4225 | goto done; |
| 4226 | |
| 4227 | #ifndef MODULE |
| 4228 | case RAID_AUTORUN: |
| 4229 | err = 0; |
| 4230 | autostart_arrays(arg); |
| 4231 | goto done; |
| 4232 | #endif |
| 4233 | default:; |
| 4234 | } |
| 4235 | |
| 4236 | /* |
| 4237 | * Commands creating/starting a new array: |
| 4238 | */ |
| 4239 | |
| 4240 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4241 | |
| 4242 | if (!mddev) { |
| 4243 | BUG(); |
| 4244 | goto abort; |
| 4245 | } |
| 4246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | err = mddev_lock(mddev); |
| 4248 | if (err) { |
| 4249 | printk(KERN_INFO |
| 4250 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4251 | err, cmd); |
| 4252 | goto abort; |
| 4253 | } |
| 4254 | |
| 4255 | switch (cmd) |
| 4256 | { |
| 4257 | case SET_ARRAY_INFO: |
| 4258 | { |
| 4259 | mdu_array_info_t info; |
| 4260 | if (!arg) |
| 4261 | memset(&info, 0, sizeof(info)); |
| 4262 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4263 | err = -EFAULT; |
| 4264 | goto abort_unlock; |
| 4265 | } |
| 4266 | if (mddev->pers) { |
| 4267 | err = update_array_info(mddev, &info); |
| 4268 | if (err) { |
| 4269 | printk(KERN_WARNING "md: couldn't update" |
| 4270 | " array info. %d\n", err); |
| 4271 | goto abort_unlock; |
| 4272 | } |
| 4273 | goto done_unlock; |
| 4274 | } |
| 4275 | if (!list_empty(&mddev->disks)) { |
| 4276 | printk(KERN_WARNING |
| 4277 | "md: array %s already has disks!\n", |
| 4278 | mdname(mddev)); |
| 4279 | err = -EBUSY; |
| 4280 | goto abort_unlock; |
| 4281 | } |
| 4282 | if (mddev->raid_disks) { |
| 4283 | printk(KERN_WARNING |
| 4284 | "md: array %s already initialised!\n", |
| 4285 | mdname(mddev)); |
| 4286 | err = -EBUSY; |
| 4287 | goto abort_unlock; |
| 4288 | } |
| 4289 | err = set_array_info(mddev, &info); |
| 4290 | if (err) { |
| 4291 | printk(KERN_WARNING "md: couldn't set" |
| 4292 | " array info. %d\n", err); |
| 4293 | goto abort_unlock; |
| 4294 | } |
| 4295 | } |
| 4296 | goto done_unlock; |
| 4297 | |
| 4298 | default:; |
| 4299 | } |
| 4300 | |
| 4301 | /* |
| 4302 | * Commands querying/configuring an existing array: |
| 4303 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4304 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
| 4305 | * RUN_ARRAY, and SET_BITMAP_FILE are allowed */ |
| 4306 | if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 4307 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | err = -ENODEV; |
| 4309 | goto abort_unlock; |
| 4310 | } |
| 4311 | |
| 4312 | /* |
| 4313 | * Commands even a read-only array can execute: |
| 4314 | */ |
| 4315 | switch (cmd) |
| 4316 | { |
| 4317 | case GET_ARRAY_INFO: |
| 4318 | err = get_array_info(mddev, argp); |
| 4319 | goto done_unlock; |
| 4320 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4321 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4322 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4323 | goto done_unlock; |
| 4324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | case GET_DISK_INFO: |
| 4326 | err = get_disk_info(mddev, argp); |
| 4327 | goto done_unlock; |
| 4328 | |
| 4329 | case RESTART_ARRAY_RW: |
| 4330 | err = restart_array(mddev); |
| 4331 | goto done_unlock; |
| 4332 | |
| 4333 | case STOP_ARRAY: |
| 4334 | err = do_md_stop (mddev, 0); |
| 4335 | goto done_unlock; |
| 4336 | |
| 4337 | case STOP_ARRAY_RO: |
| 4338 | err = do_md_stop (mddev, 1); |
| 4339 | goto done_unlock; |
| 4340 | |
| 4341 | /* |
| 4342 | * We have a problem here : there is no easy way to give a CHS |
| 4343 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4344 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4345 | * dosfs just mad... ;-) |
| 4346 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | /* |
| 4350 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4351 | * superblock, so we do not allow them on read-only arrays. |
| 4352 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4353 | * here and hit the 'default' below, so only disallow |
| 4354 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | */ |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4356 | if (_IOC_TYPE(cmd) == MD_MAJOR && |
| 4357 | mddev->ro && mddev->pers) { |
| 4358 | if (mddev->ro == 2) { |
| 4359 | mddev->ro = 0; |
| 4360 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4361 | md_wakeup_thread(mddev->thread); |
| 4362 | |
| 4363 | } else { |
| 4364 | err = -EROFS; |
| 4365 | goto abort_unlock; |
| 4366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | switch (cmd) |
| 4370 | { |
| 4371 | case ADD_NEW_DISK: |
| 4372 | { |
| 4373 | mdu_disk_info_t info; |
| 4374 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4375 | err = -EFAULT; |
| 4376 | else |
| 4377 | err = add_new_disk(mddev, &info); |
| 4378 | goto done_unlock; |
| 4379 | } |
| 4380 | |
| 4381 | case HOT_REMOVE_DISK: |
| 4382 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4383 | goto done_unlock; |
| 4384 | |
| 4385 | case HOT_ADD_DISK: |
| 4386 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4387 | goto done_unlock; |
| 4388 | |
| 4389 | case SET_DISK_FAULTY: |
| 4390 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4391 | goto done_unlock; |
| 4392 | |
| 4393 | case RUN_ARRAY: |
| 4394 | err = do_md_run (mddev); |
| 4395 | goto done_unlock; |
| 4396 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4397 | case SET_BITMAP_FILE: |
| 4398 | err = set_bitmap_file(mddev, (int)arg); |
| 4399 | goto done_unlock; |
| 4400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | err = -EINVAL; |
| 4403 | goto abort_unlock; |
| 4404 | } |
| 4405 | |
| 4406 | done_unlock: |
| 4407 | abort_unlock: |
| 4408 | mddev_unlock(mddev); |
| 4409 | |
| 4410 | return err; |
| 4411 | done: |
| 4412 | if (err) |
| 4413 | MD_BUG(); |
| 4414 | abort: |
| 4415 | return err; |
| 4416 | } |
| 4417 | |
| 4418 | static int md_open(struct inode *inode, struct file *file) |
| 4419 | { |
| 4420 | /* |
| 4421 | * Succeed if we can lock the mddev, which confirms that |
| 4422 | * it isn't being stopped right now. |
| 4423 | */ |
| 4424 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4425 | int err; |
| 4426 | |
| 4427 | if ((err = mddev_lock(mddev))) |
| 4428 | goto out; |
| 4429 | |
| 4430 | err = 0; |
| 4431 | mddev_get(mddev); |
| 4432 | mddev_unlock(mddev); |
| 4433 | |
| 4434 | check_disk_change(inode->i_bdev); |
| 4435 | out: |
| 4436 | return err; |
| 4437 | } |
| 4438 | |
| 4439 | static int md_release(struct inode *inode, struct file * file) |
| 4440 | { |
| 4441 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4442 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 4443 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | mddev_put(mddev); |
| 4445 | |
| 4446 | return 0; |
| 4447 | } |
| 4448 | |
| 4449 | static int md_media_changed(struct gendisk *disk) |
| 4450 | { |
| 4451 | mddev_t *mddev = disk->private_data; |
| 4452 | |
| 4453 | return mddev->changed; |
| 4454 | } |
| 4455 | |
| 4456 | static int md_revalidate(struct gendisk *disk) |
| 4457 | { |
| 4458 | mddev_t *mddev = disk->private_data; |
| 4459 | |
| 4460 | mddev->changed = 0; |
| 4461 | return 0; |
| 4462 | } |
| 4463 | static struct block_device_operations md_fops = |
| 4464 | { |
| 4465 | .owner = THIS_MODULE, |
| 4466 | .open = md_open, |
| 4467 | .release = md_release, |
| 4468 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4469 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | .media_changed = md_media_changed, |
| 4471 | .revalidate_disk= md_revalidate, |
| 4472 | }; |
| 4473 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4474 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | { |
| 4476 | mdk_thread_t *thread = arg; |
| 4477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | /* |
| 4479 | * md_thread is a 'system-thread', it's priority should be very |
| 4480 | * high. We avoid resource deadlocks individually in each |
| 4481 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 4482 | * the very same RT priority as kswapd, thus we will never get |
| 4483 | * into a priority inversion deadlock. |
| 4484 | * |
| 4485 | * we definitely have to have equal or higher priority than |
| 4486 | * bdflush, otherwise bdflush will deadlock if there are too |
| 4487 | * many dirty RAID5 blocks. |
| 4488 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 4490 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4491 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 4493 | /* We need to wait INTERRUPTIBLE so that |
| 4494 | * we don't add to the load-average. |
| 4495 | * That means we need to be sure no signals are |
| 4496 | * pending |
| 4497 | */ |
| 4498 | if (signal_pending(current)) |
| 4499 | flush_signals(current); |
| 4500 | |
| 4501 | wait_event_interruptible_timeout |
| 4502 | (thread->wqueue, |
| 4503 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 4504 | || kthread_should_stop(), |
| 4505 | thread->timeout); |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 4506 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | |
| 4508 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 4509 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 4510 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | return 0; |
| 4514 | } |
| 4515 | |
| 4516 | void md_wakeup_thread(mdk_thread_t *thread) |
| 4517 | { |
| 4518 | if (thread) { |
| 4519 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 4520 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 4521 | wake_up(&thread->wqueue); |
| 4522 | } |
| 4523 | } |
| 4524 | |
| 4525 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 4526 | const char *name) |
| 4527 | { |
| 4528 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4530 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | if (!thread) |
| 4532 | return NULL; |
| 4533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | init_waitqueue_head(&thread->wqueue); |
| 4535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | thread->run = run; |
| 4537 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4538 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 4539 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4540 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | kfree(thread); |
| 4542 | return NULL; |
| 4543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | return thread; |
| 4545 | } |
| 4546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | void md_unregister_thread(mdk_thread_t *thread) |
| 4548 | { |
NeilBrown | d28446f | 2005-04-16 15:26:41 -0700 | [diff] [blame] | 4549 | dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4550 | |
| 4551 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | kfree(thread); |
| 4553 | } |
| 4554 | |
| 4555 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 4556 | { |
| 4557 | if (!mddev) { |
| 4558 | MD_BUG(); |
| 4559 | return; |
| 4560 | } |
| 4561 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4562 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4564 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 4566 | mdname(mddev), |
| 4567 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 4568 | __builtin_return_address(0),__builtin_return_address(1), |
| 4569 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4570 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 4571 | if (!mddev->pers) |
| 4572 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | if (!mddev->pers->error_handler) |
| 4574 | return; |
| 4575 | mddev->pers->error_handler(mddev,rdev); |
| 4576 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4577 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4578 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 4579 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | /* seq_file implementation /proc/mdstat */ |
| 4583 | |
| 4584 | static void status_unused(struct seq_file *seq) |
| 4585 | { |
| 4586 | int i = 0; |
| 4587 | mdk_rdev_t *rdev; |
| 4588 | struct list_head *tmp; |
| 4589 | |
| 4590 | seq_printf(seq, "unused devices: "); |
| 4591 | |
| 4592 | ITERATE_RDEV_PENDING(rdev,tmp) { |
| 4593 | char b[BDEVNAME_SIZE]; |
| 4594 | i++; |
| 4595 | seq_printf(seq, "%s ", |
| 4596 | bdevname(rdev->bdev,b)); |
| 4597 | } |
| 4598 | if (!i) |
| 4599 | seq_printf(seq, "<none>"); |
| 4600 | |
| 4601 | seq_printf(seq, "\n"); |
| 4602 | } |
| 4603 | |
| 4604 | |
| 4605 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 4606 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4607 | sector_t max_blocks, resync, res; |
| 4608 | unsigned long dt, db, rt; |
| 4609 | int scale; |
| 4610 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | |
| 4612 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 4613 | |
| 4614 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 4615 | max_blocks = mddev->resync_max_sectors >> 1; |
| 4616 | else |
| 4617 | max_blocks = mddev->size; |
| 4618 | |
| 4619 | /* |
| 4620 | * Should not happen. |
| 4621 | */ |
| 4622 | if (!max_blocks) { |
| 4623 | MD_BUG(); |
| 4624 | return; |
| 4625 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4626 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 4627 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 4628 | * u32, as those are the requirements for sector_div. |
| 4629 | * Thus 'scale' must be at least 10 |
| 4630 | */ |
| 4631 | scale = 10; |
| 4632 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 4633 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 4634 | scale++; |
| 4635 | } |
| 4636 | res = (resync>>scale)*1000; |
| 4637 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 4638 | |
| 4639 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4641 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | seq_printf(seq, "["); |
| 4643 | for (i = 0; i < x; i++) |
| 4644 | seq_printf(seq, "="); |
| 4645 | seq_printf(seq, ">"); |
| 4646 | for (i = 0; i < y; i++) |
| 4647 | seq_printf(seq, "."); |
| 4648 | seq_printf(seq, "] "); |
| 4649 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4650 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4651 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 4652 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 4653 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 4654 | "check" : |
| 4655 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 4656 | "resync" : "recovery"))), |
| 4657 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4658 | (unsigned long long) resync, |
| 4659 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | |
| 4661 | /* |
| 4662 | * We do not want to overflow, so the order of operands and |
| 4663 | * the * 100 / 100 trick are important. We do a +1 to be |
| 4664 | * safe against division by zero. We only estimate anyway. |
| 4665 | * |
| 4666 | * dt: time from mark until now |
| 4667 | * db: blocks written from mark until now |
| 4668 | * rt: remaining time |
| 4669 | */ |
| 4670 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 4671 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 4672 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 4673 | - mddev->resync_mark_cnt; |
| 4674 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | |
| 4676 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 4677 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 4678 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 4682 | { |
| 4683 | struct list_head *tmp; |
| 4684 | loff_t l = *pos; |
| 4685 | mddev_t *mddev; |
| 4686 | |
| 4687 | if (l >= 0x10000) |
| 4688 | return NULL; |
| 4689 | if (!l--) |
| 4690 | /* header */ |
| 4691 | return (void*)1; |
| 4692 | |
| 4693 | spin_lock(&all_mddevs_lock); |
| 4694 | list_for_each(tmp,&all_mddevs) |
| 4695 | if (!l--) { |
| 4696 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 4697 | mddev_get(mddev); |
| 4698 | spin_unlock(&all_mddevs_lock); |
| 4699 | return mddev; |
| 4700 | } |
| 4701 | spin_unlock(&all_mddevs_lock); |
| 4702 | if (!l--) |
| 4703 | return (void*)2;/* tail */ |
| 4704 | return NULL; |
| 4705 | } |
| 4706 | |
| 4707 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 4708 | { |
| 4709 | struct list_head *tmp; |
| 4710 | mddev_t *next_mddev, *mddev = v; |
| 4711 | |
| 4712 | ++*pos; |
| 4713 | if (v == (void*)2) |
| 4714 | return NULL; |
| 4715 | |
| 4716 | spin_lock(&all_mddevs_lock); |
| 4717 | if (v == (void*)1) |
| 4718 | tmp = all_mddevs.next; |
| 4719 | else |
| 4720 | tmp = mddev->all_mddevs.next; |
| 4721 | if (tmp != &all_mddevs) |
| 4722 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 4723 | else { |
| 4724 | next_mddev = (void*)2; |
| 4725 | *pos = 0x10000; |
| 4726 | } |
| 4727 | spin_unlock(&all_mddevs_lock); |
| 4728 | |
| 4729 | if (v != (void*)1) |
| 4730 | mddev_put(mddev); |
| 4731 | return next_mddev; |
| 4732 | |
| 4733 | } |
| 4734 | |
| 4735 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 4736 | { |
| 4737 | mddev_t *mddev = v; |
| 4738 | |
| 4739 | if (mddev && v != (void*)1 && v != (void*)2) |
| 4740 | mddev_put(mddev); |
| 4741 | } |
| 4742 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4743 | struct mdstat_info { |
| 4744 | int event; |
| 4745 | }; |
| 4746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | static int md_seq_show(struct seq_file *seq, void *v) |
| 4748 | { |
| 4749 | mddev_t *mddev = v; |
| 4750 | sector_t size; |
| 4751 | struct list_head *tmp2; |
| 4752 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4753 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4754 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | |
| 4756 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4757 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | seq_printf(seq, "Personalities : "); |
| 4759 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4760 | list_for_each_entry(pers, &pers_list, list) |
| 4761 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | |
| 4763 | spin_unlock(&pers_lock); |
| 4764 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4765 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | return 0; |
| 4767 | } |
| 4768 | if (v == (void*)2) { |
| 4769 | status_unused(seq); |
| 4770 | return 0; |
| 4771 | } |
| 4772 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4773 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 4777 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 4778 | mddev->pers ? "" : "in"); |
| 4779 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4780 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4782 | if (mddev->ro==2) |
| 4783 | seq_printf(seq, "(auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | seq_printf(seq, " %s", mddev->pers->name); |
| 4785 | } |
| 4786 | |
| 4787 | size = 0; |
| 4788 | ITERATE_RDEV(mddev,rdev,tmp2) { |
| 4789 | char b[BDEVNAME_SIZE]; |
| 4790 | seq_printf(seq, " %s[%d]", |
| 4791 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4792 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4793 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4794 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4795 | seq_printf(seq, "(F)"); |
| 4796 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 4797 | } else if (rdev->raid_disk < 0) |
| 4798 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | size += rdev->size; |
| 4800 | } |
| 4801 | |
| 4802 | if (!list_empty(&mddev->disks)) { |
| 4803 | if (mddev->pers) |
| 4804 | seq_printf(seq, "\n %llu blocks", |
| 4805 | (unsigned long long)mddev->array_size); |
| 4806 | else |
| 4807 | seq_printf(seq, "\n %llu blocks", |
| 4808 | (unsigned long long)size); |
| 4809 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 4810 | if (mddev->persistent) { |
| 4811 | if (mddev->major_version != 0 || |
| 4812 | mddev->minor_version != 90) { |
| 4813 | seq_printf(seq," super %d.%d", |
| 4814 | mddev->major_version, |
| 4815 | mddev->minor_version); |
| 4816 | } |
| 4817 | } else |
| 4818 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | |
| 4820 | if (mddev->pers) { |
| 4821 | mddev->pers->status (seq, mddev); |
| 4822 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 4823 | if (mddev->pers->sync_request) { |
| 4824 | if (mddev->curr_resync > 2) { |
| 4825 | status_resync (seq, mddev); |
| 4826 | seq_printf(seq, "\n "); |
| 4827 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 4828 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 4829 | else if (mddev->recovery_cp < MaxSector) |
| 4830 | seq_printf(seq, "\tresync=PENDING\n "); |
| 4831 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4832 | } else |
| 4833 | seq_printf(seq, "\n "); |
| 4834 | |
| 4835 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4836 | unsigned long chunk_kb; |
| 4837 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4838 | spin_lock_irqsave(&bitmap->lock, flags); |
| 4839 | chunk_kb = bitmap->chunksize >> 10; |
| 4840 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 4841 | "%lu%s chunk", |
| 4842 | bitmap->pages - bitmap->missing_pages, |
| 4843 | bitmap->pages, |
| 4844 | (bitmap->pages - bitmap->missing_pages) |
| 4845 | << (PAGE_SHIFT - 10), |
| 4846 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 4847 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 4848 | if (bitmap->file) { |
| 4849 | seq_printf(seq, ", file: "); |
| 4850 | seq_path(seq, bitmap->file->f_vfsmnt, |
| 4851 | bitmap->file->f_dentry," \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4852 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 4853 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4854 | seq_printf(seq, "\n"); |
| 4855 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | seq_printf(seq, "\n"); |
| 4859 | } |
| 4860 | mddev_unlock(mddev); |
| 4861 | |
| 4862 | return 0; |
| 4863 | } |
| 4864 | |
| 4865 | static struct seq_operations md_seq_ops = { |
| 4866 | .start = md_seq_start, |
| 4867 | .next = md_seq_next, |
| 4868 | .stop = md_seq_stop, |
| 4869 | .show = md_seq_show, |
| 4870 | }; |
| 4871 | |
| 4872 | static int md_seq_open(struct inode *inode, struct file *file) |
| 4873 | { |
| 4874 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4875 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 4876 | if (mi == NULL) |
| 4877 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4878 | |
| 4879 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4880 | if (error) |
| 4881 | kfree(mi); |
| 4882 | else { |
| 4883 | struct seq_file *p = file->private_data; |
| 4884 | p->private = mi; |
| 4885 | mi->event = atomic_read(&md_event_count); |
| 4886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | return error; |
| 4888 | } |
| 4889 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4890 | static int md_seq_release(struct inode *inode, struct file *file) |
| 4891 | { |
| 4892 | struct seq_file *m = file->private_data; |
| 4893 | struct mdstat_info *mi = m->private; |
| 4894 | m->private = NULL; |
| 4895 | kfree(mi); |
| 4896 | return seq_release(inode, file); |
| 4897 | } |
| 4898 | |
| 4899 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 4900 | { |
| 4901 | struct seq_file *m = filp->private_data; |
| 4902 | struct mdstat_info *mi = m->private; |
| 4903 | int mask; |
| 4904 | |
| 4905 | poll_wait(filp, &md_event_waiters, wait); |
| 4906 | |
| 4907 | /* always allow read */ |
| 4908 | mask = POLLIN | POLLRDNORM; |
| 4909 | |
| 4910 | if (mi->event != atomic_read(&md_event_count)) |
| 4911 | mask |= POLLERR | POLLPRI; |
| 4912 | return mask; |
| 4913 | } |
| 4914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | static struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 4916 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4917 | .open = md_seq_open, |
| 4918 | .read = seq_read, |
| 4919 | .llseek = seq_lseek, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4920 | .release = md_seq_release, |
| 4921 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | }; |
| 4923 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4924 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4927 | list_add_tail(&p->list, &pers_list); |
| 4928 | 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] | 4929 | spin_unlock(&pers_lock); |
| 4930 | return 0; |
| 4931 | } |
| 4932 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4933 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4935 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4937 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | spin_unlock(&pers_lock); |
| 4939 | return 0; |
| 4940 | } |
| 4941 | |
| 4942 | static int is_mddev_idle(mddev_t *mddev) |
| 4943 | { |
| 4944 | mdk_rdev_t * rdev; |
| 4945 | struct list_head *tmp; |
| 4946 | int idle; |
| 4947 | unsigned long curr_events; |
| 4948 | |
| 4949 | idle = 1; |
| 4950 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 4951 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4952 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 4953 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | atomic_read(&disk->sync_io); |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 4955 | /* The difference between curr_events and last_events |
| 4956 | * will be affected by any new non-sync IO (making |
| 4957 | * curr_events bigger) and any difference in the amount of |
| 4958 | * in-flight syncio (making current_events bigger or smaller) |
| 4959 | * The amount in-flight is currently limited to |
| 4960 | * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6 |
| 4961 | * which is at most 4096 sectors. |
| 4962 | * These numbers are fairly fragile and should be made |
| 4963 | * more robust, probably by enforcing the |
| 4964 | * 'window size' that md_do_sync sort-of uses. |
| 4965 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | * Note: the following is an unsigned comparison. |
| 4967 | */ |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 4968 | if ((curr_events - rdev->last_events + 4096) > 8192) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | rdev->last_events = curr_events; |
| 4970 | idle = 0; |
| 4971 | } |
| 4972 | } |
| 4973 | return idle; |
| 4974 | } |
| 4975 | |
| 4976 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 4977 | { |
| 4978 | /* another "blocks" (512byte) blocks have been synced */ |
| 4979 | atomic_sub(blocks, &mddev->recovery_active); |
| 4980 | wake_up(&mddev->recovery_wait); |
| 4981 | if (!ok) { |
| 4982 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 4983 | md_wakeup_thread(mddev->thread); |
| 4984 | // stop recovery, signal do_sync .... |
| 4985 | } |
| 4986 | } |
| 4987 | |
| 4988 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4989 | /* md_write_start(mddev, bi) |
| 4990 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4991 | * in superblock) before writing, schedule a superblock update |
| 4992 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4993 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4994 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4996 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4997 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4998 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4999 | BUG_ON(mddev->ro == 1); |
| 5000 | if (mddev->ro == 2) { |
| 5001 | /* need to switch to read/write */ |
| 5002 | mddev->ro = 0; |
| 5003 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5004 | md_wakeup_thread(mddev->thread); |
| 5005 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5006 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5007 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5008 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5009 | if (mddev->in_sync) { |
| 5010 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5011 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5012 | md_wakeup_thread(mddev->thread); |
| 5013 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5014 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5015 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5016 | wait_event(mddev->sb_wait, mddev->flags==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | void md_write_end(mddev_t *mddev) |
| 5020 | { |
| 5021 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5022 | if (mddev->safemode == 2) |
| 5023 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5024 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5026 | } |
| 5027 | } |
| 5028 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5029 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5030 | |
| 5031 | #define SYNC_MARKS 10 |
| 5032 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5033 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5034 | { |
| 5035 | mddev_t *mddev2; |
| 5036 | unsigned int currspeed = 0, |
| 5037 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5038 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5039 | unsigned long mark[SYNC_MARKS]; |
| 5040 | sector_t mark_cnt[SYNC_MARKS]; |
| 5041 | int last_mark,m; |
| 5042 | struct list_head *tmp; |
| 5043 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5044 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5045 | struct list_head *rtmp; |
| 5046 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5047 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5048 | |
| 5049 | /* just incase thread restarts... */ |
| 5050 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5051 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5052 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5053 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5055 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5056 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5057 | desc = "data-check"; |
| 5058 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5059 | desc = "requested-resync"; |
| 5060 | else |
| 5061 | desc = "resync"; |
| 5062 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5063 | desc = "reshape"; |
| 5064 | else |
| 5065 | desc = "recovery"; |
| 5066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | /* we overload curr_resync somewhat here. |
| 5068 | * 0 == not engaged in resync at all |
| 5069 | * 2 == checking that there is no conflict with another sync |
| 5070 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5071 | * commense |
| 5072 | * other == active in resync - this many blocks |
| 5073 | * |
| 5074 | * Before starting a resync we must have set curr_resync to |
| 5075 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5076 | * less than ours. When we find one that is the same or higher |
| 5077 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5078 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5079 | * This will mean we have to start checking from the beginning again. |
| 5080 | * |
| 5081 | */ |
| 5082 | |
| 5083 | do { |
| 5084 | mddev->curr_resync = 2; |
| 5085 | |
| 5086 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5087 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5088 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | goto skip; |
| 5090 | } |
| 5091 | ITERATE_MDDEV(mddev2,tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | if (mddev2 == mddev) |
| 5093 | continue; |
| 5094 | if (mddev2->curr_resync && |
| 5095 | match_mddev_units(mddev,mddev2)) { |
| 5096 | DEFINE_WAIT(wq); |
| 5097 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5098 | /* arbitrarily yield */ |
| 5099 | mddev->curr_resync = 1; |
| 5100 | wake_up(&resync_wait); |
| 5101 | } |
| 5102 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5103 | /* no need to wait here, we can wait the next |
| 5104 | * time 'round when curr_resync == 2 |
| 5105 | */ |
| 5106 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5107 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5108 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5109 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5110 | printk(KERN_INFO "md: delaying %s of %s" |
| 5111 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5113 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | mddev_put(mddev2); |
| 5115 | schedule(); |
| 5116 | finish_wait(&resync_wait, &wq); |
| 5117 | goto try_again; |
| 5118 | } |
| 5119 | finish_wait(&resync_wait, &wq); |
| 5120 | } |
| 5121 | } |
| 5122 | } while (mddev->curr_resync < 2); |
| 5123 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5124 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5125 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5127 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | */ |
| 5129 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5130 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5131 | /* we don't use the checkpoint if there's a bitmap */ |
| 5132 | if (!mddev->bitmap && |
| 5133 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5134 | j = mddev->recovery_cp; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5135 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5136 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5137 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | /* recovery follows the physical size of devices */ |
| 5139 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5140 | j = MaxSector; |
| 5141 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5142 | if (rdev->raid_disk >= 0 && |
| 5143 | !test_bit(Faulty, &rdev->flags) && |
| 5144 | !test_bit(In_sync, &rdev->flags) && |
| 5145 | rdev->recovery_offset < j) |
| 5146 | j = rdev->recovery_offset; |
| 5147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5149 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5150 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5151 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5152 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5153 | "(but not more than %d KB/sec) for %s.\n", |
| 5154 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | |
| 5156 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5157 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5158 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5160 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5161 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | } |
| 5163 | last_mark = 0; |
| 5164 | mddev->resync_mark = mark[last_mark]; |
| 5165 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5166 | |
| 5167 | /* |
| 5168 | * Tune reconstruction: |
| 5169 | */ |
| 5170 | window = 32*(PAGE_SIZE/512); |
| 5171 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5172 | window/2,(unsigned long long) max_sectors/2); |
| 5173 | |
| 5174 | atomic_set(&mddev->recovery_active, 0); |
| 5175 | init_waitqueue_head(&mddev->recovery_wait); |
| 5176 | last_check = 0; |
| 5177 | |
| 5178 | if (j>2) { |
| 5179 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5180 | "md: resuming %s of %s from checkpoint.\n", |
| 5181 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | mddev->curr_resync = j; |
| 5183 | } |
| 5184 | |
| 5185 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5186 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5188 | skipped = 0; |
| 5189 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5190 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5191 | if (sectors == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 5193 | goto out; |
| 5194 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5195 | |
| 5196 | if (!skipped) { /* actual IO requested */ |
| 5197 | io_sectors += sectors; |
| 5198 | atomic_add(sectors, &mddev->recovery_active); |
| 5199 | } |
| 5200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | j += sectors; |
| 5202 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5203 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5204 | if (last_check == 0) |
| 5205 | /* this is the earliers that rebuilt will be |
| 5206 | * visible in /proc/mdstat |
| 5207 | */ |
| 5208 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5209 | |
| 5210 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | continue; |
| 5212 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5213 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | |
| 5215 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) || |
| 5216 | test_bit(MD_RECOVERY_ERR, &mddev->recovery)) |
| 5217 | break; |
| 5218 | |
| 5219 | repeat: |
| 5220 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5221 | /* step marks */ |
| 5222 | int next = (last_mark+1) % SYNC_MARKS; |
| 5223 | |
| 5224 | mddev->resync_mark = mark[next]; |
| 5225 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5226 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5227 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | last_mark = next; |
| 5229 | } |
| 5230 | |
| 5231 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5232 | if (kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | /* |
| 5234 | * got a signal, exit. |
| 5235 | */ |
| 5236 | printk(KERN_INFO |
| 5237 | "md: md_do_sync() got signal ... exiting\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5239 | goto out; |
| 5240 | } |
| 5241 | |
| 5242 | /* |
| 5243 | * this loop exits only if either when we are slower than |
| 5244 | * the 'hard' speed limit, or the system was IO-idle for |
| 5245 | * a jiffy. |
| 5246 | * the system might be non-idle CPU-wise, but we only care |
| 5247 | * about not overloading the IO subsystem. (things like an |
| 5248 | * e2fsck being done on the RAID array should execute fast) |
| 5249 | */ |
| 5250 | mddev->queue->unplug_fn(mddev->queue); |
| 5251 | cond_resched(); |
| 5252 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5253 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5254 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5256 | if (currspeed > speed_min(mddev)) { |
| 5257 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5259 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | goto repeat; |
| 5261 | } |
| 5262 | } |
| 5263 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5264 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | /* |
| 5266 | * this also signals 'finished resyncing' to md_stop |
| 5267 | */ |
| 5268 | out: |
| 5269 | mddev->queue->unplug_fn(mddev->queue); |
| 5270 | |
| 5271 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5272 | |
| 5273 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5274 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | |
| 5276 | if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) && |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5277 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 5278 | !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5279 | mddev->curr_resync > 2) { |
| 5280 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5281 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5282 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5283 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5284 | "md: checkpointing %s of %s.\n", |
| 5285 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5286 | mddev->recovery_cp = mddev->curr_resync; |
| 5287 | } |
| 5288 | } else |
| 5289 | mddev->recovery_cp = MaxSector; |
| 5290 | } else { |
| 5291 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5292 | mddev->curr_resync = MaxSector; |
| 5293 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5294 | if (rdev->raid_disk >= 0 && |
| 5295 | !test_bit(Faulty, &rdev->flags) && |
| 5296 | !test_bit(In_sync, &rdev->flags) && |
| 5297 | rdev->recovery_offset < mddev->curr_resync) |
| 5298 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | } |
| 5301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | skip: |
| 5303 | mddev->curr_resync = 0; |
| 5304 | wake_up(&resync_wait); |
| 5305 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5306 | md_wakeup_thread(mddev->thread); |
| 5307 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5308 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | |
| 5310 | |
| 5311 | /* |
| 5312 | * This routine is regularly called by all per-raid-array threads to |
| 5313 | * deal with generic issues like resync and super-block update. |
| 5314 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 5315 | * need this as they never do any recovery or update the superblock. |
| 5316 | * |
| 5317 | * It does not do any resync itself, but rather "forks" off other threads |
| 5318 | * to do that as needed. |
| 5319 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 5320 | * "->recovery" and create a thread at ->sync_thread. |
| 5321 | * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR) |
| 5322 | * and wakeups up this thread which will reap the thread and finish up. |
| 5323 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 5324 | * |
| 5325 | * The overall approach is: |
| 5326 | * 1/ if the superblock needs updating, update it. |
| 5327 | * 2/ If a recovery thread is running, don't do anything else. |
| 5328 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 5329 | * 4/ If there are any faulty devices, remove them. |
| 5330 | * 5/ If array is degraded, try to add spares devices |
| 5331 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 5332 | */ |
| 5333 | void md_check_recovery(mddev_t *mddev) |
| 5334 | { |
| 5335 | mdk_rdev_t *rdev; |
| 5336 | struct list_head *rtmp; |
| 5337 | |
| 5338 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 5339 | if (mddev->bitmap) |
| 5340 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | |
| 5342 | if (mddev->ro) |
| 5343 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5344 | |
| 5345 | if (signal_pending(current)) { |
| 5346 | if (mddev->pers->sync_request) { |
| 5347 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 5348 | mdname(mddev)); |
| 5349 | mddev->safemode = 2; |
| 5350 | } |
| 5351 | flush_signals(current); |
| 5352 | } |
| 5353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | if ( ! ( |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5355 | mddev->flags || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5357 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 5358 | (mddev->safemode == 1) || |
| 5359 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 5360 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | )) |
| 5362 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5363 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5364 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | int spares =0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5366 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5367 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5368 | if (mddev->safemode && !atomic_read(&mddev->writes_pending) && |
| 5369 | !mddev->in_sync && mddev->recovery_cp == MaxSector) { |
| 5370 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5371 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5372 | } |
| 5373 | if (mddev->safemode == 1) |
| 5374 | mddev->safemode = 0; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5375 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5376 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5377 | if (mddev->flags) |
| 5378 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5379 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 5382 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 5383 | /* resync/recovery still happening */ |
| 5384 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5385 | goto unlock; |
| 5386 | } |
| 5387 | if (mddev->sync_thread) { |
| 5388 | /* resync has finished, collect result */ |
| 5389 | md_unregister_thread(mddev->sync_thread); |
| 5390 | mddev->sync_thread = NULL; |
| 5391 | if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) && |
| 5392 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5393 | /* success...*/ |
| 5394 | /* activate any spares */ |
| 5395 | mddev->pers->spare_active(mddev); |
| 5396 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5397 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5398 | |
| 5399 | /* if array is no-longer degraded, then any saved_raid_disk |
| 5400 | * information must be scrapped |
| 5401 | */ |
| 5402 | if (!mddev->degraded) |
| 5403 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5404 | rdev->saved_raid_disk = -1; |
| 5405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | mddev->recovery = 0; |
| 5407 | /* flag recovery needed just to double check */ |
| 5408 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5409 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | goto unlock; |
| 5411 | } |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5412 | /* Clear some bits that don't mean anything, but |
| 5413 | * might be left set |
| 5414 | */ |
| 5415 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5416 | clear_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 5417 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5418 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5420 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 5421 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | /* no recovery is running. |
| 5423 | * remove any failed drives, then |
| 5424 | * add spares if possible. |
| 5425 | * Spare are also removed and re-added, to allow |
| 5426 | * the personality to fail the re-add. |
| 5427 | */ |
| 5428 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5429 | if (rdev->raid_disk >= 0 && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5430 | (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | atomic_read(&rdev->nr_pending)==0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 5432 | if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) { |
| 5433 | char nm[20]; |
| 5434 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5435 | sysfs_remove_link(&mddev->kobj, nm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | rdev->raid_disk = -1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 5437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | if (mddev->degraded) { |
| 5441 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5442 | if (rdev->raid_disk < 0 |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5443 | && !test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5444 | rdev->recovery_offset = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 5445 | if (mddev->pers->hot_add_disk(mddev,rdev)) { |
| 5446 | char nm[20]; |
| 5447 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 5448 | sysfs_create_link(&mddev->kobj, &rdev->kobj, nm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | spares++; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5450 | md_new_event(mddev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 5451 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | break; |
| 5453 | } |
| 5454 | } |
| 5455 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5456 | if (spares) { |
| 5457 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5458 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5459 | } else if (mddev->recovery_cp < MaxSector) { |
| 5460 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5461 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5462 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5465 | if (mddev->pers->sync_request) { |
| 5466 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 5467 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 5468 | /* We are adding a device or devices to an array |
| 5469 | * which has the bitmap stored on all devices. |
| 5470 | * So make sure all bitmap pages get written |
| 5471 | */ |
| 5472 | bitmap_write_all(mddev->bitmap); |
| 5473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 5475 | mddev, |
| 5476 | "%s_resync"); |
| 5477 | if (!mddev->sync_thread) { |
| 5478 | printk(KERN_ERR "%s: could not start resync" |
| 5479 | " thread...\n", |
| 5480 | mdname(mddev)); |
| 5481 | /* leave the spares where they are, it shouldn't hurt */ |
| 5482 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5483 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5485 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | } |
| 5487 | unlock: |
| 5488 | mddev_unlock(mddev); |
| 5489 | } |
| 5490 | } |
| 5491 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5492 | static int md_notify_reboot(struct notifier_block *this, |
| 5493 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | { |
| 5495 | struct list_head *tmp; |
| 5496 | mddev_t *mddev; |
| 5497 | |
| 5498 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 5499 | |
| 5500 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 5501 | |
| 5502 | ITERATE_MDDEV(mddev,tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 5503 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | do_md_stop (mddev, 1); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 5505 | mddev_unlock(mddev); |
| 5506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | /* |
| 5508 | * certain more exotic SCSI devices are known to be |
| 5509 | * volatile wrt too early system reboots. While the |
| 5510 | * right place to handle this issue is the given |
| 5511 | * driver, we do want to have a safe RAID driver ... |
| 5512 | */ |
| 5513 | mdelay(1000*1); |
| 5514 | } |
| 5515 | return NOTIFY_DONE; |
| 5516 | } |
| 5517 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5518 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | .notifier_call = md_notify_reboot, |
| 5520 | .next = NULL, |
| 5521 | .priority = INT_MAX, /* before any real devices */ |
| 5522 | }; |
| 5523 | |
| 5524 | static void md_geninit(void) |
| 5525 | { |
| 5526 | struct proc_dir_entry *p; |
| 5527 | |
| 5528 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 5529 | |
| 5530 | p = create_proc_entry("mdstat", S_IRUGO, NULL); |
| 5531 | if (p) |
| 5532 | p->proc_fops = &md_seq_fops; |
| 5533 | } |
| 5534 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5535 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | if (register_blkdev(MAJOR_NR, "md")) |
| 5538 | return -1; |
| 5539 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 5540 | unregister_blkdev(MAJOR_NR, "md"); |
| 5541 | return -1; |
| 5542 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5543 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 5544 | md_probe, NULL, NULL); |
| 5545 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | md_probe, NULL, NULL); |
| 5547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5548 | register_reboot_notifier(&md_notifier); |
| 5549 | raid_table_header = register_sysctl_table(raid_root_table, 1); |
| 5550 | |
| 5551 | md_geninit(); |
| 5552 | return (0); |
| 5553 | } |
| 5554 | |
| 5555 | |
| 5556 | #ifndef MODULE |
| 5557 | |
| 5558 | /* |
| 5559 | * Searches all registered partitions for autorun RAID arrays |
| 5560 | * at boot time. |
| 5561 | */ |
| 5562 | static dev_t detected_devices[128]; |
| 5563 | static int dev_cnt; |
| 5564 | |
| 5565 | void md_autodetect_dev(dev_t dev) |
| 5566 | { |
| 5567 | if (dev_cnt >= 0 && dev_cnt < 127) |
| 5568 | detected_devices[dev_cnt++] = dev; |
| 5569 | } |
| 5570 | |
| 5571 | |
| 5572 | static void autostart_arrays(int part) |
| 5573 | { |
| 5574 | mdk_rdev_t *rdev; |
| 5575 | int i; |
| 5576 | |
| 5577 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 5578 | |
| 5579 | for (i = 0; i < dev_cnt; i++) { |
| 5580 | dev_t dev = detected_devices[i]; |
| 5581 | |
| 5582 | rdev = md_import_device(dev,0, 0); |
| 5583 | if (IS_ERR(rdev)) |
| 5584 | continue; |
| 5585 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5586 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | MD_BUG(); |
| 5588 | continue; |
| 5589 | } |
| 5590 | list_add(&rdev->same_set, &pending_raid_disks); |
| 5591 | } |
| 5592 | dev_cnt = 0; |
| 5593 | |
| 5594 | autorun_devices(part); |
| 5595 | } |
| 5596 | |
| 5597 | #endif |
| 5598 | |
| 5599 | static __exit void md_exit(void) |
| 5600 | { |
| 5601 | mddev_t *mddev; |
| 5602 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5603 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5604 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 5605 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | |
| 5607 | unregister_blkdev(MAJOR_NR,"md"); |
| 5608 | unregister_blkdev(mdp_major, "mdp"); |
| 5609 | unregister_reboot_notifier(&md_notifier); |
| 5610 | unregister_sysctl_table(raid_table_header); |
| 5611 | remove_proc_entry("mdstat", NULL); |
| 5612 | ITERATE_MDDEV(mddev,tmp) { |
| 5613 | struct gendisk *disk = mddev->gendisk; |
| 5614 | if (!disk) |
| 5615 | continue; |
| 5616 | export_array(mddev); |
| 5617 | del_gendisk(disk); |
| 5618 | put_disk(disk); |
| 5619 | mddev->gendisk = NULL; |
| 5620 | mddev_put(mddev); |
| 5621 | } |
| 5622 | } |
| 5623 | |
| 5624 | module_init(md_init) |
| 5625 | module_exit(md_exit) |
| 5626 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5627 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 5628 | { |
| 5629 | return sprintf(buffer, "%d", start_readonly); |
| 5630 | } |
| 5631 | static int set_ro(const char *val, struct kernel_param *kp) |
| 5632 | { |
| 5633 | char *e; |
| 5634 | int num = simple_strtoul(val, &e, 10); |
| 5635 | if (*val && (*e == '\0' || *e == '\n')) { |
| 5636 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 5637 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5638 | } |
| 5639 | return -EINVAL; |
| 5640 | } |
| 5641 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 5642 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 5643 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5644 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | EXPORT_SYMBOL(register_md_personality); |
| 5647 | EXPORT_SYMBOL(unregister_md_personality); |
| 5648 | EXPORT_SYMBOL(md_error); |
| 5649 | EXPORT_SYMBOL(md_done_sync); |
| 5650 | EXPORT_SYMBOL(md_write_start); |
| 5651 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | EXPORT_SYMBOL(md_register_thread); |
| 5653 | EXPORT_SYMBOL(md_unregister_thread); |
| 5654 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | EXPORT_SYMBOL(md_check_recovery); |
| 5656 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 5657 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 5658 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |