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