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