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