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