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