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