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 */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 42 | #include <linux/poll.h> |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 43 | #include <linux/mutex.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 44 | #include <linux/ctype.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 45 | #include <linux/freezer.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 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
| 1300 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1301 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1302 | struct list_head *tmp, *tmp2; |
| 1303 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
| 1305 | ITERATE_RDEV(mddev1,rdev,tmp) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1306 | ITERATE_RDEV(mddev2, rdev2, tmp2) |
| 1307 | if (rdev->bdev->bd_contains == |
| 1308 | rdev2->bdev->bd_contains) |
| 1309 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | static LIST_HEAD(pending_raid_disks); |
| 1315 | |
| 1316 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1317 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1318 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1319 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1320 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1321 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| 1323 | if (rdev->mddev) { |
| 1324 | MD_BUG(); |
| 1325 | return -EINVAL; |
| 1326 | } |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1327 | /* make sure rdev->size exceeds mddev->size */ |
| 1328 | if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) { |
| 1329 | if (mddev->pers) |
| 1330 | /* Cannot change size, so fail */ |
| 1331 | return -ENOSPC; |
| 1332 | else |
| 1333 | mddev->size = rdev->size; |
| 1334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| 1336 | /* Verify rdev->desc_nr is unique. |
| 1337 | * If it is -1, assign a free number, else |
| 1338 | * check number is not in use |
| 1339 | */ |
| 1340 | if (rdev->desc_nr < 0) { |
| 1341 | int choice = 0; |
| 1342 | if (mddev->pers) choice = mddev->raid_disks; |
| 1343 | while (find_rdev_nr(mddev, choice)) |
| 1344 | choice++; |
| 1345 | rdev->desc_nr = choice; |
| 1346 | } else { |
| 1347 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1348 | return -EBUSY; |
| 1349 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1350 | bdevname(rdev->bdev,b); |
| 1351 | if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0) |
| 1352 | return -ENOMEM; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1353 | while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL) |
| 1354 | *s = '!'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1357 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1358 | |
NeilBrown | 9c79197 | 2005-11-08 21:39:28 -0800 | [diff] [blame] | 1359 | rdev->kobj.parent = &mddev->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1360 | if ((err = kobject_add(&rdev->kobj))) |
| 1361 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1362 | |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1363 | if (rdev->bdev->bd_part) |
| 1364 | ko = &rdev->bdev->bd_part->kobj; |
| 1365 | else |
| 1366 | ko = &rdev->bdev->bd_disk->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1367 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1368 | kobject_del(&rdev->kobj); |
| 1369 | goto fail; |
| 1370 | } |
| 1371 | list_add(&rdev->same_set, &mddev->disks); |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1372 | bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1374 | |
| 1375 | fail: |
| 1376 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1377 | b, mdname(mddev)); |
| 1378 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1381 | static void delayed_delete(struct work_struct *ws) |
| 1382 | { |
| 1383 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1384 | kobject_del(&rdev->kobj); |
| 1385 | } |
| 1386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1388 | { |
| 1389 | char b[BDEVNAME_SIZE]; |
| 1390 | if (!rdev->mddev) { |
| 1391 | MD_BUG(); |
| 1392 | return; |
| 1393 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1394 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | list_del_init(&rdev->same_set); |
| 1396 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1397 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1398 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1399 | |
| 1400 | /* We need to delay this, otherwise we can deadlock when |
| 1401 | * writing to 'remove' to "dev/state" |
| 1402 | */ |
| 1403 | INIT_WORK(&rdev->del_work, delayed_delete); |
| 1404 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | /* |
| 1408 | * prevent the device from being mounted, repartitioned or |
| 1409 | * otherwise reused by a RAID array (or any other kernel |
| 1410 | * subsystem), by bd_claiming the device. |
| 1411 | */ |
| 1412 | static int lock_rdev(mdk_rdev_t *rdev, dev_t dev) |
| 1413 | { |
| 1414 | int err = 0; |
| 1415 | struct block_device *bdev; |
| 1416 | char b[BDEVNAME_SIZE]; |
| 1417 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1418 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | if (IS_ERR(bdev)) { |
| 1420 | printk(KERN_ERR "md: could not open %s.\n", |
| 1421 | __bdevname(dev, b)); |
| 1422 | return PTR_ERR(bdev); |
| 1423 | } |
| 1424 | err = bd_claim(bdev, rdev); |
| 1425 | if (err) { |
| 1426 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1427 | bdevname(bdev, b)); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1428 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | return err; |
| 1430 | } |
| 1431 | rdev->bdev = bdev; |
| 1432 | return err; |
| 1433 | } |
| 1434 | |
| 1435 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1436 | { |
| 1437 | struct block_device *bdev = rdev->bdev; |
| 1438 | rdev->bdev = NULL; |
| 1439 | if (!bdev) |
| 1440 | MD_BUG(); |
| 1441 | bd_release(bdev); |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1442 | blkdev_put(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | void md_autodetect_dev(dev_t dev); |
| 1446 | |
| 1447 | static void export_rdev(mdk_rdev_t * rdev) |
| 1448 | { |
| 1449 | char b[BDEVNAME_SIZE]; |
| 1450 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1451 | bdevname(rdev->bdev,b)); |
| 1452 | if (rdev->mddev) |
| 1453 | MD_BUG(); |
| 1454 | free_disk_sb(rdev); |
| 1455 | list_del_init(&rdev->same_set); |
| 1456 | #ifndef MODULE |
| 1457 | md_autodetect_dev(rdev->bdev->bd_dev); |
| 1458 | #endif |
| 1459 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1460 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1464 | { |
| 1465 | unbind_rdev_from_array(rdev); |
| 1466 | export_rdev(rdev); |
| 1467 | } |
| 1468 | |
| 1469 | static void export_array(mddev_t *mddev) |
| 1470 | { |
| 1471 | struct list_head *tmp; |
| 1472 | mdk_rdev_t *rdev; |
| 1473 | |
| 1474 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1475 | if (!rdev->mddev) { |
| 1476 | MD_BUG(); |
| 1477 | continue; |
| 1478 | } |
| 1479 | kick_rdev_from_array(rdev); |
| 1480 | } |
| 1481 | if (!list_empty(&mddev->disks)) |
| 1482 | MD_BUG(); |
| 1483 | mddev->raid_disks = 0; |
| 1484 | mddev->major_version = 0; |
| 1485 | } |
| 1486 | |
| 1487 | static void print_desc(mdp_disk_t *desc) |
| 1488 | { |
| 1489 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1490 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1491 | } |
| 1492 | |
| 1493 | static void print_sb(mdp_super_t *sb) |
| 1494 | { |
| 1495 | int i; |
| 1496 | |
| 1497 | printk(KERN_INFO |
| 1498 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1499 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1500 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1501 | sb->ctime); |
| 1502 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1503 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1504 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1505 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1506 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1507 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1508 | sb->failed_disks, sb->spare_disks, |
| 1509 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1510 | |
| 1511 | printk(KERN_INFO); |
| 1512 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1513 | mdp_disk_t *desc; |
| 1514 | |
| 1515 | desc = sb->disks + i; |
| 1516 | if (desc->number || desc->major || desc->minor || |
| 1517 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1518 | printk(" D %2d: ", i); |
| 1519 | print_desc(desc); |
| 1520 | } |
| 1521 | } |
| 1522 | printk(KERN_INFO "md: THIS: "); |
| 1523 | print_desc(&sb->this_disk); |
| 1524 | |
| 1525 | } |
| 1526 | |
| 1527 | static void print_rdev(mdk_rdev_t *rdev) |
| 1528 | { |
| 1529 | char b[BDEVNAME_SIZE]; |
| 1530 | printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n", |
| 1531 | bdevname(rdev->bdev,b), (unsigned long long)rdev->size, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1532 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1533 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | if (rdev->sb_loaded) { |
| 1535 | printk(KERN_INFO "md: rdev superblock:\n"); |
| 1536 | print_sb((mdp_super_t*)page_address(rdev->sb_page)); |
| 1537 | } else |
| 1538 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1539 | } |
| 1540 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1541 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | { |
| 1543 | struct list_head *tmp, *tmp2; |
| 1544 | mdk_rdev_t *rdev; |
| 1545 | mddev_t *mddev; |
| 1546 | char b[BDEVNAME_SIZE]; |
| 1547 | |
| 1548 | printk("\n"); |
| 1549 | printk("md: **********************************\n"); |
| 1550 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1551 | printk("md: **********************************\n"); |
| 1552 | ITERATE_MDDEV(mddev,tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1554 | if (mddev->bitmap) |
| 1555 | bitmap_print_sb(mddev->bitmap); |
| 1556 | else |
| 1557 | printk("%s: ", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | ITERATE_RDEV(mddev,rdev,tmp2) |
| 1559 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1560 | printk("\n"); |
| 1561 | |
| 1562 | ITERATE_RDEV(mddev,rdev,tmp2) |
| 1563 | print_rdev(rdev); |
| 1564 | } |
| 1565 | printk("md: **********************************\n"); |
| 1566 | printk("\n"); |
| 1567 | } |
| 1568 | |
| 1569 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1570 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1572 | /* Update each superblock (in-memory image), but |
| 1573 | * if we are allowed to, skip spares which already |
| 1574 | * have the right event counter, or have one earlier |
| 1575 | * (which would mean they aren't being marked as dirty |
| 1576 | * with the rest of the array) |
| 1577 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | mdk_rdev_t *rdev; |
| 1579 | struct list_head *tmp; |
| 1580 | |
| 1581 | ITERATE_RDEV(mddev,rdev,tmp) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1582 | if (rdev->sb_events == mddev->events || |
| 1583 | (nospares && |
| 1584 | rdev->raid_disk < 0 && |
| 1585 | (rdev->sb_events&1)==0 && |
| 1586 | rdev->sb_events+1 == mddev->events)) { |
| 1587 | /* Don't update this superblock */ |
| 1588 | rdev->sb_loaded = 2; |
| 1589 | } else { |
| 1590 | super_types[mddev->major_version]. |
| 1591 | sync_super(mddev, rdev); |
| 1592 | rdev->sb_loaded = 1; |
| 1593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1597 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1599 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | struct list_head *tmp; |
| 1601 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1602 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1603 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1606 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1607 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1608 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1609 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1610 | force_change = 1; |
| 1611 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1612 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1613 | * though if events isn't the right even/odd, we will have to do |
| 1614 | * spares after all |
| 1615 | */ |
| 1616 | nospares = 1; |
| 1617 | if (force_change) |
| 1618 | nospares = 0; |
| 1619 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1620 | /* If the array is degraded, then skipping spares is both |
| 1621 | * dangerous and fairly pointless. |
| 1622 | * Dangerous because a device that was removed from the array |
| 1623 | * might have a event_count that still looks up-to-date, |
| 1624 | * so it can be re-added without a resync. |
| 1625 | * Pointless because if there are any spares to skip, |
| 1626 | * then a recovery will happen and soon that array won't |
| 1627 | * be degraded any more and the spare can go back to sleep then. |
| 1628 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1629 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1630 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1631 | sync_req = mddev->in_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | mddev->utime = get_seconds(); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1633 | |
| 1634 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1635 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1636 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1637 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1638 | && (mddev->events & 1) |
| 1639 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1640 | mddev->events--; |
| 1641 | else { |
| 1642 | /* otherwise we have to go forward and ... */ |
| 1643 | mddev->events ++; |
| 1644 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1645 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1646 | if ((mddev->events&1)==0) { |
| 1647 | mddev->events++; |
| 1648 | nospares = 0; |
| 1649 | } |
| 1650 | } else { |
| 1651 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1652 | if ((mddev->events&1)) { |
| 1653 | mddev->events++; |
| 1654 | nospares = 0; |
| 1655 | } |
| 1656 | } |
| 1657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | |
| 1659 | if (!mddev->events) { |
| 1660 | /* |
| 1661 | * oops, this 64-bit counter should never wrap. |
| 1662 | * Either we are in around ~1 trillion A.C., assuming |
| 1663 | * 1 reboot per second, or we have a bug: |
| 1664 | */ |
| 1665 | MD_BUG(); |
| 1666 | mddev->events --; |
| 1667 | } |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1668 | sync_sbs(mddev, nospares); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | |
| 1670 | /* |
| 1671 | * do not write anything to disk if using |
| 1672 | * nonpersistent superblocks |
| 1673 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1674 | if (!mddev->persistent) { |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1675 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1676 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1677 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1679 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1680 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | |
| 1682 | dprintk(KERN_INFO |
| 1683 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1684 | mdname(mddev),mddev->in_sync); |
| 1685 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1686 | err = bitmap_update_sb(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1688 | char b[BDEVNAME_SIZE]; |
| 1689 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1690 | if (rdev->sb_loaded != 1) |
| 1691 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1692 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | dprintk("(skipping faulty "); |
| 1694 | |
| 1695 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1696 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1697 | md_super_write(mddev,rdev, |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1698 | rdev->sb_offset<<1, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1699 | rdev->sb_page); |
| 1700 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1701 | bdevname(rdev->bdev,b), |
| 1702 | (unsigned long long)rdev->sb_offset); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1703 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | } else |
| 1706 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1707 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | /* only need to write one superblock... */ |
| 1709 | break; |
| 1710 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1711 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1712 | /* 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] | 1713 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1714 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1715 | if (mddev->in_sync != sync_req || |
| 1716 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1717 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1718 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1719 | goto repeat; |
| 1720 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1721 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1722 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1723 | wake_up(&mddev->sb_wait); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 1727 | /* words written to sysfs files may, or my not, be \n terminated. |
| 1728 | * We want to accept with case. For this we use cmd_match. |
| 1729 | */ |
| 1730 | static int cmd_match(const char *cmd, const char *str) |
| 1731 | { |
| 1732 | /* See if cmd, written into a sysfs file, matches |
| 1733 | * str. They must either be the same, or cmd can |
| 1734 | * have a trailing newline |
| 1735 | */ |
| 1736 | while (*cmd && *str && *cmd == *str) { |
| 1737 | cmd++; |
| 1738 | str++; |
| 1739 | } |
| 1740 | if (*cmd == '\n') |
| 1741 | cmd++; |
| 1742 | if (*str || *cmd) |
| 1743 | return 0; |
| 1744 | return 1; |
| 1745 | } |
| 1746 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1747 | struct rdev_sysfs_entry { |
| 1748 | struct attribute attr; |
| 1749 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 1750 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 1751 | }; |
| 1752 | |
| 1753 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1754 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1755 | { |
| 1756 | char *sep = ""; |
| 1757 | int len=0; |
| 1758 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1759 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1760 | len+= sprintf(page+len, "%sfaulty",sep); |
| 1761 | sep = ","; |
| 1762 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1763 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1764 | len += sprintf(page+len, "%sin_sync",sep); |
| 1765 | sep = ","; |
| 1766 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1767 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 1768 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 1769 | sep = ","; |
| 1770 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1771 | if (!test_bit(Faulty, &rdev->flags) && |
| 1772 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1773 | len += sprintf(page+len, "%sspare", sep); |
| 1774 | sep = ","; |
| 1775 | } |
| 1776 | return len+sprintf(page+len, "\n"); |
| 1777 | } |
| 1778 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1779 | static ssize_t |
| 1780 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1781 | { |
| 1782 | /* can write |
| 1783 | * faulty - simulates and error |
| 1784 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1785 | * writemostly - sets write_mostly |
| 1786 | * -writemostly - clears write_mostly |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1787 | */ |
| 1788 | int err = -EINVAL; |
| 1789 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 1790 | md_error(rdev->mddev, rdev); |
| 1791 | err = 0; |
| 1792 | } else if (cmd_match(buf, "remove")) { |
| 1793 | if (rdev->raid_disk >= 0) |
| 1794 | err = -EBUSY; |
| 1795 | else { |
| 1796 | mddev_t *mddev = rdev->mddev; |
| 1797 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 1798 | if (mddev->pers) |
| 1799 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1800 | md_new_event(mddev); |
| 1801 | err = 0; |
| 1802 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 1803 | } else if (cmd_match(buf, "writemostly")) { |
| 1804 | set_bit(WriteMostly, &rdev->flags); |
| 1805 | err = 0; |
| 1806 | } else if (cmd_match(buf, "-writemostly")) { |
| 1807 | clear_bit(WriteMostly, &rdev->flags); |
| 1808 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 1809 | } |
| 1810 | return err ? err : len; |
| 1811 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1812 | static struct rdev_sysfs_entry rdev_state = |
| 1813 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1814 | |
| 1815 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1816 | super_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1817 | { |
| 1818 | if (rdev->sb_loaded && rdev->sb_size) { |
| 1819 | memcpy(page, page_address(rdev->sb_page), rdev->sb_size); |
| 1820 | return rdev->sb_size; |
| 1821 | } else |
| 1822 | return 0; |
| 1823 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 1824 | static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super); |
| 1825 | |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1826 | static ssize_t |
| 1827 | errors_show(mdk_rdev_t *rdev, char *page) |
| 1828 | { |
| 1829 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 1830 | } |
| 1831 | |
| 1832 | static ssize_t |
| 1833 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1834 | { |
| 1835 | char *e; |
| 1836 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 1837 | if (*buf && (*e == 0 || *e == '\n')) { |
| 1838 | atomic_set(&rdev->corrected_errors, n); |
| 1839 | return len; |
| 1840 | } |
| 1841 | return -EINVAL; |
| 1842 | } |
| 1843 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1844 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1845 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1846 | static ssize_t |
| 1847 | slot_show(mdk_rdev_t *rdev, char *page) |
| 1848 | { |
| 1849 | if (rdev->raid_disk < 0) |
| 1850 | return sprintf(page, "none\n"); |
| 1851 | else |
| 1852 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 1853 | } |
| 1854 | |
| 1855 | static ssize_t |
| 1856 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1857 | { |
| 1858 | char *e; |
| 1859 | int slot = simple_strtoul(buf, &e, 10); |
| 1860 | if (strncmp(buf, "none", 4)==0) |
| 1861 | slot = -1; |
| 1862 | else if (e==buf || (*e && *e!= '\n')) |
| 1863 | return -EINVAL; |
| 1864 | if (rdev->mddev->pers) |
| 1865 | /* Cannot set slot in active array (yet) */ |
| 1866 | return -EBUSY; |
| 1867 | if (slot >= rdev->mddev->raid_disks) |
| 1868 | return -ENOSPC; |
| 1869 | rdev->raid_disk = slot; |
| 1870 | /* assume it is working */ |
| 1871 | rdev->flags = 0; |
| 1872 | set_bit(In_sync, &rdev->flags); |
| 1873 | return len; |
| 1874 | } |
| 1875 | |
| 1876 | |
| 1877 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1878 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1879 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1880 | static ssize_t |
| 1881 | offset_show(mdk_rdev_t *rdev, char *page) |
| 1882 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 1883 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | static ssize_t |
| 1887 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1888 | { |
| 1889 | char *e; |
| 1890 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 1891 | if (e==buf || (*e && *e != '\n')) |
| 1892 | return -EINVAL; |
| 1893 | if (rdev->mddev->pers) |
| 1894 | return -EBUSY; |
| 1895 | rdev->data_offset = offset; |
| 1896 | return len; |
| 1897 | } |
| 1898 | |
| 1899 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1900 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1901 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 1902 | static ssize_t |
| 1903 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 1904 | { |
| 1905 | return sprintf(page, "%llu\n", (unsigned long long)rdev->size); |
| 1906 | } |
| 1907 | |
| 1908 | static ssize_t |
| 1909 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 1910 | { |
| 1911 | char *e; |
| 1912 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 1913 | if (e==buf || (*e && *e != '\n')) |
| 1914 | return -EINVAL; |
| 1915 | if (rdev->mddev->pers) |
| 1916 | return -EBUSY; |
| 1917 | rdev->size = size; |
| 1918 | if (size < rdev->mddev->size || rdev->mddev->size == 0) |
| 1919 | rdev->mddev->size = size; |
| 1920 | return len; |
| 1921 | } |
| 1922 | |
| 1923 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 1924 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 1925 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1926 | static struct attribute *rdev_default_attrs[] = { |
| 1927 | &rdev_state.attr, |
| 1928 | &rdev_super.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1929 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1930 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 1931 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 1932 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1933 | NULL, |
| 1934 | }; |
| 1935 | static ssize_t |
| 1936 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 1937 | { |
| 1938 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 1939 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
| 1940 | |
| 1941 | if (!entry->show) |
| 1942 | return -EIO; |
| 1943 | return entry->show(rdev, page); |
| 1944 | } |
| 1945 | |
| 1946 | static ssize_t |
| 1947 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 1948 | const char *page, size_t length) |
| 1949 | { |
| 1950 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 1951 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
| 1952 | |
| 1953 | if (!entry->store) |
| 1954 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 1955 | if (!capable(CAP_SYS_ADMIN)) |
| 1956 | return -EACCES; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1957 | return entry->store(rdev, page, length); |
| 1958 | } |
| 1959 | |
| 1960 | static void rdev_free(struct kobject *ko) |
| 1961 | { |
| 1962 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 1963 | kfree(rdev); |
| 1964 | } |
| 1965 | static struct sysfs_ops rdev_sysfs_ops = { |
| 1966 | .show = rdev_attr_show, |
| 1967 | .store = rdev_attr_store, |
| 1968 | }; |
| 1969 | static struct kobj_type rdev_ktype = { |
| 1970 | .release = rdev_free, |
| 1971 | .sysfs_ops = &rdev_sysfs_ops, |
| 1972 | .default_attrs = rdev_default_attrs, |
| 1973 | }; |
| 1974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | /* |
| 1976 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 1977 | * |
| 1978 | * mark the device faulty if: |
| 1979 | * |
| 1980 | * - the device is nonexistent (zero size) |
| 1981 | * - the device has no valid superblock |
| 1982 | * |
| 1983 | * a faulty rdev _never_ has rdev->sb set. |
| 1984 | */ |
| 1985 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 1986 | { |
| 1987 | char b[BDEVNAME_SIZE]; |
| 1988 | int err; |
| 1989 | mdk_rdev_t *rdev; |
| 1990 | sector_t size; |
| 1991 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 1992 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | if (!rdev) { |
| 1994 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 1995 | return ERR_PTR(-ENOMEM); |
| 1996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | |
| 1998 | if ((err = alloc_disk_sb(rdev))) |
| 1999 | goto abort_free; |
| 2000 | |
| 2001 | err = lock_rdev(rdev, newdev); |
| 2002 | if (err) |
| 2003 | goto abort_free; |
| 2004 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2005 | rdev->kobj.parent = NULL; |
| 2006 | rdev->kobj.ktype = &rdev_ktype; |
| 2007 | kobject_init(&rdev->kobj); |
| 2008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2010 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2011 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2012 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2014 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2016 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2017 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
| 2019 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2020 | if (!size) { |
| 2021 | printk(KERN_WARNING |
| 2022 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2023 | bdevname(rdev->bdev,b)); |
| 2024 | err = -EINVAL; |
| 2025 | goto abort_free; |
| 2026 | } |
| 2027 | |
| 2028 | if (super_format >= 0) { |
| 2029 | err = super_types[super_format]. |
| 2030 | load_super(rdev, NULL, super_minor); |
| 2031 | if (err == -EINVAL) { |
| 2032 | printk(KERN_WARNING |
| 2033 | "md: %s has invalid sb, not importing!\n", |
| 2034 | bdevname(rdev->bdev,b)); |
| 2035 | goto abort_free; |
| 2036 | } |
| 2037 | if (err < 0) { |
| 2038 | printk(KERN_WARNING |
| 2039 | "md: could not read %s's sb, not importing!\n", |
| 2040 | bdevname(rdev->bdev,b)); |
| 2041 | goto abort_free; |
| 2042 | } |
| 2043 | } |
| 2044 | INIT_LIST_HEAD(&rdev->same_set); |
| 2045 | |
| 2046 | return rdev; |
| 2047 | |
| 2048 | abort_free: |
| 2049 | if (rdev->sb_page) { |
| 2050 | if (rdev->bdev) |
| 2051 | unlock_rdev(rdev); |
| 2052 | free_disk_sb(rdev); |
| 2053 | } |
| 2054 | kfree(rdev); |
| 2055 | return ERR_PTR(err); |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * Check a full RAID array for plausibility |
| 2060 | */ |
| 2061 | |
| 2062 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2063 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | { |
| 2065 | int i; |
| 2066 | struct list_head *tmp; |
| 2067 | mdk_rdev_t *rdev, *freshest; |
| 2068 | char b[BDEVNAME_SIZE]; |
| 2069 | |
| 2070 | freshest = NULL; |
| 2071 | ITERATE_RDEV(mddev,rdev,tmp) |
| 2072 | switch (super_types[mddev->major_version]. |
| 2073 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2074 | case 1: |
| 2075 | freshest = rdev; |
| 2076 | break; |
| 2077 | case 0: |
| 2078 | break; |
| 2079 | default: |
| 2080 | printk( KERN_ERR \ |
| 2081 | "md: fatal superblock inconsistency in %s" |
| 2082 | " -- removing from array\n", |
| 2083 | bdevname(rdev->bdev,b)); |
| 2084 | kick_rdev_from_array(rdev); |
| 2085 | } |
| 2086 | |
| 2087 | |
| 2088 | super_types[mddev->major_version]. |
| 2089 | validate_super(mddev, freshest); |
| 2090 | |
| 2091 | i = 0; |
| 2092 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 2093 | if (rdev != freshest) |
| 2094 | if (super_types[mddev->major_version]. |
| 2095 | validate_super(mddev, rdev)) { |
| 2096 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2097 | " from array!\n", |
| 2098 | bdevname(rdev->bdev,b)); |
| 2099 | kick_rdev_from_array(rdev); |
| 2100 | continue; |
| 2101 | } |
| 2102 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2103 | rdev->desc_nr = i++; |
| 2104 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2105 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | |
| 2110 | |
| 2111 | if (mddev->recovery_cp != MaxSector && |
| 2112 | mddev->level >= 1) |
| 2113 | printk(KERN_ERR "md: %s: raid array is not clean" |
| 2114 | " -- starting background reconstruction\n", |
| 2115 | mdname(mddev)); |
| 2116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2119 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2120 | safe_delay_show(mddev_t *mddev, char *page) |
| 2121 | { |
| 2122 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2123 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2124 | } |
| 2125 | static ssize_t |
| 2126 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2127 | { |
| 2128 | int scale=1; |
| 2129 | int dot=0; |
| 2130 | int i; |
| 2131 | unsigned long msec; |
| 2132 | char buf[30]; |
| 2133 | char *e; |
| 2134 | /* remove a period, and count digits after it */ |
| 2135 | if (len >= sizeof(buf)) |
| 2136 | return -EINVAL; |
| 2137 | strlcpy(buf, cbuf, len); |
| 2138 | buf[len] = 0; |
| 2139 | for (i=0; i<len; i++) { |
| 2140 | if (dot) { |
| 2141 | if (isdigit(buf[i])) { |
| 2142 | buf[i-1] = buf[i]; |
| 2143 | scale *= 10; |
| 2144 | } |
| 2145 | buf[i] = 0; |
| 2146 | } else if (buf[i] == '.') { |
| 2147 | dot=1; |
| 2148 | buf[i] = 0; |
| 2149 | } |
| 2150 | } |
| 2151 | msec = simple_strtoul(buf, &e, 10); |
| 2152 | if (e == buf || (*e && *e != '\n')) |
| 2153 | return -EINVAL; |
| 2154 | msec = (msec * 1000) / scale; |
| 2155 | if (msec == 0) |
| 2156 | mddev->safemode_delay = 0; |
| 2157 | else { |
| 2158 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2159 | if (mddev->safemode_delay == 0) |
| 2160 | mddev->safemode_delay = 1; |
| 2161 | } |
| 2162 | return len; |
| 2163 | } |
| 2164 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2165 | __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] | 2166 | |
| 2167 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2168 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2169 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2170 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2171 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2172 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2173 | else if (mddev->clevel[0]) |
| 2174 | return sprintf(page, "%s\n", mddev->clevel); |
| 2175 | else if (mddev->level != LEVEL_NONE) |
| 2176 | return sprintf(page, "%d\n", mddev->level); |
| 2177 | else |
| 2178 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2179 | } |
| 2180 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2181 | static ssize_t |
| 2182 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2183 | { |
| 2184 | int rv = len; |
| 2185 | if (mddev->pers) |
| 2186 | return -EBUSY; |
| 2187 | if (len == 0) |
| 2188 | return 0; |
| 2189 | if (len >= sizeof(mddev->clevel)) |
| 2190 | return -ENOSPC; |
| 2191 | strncpy(mddev->clevel, buf, len); |
| 2192 | if (mddev->clevel[len-1] == '\n') |
| 2193 | len--; |
| 2194 | mddev->clevel[len] = 0; |
| 2195 | mddev->level = LEVEL_NONE; |
| 2196 | return rv; |
| 2197 | } |
| 2198 | |
| 2199 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2200 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2201 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2202 | |
| 2203 | static ssize_t |
| 2204 | layout_show(mddev_t *mddev, char *page) |
| 2205 | { |
| 2206 | /* just a number, not meaningful for all levels */ |
| 2207 | return sprintf(page, "%d\n", mddev->layout); |
| 2208 | } |
| 2209 | |
| 2210 | static ssize_t |
| 2211 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2212 | { |
| 2213 | char *e; |
| 2214 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2215 | if (mddev->pers) |
| 2216 | return -EBUSY; |
| 2217 | |
| 2218 | if (!*buf || (*e && *e != '\n')) |
| 2219 | return -EINVAL; |
| 2220 | |
| 2221 | mddev->layout = n; |
| 2222 | return len; |
| 2223 | } |
| 2224 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2225 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2226 | |
| 2227 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2228 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2229 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2230 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2231 | if (mddev->raid_disks == 0) |
| 2232 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2233 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2234 | } |
| 2235 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2236 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2237 | |
| 2238 | static ssize_t |
| 2239 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2240 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2241 | char *e; |
| 2242 | int rv = 0; |
| 2243 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2244 | |
| 2245 | if (!*buf || (*e && *e != '\n')) |
| 2246 | return -EINVAL; |
| 2247 | |
| 2248 | if (mddev->pers) |
| 2249 | rv = update_raid_disks(mddev, n); |
| 2250 | else |
| 2251 | mddev->raid_disks = n; |
| 2252 | return rv ? rv : len; |
| 2253 | } |
| 2254 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2255 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2256 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2257 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2258 | chunk_size_show(mddev_t *mddev, char *page) |
| 2259 | { |
| 2260 | return sprintf(page, "%d\n", mddev->chunk_size); |
| 2261 | } |
| 2262 | |
| 2263 | static ssize_t |
| 2264 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2265 | { |
| 2266 | /* can only set chunk_size if array is not yet active */ |
| 2267 | char *e; |
| 2268 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2269 | |
| 2270 | if (mddev->pers) |
| 2271 | return -EBUSY; |
| 2272 | if (!*buf || (*e && *e != '\n')) |
| 2273 | return -EINVAL; |
| 2274 | |
| 2275 | mddev->chunk_size = n; |
| 2276 | return len; |
| 2277 | } |
| 2278 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2279 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2280 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2281 | static ssize_t |
| 2282 | resync_start_show(mddev_t *mddev, char *page) |
| 2283 | { |
| 2284 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2285 | } |
| 2286 | |
| 2287 | static ssize_t |
| 2288 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2289 | { |
| 2290 | /* can only set chunk_size if array is not yet active */ |
| 2291 | char *e; |
| 2292 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2293 | |
| 2294 | if (mddev->pers) |
| 2295 | return -EBUSY; |
| 2296 | if (!*buf || (*e && *e != '\n')) |
| 2297 | return -EINVAL; |
| 2298 | |
| 2299 | mddev->recovery_cp = n; |
| 2300 | return len; |
| 2301 | } |
| 2302 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2303 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2304 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2305 | /* |
| 2306 | * The array state can be: |
| 2307 | * |
| 2308 | * clear |
| 2309 | * No devices, no size, no level |
| 2310 | * Equivalent to STOP_ARRAY ioctl |
| 2311 | * inactive |
| 2312 | * May have some settings, but array is not active |
| 2313 | * all IO results in error |
| 2314 | * When written, doesn't tear down array, but just stops it |
| 2315 | * suspended (not supported yet) |
| 2316 | * All IO requests will block. The array can be reconfigured. |
| 2317 | * Writing this, if accepted, will block until array is quiessent |
| 2318 | * readonly |
| 2319 | * no resync can happen. no superblocks get written. |
| 2320 | * write requests fail |
| 2321 | * read-auto |
| 2322 | * like readonly, but behaves like 'clean' on a write request. |
| 2323 | * |
| 2324 | * clean - no pending writes, but otherwise active. |
| 2325 | * When written to inactive array, starts without resync |
| 2326 | * If a write request arrives then |
| 2327 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2328 | * if not known, block and switch to write-pending |
| 2329 | * If written to an active array that has pending writes, then fails. |
| 2330 | * active |
| 2331 | * fully active: IO and resync can be happening. |
| 2332 | * When written to inactive array, starts with resync |
| 2333 | * |
| 2334 | * write-pending |
| 2335 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2336 | * |
| 2337 | * active-idle |
| 2338 | * like active, but no writes have been seen for a while (100msec). |
| 2339 | * |
| 2340 | */ |
| 2341 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2342 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2343 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2344 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2345 | "write-pending", "active-idle", NULL }; |
| 2346 | |
| 2347 | static int match_word(const char *word, char **list) |
| 2348 | { |
| 2349 | int n; |
| 2350 | for (n=0; list[n]; n++) |
| 2351 | if (cmd_match(word, list[n])) |
| 2352 | break; |
| 2353 | return n; |
| 2354 | } |
| 2355 | |
| 2356 | static ssize_t |
| 2357 | array_state_show(mddev_t *mddev, char *page) |
| 2358 | { |
| 2359 | enum array_state st = inactive; |
| 2360 | |
| 2361 | if (mddev->pers) |
| 2362 | switch(mddev->ro) { |
| 2363 | case 1: |
| 2364 | st = readonly; |
| 2365 | break; |
| 2366 | case 2: |
| 2367 | st = read_auto; |
| 2368 | break; |
| 2369 | case 0: |
| 2370 | if (mddev->in_sync) |
| 2371 | st = clean; |
| 2372 | else if (mddev->safemode) |
| 2373 | st = active_idle; |
| 2374 | else |
| 2375 | st = active; |
| 2376 | } |
| 2377 | else { |
| 2378 | if (list_empty(&mddev->disks) && |
| 2379 | mddev->raid_disks == 0 && |
| 2380 | mddev->size == 0) |
| 2381 | st = clear; |
| 2382 | else |
| 2383 | st = inactive; |
| 2384 | } |
| 2385 | return sprintf(page, "%s\n", array_states[st]); |
| 2386 | } |
| 2387 | |
| 2388 | static int do_md_stop(mddev_t * mddev, int ro); |
| 2389 | static int do_md_run(mddev_t * mddev); |
| 2390 | static int restart_array(mddev_t *mddev); |
| 2391 | |
| 2392 | static ssize_t |
| 2393 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2394 | { |
| 2395 | int err = -EINVAL; |
| 2396 | enum array_state st = match_word(buf, array_states); |
| 2397 | switch(st) { |
| 2398 | case bad_word: |
| 2399 | break; |
| 2400 | case clear: |
| 2401 | /* stopping an active array */ |
| 2402 | if (mddev->pers) { |
| 2403 | if (atomic_read(&mddev->active) > 1) |
| 2404 | return -EBUSY; |
| 2405 | err = do_md_stop(mddev, 0); |
| 2406 | } |
| 2407 | break; |
| 2408 | case inactive: |
| 2409 | /* stopping an active array */ |
| 2410 | if (mddev->pers) { |
| 2411 | if (atomic_read(&mddev->active) > 1) |
| 2412 | return -EBUSY; |
| 2413 | err = do_md_stop(mddev, 2); |
| 2414 | } |
| 2415 | break; |
| 2416 | case suspended: |
| 2417 | break; /* not supported yet */ |
| 2418 | case readonly: |
| 2419 | if (mddev->pers) |
| 2420 | err = do_md_stop(mddev, 1); |
| 2421 | else { |
| 2422 | mddev->ro = 1; |
| 2423 | err = do_md_run(mddev); |
| 2424 | } |
| 2425 | break; |
| 2426 | case read_auto: |
| 2427 | /* stopping an active array */ |
| 2428 | if (mddev->pers) { |
| 2429 | err = do_md_stop(mddev, 1); |
| 2430 | if (err == 0) |
| 2431 | mddev->ro = 2; /* FIXME mark devices writable */ |
| 2432 | } else { |
| 2433 | mddev->ro = 2; |
| 2434 | err = do_md_run(mddev); |
| 2435 | } |
| 2436 | break; |
| 2437 | case clean: |
| 2438 | if (mddev->pers) { |
| 2439 | restart_array(mddev); |
| 2440 | spin_lock_irq(&mddev->write_lock); |
| 2441 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 2442 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2443 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2444 | } |
| 2445 | spin_unlock_irq(&mddev->write_lock); |
| 2446 | } else { |
| 2447 | mddev->ro = 0; |
| 2448 | mddev->recovery_cp = MaxSector; |
| 2449 | err = do_md_run(mddev); |
| 2450 | } |
| 2451 | break; |
| 2452 | case active: |
| 2453 | if (mddev->pers) { |
| 2454 | restart_array(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2455 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2456 | wake_up(&mddev->sb_wait); |
| 2457 | err = 0; |
| 2458 | } else { |
| 2459 | mddev->ro = 0; |
| 2460 | err = do_md_run(mddev); |
| 2461 | } |
| 2462 | break; |
| 2463 | case write_pending: |
| 2464 | case active_idle: |
| 2465 | /* these cannot be set */ |
| 2466 | break; |
| 2467 | } |
| 2468 | if (err) |
| 2469 | return err; |
| 2470 | else |
| 2471 | return len; |
| 2472 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2473 | static struct md_sysfs_entry md_array_state = |
| 2474 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2475 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2476 | static ssize_t |
| 2477 | null_show(mddev_t *mddev, char *page) |
| 2478 | { |
| 2479 | return -EINVAL; |
| 2480 | } |
| 2481 | |
| 2482 | static ssize_t |
| 2483 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 2484 | { |
| 2485 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 2486 | /* The new device is added to the array. |
| 2487 | * If the array has a persistent superblock, we read the |
| 2488 | * superblock to initialise info and check validity. |
| 2489 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 2490 | * which mainly checks size. |
| 2491 | */ |
| 2492 | char *e; |
| 2493 | int major = simple_strtoul(buf, &e, 10); |
| 2494 | int minor; |
| 2495 | dev_t dev; |
| 2496 | mdk_rdev_t *rdev; |
| 2497 | int err; |
| 2498 | |
| 2499 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 2500 | return -EINVAL; |
| 2501 | minor = simple_strtoul(e+1, &e, 10); |
| 2502 | if (*e && *e != '\n') |
| 2503 | return -EINVAL; |
| 2504 | dev = MKDEV(major, minor); |
| 2505 | if (major != MAJOR(dev) || |
| 2506 | minor != MINOR(dev)) |
| 2507 | return -EOVERFLOW; |
| 2508 | |
| 2509 | |
| 2510 | if (mddev->persistent) { |
| 2511 | rdev = md_import_device(dev, mddev->major_version, |
| 2512 | mddev->minor_version); |
| 2513 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 2514 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 2515 | mdk_rdev_t, same_set); |
| 2516 | err = super_types[mddev->major_version] |
| 2517 | .load_super(rdev, rdev0, mddev->minor_version); |
| 2518 | if (err < 0) |
| 2519 | goto out; |
| 2520 | } |
| 2521 | } else |
| 2522 | rdev = md_import_device(dev, -1, -1); |
| 2523 | |
| 2524 | if (IS_ERR(rdev)) |
| 2525 | return PTR_ERR(rdev); |
| 2526 | err = bind_rdev_to_array(rdev, mddev); |
| 2527 | out: |
| 2528 | if (err) |
| 2529 | export_rdev(rdev); |
| 2530 | return err ? err : len; |
| 2531 | } |
| 2532 | |
| 2533 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2534 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2535 | |
| 2536 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2537 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 2538 | { |
| 2539 | char *end; |
| 2540 | unsigned long chunk, end_chunk; |
| 2541 | |
| 2542 | if (!mddev->bitmap) |
| 2543 | goto out; |
| 2544 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 2545 | while (*buf) { |
| 2546 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 2547 | if (buf == end) break; |
| 2548 | if (*end == '-') { /* range */ |
| 2549 | buf = end + 1; |
| 2550 | end_chunk = simple_strtoul(buf, &end, 0); |
| 2551 | if (buf == end) break; |
| 2552 | } |
| 2553 | if (*end && !isspace(*end)) break; |
| 2554 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 2555 | buf = end; |
| 2556 | while (isspace(*buf)) buf++; |
| 2557 | } |
| 2558 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 2559 | out: |
| 2560 | return len; |
| 2561 | } |
| 2562 | |
| 2563 | static struct md_sysfs_entry md_bitmap = |
| 2564 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 2565 | |
| 2566 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2567 | size_show(mddev_t *mddev, char *page) |
| 2568 | { |
| 2569 | return sprintf(page, "%llu\n", (unsigned long long)mddev->size); |
| 2570 | } |
| 2571 | |
| 2572 | static int update_size(mddev_t *mddev, unsigned long size); |
| 2573 | |
| 2574 | static ssize_t |
| 2575 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2576 | { |
| 2577 | /* If array is inactive, we can reduce the component size, but |
| 2578 | * not increase it (except from 0). |
| 2579 | * If array is active, we can try an on-line resize |
| 2580 | */ |
| 2581 | char *e; |
| 2582 | int err = 0; |
| 2583 | unsigned long long size = simple_strtoull(buf, &e, 10); |
| 2584 | if (!*buf || *buf == '\n' || |
| 2585 | (*e && *e != '\n')) |
| 2586 | return -EINVAL; |
| 2587 | |
| 2588 | if (mddev->pers) { |
| 2589 | err = update_size(mddev, size); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2590 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2591 | } else { |
| 2592 | if (mddev->size == 0 || |
| 2593 | mddev->size > size) |
| 2594 | mddev->size = size; |
| 2595 | else |
| 2596 | err = -ENOSPC; |
| 2597 | } |
| 2598 | return err ? err : len; |
| 2599 | } |
| 2600 | |
| 2601 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2602 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2603 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2604 | |
| 2605 | /* Metdata version. |
| 2606 | * This is either 'none' for arrays with externally managed metadata, |
| 2607 | * or N.M for internally known formats |
| 2608 | */ |
| 2609 | static ssize_t |
| 2610 | metadata_show(mddev_t *mddev, char *page) |
| 2611 | { |
| 2612 | if (mddev->persistent) |
| 2613 | return sprintf(page, "%d.%d\n", |
| 2614 | mddev->major_version, mddev->minor_version); |
| 2615 | else |
| 2616 | return sprintf(page, "none\n"); |
| 2617 | } |
| 2618 | |
| 2619 | static ssize_t |
| 2620 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 2621 | { |
| 2622 | int major, minor; |
| 2623 | char *e; |
| 2624 | if (!list_empty(&mddev->disks)) |
| 2625 | return -EBUSY; |
| 2626 | |
| 2627 | if (cmd_match(buf, "none")) { |
| 2628 | mddev->persistent = 0; |
| 2629 | mddev->major_version = 0; |
| 2630 | mddev->minor_version = 90; |
| 2631 | return len; |
| 2632 | } |
| 2633 | major = simple_strtoul(buf, &e, 10); |
| 2634 | if (e==buf || *e != '.') |
| 2635 | return -EINVAL; |
| 2636 | buf = e+1; |
| 2637 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2638 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2639 | return -EINVAL; |
| 2640 | if (major >= sizeof(super_types)/sizeof(super_types[0]) || |
| 2641 | super_types[major].name == NULL) |
| 2642 | return -ENOENT; |
| 2643 | mddev->major_version = major; |
| 2644 | mddev->minor_version = minor; |
| 2645 | mddev->persistent = 1; |
| 2646 | return len; |
| 2647 | } |
| 2648 | |
| 2649 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2650 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2651 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2652 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2653 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2654 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2655 | char *type = "idle"; |
NeilBrown | 31399d9 | 2005-11-08 21:39:29 -0800 | [diff] [blame] | 2656 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2657 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2658 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2659 | type = "reshape"; |
| 2660 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2661 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 2662 | type = "resync"; |
| 2663 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2664 | type = "check"; |
| 2665 | else |
| 2666 | type = "repair"; |
| 2667 | } else |
| 2668 | type = "recover"; |
| 2669 | } |
| 2670 | return sprintf(page, "%s\n", type); |
| 2671 | } |
| 2672 | |
| 2673 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2674 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2675 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2676 | if (!mddev->pers || !mddev->pers->sync_request) |
| 2677 | return -EINVAL; |
| 2678 | |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2679 | if (cmd_match(page, "idle")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2680 | if (mddev->sync_thread) { |
| 2681 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2682 | md_unregister_thread(mddev->sync_thread); |
| 2683 | mddev->sync_thread = NULL; |
| 2684 | mddev->recovery = 0; |
| 2685 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2686 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 2687 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2688 | return -EBUSY; |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2689 | else if (cmd_match(page, "resync") || cmd_match(page, "recover")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2690 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 2691 | else if (cmd_match(page, "reshape")) { |
| 2692 | int err; |
| 2693 | if (mddev->pers->start_reshape == NULL) |
| 2694 | return -EINVAL; |
| 2695 | err = mddev->pers->start_reshape(mddev); |
| 2696 | if (err) |
| 2697 | return err; |
| 2698 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2699 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2700 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 2701 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2702 | return -EINVAL; |
| 2703 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 2704 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 2705 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 2706 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2707 | md_wakeup_thread(mddev->thread); |
| 2708 | return len; |
| 2709 | } |
| 2710 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2711 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2712 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2713 | { |
| 2714 | return sprintf(page, "%llu\n", |
| 2715 | (unsigned long long) mddev->resync_mismatches); |
| 2716 | } |
| 2717 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2718 | static struct md_sysfs_entry md_scan_mode = |
| 2719 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2720 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2721 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2722 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2723 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2724 | static ssize_t |
| 2725 | sync_min_show(mddev_t *mddev, char *page) |
| 2726 | { |
| 2727 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 2728 | mddev->sync_speed_min ? "local": "system"); |
| 2729 | } |
| 2730 | |
| 2731 | static ssize_t |
| 2732 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 2733 | { |
| 2734 | int min; |
| 2735 | char *e; |
| 2736 | if (strncmp(buf, "system", 6)==0) { |
| 2737 | mddev->sync_speed_min = 0; |
| 2738 | return len; |
| 2739 | } |
| 2740 | min = simple_strtoul(buf, &e, 10); |
| 2741 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 2742 | return -EINVAL; |
| 2743 | mddev->sync_speed_min = min; |
| 2744 | return len; |
| 2745 | } |
| 2746 | |
| 2747 | static struct md_sysfs_entry md_sync_min = |
| 2748 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 2749 | |
| 2750 | static ssize_t |
| 2751 | sync_max_show(mddev_t *mddev, char *page) |
| 2752 | { |
| 2753 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 2754 | mddev->sync_speed_max ? "local": "system"); |
| 2755 | } |
| 2756 | |
| 2757 | static ssize_t |
| 2758 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 2759 | { |
| 2760 | int max; |
| 2761 | char *e; |
| 2762 | if (strncmp(buf, "system", 6)==0) { |
| 2763 | mddev->sync_speed_max = 0; |
| 2764 | return len; |
| 2765 | } |
| 2766 | max = simple_strtoul(buf, &e, 10); |
| 2767 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 2768 | return -EINVAL; |
| 2769 | mddev->sync_speed_max = max; |
| 2770 | return len; |
| 2771 | } |
| 2772 | |
| 2773 | static struct md_sysfs_entry md_sync_max = |
| 2774 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 2775 | |
| 2776 | |
| 2777 | static ssize_t |
| 2778 | sync_speed_show(mddev_t *mddev, char *page) |
| 2779 | { |
| 2780 | unsigned long resync, dt, db; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 2781 | resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2782 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 2783 | if (!dt) dt++; |
| 2784 | db = resync - (mddev->resync_mark_cnt); |
| 2785 | return sprintf(page, "%ld\n", db/dt/2); /* K/sec */ |
| 2786 | } |
| 2787 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2788 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2789 | |
| 2790 | static ssize_t |
| 2791 | sync_completed_show(mddev_t *mddev, char *page) |
| 2792 | { |
| 2793 | unsigned long max_blocks, resync; |
| 2794 | |
| 2795 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 2796 | max_blocks = mddev->resync_max_sectors; |
| 2797 | else |
| 2798 | max_blocks = mddev->size << 1; |
| 2799 | |
| 2800 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active)); |
| 2801 | return sprintf(page, "%lu / %lu\n", resync, max_blocks); |
| 2802 | } |
| 2803 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2804 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2805 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 2806 | static ssize_t |
| 2807 | suspend_lo_show(mddev_t *mddev, char *page) |
| 2808 | { |
| 2809 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 2810 | } |
| 2811 | |
| 2812 | static ssize_t |
| 2813 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 2814 | { |
| 2815 | char *e; |
| 2816 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 2817 | |
| 2818 | if (mddev->pers->quiesce == NULL) |
| 2819 | return -EINVAL; |
| 2820 | if (buf == e || (*e && *e != '\n')) |
| 2821 | return -EINVAL; |
| 2822 | if (new >= mddev->suspend_hi || |
| 2823 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 2824 | mddev->suspend_lo = new; |
| 2825 | mddev->pers->quiesce(mddev, 2); |
| 2826 | return len; |
| 2827 | } else |
| 2828 | return -EINVAL; |
| 2829 | } |
| 2830 | static struct md_sysfs_entry md_suspend_lo = |
| 2831 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 2832 | |
| 2833 | |
| 2834 | static ssize_t |
| 2835 | suspend_hi_show(mddev_t *mddev, char *page) |
| 2836 | { |
| 2837 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 2838 | } |
| 2839 | |
| 2840 | static ssize_t |
| 2841 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 2842 | { |
| 2843 | char *e; |
| 2844 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 2845 | |
| 2846 | if (mddev->pers->quiesce == NULL) |
| 2847 | return -EINVAL; |
| 2848 | if (buf == e || (*e && *e != '\n')) |
| 2849 | return -EINVAL; |
| 2850 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 2851 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 2852 | mddev->suspend_hi = new; |
| 2853 | mddev->pers->quiesce(mddev, 1); |
| 2854 | mddev->pers->quiesce(mddev, 0); |
| 2855 | return len; |
| 2856 | } else |
| 2857 | return -EINVAL; |
| 2858 | } |
| 2859 | static struct md_sysfs_entry md_suspend_hi = |
| 2860 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 2861 | |
| 2862 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2863 | static struct attribute *md_default_attrs[] = { |
| 2864 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2865 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2866 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2867 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 2868 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2869 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 2870 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 2871 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2872 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2873 | &md_array_state.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 2874 | NULL, |
| 2875 | }; |
| 2876 | |
| 2877 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2878 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2879 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 2880 | &md_sync_min.attr, |
| 2881 | &md_sync_max.attr, |
| 2882 | &md_sync_speed.attr, |
| 2883 | &md_sync_completed.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 2884 | &md_suspend_lo.attr, |
| 2885 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 2886 | &md_bitmap.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2887 | NULL, |
| 2888 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 2889 | static struct attribute_group md_redundancy_group = { |
| 2890 | .name = NULL, |
| 2891 | .attrs = md_redundancy_attrs, |
| 2892 | }; |
| 2893 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2894 | |
| 2895 | static ssize_t |
| 2896 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2897 | { |
| 2898 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 2899 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2900 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2901 | |
| 2902 | if (!entry->show) |
| 2903 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 2904 | rv = mddev_lock(mddev); |
| 2905 | if (!rv) { |
| 2906 | rv = entry->show(mddev, page); |
| 2907 | mddev_unlock(mddev); |
| 2908 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2909 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | static ssize_t |
| 2913 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2914 | const char *page, size_t length) |
| 2915 | { |
| 2916 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 2917 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2918 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2919 | |
| 2920 | if (!entry->store) |
| 2921 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2922 | if (!capable(CAP_SYS_ADMIN)) |
| 2923 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 2924 | rv = mddev_lock(mddev); |
| 2925 | if (!rv) { |
| 2926 | rv = entry->store(mddev, page, length); |
| 2927 | mddev_unlock(mddev); |
| 2928 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2929 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | static void md_free(struct kobject *ko) |
| 2933 | { |
| 2934 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
| 2935 | kfree(mddev); |
| 2936 | } |
| 2937 | |
| 2938 | static struct sysfs_ops md_sysfs_ops = { |
| 2939 | .show = md_attr_show, |
| 2940 | .store = md_attr_store, |
| 2941 | }; |
| 2942 | static struct kobj_type md_ktype = { |
| 2943 | .release = md_free, |
| 2944 | .sysfs_ops = &md_sysfs_ops, |
| 2945 | .default_attrs = md_default_attrs, |
| 2946 | }; |
| 2947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | int mdp_major = 0; |
| 2949 | |
| 2950 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 2951 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2952 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | mddev_t *mddev = mddev_find(dev); |
| 2954 | struct gendisk *disk; |
| 2955 | int partitioned = (MAJOR(dev) != MD_MAJOR); |
| 2956 | int shift = partitioned ? MdpMinorShift : 0; |
| 2957 | int unit = MINOR(dev) >> shift; |
| 2958 | |
| 2959 | if (!mddev) |
| 2960 | return NULL; |
| 2961 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2962 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2964 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | mddev_put(mddev); |
| 2966 | return NULL; |
| 2967 | } |
| 2968 | disk = alloc_disk(1 << shift); |
| 2969 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2970 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | mddev_put(mddev); |
| 2972 | return NULL; |
| 2973 | } |
| 2974 | disk->major = MAJOR(dev); |
| 2975 | disk->first_minor = unit << shift; |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2976 | if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2978 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | disk->fops = &md_fops; |
| 2981 | disk->private_data = mddev; |
| 2982 | disk->queue = mddev->queue; |
| 2983 | add_disk(disk); |
| 2984 | mddev->gendisk = disk; |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 2985 | mutex_unlock(&disks_mutex); |
NeilBrown | 9c79197 | 2005-11-08 21:39:28 -0800 | [diff] [blame] | 2986 | mddev->kobj.parent = &disk->kobj; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2987 | mddev->kobj.k_name = NULL; |
| 2988 | snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md"); |
| 2989 | mddev->kobj.ktype = &md_ktype; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2990 | if (kobject_register(&mddev->kobj)) |
| 2991 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 2992 | disk->disk_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | return NULL; |
| 2994 | } |
| 2995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | static void md_safemode_timeout(unsigned long data) |
| 2997 | { |
| 2998 | mddev_t *mddev = (mddev_t *) data; |
| 2999 | |
| 3000 | mddev->safemode = 1; |
| 3001 | md_wakeup_thread(mddev->thread); |
| 3002 | } |
| 3003 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3004 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | |
| 3006 | static int do_md_run(mddev_t * mddev) |
| 3007 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3008 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | int chunk_size; |
| 3010 | struct list_head *tmp; |
| 3011 | mdk_rdev_t *rdev; |
| 3012 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3013 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | char b[BDEVNAME_SIZE]; |
| 3015 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3016 | if (list_empty(&mddev->disks)) |
| 3017 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | |
| 3020 | if (mddev->pers) |
| 3021 | return -EBUSY; |
| 3022 | |
| 3023 | /* |
| 3024 | * Analyze all RAID superblock(s) |
| 3025 | */ |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3026 | if (!mddev->raid_disks) |
| 3027 | analyze_sbs(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | |
| 3029 | chunk_size = mddev->chunk_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3031 | if (chunk_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | if (chunk_size > MAX_CHUNK_SIZE) { |
| 3033 | printk(KERN_ERR "too big chunk_size: %d > %d\n", |
| 3034 | chunk_size, MAX_CHUNK_SIZE); |
| 3035 | return -EINVAL; |
| 3036 | } |
| 3037 | /* |
| 3038 | * chunk-size has to be a power of 2 and multiples of PAGE_SIZE |
| 3039 | */ |
| 3040 | if ( (1 << ffz(~chunk_size)) != chunk_size) { |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3041 | printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | return -EINVAL; |
| 3043 | } |
| 3044 | if (chunk_size < PAGE_SIZE) { |
| 3045 | printk(KERN_ERR "too small chunk_size: %d < %ld\n", |
| 3046 | chunk_size, PAGE_SIZE); |
| 3047 | return -EINVAL; |
| 3048 | } |
| 3049 | |
| 3050 | /* devices must have minimum size of one chunk */ |
| 3051 | ITERATE_RDEV(mddev,rdev,tmp) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3052 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | continue; |
| 3054 | if (rdev->size < chunk_size / 1024) { |
| 3055 | printk(KERN_WARNING |
| 3056 | "md: Dev %s smaller than chunk_size:" |
| 3057 | " %lluk < %dk\n", |
| 3058 | bdevname(rdev->bdev,b), |
| 3059 | (unsigned long long)rdev->size, |
| 3060 | chunk_size / 1024); |
| 3061 | return -EINVAL; |
| 3062 | } |
| 3063 | } |
| 3064 | } |
| 3065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | #ifdef CONFIG_KMOD |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3067 | if (mddev->level != LEVEL_NONE) |
| 3068 | request_module("md-level-%d", mddev->level); |
| 3069 | else if (mddev->clevel[0]) |
| 3070 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | #endif |
| 3072 | |
| 3073 | /* |
| 3074 | * Drop all container device buffers, from now on |
| 3075 | * the only valid external interface is through the md |
| 3076 | * device. |
| 3077 | * Also find largest hardsector size |
| 3078 | */ |
| 3079 | ITERATE_RDEV(mddev,rdev,tmp) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3080 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | continue; |
| 3082 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame^] | 3083 | invalidate_bdev(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | md_probe(mddev->unit, NULL, NULL); |
| 3087 | disk = mddev->gendisk; |
| 3088 | if (!disk) |
| 3089 | return -ENOMEM; |
| 3090 | |
| 3091 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3092 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3093 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3095 | if (mddev->level != LEVEL_NONE) |
| 3096 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 3097 | mddev->level); |
| 3098 | else |
| 3099 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 3100 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | return -EINVAL; |
| 3102 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3103 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3105 | mddev->level = pers->level; |
| 3106 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3108 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3109 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 3110 | /* This personality cannot handle reshaping... */ |
| 3111 | mddev->pers = NULL; |
| 3112 | module_put(pers->owner); |
| 3113 | return -EINVAL; |
| 3114 | } |
| 3115 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 3116 | if (pers->sync_request) { |
| 3117 | /* Warn if this is a potentially silly |
| 3118 | * configuration. |
| 3119 | */ |
| 3120 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3121 | mdk_rdev_t *rdev2; |
| 3122 | struct list_head *tmp2; |
| 3123 | int warned = 0; |
| 3124 | ITERATE_RDEV(mddev, rdev, tmp) { |
| 3125 | ITERATE_RDEV(mddev, rdev2, tmp2) { |
| 3126 | if (rdev < rdev2 && |
| 3127 | rdev->bdev->bd_contains == |
| 3128 | rdev2->bdev->bd_contains) { |
| 3129 | printk(KERN_WARNING |
| 3130 | "%s: WARNING: %s appears to be" |
| 3131 | " on the same physical disk as" |
| 3132 | " %s.\n", |
| 3133 | mdname(mddev), |
| 3134 | bdevname(rdev->bdev,b), |
| 3135 | bdevname(rdev2->bdev,b2)); |
| 3136 | warned = 1; |
| 3137 | } |
| 3138 | } |
| 3139 | } |
| 3140 | if (warned) |
| 3141 | printk(KERN_WARNING |
| 3142 | "True protection against single-disk" |
| 3143 | " failure might be compromised.\n"); |
| 3144 | } |
| 3145 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 3146 | mddev->recovery = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3148 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3149 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3151 | if (start_readonly) |
| 3152 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 3153 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3154 | err = mddev->pers->run(mddev); |
| 3155 | if (!err && mddev->pers->sync_request) { |
| 3156 | err = bitmap_create(mddev); |
| 3157 | if (err) { |
| 3158 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 3159 | mdname(mddev), err); |
| 3160 | mddev->pers->stop(mddev); |
| 3161 | } |
| 3162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | if (err) { |
| 3164 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
| 3165 | module_put(mddev->pers->owner); |
| 3166 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3167 | bitmap_destroy(mddev); |
| 3168 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3170 | if (mddev->pers->sync_request) { |
| 3171 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3172 | printk(KERN_WARNING |
| 3173 | "md: cannot register extra attributes for %s\n", |
| 3174 | mdname(mddev)); |
| 3175 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 3176 | mddev->ro = 0; |
| 3177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | atomic_set(&mddev->writes_pending,0); |
| 3179 | mddev->safemode = 0; |
| 3180 | mddev->safemode_timer.function = md_safemode_timeout; |
| 3181 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3182 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3184 | |
| 3185 | ITERATE_RDEV(mddev,rdev,tmp) |
| 3186 | if (rdev->raid_disk >= 0) { |
| 3187 | char nm[20]; |
| 3188 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3189 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3190 | printk("md: cannot register %s for %s\n", |
| 3191 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | |
| 3194 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3195 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3196 | if (mddev->flags) |
| 3197 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | |
| 3199 | set_capacity(disk, mddev->array_size<<1); |
| 3200 | |
| 3201 | /* If we call blk_queue_make_request here, it will |
| 3202 | * re-initialise max_sectors etc which may have been |
| 3203 | * refined inside -> run. So just set the bits we need to set. |
| 3204 | * Most initialisation happended when we called |
| 3205 | * blk_queue_make_request(..., md_fail_request) |
| 3206 | * earlier. |
| 3207 | */ |
| 3208 | mddev->queue->queuedata = mddev; |
| 3209 | mddev->queue->make_request_fn = mddev->pers->make_request; |
| 3210 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3211 | /* If there is a partially-recovered drive we need to |
| 3212 | * start recovery here. If we leave it to md_check_recovery, |
| 3213 | * it will remove the drives and not do the right thing |
| 3214 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3215 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3216 | struct list_head *rtmp; |
| 3217 | int spares = 0; |
| 3218 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 3219 | if (rdev->raid_disk >= 0 && |
| 3220 | !test_bit(In_sync, &rdev->flags) && |
| 3221 | !test_bit(Faulty, &rdev->flags)) |
| 3222 | /* complete an interrupted recovery */ |
| 3223 | spares++; |
| 3224 | if (spares && mddev->pers->sync_request) { |
| 3225 | mddev->recovery = 0; |
| 3226 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3227 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 3228 | mddev, |
| 3229 | "%s_resync"); |
| 3230 | if (!mddev->sync_thread) { |
| 3231 | printk(KERN_ERR "%s: could not start resync" |
| 3232 | " thread...\n", |
| 3233 | mdname(mddev)); |
| 3234 | /* leave the spares where they are, it shouldn't hurt */ |
| 3235 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3236 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3237 | } |
| 3238 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 3239 | md_wakeup_thread(mddev->thread); |
| 3240 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3243 | md_new_event(mddev); |
NeilBrown | 2f47130 | 2006-11-08 17:44:47 -0800 | [diff] [blame] | 3244 | kobject_uevent(&mddev->gendisk->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | return 0; |
| 3246 | } |
| 3247 | |
| 3248 | static int restart_array(mddev_t *mddev) |
| 3249 | { |
| 3250 | struct gendisk *disk = mddev->gendisk; |
| 3251 | int err; |
| 3252 | |
| 3253 | /* |
| 3254 | * Complain if it has no devices |
| 3255 | */ |
| 3256 | err = -ENXIO; |
| 3257 | if (list_empty(&mddev->disks)) |
| 3258 | goto out; |
| 3259 | |
| 3260 | if (mddev->pers) { |
| 3261 | err = -EBUSY; |
| 3262 | if (!mddev->ro) |
| 3263 | goto out; |
| 3264 | |
| 3265 | mddev->safemode = 0; |
| 3266 | mddev->ro = 0; |
| 3267 | set_disk_ro(disk, 0); |
| 3268 | |
| 3269 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 3270 | mdname(mddev)); |
| 3271 | /* |
| 3272 | * Kick recovery or resync if necessary |
| 3273 | */ |
| 3274 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3275 | md_wakeup_thread(mddev->thread); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3276 | md_wakeup_thread(mddev->sync_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3278 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | |
| 3281 | out: |
| 3282 | return err; |
| 3283 | } |
| 3284 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3285 | /* similar to deny_write_access, but accounts for our holding a reference |
| 3286 | * to the file ourselves */ |
| 3287 | static int deny_bitmap_write_access(struct file * file) |
| 3288 | { |
| 3289 | struct inode *inode = file->f_mapping->host; |
| 3290 | |
| 3291 | spin_lock(&inode->i_lock); |
| 3292 | if (atomic_read(&inode->i_writecount) > 1) { |
| 3293 | spin_unlock(&inode->i_lock); |
| 3294 | return -ETXTBSY; |
| 3295 | } |
| 3296 | atomic_set(&inode->i_writecount, -1); |
| 3297 | spin_unlock(&inode->i_lock); |
| 3298 | |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
| 3302 | static void restore_bitmap_write_access(struct file *file) |
| 3303 | { |
| 3304 | struct inode *inode = file->f_mapping->host; |
| 3305 | |
| 3306 | spin_lock(&inode->i_lock); |
| 3307 | atomic_set(&inode->i_writecount, 1); |
| 3308 | spin_unlock(&inode->i_lock); |
| 3309 | } |
| 3310 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3311 | /* mode: |
| 3312 | * 0 - completely stop and dis-assemble array |
| 3313 | * 1 - switch to readonly |
| 3314 | * 2 - stop but do not disassemble array |
| 3315 | */ |
| 3316 | static int do_md_stop(mddev_t * mddev, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | { |
| 3318 | int err = 0; |
| 3319 | struct gendisk *disk = mddev->gendisk; |
| 3320 | |
| 3321 | if (mddev->pers) { |
| 3322 | if (atomic_read(&mddev->active)>2) { |
| 3323 | printk("md: %s still in use.\n",mdname(mddev)); |
| 3324 | return -EBUSY; |
| 3325 | } |
| 3326 | |
| 3327 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3328 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3330 | md_unregister_thread(mddev->sync_thread); |
| 3331 | mddev->sync_thread = NULL; |
| 3332 | } |
| 3333 | |
| 3334 | del_timer_sync(&mddev->safemode_timer); |
| 3335 | |
| 3336 | invalidate_partition(disk, 0); |
| 3337 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3338 | switch(mode) { |
| 3339 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 3341 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | goto out; |
| 3343 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3344 | break; |
| 3345 | case 0: /* disassemble */ |
| 3346 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 3347 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 3348 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | if (mddev->ro) |
| 3350 | set_disk_ro(disk, 0); |
| 3351 | blk_queue_make_request(mddev->queue, md_fail_request); |
| 3352 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 3353 | mddev->queue->merge_bvec_fn = NULL; |
| 3354 | mddev->queue->unplug_fn = NULL; |
| 3355 | mddev->queue->issue_flush_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3356 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3357 | if (mddev->pers->sync_request) |
| 3358 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | module_put(mddev->pers->owner); |
| 3361 | mddev->pers = NULL; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3362 | |
| 3363 | set_capacity(disk, 0); |
| 3364 | mddev->changed = 1; |
| 3365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | if (mddev->ro) |
| 3367 | mddev->ro = 0; |
| 3368 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3369 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | /* mark array as shutdown cleanly */ |
| 3371 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3372 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3374 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3376 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | /* |
| 3380 | * Free resources if final stop |
| 3381 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3382 | if (mode == 0) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3383 | mdk_rdev_t *rdev; |
| 3384 | struct list_head *tmp; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 3385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 3387 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3388 | bitmap_destroy(mddev); |
| 3389 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3390 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 3391 | fput(mddev->bitmap_file); |
| 3392 | mddev->bitmap_file = NULL; |
| 3393 | } |
| 3394 | mddev->bitmap_offset = 0; |
| 3395 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3396 | ITERATE_RDEV(mddev,rdev,tmp) |
| 3397 | if (rdev->raid_disk >= 0) { |
| 3398 | char nm[20]; |
| 3399 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3400 | sysfs_remove_link(&mddev->kobj, nm); |
| 3401 | } |
| 3402 | |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 3403 | /* make sure all delayed_delete calls have finished */ |
| 3404 | flush_scheduled_work(); |
| 3405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | export_array(mddev); |
| 3407 | |
| 3408 | mddev->array_size = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3409 | mddev->size = 0; |
| 3410 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3411 | mddev->recovery_cp = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3412 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 3413 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3414 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 3415 | mdname(mddev)); |
| 3416 | err = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3417 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | out: |
| 3419 | return err; |
| 3420 | } |
| 3421 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3422 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | static void autorun_array(mddev_t *mddev) |
| 3424 | { |
| 3425 | mdk_rdev_t *rdev; |
| 3426 | struct list_head *tmp; |
| 3427 | int err; |
| 3428 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3429 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | |
| 3432 | printk(KERN_INFO "md: running: "); |
| 3433 | |
| 3434 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 3435 | char b[BDEVNAME_SIZE]; |
| 3436 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 3437 | } |
| 3438 | printk("\n"); |
| 3439 | |
| 3440 | err = do_md_run (mddev); |
| 3441 | if (err) { |
| 3442 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
| 3443 | do_md_stop (mddev, 0); |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | /* |
| 3448 | * lets try to run arrays based on all disks that have arrived |
| 3449 | * until now. (those are in pending_raid_disks) |
| 3450 | * |
| 3451 | * the method: pick the first pending disk, collect all disks with |
| 3452 | * the same UUID, remove all from the pending list and put them into |
| 3453 | * the 'same_array' list. Then order this list based on superblock |
| 3454 | * update time (freshest comes first), kick out 'old' disks and |
| 3455 | * compare superblocks. If everything's fine then run it. |
| 3456 | * |
| 3457 | * If "unit" is allocated, then bump its reference count |
| 3458 | */ |
| 3459 | static void autorun_devices(int part) |
| 3460 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | struct list_head *tmp; |
| 3462 | mdk_rdev_t *rdev0, *rdev; |
| 3463 | mddev_t *mddev; |
| 3464 | char b[BDEVNAME_SIZE]; |
| 3465 | |
| 3466 | printk(KERN_INFO "md: autorun ...\n"); |
| 3467 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3468 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3469 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 3470 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | rdev0 = list_entry(pending_raid_disks.next, |
| 3472 | mdk_rdev_t, same_set); |
| 3473 | |
| 3474 | printk(KERN_INFO "md: considering %s ...\n", |
| 3475 | bdevname(rdev0->bdev,b)); |
| 3476 | INIT_LIST_HEAD(&candidates); |
| 3477 | ITERATE_RDEV_PENDING(rdev,tmp) |
| 3478 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 3479 | printk(KERN_INFO "md: adding %s ...\n", |
| 3480 | bdevname(rdev->bdev,b)); |
| 3481 | list_move(&rdev->same_set, &candidates); |
| 3482 | } |
| 3483 | /* |
| 3484 | * now we have a set of devices, with all of them having |
| 3485 | * mostly sane superblocks. It's time to allocate the |
| 3486 | * mddev. |
| 3487 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 3488 | if (part) { |
| 3489 | dev = MKDEV(mdp_major, |
| 3490 | rdev0->preferred_minor << MdpMinorShift); |
| 3491 | unit = MINOR(dev) >> MdpMinorShift; |
| 3492 | } else { |
| 3493 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 3494 | unit = MINOR(dev); |
| 3495 | } |
| 3496 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 3498 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 3499 | break; |
| 3500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | |
| 3502 | md_probe(dev, NULL, NULL); |
| 3503 | mddev = mddev_find(dev); |
| 3504 | if (!mddev) { |
| 3505 | printk(KERN_ERR |
| 3506 | "md: cannot allocate memory for md drive.\n"); |
| 3507 | break; |
| 3508 | } |
| 3509 | if (mddev_lock(mddev)) |
| 3510 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 3511 | mdname(mddev)); |
| 3512 | else if (mddev->raid_disks || mddev->major_version |
| 3513 | || !list_empty(&mddev->disks)) { |
| 3514 | printk(KERN_WARNING |
| 3515 | "md: %s already running, cannot run %s\n", |
| 3516 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 3517 | mddev_unlock(mddev); |
| 3518 | } else { |
| 3519 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
| 3520 | ITERATE_RDEV_GENERIC(candidates,rdev,tmp) { |
| 3521 | list_del_init(&rdev->same_set); |
| 3522 | if (bind_rdev_to_array(rdev, mddev)) |
| 3523 | export_rdev(rdev); |
| 3524 | } |
| 3525 | autorun_array(mddev); |
| 3526 | mddev_unlock(mddev); |
| 3527 | } |
| 3528 | /* on success, candidates will be empty, on error |
| 3529 | * it won't... |
| 3530 | */ |
| 3531 | ITERATE_RDEV_GENERIC(candidates,rdev,tmp) |
| 3532 | export_rdev(rdev); |
| 3533 | mddev_put(mddev); |
| 3534 | } |
| 3535 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 3536 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 3537 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | static int get_version(void __user * arg) |
| 3540 | { |
| 3541 | mdu_version_t ver; |
| 3542 | |
| 3543 | ver.major = MD_MAJOR_VERSION; |
| 3544 | ver.minor = MD_MINOR_VERSION; |
| 3545 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 3546 | |
| 3547 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 3548 | return -EFAULT; |
| 3549 | |
| 3550 | return 0; |
| 3551 | } |
| 3552 | |
| 3553 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 3554 | { |
| 3555 | mdu_array_info_t info; |
| 3556 | int nr,working,active,failed,spare; |
| 3557 | mdk_rdev_t *rdev; |
| 3558 | struct list_head *tmp; |
| 3559 | |
| 3560 | nr=working=active=failed=spare=0; |
| 3561 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 3562 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3563 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | failed++; |
| 3565 | else { |
| 3566 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3567 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | active++; |
| 3569 | else |
| 3570 | spare++; |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | info.major_version = mddev->major_version; |
| 3575 | info.minor_version = mddev->minor_version; |
| 3576 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 3577 | info.ctime = mddev->ctime; |
| 3578 | info.level = mddev->level; |
| 3579 | info.size = mddev->size; |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 3580 | if (info.size != mddev->size) /* overflow */ |
| 3581 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | info.nr_disks = nr; |
| 3583 | info.raid_disks = mddev->raid_disks; |
| 3584 | info.md_minor = mddev->md_minor; |
| 3585 | info.not_persistent= !mddev->persistent; |
| 3586 | |
| 3587 | info.utime = mddev->utime; |
| 3588 | info.state = 0; |
| 3589 | if (mddev->in_sync) |
| 3590 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3591 | if (mddev->bitmap && mddev->bitmap_offset) |
| 3592 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3593 | info.active_disks = active; |
| 3594 | info.working_disks = working; |
| 3595 | info.failed_disks = failed; |
| 3596 | info.spare_disks = spare; |
| 3597 | |
| 3598 | info.layout = mddev->layout; |
| 3599 | info.chunk_size = mddev->chunk_size; |
| 3600 | |
| 3601 | if (copy_to_user(arg, &info, sizeof(info))) |
| 3602 | return -EFAULT; |
| 3603 | |
| 3604 | return 0; |
| 3605 | } |
| 3606 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 3607 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3608 | { |
| 3609 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 3610 | char *ptr, *buf = NULL; |
| 3611 | int err = -ENOMEM; |
| 3612 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 3613 | md_allow_write(mddev); |
| 3614 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3615 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
| 3616 | if (!file) |
| 3617 | goto out; |
| 3618 | |
| 3619 | /* bitmap disabled, zero the first byte and copy out */ |
| 3620 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 3621 | file->pathname[0] = '\0'; |
| 3622 | goto copy_out; |
| 3623 | } |
| 3624 | |
| 3625 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 3626 | if (!buf) |
| 3627 | goto out; |
| 3628 | |
| 3629 | ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname)); |
| 3630 | if (!ptr) |
| 3631 | goto out; |
| 3632 | |
| 3633 | strcpy(file->pathname, ptr); |
| 3634 | |
| 3635 | copy_out: |
| 3636 | err = 0; |
| 3637 | if (copy_to_user(arg, file, sizeof(*file))) |
| 3638 | err = -EFAULT; |
| 3639 | out: |
| 3640 | kfree(buf); |
| 3641 | kfree(file); |
| 3642 | return err; |
| 3643 | } |
| 3644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 3646 | { |
| 3647 | mdu_disk_info_t info; |
| 3648 | unsigned int nr; |
| 3649 | mdk_rdev_t *rdev; |
| 3650 | |
| 3651 | if (copy_from_user(&info, arg, sizeof(info))) |
| 3652 | return -EFAULT; |
| 3653 | |
| 3654 | nr = info.number; |
| 3655 | |
| 3656 | rdev = find_rdev_nr(mddev, nr); |
| 3657 | if (rdev) { |
| 3658 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 3659 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 3660 | info.raid_disk = rdev->raid_disk; |
| 3661 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3662 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3664 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | info.state |= (1<<MD_DISK_ACTIVE); |
| 3666 | info.state |= (1<<MD_DISK_SYNC); |
| 3667 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3668 | if (test_bit(WriteMostly, &rdev->flags)) |
| 3669 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | } else { |
| 3671 | info.major = info.minor = 0; |
| 3672 | info.raid_disk = -1; |
| 3673 | info.state = (1<<MD_DISK_REMOVED); |
| 3674 | } |
| 3675 | |
| 3676 | if (copy_to_user(arg, &info, sizeof(info))) |
| 3677 | return -EFAULT; |
| 3678 | |
| 3679 | return 0; |
| 3680 | } |
| 3681 | |
| 3682 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 3683 | { |
| 3684 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 3685 | mdk_rdev_t *rdev; |
| 3686 | dev_t dev = MKDEV(info->major,info->minor); |
| 3687 | |
| 3688 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 3689 | return -EOVERFLOW; |
| 3690 | |
| 3691 | if (!mddev->raid_disks) { |
| 3692 | int err; |
| 3693 | /* expecting a device which has a superblock */ |
| 3694 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 3695 | if (IS_ERR(rdev)) { |
| 3696 | printk(KERN_WARNING |
| 3697 | "md: md_import_device returned %ld\n", |
| 3698 | PTR_ERR(rdev)); |
| 3699 | return PTR_ERR(rdev); |
| 3700 | } |
| 3701 | if (!list_empty(&mddev->disks)) { |
| 3702 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3703 | mdk_rdev_t, same_set); |
| 3704 | int err = super_types[mddev->major_version] |
| 3705 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3706 | if (err < 0) { |
| 3707 | printk(KERN_WARNING |
| 3708 | "md: %s has different UUID to %s\n", |
| 3709 | bdevname(rdev->bdev,b), |
| 3710 | bdevname(rdev0->bdev,b2)); |
| 3711 | export_rdev(rdev); |
| 3712 | return -EINVAL; |
| 3713 | } |
| 3714 | } |
| 3715 | err = bind_rdev_to_array(rdev, mddev); |
| 3716 | if (err) |
| 3717 | export_rdev(rdev); |
| 3718 | return err; |
| 3719 | } |
| 3720 | |
| 3721 | /* |
| 3722 | * add_new_disk can be used once the array is assembled |
| 3723 | * to add "hot spares". They must already have a superblock |
| 3724 | * written |
| 3725 | */ |
| 3726 | if (mddev->pers) { |
| 3727 | int err; |
| 3728 | if (!mddev->pers->hot_add_disk) { |
| 3729 | printk(KERN_WARNING |
| 3730 | "%s: personality does not support diskops!\n", |
| 3731 | mdname(mddev)); |
| 3732 | return -EINVAL; |
| 3733 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 3734 | if (mddev->persistent) |
| 3735 | rdev = md_import_device(dev, mddev->major_version, |
| 3736 | mddev->minor_version); |
| 3737 | else |
| 3738 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3739 | if (IS_ERR(rdev)) { |
| 3740 | printk(KERN_WARNING |
| 3741 | "md: md_import_device returned %ld\n", |
| 3742 | PTR_ERR(rdev)); |
| 3743 | return PTR_ERR(rdev); |
| 3744 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 3745 | /* set save_raid_disk if appropriate */ |
| 3746 | if (!mddev->persistent) { |
| 3747 | if (info->state & (1<<MD_DISK_SYNC) && |
| 3748 | info->raid_disk < mddev->raid_disks) |
| 3749 | rdev->raid_disk = info->raid_disk; |
| 3750 | else |
| 3751 | rdev->raid_disk = -1; |
| 3752 | } else |
| 3753 | super_types[mddev->major_version]. |
| 3754 | validate_super(mddev, rdev); |
| 3755 | rdev->saved_raid_disk = rdev->raid_disk; |
| 3756 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3757 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
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 | rdev->raid_disk = -1; |
| 3762 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 3763 | if (!err && !mddev->pers->hot_remove_disk) { |
| 3764 | /* If there is hot_add_disk but no hot_remove_disk |
| 3765 | * then added disks for geometry changes, |
| 3766 | * and should be added immediately. |
| 3767 | */ |
| 3768 | super_types[mddev->major_version]. |
| 3769 | validate_super(mddev, rdev); |
| 3770 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 3771 | if (err) |
| 3772 | unbind_rdev_from_array(rdev); |
| 3773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | if (err) |
| 3775 | export_rdev(rdev); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 3776 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 3777 | md_update_sb(mddev, 1); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 3778 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 3779 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3780 | return err; |
| 3781 | } |
| 3782 | |
| 3783 | /* otherwise, add_new_disk is only allowed |
| 3784 | * for major_version==0 superblocks |
| 3785 | */ |
| 3786 | if (mddev->major_version != 0) { |
| 3787 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 3788 | mdname(mddev)); |
| 3789 | return -EINVAL; |
| 3790 | } |
| 3791 | |
| 3792 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 3793 | int err; |
| 3794 | rdev = md_import_device (dev, -1, 0); |
| 3795 | if (IS_ERR(rdev)) { |
| 3796 | printk(KERN_WARNING |
| 3797 | "md: error, md_import_device() returned %ld\n", |
| 3798 | PTR_ERR(rdev)); |
| 3799 | return PTR_ERR(rdev); |
| 3800 | } |
| 3801 | rdev->desc_nr = info->number; |
| 3802 | if (info->raid_disk < mddev->raid_disks) |
| 3803 | rdev->raid_disk = info->raid_disk; |
| 3804 | else |
| 3805 | rdev->raid_disk = -1; |
| 3806 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3807 | rdev->flags = 0; |
| 3808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3810 | if (info->state & (1<<MD_DISK_SYNC)) |
| 3811 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3813 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 3814 | set_bit(WriteMostly, &rdev->flags); |
| 3815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | if (!mddev->persistent) { |
| 3817 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
| 3818 | rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 3819 | } else |
| 3820 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 3821 | rdev->size = calc_dev_size(rdev, mddev->chunk_size); |
| 3822 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3823 | err = bind_rdev_to_array(rdev, mddev); |
| 3824 | if (err) { |
| 3825 | export_rdev(rdev); |
| 3826 | return err; |
| 3827 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | return 0; |
| 3831 | } |
| 3832 | |
| 3833 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 3834 | { |
| 3835 | char b[BDEVNAME_SIZE]; |
| 3836 | mdk_rdev_t *rdev; |
| 3837 | |
| 3838 | if (!mddev->pers) |
| 3839 | return -ENODEV; |
| 3840 | |
| 3841 | rdev = find_rdev(mddev, dev); |
| 3842 | if (!rdev) |
| 3843 | return -ENXIO; |
| 3844 | |
| 3845 | if (rdev->raid_disk >= 0) |
| 3846 | goto busy; |
| 3847 | |
| 3848 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3849 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3850 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | |
| 3852 | return 0; |
| 3853 | busy: |
| 3854 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n", |
| 3855 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 3856 | return -EBUSY; |
| 3857 | } |
| 3858 | |
| 3859 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 3860 | { |
| 3861 | char b[BDEVNAME_SIZE]; |
| 3862 | int err; |
| 3863 | unsigned int size; |
| 3864 | mdk_rdev_t *rdev; |
| 3865 | |
| 3866 | if (!mddev->pers) |
| 3867 | return -ENODEV; |
| 3868 | |
| 3869 | if (mddev->major_version != 0) { |
| 3870 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 3871 | " version-0 superblocks.\n", |
| 3872 | mdname(mddev)); |
| 3873 | return -EINVAL; |
| 3874 | } |
| 3875 | if (!mddev->pers->hot_add_disk) { |
| 3876 | printk(KERN_WARNING |
| 3877 | "%s: personality does not support diskops!\n", |
| 3878 | mdname(mddev)); |
| 3879 | return -EINVAL; |
| 3880 | } |
| 3881 | |
| 3882 | rdev = md_import_device (dev, -1, 0); |
| 3883 | if (IS_ERR(rdev)) { |
| 3884 | printk(KERN_WARNING |
| 3885 | "md: error, md_import_device() returned %ld\n", |
| 3886 | PTR_ERR(rdev)); |
| 3887 | return -EINVAL; |
| 3888 | } |
| 3889 | |
| 3890 | if (mddev->persistent) |
| 3891 | rdev->sb_offset = calc_dev_sboffset(rdev->bdev); |
| 3892 | else |
| 3893 | rdev->sb_offset = |
| 3894 | rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 3895 | |
| 3896 | size = calc_dev_size(rdev, mddev->chunk_size); |
| 3897 | rdev->size = size; |
| 3898 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3899 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | printk(KERN_WARNING |
| 3901 | "md: can not hot-add faulty %s disk to %s!\n", |
| 3902 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 3903 | err = -EINVAL; |
| 3904 | goto abort_export; |
| 3905 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3906 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 3908 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3909 | err = bind_rdev_to_array(rdev, mddev); |
| 3910 | if (err) |
| 3911 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | |
| 3913 | /* |
| 3914 | * The rest should better be atomic, we can have disk failures |
| 3915 | * noticed in interrupt contexts ... |
| 3916 | */ |
| 3917 | |
| 3918 | if (rdev->desc_nr == mddev->max_disks) { |
| 3919 | printk(KERN_WARNING "%s: can not hot-add to full array!\n", |
| 3920 | mdname(mddev)); |
| 3921 | err = -EBUSY; |
| 3922 | goto abort_unbind_export; |
| 3923 | } |
| 3924 | |
| 3925 | rdev->raid_disk = -1; |
| 3926 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3927 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | |
| 3929 | /* |
| 3930 | * Kick recovery, maybe this spare has to be added to the |
| 3931 | * array immediately. |
| 3932 | */ |
| 3933 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3934 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 3935 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | return 0; |
| 3937 | |
| 3938 | abort_unbind_export: |
| 3939 | unbind_rdev_from_array(rdev); |
| 3940 | |
| 3941 | abort_export: |
| 3942 | export_rdev(rdev); |
| 3943 | return err; |
| 3944 | } |
| 3945 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3946 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 3947 | { |
| 3948 | int err; |
| 3949 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3950 | if (mddev->pers) { |
| 3951 | if (!mddev->pers->quiesce) |
| 3952 | return -EBUSY; |
| 3953 | if (mddev->recovery || mddev->sync_thread) |
| 3954 | return -EBUSY; |
| 3955 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 3956 | } |
| 3957 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3958 | |
| 3959 | if (fd >= 0) { |
| 3960 | if (mddev->bitmap) |
| 3961 | return -EEXIST; /* cannot add when bitmap is present */ |
| 3962 | mddev->bitmap_file = fget(fd); |
| 3963 | |
| 3964 | if (mddev->bitmap_file == NULL) { |
| 3965 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 3966 | mdname(mddev)); |
| 3967 | return -EBADF; |
| 3968 | } |
| 3969 | |
| 3970 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 3971 | if (err) { |
| 3972 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 3973 | mdname(mddev)); |
| 3974 | fput(mddev->bitmap_file); |
| 3975 | mddev->bitmap_file = NULL; |
| 3976 | return err; |
| 3977 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 3978 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3979 | } else if (mddev->bitmap == NULL) |
| 3980 | return -ENOENT; /* cannot remove what isn't there */ |
| 3981 | err = 0; |
| 3982 | if (mddev->pers) { |
| 3983 | mddev->pers->quiesce(mddev, 1); |
| 3984 | if (fd >= 0) |
| 3985 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3986 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3987 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3988 | fd = -1; /* make sure to put the file */ |
| 3989 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3990 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 3991 | } |
| 3992 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3993 | if (mddev->bitmap_file) { |
| 3994 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3995 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 3996 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 3997 | mddev->bitmap_file = NULL; |
| 3998 | } |
| 3999 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4000 | return err; |
| 4001 | } |
| 4002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | /* |
| 4004 | * set_array_info is used two different ways |
| 4005 | * The original usage is when creating a new array. |
| 4006 | * In this usage, raid_disks is > 0 and it together with |
| 4007 | * level, size, not_persistent,layout,chunksize determine the |
| 4008 | * shape of the array. |
| 4009 | * This will always create an array with a type-0.90.0 superblock. |
| 4010 | * The newer usage is when assembling an array. |
| 4011 | * In this case raid_disks will be 0, and the major_version field is |
| 4012 | * use to determine which style super-blocks are to be found on the devices. |
| 4013 | * The minor and patch _version numbers are also kept incase the |
| 4014 | * super_block handler wishes to interpret them. |
| 4015 | */ |
| 4016 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4017 | { |
| 4018 | |
| 4019 | if (info->raid_disks == 0) { |
| 4020 | /* just setting version number for superblock loading */ |
| 4021 | if (info->major_version < 0 || |
| 4022 | info->major_version >= sizeof(super_types)/sizeof(super_types[0]) || |
| 4023 | super_types[info->major_version].name == NULL) { |
| 4024 | /* maybe try to auto-load a module? */ |
| 4025 | printk(KERN_INFO |
| 4026 | "md: superblock version %d not known\n", |
| 4027 | info->major_version); |
| 4028 | return -EINVAL; |
| 4029 | } |
| 4030 | mddev->major_version = info->major_version; |
| 4031 | mddev->minor_version = info->minor_version; |
| 4032 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4033 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | return 0; |
| 4035 | } |
| 4036 | mddev->major_version = MD_MAJOR_VERSION; |
| 4037 | mddev->minor_version = MD_MINOR_VERSION; |
| 4038 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 4039 | mddev->ctime = get_seconds(); |
| 4040 | |
| 4041 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 4042 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | mddev->size = info->size; |
| 4044 | mddev->raid_disks = info->raid_disks; |
| 4045 | /* don't set md_minor, it is determined by which /dev/md* was |
| 4046 | * openned |
| 4047 | */ |
| 4048 | if (info->state & (1<<MD_SB_CLEAN)) |
| 4049 | mddev->recovery_cp = MaxSector; |
| 4050 | else |
| 4051 | mddev->recovery_cp = 0; |
| 4052 | mddev->persistent = ! info->not_persistent; |
| 4053 | |
| 4054 | mddev->layout = info->layout; |
| 4055 | mddev->chunk_size = info->chunk_size; |
| 4056 | |
| 4057 | mddev->max_disks = MD_SB_DISKS; |
| 4058 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4059 | mddev->flags = 0; |
| 4060 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 4062 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 4063 | mddev->bitmap_offset = 0; |
| 4064 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4065 | mddev->reshape_position = MaxSector; |
| 4066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | /* |
| 4068 | * Generate a 128 bit UUID |
| 4069 | */ |
| 4070 | get_random_bytes(mddev->uuid, 16); |
| 4071 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4072 | mddev->new_level = mddev->level; |
| 4073 | mddev->new_chunk = mddev->chunk_size; |
| 4074 | mddev->new_layout = mddev->layout; |
| 4075 | mddev->delta_disks = 0; |
| 4076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | return 0; |
| 4078 | } |
| 4079 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4080 | static int update_size(mddev_t *mddev, unsigned long size) |
| 4081 | { |
| 4082 | mdk_rdev_t * rdev; |
| 4083 | int rv; |
| 4084 | struct list_head *tmp; |
NeilBrown | 8ddeeae | 2006-03-27 01:18:18 -0800 | [diff] [blame] | 4085 | int fit = (size == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4086 | |
| 4087 | if (mddev->pers->resize == NULL) |
| 4088 | return -EINVAL; |
| 4089 | /* The "size" is the amount of each device that is used. |
| 4090 | * This can only make sense for arrays with redundancy. |
| 4091 | * linear and raid0 always use whatever space is available |
| 4092 | * We can only consider changing the size if no resync |
| 4093 | * or reconstruction is happening, and if the new size |
| 4094 | * is acceptable. It must fit before the sb_offset or, |
| 4095 | * if that is <data_offset, it must fit before the |
| 4096 | * size of each device. |
| 4097 | * If size is zero, we find the largest size that fits. |
| 4098 | */ |
| 4099 | if (mddev->sync_thread) |
| 4100 | return -EBUSY; |
| 4101 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 4102 | sector_t avail; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 4103 | avail = rdev->size * 2; |
| 4104 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4105 | if (fit && (size == 0 || size > avail/2)) |
| 4106 | size = avail/2; |
| 4107 | if (avail < ((sector_t)size << 1)) |
| 4108 | return -ENOSPC; |
| 4109 | } |
| 4110 | rv = mddev->pers->resize(mddev, (sector_t)size *2); |
| 4111 | if (!rv) { |
| 4112 | struct block_device *bdev; |
| 4113 | |
| 4114 | bdev = bdget_disk(mddev->gendisk, 0); |
| 4115 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4116 | mutex_lock(&bdev->bd_inode->i_mutex); |
NeilBrown | 6d89332 | 2006-02-02 14:28:03 -0800 | [diff] [blame] | 4117 | i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4118 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4119 | bdput(bdev); |
| 4120 | } |
| 4121 | } |
| 4122 | return rv; |
| 4123 | } |
| 4124 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4125 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 4126 | { |
| 4127 | int rv; |
| 4128 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4129 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4130 | return -EINVAL; |
| 4131 | if (raid_disks <= 0 || |
| 4132 | raid_disks >= mddev->max_disks) |
| 4133 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4134 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4135 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4136 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 4137 | |
| 4138 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4139 | return rv; |
| 4140 | } |
| 4141 | |
| 4142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4143 | /* |
| 4144 | * update_array_info is used to change the configuration of an |
| 4145 | * on-line array. |
| 4146 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 4147 | * fields in the info are checked against the array. |
| 4148 | * Any differences that cannot be handled will cause an error. |
| 4149 | * Normally, only one change can be managed at a time. |
| 4150 | */ |
| 4151 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 4152 | { |
| 4153 | int rv = 0; |
| 4154 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4155 | int state = 0; |
| 4156 | |
| 4157 | /* calculate expected state,ignoring low bits */ |
| 4158 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4159 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | |
| 4161 | if (mddev->major_version != info->major_version || |
| 4162 | mddev->minor_version != info->minor_version || |
| 4163 | /* mddev->patch_version != info->patch_version || */ |
| 4164 | mddev->ctime != info->ctime || |
| 4165 | mddev->level != info->level || |
| 4166 | /* mddev->layout != info->layout || */ |
| 4167 | !mddev->persistent != info->not_persistent|| |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4168 | mddev->chunk_size != info->chunk_size || |
| 4169 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 4170 | ((state^info->state) & 0xfffffe00) |
| 4171 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | return -EINVAL; |
| 4173 | /* Check there is only one change */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4174 | if (info->size >= 0 && mddev->size != info->size) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | if (mddev->raid_disks != info->raid_disks) cnt++; |
| 4176 | if (mddev->layout != info->layout) cnt++; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4177 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | if (cnt == 0) return 0; |
| 4179 | if (cnt > 1) return -EINVAL; |
| 4180 | |
| 4181 | if (mddev->layout != info->layout) { |
| 4182 | /* Change layout |
| 4183 | * we don't need to do anything at the md level, the |
| 4184 | * personality will take care of it all. |
| 4185 | */ |
| 4186 | if (mddev->pers->reconfig == NULL) |
| 4187 | return -EINVAL; |
| 4188 | else |
| 4189 | return mddev->pers->reconfig(mddev, info->layout, -1); |
| 4190 | } |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4191 | if (info->size >= 0 && mddev->size != info->size) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4192 | rv = update_size(mddev, info->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4194 | if (mddev->raid_disks != info->raid_disks) |
| 4195 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4197 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4198 | if (mddev->pers->quiesce == NULL) |
| 4199 | return -EINVAL; |
| 4200 | if (mddev->recovery || mddev->sync_thread) |
| 4201 | return -EBUSY; |
| 4202 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 4203 | /* add the bitmap */ |
| 4204 | if (mddev->bitmap) |
| 4205 | return -EEXIST; |
| 4206 | if (mddev->default_bitmap_offset == 0) |
| 4207 | return -EINVAL; |
| 4208 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 4209 | mddev->pers->quiesce(mddev, 1); |
| 4210 | rv = bitmap_create(mddev); |
| 4211 | if (rv) |
| 4212 | bitmap_destroy(mddev); |
| 4213 | mddev->pers->quiesce(mddev, 0); |
| 4214 | } else { |
| 4215 | /* remove the bitmap */ |
| 4216 | if (!mddev->bitmap) |
| 4217 | return -ENOENT; |
| 4218 | if (mddev->bitmap->file) |
| 4219 | return -EINVAL; |
| 4220 | mddev->pers->quiesce(mddev, 1); |
| 4221 | bitmap_destroy(mddev); |
| 4222 | mddev->pers->quiesce(mddev, 0); |
| 4223 | mddev->bitmap_offset = 0; |
| 4224 | } |
| 4225 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4226 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | return rv; |
| 4228 | } |
| 4229 | |
| 4230 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 4231 | { |
| 4232 | mdk_rdev_t *rdev; |
| 4233 | |
| 4234 | if (mddev->pers == NULL) |
| 4235 | return -ENODEV; |
| 4236 | |
| 4237 | rdev = find_rdev(mddev, dev); |
| 4238 | if (!rdev) |
| 4239 | return -ENODEV; |
| 4240 | |
| 4241 | md_error(mddev, rdev); |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4245 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 4246 | { |
| 4247 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 4248 | |
| 4249 | geo->heads = 2; |
| 4250 | geo->sectors = 4; |
| 4251 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 4252 | return 0; |
| 4253 | } |
| 4254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | static int md_ioctl(struct inode *inode, struct file *file, |
| 4256 | unsigned int cmd, unsigned long arg) |
| 4257 | { |
| 4258 | int err = 0; |
| 4259 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | mddev_t *mddev = NULL; |
| 4261 | |
| 4262 | if (!capable(CAP_SYS_ADMIN)) |
| 4263 | return -EACCES; |
| 4264 | |
| 4265 | /* |
| 4266 | * Commands dealing with the RAID driver but not any |
| 4267 | * particular array: |
| 4268 | */ |
| 4269 | switch (cmd) |
| 4270 | { |
| 4271 | case RAID_VERSION: |
| 4272 | err = get_version(argp); |
| 4273 | goto done; |
| 4274 | |
| 4275 | case PRINT_RAID_DEBUG: |
| 4276 | err = 0; |
| 4277 | md_print_devices(); |
| 4278 | goto done; |
| 4279 | |
| 4280 | #ifndef MODULE |
| 4281 | case RAID_AUTORUN: |
| 4282 | err = 0; |
| 4283 | autostart_arrays(arg); |
| 4284 | goto done; |
| 4285 | #endif |
| 4286 | default:; |
| 4287 | } |
| 4288 | |
| 4289 | /* |
| 4290 | * Commands creating/starting a new array: |
| 4291 | */ |
| 4292 | |
| 4293 | mddev = inode->i_bdev->bd_disk->private_data; |
| 4294 | |
| 4295 | if (!mddev) { |
| 4296 | BUG(); |
| 4297 | goto abort; |
| 4298 | } |
| 4299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | err = mddev_lock(mddev); |
| 4301 | if (err) { |
| 4302 | printk(KERN_INFO |
| 4303 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 4304 | err, cmd); |
| 4305 | goto abort; |
| 4306 | } |
| 4307 | |
| 4308 | switch (cmd) |
| 4309 | { |
| 4310 | case SET_ARRAY_INFO: |
| 4311 | { |
| 4312 | mdu_array_info_t info; |
| 4313 | if (!arg) |
| 4314 | memset(&info, 0, sizeof(info)); |
| 4315 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 4316 | err = -EFAULT; |
| 4317 | goto abort_unlock; |
| 4318 | } |
| 4319 | if (mddev->pers) { |
| 4320 | err = update_array_info(mddev, &info); |
| 4321 | if (err) { |
| 4322 | printk(KERN_WARNING "md: couldn't update" |
| 4323 | " array info. %d\n", err); |
| 4324 | goto abort_unlock; |
| 4325 | } |
| 4326 | goto done_unlock; |
| 4327 | } |
| 4328 | if (!list_empty(&mddev->disks)) { |
| 4329 | printk(KERN_WARNING |
| 4330 | "md: array %s already has disks!\n", |
| 4331 | mdname(mddev)); |
| 4332 | err = -EBUSY; |
| 4333 | goto abort_unlock; |
| 4334 | } |
| 4335 | if (mddev->raid_disks) { |
| 4336 | printk(KERN_WARNING |
| 4337 | "md: array %s already initialised!\n", |
| 4338 | mdname(mddev)); |
| 4339 | err = -EBUSY; |
| 4340 | goto abort_unlock; |
| 4341 | } |
| 4342 | err = set_array_info(mddev, &info); |
| 4343 | if (err) { |
| 4344 | printk(KERN_WARNING "md: couldn't set" |
| 4345 | " array info. %d\n", err); |
| 4346 | goto abort_unlock; |
| 4347 | } |
| 4348 | } |
| 4349 | goto done_unlock; |
| 4350 | |
| 4351 | default:; |
| 4352 | } |
| 4353 | |
| 4354 | /* |
| 4355 | * Commands querying/configuring an existing array: |
| 4356 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4357 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4358 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4359 | if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4360 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 4361 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | err = -ENODEV; |
| 4363 | goto abort_unlock; |
| 4364 | } |
| 4365 | |
| 4366 | /* |
| 4367 | * Commands even a read-only array can execute: |
| 4368 | */ |
| 4369 | switch (cmd) |
| 4370 | { |
| 4371 | case GET_ARRAY_INFO: |
| 4372 | err = get_array_info(mddev, argp); |
| 4373 | goto done_unlock; |
| 4374 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4375 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4376 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4377 | goto done_unlock; |
| 4378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | case GET_DISK_INFO: |
| 4380 | err = get_disk_info(mddev, argp); |
| 4381 | goto done_unlock; |
| 4382 | |
| 4383 | case RESTART_ARRAY_RW: |
| 4384 | err = restart_array(mddev); |
| 4385 | goto done_unlock; |
| 4386 | |
| 4387 | case STOP_ARRAY: |
| 4388 | err = do_md_stop (mddev, 0); |
| 4389 | goto done_unlock; |
| 4390 | |
| 4391 | case STOP_ARRAY_RO: |
| 4392 | err = do_md_stop (mddev, 1); |
| 4393 | goto done_unlock; |
| 4394 | |
| 4395 | /* |
| 4396 | * We have a problem here : there is no easy way to give a CHS |
| 4397 | * virtual geometry. We currently pretend that we have a 2 heads |
| 4398 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 4399 | * dosfs just mad... ;-) |
| 4400 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | /* |
| 4404 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4405 | * superblock, so we do not allow them on read-only arrays. |
| 4406 | * However non-MD ioctls (e.g. get-size) will still come through |
| 4407 | * here and hit the 'default' below, so only disallow |
| 4408 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | */ |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4410 | if (_IOC_TYPE(cmd) == MD_MAJOR && |
| 4411 | mddev->ro && mddev->pers) { |
| 4412 | if (mddev->ro == 2) { |
| 4413 | mddev->ro = 0; |
| 4414 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4415 | md_wakeup_thread(mddev->thread); |
| 4416 | |
| 4417 | } else { |
| 4418 | err = -EROFS; |
| 4419 | goto abort_unlock; |
| 4420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | switch (cmd) |
| 4424 | { |
| 4425 | case ADD_NEW_DISK: |
| 4426 | { |
| 4427 | mdu_disk_info_t info; |
| 4428 | if (copy_from_user(&info, argp, sizeof(info))) |
| 4429 | err = -EFAULT; |
| 4430 | else |
| 4431 | err = add_new_disk(mddev, &info); |
| 4432 | goto done_unlock; |
| 4433 | } |
| 4434 | |
| 4435 | case HOT_REMOVE_DISK: |
| 4436 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 4437 | goto done_unlock; |
| 4438 | |
| 4439 | case HOT_ADD_DISK: |
| 4440 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 4441 | goto done_unlock; |
| 4442 | |
| 4443 | case SET_DISK_FAULTY: |
| 4444 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 4445 | goto done_unlock; |
| 4446 | |
| 4447 | case RUN_ARRAY: |
| 4448 | err = do_md_run (mddev); |
| 4449 | goto done_unlock; |
| 4450 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4451 | case SET_BITMAP_FILE: |
| 4452 | err = set_bitmap_file(mddev, (int)arg); |
| 4453 | goto done_unlock; |
| 4454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | err = -EINVAL; |
| 4457 | goto abort_unlock; |
| 4458 | } |
| 4459 | |
| 4460 | done_unlock: |
| 4461 | abort_unlock: |
| 4462 | mddev_unlock(mddev); |
| 4463 | |
| 4464 | return err; |
| 4465 | done: |
| 4466 | if (err) |
| 4467 | MD_BUG(); |
| 4468 | abort: |
| 4469 | return err; |
| 4470 | } |
| 4471 | |
| 4472 | static int md_open(struct inode *inode, struct file *file) |
| 4473 | { |
| 4474 | /* |
| 4475 | * Succeed if we can lock the mddev, which confirms that |
| 4476 | * it isn't being stopped right now. |
| 4477 | */ |
| 4478 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4479 | int err; |
| 4480 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 4481 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | goto out; |
| 4483 | |
| 4484 | err = 0; |
| 4485 | mddev_get(mddev); |
| 4486 | mddev_unlock(mddev); |
| 4487 | |
| 4488 | check_disk_change(inode->i_bdev); |
| 4489 | out: |
| 4490 | return err; |
| 4491 | } |
| 4492 | |
| 4493 | static int md_release(struct inode *inode, struct file * file) |
| 4494 | { |
| 4495 | mddev_t *mddev = inode->i_bdev->bd_disk->private_data; |
| 4496 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 4497 | BUG_ON(!mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | mddev_put(mddev); |
| 4499 | |
| 4500 | return 0; |
| 4501 | } |
| 4502 | |
| 4503 | static int md_media_changed(struct gendisk *disk) |
| 4504 | { |
| 4505 | mddev_t *mddev = disk->private_data; |
| 4506 | |
| 4507 | return mddev->changed; |
| 4508 | } |
| 4509 | |
| 4510 | static int md_revalidate(struct gendisk *disk) |
| 4511 | { |
| 4512 | mddev_t *mddev = disk->private_data; |
| 4513 | |
| 4514 | mddev->changed = 0; |
| 4515 | return 0; |
| 4516 | } |
| 4517 | static struct block_device_operations md_fops = |
| 4518 | { |
| 4519 | .owner = THIS_MODULE, |
| 4520 | .open = md_open, |
| 4521 | .release = md_release, |
| 4522 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 4523 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | .media_changed = md_media_changed, |
| 4525 | .revalidate_disk= md_revalidate, |
| 4526 | }; |
| 4527 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 4528 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | { |
| 4530 | mdk_thread_t *thread = arg; |
| 4531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | /* |
| 4533 | * md_thread is a 'system-thread', it's priority should be very |
| 4534 | * high. We avoid resource deadlocks individually in each |
| 4535 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 4536 | * the very same RT priority as kswapd, thus we will never get |
| 4537 | * into a priority inversion deadlock. |
| 4538 | * |
| 4539 | * we definitely have to have equal or higher priority than |
| 4540 | * bdflush, otherwise bdflush will deadlock if there are too |
| 4541 | * many dirty RAID5 blocks. |
| 4542 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | |
Rafael J. Wysocki | 4b438a2 | 2006-11-08 17:44:48 -0800 | [diff] [blame] | 4544 | current->flags |= PF_NOFREEZE; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 4545 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4546 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4547 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 4548 | /* We need to wait INTERRUPTIBLE so that |
| 4549 | * we don't add to the load-average. |
| 4550 | * That means we need to be sure no signals are |
| 4551 | * pending |
| 4552 | */ |
| 4553 | if (signal_pending(current)) |
| 4554 | flush_signals(current); |
| 4555 | |
| 4556 | wait_event_interruptible_timeout |
| 4557 | (thread->wqueue, |
| 4558 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 4559 | || kthread_should_stop(), |
| 4560 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | |
| 4562 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 4563 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 4564 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | return 0; |
| 4568 | } |
| 4569 | |
| 4570 | void md_wakeup_thread(mdk_thread_t *thread) |
| 4571 | { |
| 4572 | if (thread) { |
| 4573 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 4574 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 4575 | wake_up(&thread->wqueue); |
| 4576 | } |
| 4577 | } |
| 4578 | |
| 4579 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 4580 | const char *name) |
| 4581 | { |
| 4582 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 4584 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | if (!thread) |
| 4586 | return NULL; |
| 4587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | init_waitqueue_head(&thread->wqueue); |
| 4589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | thread->run = run; |
| 4591 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4592 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 4593 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4594 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | kfree(thread); |
| 4596 | return NULL; |
| 4597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | return thread; |
| 4599 | } |
| 4600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4601 | void md_unregister_thread(mdk_thread_t *thread) |
| 4602 | { |
NeilBrown | d28446f | 2005-04-16 15:26:41 -0700 | [diff] [blame] | 4603 | dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 4604 | |
| 4605 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | kfree(thread); |
| 4607 | } |
| 4608 | |
| 4609 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 4610 | { |
| 4611 | if (!mddev) { |
| 4612 | MD_BUG(); |
| 4613 | return; |
| 4614 | } |
| 4615 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4616 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | return; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4618 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4619 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 4620 | mdname(mddev), |
| 4621 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 4622 | __builtin_return_address(0),__builtin_return_address(1), |
| 4623 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4624 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 4625 | if (!mddev->pers) |
| 4626 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | if (!mddev->pers->error_handler) |
| 4628 | return; |
| 4629 | mddev->pers->error_handler(mddev,rdev); |
| 4630 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4631 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4632 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 4633 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | /* seq_file implementation /proc/mdstat */ |
| 4637 | |
| 4638 | static void status_unused(struct seq_file *seq) |
| 4639 | { |
| 4640 | int i = 0; |
| 4641 | mdk_rdev_t *rdev; |
| 4642 | struct list_head *tmp; |
| 4643 | |
| 4644 | seq_printf(seq, "unused devices: "); |
| 4645 | |
| 4646 | ITERATE_RDEV_PENDING(rdev,tmp) { |
| 4647 | char b[BDEVNAME_SIZE]; |
| 4648 | i++; |
| 4649 | seq_printf(seq, "%s ", |
| 4650 | bdevname(rdev->bdev,b)); |
| 4651 | } |
| 4652 | if (!i) |
| 4653 | seq_printf(seq, "<none>"); |
| 4654 | |
| 4655 | seq_printf(seq, "\n"); |
| 4656 | } |
| 4657 | |
| 4658 | |
| 4659 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 4660 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4661 | sector_t max_blocks, resync, res; |
| 4662 | unsigned long dt, db, rt; |
| 4663 | int scale; |
| 4664 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4665 | |
| 4666 | resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2; |
| 4667 | |
| 4668 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 4669 | max_blocks = mddev->resync_max_sectors >> 1; |
| 4670 | else |
| 4671 | max_blocks = mddev->size; |
| 4672 | |
| 4673 | /* |
| 4674 | * Should not happen. |
| 4675 | */ |
| 4676 | if (!max_blocks) { |
| 4677 | MD_BUG(); |
| 4678 | return; |
| 4679 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4680 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
| 4681 | * in a sector_t, and (max_blocks>>scale) will fit in a |
| 4682 | * u32, as those are the requirements for sector_div. |
| 4683 | * Thus 'scale' must be at least 10 |
| 4684 | */ |
| 4685 | scale = 10; |
| 4686 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
| 4687 | while ( max_blocks/2 > (1ULL<<(scale+32))) |
| 4688 | scale++; |
| 4689 | } |
| 4690 | res = (resync>>scale)*1000; |
| 4691 | sector_div(res, (u32)((max_blocks>>scale)+1)); |
| 4692 | |
| 4693 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4695 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | seq_printf(seq, "["); |
| 4697 | for (i = 0; i < x; i++) |
| 4698 | seq_printf(seq, "="); |
| 4699 | seq_printf(seq, ">"); |
| 4700 | for (i = 0; i < y; i++) |
| 4701 | seq_printf(seq, "."); |
| 4702 | seq_printf(seq, "] "); |
| 4703 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4704 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4705 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 4706 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 4707 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 4708 | "check" : |
| 4709 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 4710 | "resync" : "recovery"))), |
| 4711 | per_milli/10, per_milli % 10, |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 4712 | (unsigned long long) resync, |
| 4713 | (unsigned long long) max_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | |
| 4715 | /* |
| 4716 | * We do not want to overflow, so the order of operands and |
| 4717 | * the * 100 / 100 trick are important. We do a +1 to be |
| 4718 | * safe against division by zero. We only estimate anyway. |
| 4719 | * |
| 4720 | * dt: time from mark until now |
| 4721 | * db: blocks written from mark until now |
| 4722 | * rt: remaining time |
| 4723 | */ |
| 4724 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 4725 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 4726 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 4727 | - mddev->resync_mark_cnt; |
| 4728 | rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | |
| 4730 | seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6); |
| 4731 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 4732 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | } |
| 4734 | |
| 4735 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 4736 | { |
| 4737 | struct list_head *tmp; |
| 4738 | loff_t l = *pos; |
| 4739 | mddev_t *mddev; |
| 4740 | |
| 4741 | if (l >= 0x10000) |
| 4742 | return NULL; |
| 4743 | if (!l--) |
| 4744 | /* header */ |
| 4745 | return (void*)1; |
| 4746 | |
| 4747 | spin_lock(&all_mddevs_lock); |
| 4748 | list_for_each(tmp,&all_mddevs) |
| 4749 | if (!l--) { |
| 4750 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 4751 | mddev_get(mddev); |
| 4752 | spin_unlock(&all_mddevs_lock); |
| 4753 | return mddev; |
| 4754 | } |
| 4755 | spin_unlock(&all_mddevs_lock); |
| 4756 | if (!l--) |
| 4757 | return (void*)2;/* tail */ |
| 4758 | return NULL; |
| 4759 | } |
| 4760 | |
| 4761 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 4762 | { |
| 4763 | struct list_head *tmp; |
| 4764 | mddev_t *next_mddev, *mddev = v; |
| 4765 | |
| 4766 | ++*pos; |
| 4767 | if (v == (void*)2) |
| 4768 | return NULL; |
| 4769 | |
| 4770 | spin_lock(&all_mddevs_lock); |
| 4771 | if (v == (void*)1) |
| 4772 | tmp = all_mddevs.next; |
| 4773 | else |
| 4774 | tmp = mddev->all_mddevs.next; |
| 4775 | if (tmp != &all_mddevs) |
| 4776 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 4777 | else { |
| 4778 | next_mddev = (void*)2; |
| 4779 | *pos = 0x10000; |
| 4780 | } |
| 4781 | spin_unlock(&all_mddevs_lock); |
| 4782 | |
| 4783 | if (v != (void*)1) |
| 4784 | mddev_put(mddev); |
| 4785 | return next_mddev; |
| 4786 | |
| 4787 | } |
| 4788 | |
| 4789 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 4790 | { |
| 4791 | mddev_t *mddev = v; |
| 4792 | |
| 4793 | if (mddev && v != (void*)1 && v != (void*)2) |
| 4794 | mddev_put(mddev); |
| 4795 | } |
| 4796 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4797 | struct mdstat_info { |
| 4798 | int event; |
| 4799 | }; |
| 4800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | static int md_seq_show(struct seq_file *seq, void *v) |
| 4802 | { |
| 4803 | mddev_t *mddev = v; |
| 4804 | sector_t size; |
| 4805 | struct list_head *tmp2; |
| 4806 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4807 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4808 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | |
| 4810 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4811 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | seq_printf(seq, "Personalities : "); |
| 4813 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4814 | list_for_each_entry(pers, &pers_list, list) |
| 4815 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | |
| 4817 | spin_unlock(&pers_lock); |
| 4818 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4819 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | return 0; |
| 4821 | } |
| 4822 | if (v == (void*)2) { |
| 4823 | status_unused(seq); |
| 4824 | return 0; |
| 4825 | } |
| 4826 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4827 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 4831 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 4832 | mddev->pers ? "" : "in"); |
| 4833 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4834 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4836 | if (mddev->ro==2) |
| 4837 | seq_printf(seq, "(auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | seq_printf(seq, " %s", mddev->pers->name); |
| 4839 | } |
| 4840 | |
| 4841 | size = 0; |
| 4842 | ITERATE_RDEV(mddev,rdev,tmp2) { |
| 4843 | char b[BDEVNAME_SIZE]; |
| 4844 | seq_printf(seq, " %s[%d]", |
| 4845 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4846 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4847 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4848 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | seq_printf(seq, "(F)"); |
| 4850 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 4851 | } else if (rdev->raid_disk < 0) |
| 4852 | seq_printf(seq, "(S)"); /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | size += rdev->size; |
| 4854 | } |
| 4855 | |
| 4856 | if (!list_empty(&mddev->disks)) { |
| 4857 | if (mddev->pers) |
| 4858 | seq_printf(seq, "\n %llu blocks", |
| 4859 | (unsigned long long)mddev->array_size); |
| 4860 | else |
| 4861 | seq_printf(seq, "\n %llu blocks", |
| 4862 | (unsigned long long)size); |
| 4863 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 4864 | if (mddev->persistent) { |
| 4865 | if (mddev->major_version != 0 || |
| 4866 | mddev->minor_version != 90) { |
| 4867 | seq_printf(seq," super %d.%d", |
| 4868 | mddev->major_version, |
| 4869 | mddev->minor_version); |
| 4870 | } |
| 4871 | } else |
| 4872 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | |
| 4874 | if (mddev->pers) { |
| 4875 | mddev->pers->status (seq, mddev); |
| 4876 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 4877 | if (mddev->pers->sync_request) { |
| 4878 | if (mddev->curr_resync > 2) { |
| 4879 | status_resync (seq, mddev); |
| 4880 | seq_printf(seq, "\n "); |
| 4881 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 4882 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 4883 | else if (mddev->recovery_cp < MaxSector) |
| 4884 | seq_printf(seq, "\tresync=PENDING\n "); |
| 4885 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4886 | } else |
| 4887 | seq_printf(seq, "\n "); |
| 4888 | |
| 4889 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4890 | unsigned long chunk_kb; |
| 4891 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4892 | spin_lock_irqsave(&bitmap->lock, flags); |
| 4893 | chunk_kb = bitmap->chunksize >> 10; |
| 4894 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 4895 | "%lu%s chunk", |
| 4896 | bitmap->pages - bitmap->missing_pages, |
| 4897 | bitmap->pages, |
| 4898 | (bitmap->pages - bitmap->missing_pages) |
| 4899 | << (PAGE_SHIFT - 10), |
| 4900 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 4901 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 4902 | if (bitmap->file) { |
| 4903 | seq_printf(seq, ", file: "); |
Josef Sipek | c649bb9 | 2006-12-08 02:37:19 -0800 | [diff] [blame] | 4904 | seq_path(seq, bitmap->file->f_path.mnt, |
| 4905 | bitmap->file->f_path.dentry," \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4906 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 4907 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4908 | seq_printf(seq, "\n"); |
| 4909 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | seq_printf(seq, "\n"); |
| 4913 | } |
| 4914 | mddev_unlock(mddev); |
| 4915 | |
| 4916 | return 0; |
| 4917 | } |
| 4918 | |
| 4919 | static struct seq_operations md_seq_ops = { |
| 4920 | .start = md_seq_start, |
| 4921 | .next = md_seq_next, |
| 4922 | .stop = md_seq_stop, |
| 4923 | .show = md_seq_show, |
| 4924 | }; |
| 4925 | |
| 4926 | static int md_seq_open(struct inode *inode, struct file *file) |
| 4927 | { |
| 4928 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4929 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 4930 | if (mi == NULL) |
| 4931 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | |
| 4933 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4934 | if (error) |
| 4935 | kfree(mi); |
| 4936 | else { |
| 4937 | struct seq_file *p = file->private_data; |
| 4938 | p->private = mi; |
| 4939 | mi->event = atomic_read(&md_event_count); |
| 4940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4941 | return error; |
| 4942 | } |
| 4943 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4944 | static int md_seq_release(struct inode *inode, struct file *file) |
| 4945 | { |
| 4946 | struct seq_file *m = file->private_data; |
| 4947 | struct mdstat_info *mi = m->private; |
| 4948 | m->private = NULL; |
| 4949 | kfree(mi); |
| 4950 | return seq_release(inode, file); |
| 4951 | } |
| 4952 | |
| 4953 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 4954 | { |
| 4955 | struct seq_file *m = filp->private_data; |
| 4956 | struct mdstat_info *mi = m->private; |
| 4957 | int mask; |
| 4958 | |
| 4959 | poll_wait(filp, &md_event_waiters, wait); |
| 4960 | |
| 4961 | /* always allow read */ |
| 4962 | mask = POLLIN | POLLRDNORM; |
| 4963 | |
| 4964 | if (mi->event != atomic_read(&md_event_count)) |
| 4965 | mask |= POLLERR | POLLPRI; |
| 4966 | return mask; |
| 4967 | } |
| 4968 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 4969 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 4970 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | .open = md_seq_open, |
| 4972 | .read = seq_read, |
| 4973 | .llseek = seq_lseek, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4974 | .release = md_seq_release, |
| 4975 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | }; |
| 4977 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4978 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4981 | list_add_tail(&p->list, &pers_list); |
| 4982 | 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] | 4983 | spin_unlock(&pers_lock); |
| 4984 | return 0; |
| 4985 | } |
| 4986 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4987 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4989 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4990 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4991 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | spin_unlock(&pers_lock); |
| 4993 | return 0; |
| 4994 | } |
| 4995 | |
| 4996 | static int is_mddev_idle(mddev_t *mddev) |
| 4997 | { |
| 4998 | mdk_rdev_t * rdev; |
| 4999 | struct list_head *tmp; |
| 5000 | int idle; |
| 5001 | unsigned long curr_events; |
| 5002 | |
| 5003 | idle = 1; |
| 5004 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 5005 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5006 | curr_events = disk_stat_read(disk, sectors[0]) + |
| 5007 | disk_stat_read(disk, sectors[1]) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | atomic_read(&disk->sync_io); |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5009 | /* The difference between curr_events and last_events |
| 5010 | * will be affected by any new non-sync IO (making |
| 5011 | * curr_events bigger) and any difference in the amount of |
| 5012 | * in-flight syncio (making current_events bigger or smaller) |
| 5013 | * The amount in-flight is currently limited to |
| 5014 | * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6 |
| 5015 | * which is at most 4096 sectors. |
| 5016 | * These numbers are fairly fragile and should be made |
| 5017 | * more robust, probably by enforcing the |
| 5018 | * 'window size' that md_do_sync sort-of uses. |
| 5019 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | * Note: the following is an unsigned comparison. |
| 5021 | */ |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5022 | if ((curr_events - rdev->last_events + 4096) > 8192) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | rdev->last_events = curr_events; |
| 5024 | idle = 0; |
| 5025 | } |
| 5026 | } |
| 5027 | return idle; |
| 5028 | } |
| 5029 | |
| 5030 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 5031 | { |
| 5032 | /* another "blocks" (512byte) blocks have been synced */ |
| 5033 | atomic_sub(blocks, &mddev->recovery_active); |
| 5034 | wake_up(&mddev->recovery_wait); |
| 5035 | if (!ok) { |
| 5036 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 5037 | md_wakeup_thread(mddev->thread); |
| 5038 | // stop recovery, signal do_sync .... |
| 5039 | } |
| 5040 | } |
| 5041 | |
| 5042 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5043 | /* md_write_start(mddev, bi) |
| 5044 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5045 | * in superblock) before writing, schedule a superblock update |
| 5046 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5047 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5048 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5049 | { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5050 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5051 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5052 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5053 | BUG_ON(mddev->ro == 1); |
| 5054 | if (mddev->ro == 2) { |
| 5055 | /* need to switch to read/write */ |
| 5056 | mddev->ro = 0; |
| 5057 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5058 | md_wakeup_thread(mddev->thread); |
| 5059 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5060 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5061 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5062 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5063 | if (mddev->in_sync) { |
| 5064 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5065 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5066 | md_wakeup_thread(mddev->thread); |
| 5067 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5068 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5069 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5070 | wait_event(mddev->sb_wait, mddev->flags==0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | void md_write_end(mddev_t *mddev) |
| 5074 | { |
| 5075 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 5076 | if (mddev->safemode == 2) |
| 5077 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5078 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5079 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 5080 | } |
| 5081 | } |
| 5082 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5083 | /* md_allow_write(mddev) |
| 5084 | * Calling this ensures that the array is marked 'active' so that writes |
| 5085 | * may proceed without blocking. It is important to call this before |
| 5086 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 5087 | * Must be called with mddev_lock held. |
| 5088 | */ |
| 5089 | void md_allow_write(mddev_t *mddev) |
| 5090 | { |
| 5091 | if (!mddev->pers) |
| 5092 | return; |
| 5093 | if (mddev->ro) |
| 5094 | return; |
| 5095 | |
| 5096 | spin_lock_irq(&mddev->write_lock); |
| 5097 | if (mddev->in_sync) { |
| 5098 | mddev->in_sync = 0; |
| 5099 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 5100 | if (mddev->safemode_delay && |
| 5101 | mddev->safemode == 0) |
| 5102 | mddev->safemode = 1; |
| 5103 | spin_unlock_irq(&mddev->write_lock); |
| 5104 | md_update_sb(mddev, 0); |
| 5105 | } else |
| 5106 | spin_unlock_irq(&mddev->write_lock); |
| 5107 | } |
| 5108 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 5109 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5110 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | |
| 5112 | #define SYNC_MARKS 10 |
| 5113 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5114 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | { |
| 5116 | mddev_t *mddev2; |
| 5117 | unsigned int currspeed = 0, |
| 5118 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5119 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | unsigned long mark[SYNC_MARKS]; |
| 5121 | sector_t mark_cnt[SYNC_MARKS]; |
| 5122 | int last_mark,m; |
| 5123 | struct list_head *tmp; |
| 5124 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5125 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5126 | struct list_head *rtmp; |
| 5127 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5128 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | |
| 5130 | /* just incase thread restarts... */ |
| 5131 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 5132 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5133 | if (mddev->ro) /* never try to sync a read-only array */ |
| 5134 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5135 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5136 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5137 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 5138 | desc = "data-check"; |
| 5139 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5140 | desc = "requested-resync"; |
| 5141 | else |
| 5142 | desc = "resync"; |
| 5143 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5144 | desc = "reshape"; |
| 5145 | else |
| 5146 | desc = "recovery"; |
| 5147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | /* we overload curr_resync somewhat here. |
| 5149 | * 0 == not engaged in resync at all |
| 5150 | * 2 == checking that there is no conflict with another sync |
| 5151 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 5152 | * commense |
| 5153 | * other == active in resync - this many blocks |
| 5154 | * |
| 5155 | * Before starting a resync we must have set curr_resync to |
| 5156 | * 2, and then checked that every "conflicting" array has curr_resync |
| 5157 | * less than ours. When we find one that is the same or higher |
| 5158 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 5159 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 5160 | * This will mean we have to start checking from the beginning again. |
| 5161 | * |
| 5162 | */ |
| 5163 | |
| 5164 | do { |
| 5165 | mddev->curr_resync = 2; |
| 5166 | |
| 5167 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5168 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5169 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | goto skip; |
| 5171 | } |
| 5172 | ITERATE_MDDEV(mddev2,tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | if (mddev2 == mddev) |
| 5174 | continue; |
| 5175 | if (mddev2->curr_resync && |
| 5176 | match_mddev_units(mddev,mddev2)) { |
| 5177 | DEFINE_WAIT(wq); |
| 5178 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 5179 | /* arbitrarily yield */ |
| 5180 | mddev->curr_resync = 1; |
| 5181 | wake_up(&resync_wait); |
| 5182 | } |
| 5183 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 5184 | /* no need to wait here, we can wait the next |
| 5185 | * time 'round when curr_resync == 2 |
| 5186 | */ |
| 5187 | continue; |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5188 | prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 5189 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 5190 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5191 | printk(KERN_INFO "md: delaying %s of %s" |
| 5192 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5194 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | mddev_put(mddev2); |
| 5196 | schedule(); |
| 5197 | finish_wait(&resync_wait, &wq); |
| 5198 | goto try_again; |
| 5199 | } |
| 5200 | finish_wait(&resync_wait, &wq); |
| 5201 | } |
| 5202 | } |
| 5203 | } while (mddev->curr_resync < 2); |
| 5204 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5205 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5206 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5208 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | */ |
| 5210 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5211 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5212 | /* we don't use the checkpoint if there's a bitmap */ |
| 5213 | if (!mddev->bitmap && |
| 5214 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 5215 | j = mddev->recovery_cp; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5216 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5217 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5218 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5219 | /* recovery follows the physical size of devices */ |
| 5220 | max_sectors = mddev->size << 1; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5221 | j = MaxSector; |
| 5222 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5223 | if (rdev->raid_disk >= 0 && |
| 5224 | !test_bit(Faulty, &rdev->flags) && |
| 5225 | !test_bit(In_sync, &rdev->flags) && |
| 5226 | rdev->recovery_offset < j) |
| 5227 | j = rdev->recovery_offset; |
| 5228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5230 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 5231 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 5232 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 5233 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5234 | "(but not more than %d KB/sec) for %s.\n", |
| 5235 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | |
| 5237 | is_mddev_idle(mddev); /* this also initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5238 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5239 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | for (m = 0; m < SYNC_MARKS; m++) { |
| 5241 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5242 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | } |
| 5244 | last_mark = 0; |
| 5245 | mddev->resync_mark = mark[last_mark]; |
| 5246 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 5247 | |
| 5248 | /* |
| 5249 | * Tune reconstruction: |
| 5250 | */ |
| 5251 | window = 32*(PAGE_SIZE/512); |
| 5252 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 5253 | window/2,(unsigned long long) max_sectors/2); |
| 5254 | |
| 5255 | atomic_set(&mddev->recovery_active, 0); |
| 5256 | init_waitqueue_head(&mddev->recovery_wait); |
| 5257 | last_check = 0; |
| 5258 | |
| 5259 | if (j>2) { |
| 5260 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5261 | "md: resuming %s of %s from checkpoint.\n", |
| 5262 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | mddev->curr_resync = j; |
| 5264 | } |
| 5265 | |
| 5266 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5267 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5269 | skipped = 0; |
| 5270 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5271 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5272 | if (sectors == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 5274 | goto out; |
| 5275 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5276 | |
| 5277 | if (!skipped) { /* actual IO requested */ |
| 5278 | io_sectors += sectors; |
| 5279 | atomic_add(sectors, &mddev->recovery_active); |
| 5280 | } |
| 5281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | j += sectors; |
| 5283 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5284 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5285 | if (last_check == 0) |
| 5286 | /* this is the earliers that rebuilt will be |
| 5287 | * visible in /proc/mdstat |
| 5288 | */ |
| 5289 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5290 | |
| 5291 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | continue; |
| 5293 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5294 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | |
| 5296 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) || |
| 5297 | test_bit(MD_RECOVERY_ERR, &mddev->recovery)) |
| 5298 | break; |
| 5299 | |
| 5300 | repeat: |
| 5301 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 5302 | /* step marks */ |
| 5303 | int next = (last_mark+1) % SYNC_MARKS; |
| 5304 | |
| 5305 | mddev->resync_mark = mark[next]; |
| 5306 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 5307 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5308 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | last_mark = next; |
| 5310 | } |
| 5311 | |
| 5312 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5313 | if (kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | /* |
| 5315 | * got a signal, exit. |
| 5316 | */ |
| 5317 | printk(KERN_INFO |
| 5318 | "md: md_do_sync() got signal ... exiting\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5320 | goto out; |
| 5321 | } |
| 5322 | |
| 5323 | /* |
| 5324 | * this loop exits only if either when we are slower than |
| 5325 | * the 'hard' speed limit, or the system was IO-idle for |
| 5326 | * a jiffy. |
| 5327 | * the system might be non-idle CPU-wise, but we only care |
| 5328 | * about not overloading the IO subsystem. (things like an |
| 5329 | * e2fsck being done on the RAID array should execute fast) |
| 5330 | */ |
| 5331 | mddev->queue->unplug_fn(mddev->queue); |
| 5332 | cond_resched(); |
| 5333 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5334 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 5335 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5337 | if (currspeed > speed_min(mddev)) { |
| 5338 | if ((currspeed > speed_max(mddev)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | !is_mddev_idle(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 5340 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | goto repeat; |
| 5342 | } |
| 5343 | } |
| 5344 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5345 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | /* |
| 5347 | * this also signals 'finished resyncing' to md_stop |
| 5348 | */ |
| 5349 | out: |
| 5350 | mddev->queue->unplug_fn(mddev->queue); |
| 5351 | |
| 5352 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 5353 | |
| 5354 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5355 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | |
| 5357 | if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) && |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5358 | !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5359 | mddev->curr_resync > 2) { |
| 5360 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 5361 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5362 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 5363 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5364 | "md: checkpointing %s of %s.\n", |
| 5365 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5366 | mddev->recovery_cp = mddev->curr_resync; |
| 5367 | } |
| 5368 | } else |
| 5369 | mddev->recovery_cp = MaxSector; |
| 5370 | } else { |
| 5371 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5372 | mddev->curr_resync = MaxSector; |
| 5373 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5374 | if (rdev->raid_disk >= 0 && |
| 5375 | !test_bit(Faulty, &rdev->flags) && |
| 5376 | !test_bit(In_sync, &rdev->flags) && |
| 5377 | rdev->recovery_offset < mddev->curr_resync) |
| 5378 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5381 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | skip: |
| 5384 | mddev->curr_resync = 0; |
| 5385 | wake_up(&resync_wait); |
| 5386 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 5387 | md_wakeup_thread(mddev->thread); |
| 5388 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5389 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | |
| 5391 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5392 | static int remove_and_add_spares(mddev_t *mddev) |
| 5393 | { |
| 5394 | mdk_rdev_t *rdev; |
| 5395 | struct list_head *rtmp; |
| 5396 | int spares = 0; |
| 5397 | |
| 5398 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5399 | if (rdev->raid_disk >= 0 && |
| 5400 | (test_bit(Faulty, &rdev->flags) || |
| 5401 | ! test_bit(In_sync, &rdev->flags)) && |
| 5402 | atomic_read(&rdev->nr_pending)==0) { |
| 5403 | if (mddev->pers->hot_remove_disk( |
| 5404 | mddev, rdev->raid_disk)==0) { |
| 5405 | char nm[20]; |
| 5406 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 5407 | sysfs_remove_link(&mddev->kobj, nm); |
| 5408 | rdev->raid_disk = -1; |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | if (mddev->degraded) { |
| 5413 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5414 | if (rdev->raid_disk < 0 |
| 5415 | && !test_bit(Faulty, &rdev->flags)) { |
| 5416 | rdev->recovery_offset = 0; |
| 5417 | if (mddev->pers->hot_add_disk(mddev,rdev)) { |
| 5418 | char nm[20]; |
| 5419 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5420 | if (sysfs_create_link(&mddev->kobj, |
| 5421 | &rdev->kobj, nm)) |
| 5422 | printk(KERN_WARNING |
| 5423 | "md: cannot register " |
| 5424 | "%s for %s\n", |
| 5425 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5426 | spares++; |
| 5427 | md_new_event(mddev); |
| 5428 | } else |
| 5429 | break; |
| 5430 | } |
| 5431 | } |
| 5432 | return spares; |
| 5433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | /* |
| 5435 | * This routine is regularly called by all per-raid-array threads to |
| 5436 | * deal with generic issues like resync and super-block update. |
| 5437 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 5438 | * need this as they never do any recovery or update the superblock. |
| 5439 | * |
| 5440 | * It does not do any resync itself, but rather "forks" off other threads |
| 5441 | * to do that as needed. |
| 5442 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 5443 | * "->recovery" and create a thread at ->sync_thread. |
| 5444 | * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR) |
| 5445 | * and wakeups up this thread which will reap the thread and finish up. |
| 5446 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 5447 | * |
| 5448 | * The overall approach is: |
| 5449 | * 1/ if the superblock needs updating, update it. |
| 5450 | * 2/ If a recovery thread is running, don't do anything else. |
| 5451 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 5452 | * 4/ If there are any faulty devices, remove them. |
| 5453 | * 5/ If array is degraded, try to add spares devices |
| 5454 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 5455 | */ |
| 5456 | void md_check_recovery(mddev_t *mddev) |
| 5457 | { |
| 5458 | mdk_rdev_t *rdev; |
| 5459 | struct list_head *rtmp; |
| 5460 | |
| 5461 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 5462 | if (mddev->bitmap) |
| 5463 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | |
| 5465 | if (mddev->ro) |
| 5466 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5467 | |
| 5468 | if (signal_pending(current)) { |
| 5469 | if (mddev->pers->sync_request) { |
| 5470 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 5471 | mdname(mddev)); |
| 5472 | mddev->safemode = 2; |
| 5473 | } |
| 5474 | flush_signals(current); |
| 5475 | } |
| 5476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | if ( ! ( |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5478 | mddev->flags || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5480 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 5481 | (mddev->safemode == 1) || |
| 5482 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 5483 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | )) |
| 5485 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5486 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5487 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5488 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5489 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5490 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5491 | if (mddev->safemode && !atomic_read(&mddev->writes_pending) && |
| 5492 | !mddev->in_sync && mddev->recovery_cp == MaxSector) { |
| 5493 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5494 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5495 | } |
| 5496 | if (mddev->safemode == 1) |
| 5497 | mddev->safemode = 0; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 5498 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 5499 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5500 | if (mddev->flags) |
| 5501 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5502 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 5505 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 5506 | /* resync/recovery still happening */ |
| 5507 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5508 | goto unlock; |
| 5509 | } |
| 5510 | if (mddev->sync_thread) { |
| 5511 | /* resync has finished, collect result */ |
| 5512 | md_unregister_thread(mddev->sync_thread); |
| 5513 | mddev->sync_thread = NULL; |
| 5514 | if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) && |
| 5515 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 5516 | /* success...*/ |
| 5517 | /* activate any spares */ |
| 5518 | mddev->pers->spare_active(mddev); |
| 5519 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5520 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5521 | |
| 5522 | /* if array is no-longer degraded, then any saved_raid_disk |
| 5523 | * information must be scrapped |
| 5524 | */ |
| 5525 | if (!mddev->degraded) |
| 5526 | ITERATE_RDEV(mddev,rdev,rtmp) |
| 5527 | rdev->saved_raid_disk = -1; |
| 5528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | mddev->recovery = 0; |
| 5530 | /* flag recovery needed just to double check */ |
| 5531 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5532 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | goto unlock; |
| 5534 | } |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5535 | /* Clear some bits that don't mean anything, but |
| 5536 | * might be left set |
| 5537 | */ |
| 5538 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5539 | clear_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 5540 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5541 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 5543 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 5544 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | /* no recovery is running. |
| 5546 | * remove any failed drives, then |
| 5547 | * add spares if possible. |
| 5548 | * Spare are also removed and re-added, to allow |
| 5549 | * the personality to fail the re-add. |
| 5550 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5551 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 5552 | if (mddev->reshape_position != MaxSector) { |
| 5553 | if (mddev->pers->check_reshape(mddev) != 0) |
| 5554 | /* Cannot proceed */ |
| 5555 | goto unlock; |
| 5556 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5557 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5558 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5559 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5560 | } else if (mddev->recovery_cp < MaxSector) { |
| 5561 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5562 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 5563 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | if (mddev->pers->sync_request) { |
| 5567 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 5568 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 5569 | /* We are adding a device or devices to an array |
| 5570 | * which has the bitmap stored on all devices. |
| 5571 | * So make sure all bitmap pages get written |
| 5572 | */ |
| 5573 | bitmap_write_all(mddev->bitmap); |
| 5574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5575 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 5576 | mddev, |
| 5577 | "%s_resync"); |
| 5578 | if (!mddev->sync_thread) { |
| 5579 | printk(KERN_ERR "%s: could not start resync" |
| 5580 | " thread...\n", |
| 5581 | mdname(mddev)); |
| 5582 | /* leave the spares where they are, it shouldn't hurt */ |
| 5583 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5584 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5586 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | } |
| 5588 | unlock: |
| 5589 | mddev_unlock(mddev); |
| 5590 | } |
| 5591 | } |
| 5592 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5593 | static int md_notify_reboot(struct notifier_block *this, |
| 5594 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | { |
| 5596 | struct list_head *tmp; |
| 5597 | mddev_t *mddev; |
| 5598 | |
| 5599 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 5600 | |
| 5601 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 5602 | |
| 5603 | ITERATE_MDDEV(mddev,tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 5604 | if (mddev_trylock(mddev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | do_md_stop (mddev, 1); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 5606 | mddev_unlock(mddev); |
| 5607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | /* |
| 5609 | * certain more exotic SCSI devices are known to be |
| 5610 | * volatile wrt too early system reboots. While the |
| 5611 | * right place to handle this issue is the given |
| 5612 | * driver, we do want to have a safe RAID driver ... |
| 5613 | */ |
| 5614 | mdelay(1000*1); |
| 5615 | } |
| 5616 | return NOTIFY_DONE; |
| 5617 | } |
| 5618 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5619 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | .notifier_call = md_notify_reboot, |
| 5621 | .next = NULL, |
| 5622 | .priority = INT_MAX, /* before any real devices */ |
| 5623 | }; |
| 5624 | |
| 5625 | static void md_geninit(void) |
| 5626 | { |
| 5627 | struct proc_dir_entry *p; |
| 5628 | |
| 5629 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 5630 | |
| 5631 | p = create_proc_entry("mdstat", S_IRUGO, NULL); |
| 5632 | if (p) |
| 5633 | p->proc_fops = &md_seq_fops; |
| 5634 | } |
| 5635 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5636 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | if (register_blkdev(MAJOR_NR, "md")) |
| 5639 | return -1; |
| 5640 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
| 5641 | unregister_blkdev(MAJOR_NR, "md"); |
| 5642 | return -1; |
| 5643 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5644 | blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE, |
| 5645 | md_probe, NULL, NULL); |
| 5646 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | md_probe, NULL, NULL); |
| 5648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 5650 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | |
| 5652 | md_geninit(); |
| 5653 | return (0); |
| 5654 | } |
| 5655 | |
| 5656 | |
| 5657 | #ifndef MODULE |
| 5658 | |
| 5659 | /* |
| 5660 | * Searches all registered partitions for autorun RAID arrays |
| 5661 | * at boot time. |
| 5662 | */ |
| 5663 | static dev_t detected_devices[128]; |
| 5664 | static int dev_cnt; |
| 5665 | |
| 5666 | void md_autodetect_dev(dev_t dev) |
| 5667 | { |
| 5668 | if (dev_cnt >= 0 && dev_cnt < 127) |
| 5669 | detected_devices[dev_cnt++] = dev; |
| 5670 | } |
| 5671 | |
| 5672 | |
| 5673 | static void autostart_arrays(int part) |
| 5674 | { |
| 5675 | mdk_rdev_t *rdev; |
| 5676 | int i; |
| 5677 | |
| 5678 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 5679 | |
| 5680 | for (i = 0; i < dev_cnt; i++) { |
| 5681 | dev_t dev = detected_devices[i]; |
| 5682 | |
| 5683 | rdev = md_import_device(dev,0, 0); |
| 5684 | if (IS_ERR(rdev)) |
| 5685 | continue; |
| 5686 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5687 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | MD_BUG(); |
| 5689 | continue; |
| 5690 | } |
| 5691 | list_add(&rdev->same_set, &pending_raid_disks); |
| 5692 | } |
| 5693 | dev_cnt = 0; |
| 5694 | |
| 5695 | autorun_devices(part); |
| 5696 | } |
| 5697 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5698 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | |
| 5700 | static __exit void md_exit(void) |
| 5701 | { |
| 5702 | mddev_t *mddev; |
| 5703 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5704 | |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5705 | blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS); |
| 5706 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | |
| 5708 | unregister_blkdev(MAJOR_NR,"md"); |
| 5709 | unregister_blkdev(mdp_major, "mdp"); |
| 5710 | unregister_reboot_notifier(&md_notifier); |
| 5711 | unregister_sysctl_table(raid_table_header); |
| 5712 | remove_proc_entry("mdstat", NULL); |
| 5713 | ITERATE_MDDEV(mddev,tmp) { |
| 5714 | struct gendisk *disk = mddev->gendisk; |
| 5715 | if (!disk) |
| 5716 | continue; |
| 5717 | export_array(mddev); |
| 5718 | del_gendisk(disk); |
| 5719 | put_disk(disk); |
| 5720 | mddev->gendisk = NULL; |
| 5721 | mddev_put(mddev); |
| 5722 | } |
| 5723 | } |
| 5724 | |
| 5725 | module_init(md_init) |
| 5726 | module_exit(md_exit) |
| 5727 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5728 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 5729 | { |
| 5730 | return sprintf(buffer, "%d", start_readonly); |
| 5731 | } |
| 5732 | static int set_ro(const char *val, struct kernel_param *kp) |
| 5733 | { |
| 5734 | char *e; |
| 5735 | int num = simple_strtoul(val, &e, 10); |
| 5736 | if (*val && (*e == '\0' || *e == '\n')) { |
| 5737 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 5738 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5739 | } |
| 5740 | return -EINVAL; |
| 5741 | } |
| 5742 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 5743 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 5744 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5745 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5746 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | EXPORT_SYMBOL(register_md_personality); |
| 5748 | EXPORT_SYMBOL(unregister_md_personality); |
| 5749 | EXPORT_SYMBOL(md_error); |
| 5750 | EXPORT_SYMBOL(md_done_sync); |
| 5751 | EXPORT_SYMBOL(md_write_start); |
| 5752 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | EXPORT_SYMBOL(md_register_thread); |
| 5754 | EXPORT_SYMBOL(md_unregister_thread); |
| 5755 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | EXPORT_SYMBOL(md_check_recovery); |
| 5757 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 5758 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 5759 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |