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