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 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 42 | #include <linux/hdreg.h> |
| 43 | #include <linux/proc_fs.h> |
| 44 | #include <linux/random.h> |
| 45 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 46 | #include <linux/file.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 47 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 48 | #include <linux/raid/md_p.h> |
| 49 | #include <linux/raid/md_u.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 50 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 51 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | #define DEBUG 0 |
| 54 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 55 | |
| 56 | |
| 57 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 58 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #endif |
| 60 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 61 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static DEFINE_SPINLOCK(pers_lock); |
| 63 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 64 | static void md_print_devices(void); |
| 65 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 66 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 67 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 68 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* |
| 71 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 72 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 73 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 74 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 76 | * speed limit - in case reconstruction slows down your system despite |
| 77 | * idle IO detection. |
| 78 | * |
| 79 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 80 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | static int sysctl_speed_limit_min = 1000; |
| 84 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 85 | static inline int speed_min(mddev_t *mddev) |
| 86 | { |
| 87 | return mddev->sync_speed_min ? |
| 88 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 89 | } |
| 90 | |
| 91 | static inline int speed_max(mddev_t *mddev) |
| 92 | { |
| 93 | return mddev->sync_speed_max ? |
| 94 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | static struct ctl_table_header *raid_table_header; |
| 98 | |
| 99 | static ctl_table raid_table[] = { |
| 100 | { |
| 101 | .ctl_name = DEV_RAID_SPEED_LIMIT_MIN, |
| 102 | .procname = "speed_limit_min", |
| 103 | .data = &sysctl_speed_limit_min, |
| 104 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 105 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | .proc_handler = &proc_dointvec, |
| 107 | }, |
| 108 | { |
| 109 | .ctl_name = DEV_RAID_SPEED_LIMIT_MAX, |
| 110 | .procname = "speed_limit_max", |
| 111 | .data = &sysctl_speed_limit_max, |
| 112 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 113 | .mode = S_IRUGO|S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | .proc_handler = &proc_dointvec, |
| 115 | }, |
| 116 | { .ctl_name = 0 } |
| 117 | }; |
| 118 | |
| 119 | static ctl_table raid_dir_table[] = { |
| 120 | { |
| 121 | .ctl_name = DEV_RAID, |
| 122 | .procname = "raid", |
| 123 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 124 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | .child = raid_table, |
| 126 | }, |
| 127 | { .ctl_name = 0 } |
| 128 | }; |
| 129 | |
| 130 | static ctl_table raid_root_table[] = { |
| 131 | { |
| 132 | .ctl_name = CTL_DEV, |
| 133 | .procname = "dev", |
| 134 | .maxlen = 0, |
| 135 | .mode = 0555, |
| 136 | .child = raid_dir_table, |
| 137 | }, |
| 138 | { .ctl_name = 0 } |
| 139 | }; |
| 140 | |
| 141 | static struct block_device_operations md_fops; |
| 142 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 143 | static int start_readonly; |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 146 | * We have a system wide 'event count' that is incremented |
| 147 | * on any 'interesting' event, and readers of /proc/mdstat |
| 148 | * can use 'poll' or 'select' to find out when the event |
| 149 | * count increases. |
| 150 | * |
| 151 | * Events are: |
| 152 | * start array, stop array, error, add device, remove device, |
| 153 | * start build, activate spare |
| 154 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 155 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 156 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 157 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 158 | { |
| 159 | atomic_inc(&md_event_count); |
| 160 | wake_up(&md_event_waiters); |
| 161 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 162 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 163 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 164 | /* Alternate version that can be called from interrupts |
| 165 | * when calling sysfs_notify isn't needed. |
| 166 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 167 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 168 | { |
| 169 | atomic_inc(&md_event_count); |
| 170 | wake_up(&md_event_waiters); |
| 171 | } |
| 172 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 173 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | * Enables to iterate over all existing md arrays |
| 175 | * all_mddevs_lock protects this list. |
| 176 | */ |
| 177 | static LIST_HEAD(all_mddevs); |
| 178 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 179 | |
| 180 | |
| 181 | /* |
| 182 | * iterates through all used mddevs in the system. |
| 183 | * We take care to grab the all_mddevs_lock whenever navigating |
| 184 | * the list, and to always hold a refcount when unlocked. |
| 185 | * Any code which breaks out of this loop while own |
| 186 | * a reference to the current mddev and must mddev_put it. |
| 187 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 188 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | \ |
| 190 | for (({ spin_lock(&all_mddevs_lock); \ |
| 191 | tmp = all_mddevs.next; \ |
| 192 | mddev = NULL;}); \ |
| 193 | ({ if (tmp != &all_mddevs) \ |
| 194 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 195 | spin_unlock(&all_mddevs_lock); \ |
| 196 | if (mddev) mddev_put(mddev); \ |
| 197 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 198 | tmp != &all_mddevs;}); \ |
| 199 | ({ spin_lock(&all_mddevs_lock); \ |
| 200 | tmp = tmp->next;}) \ |
| 201 | ) |
| 202 | |
| 203 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 204 | /* Rather than calling directly into the personality make_request function, |
| 205 | * IO requests come here first so that we can check if the device is |
| 206 | * being suspended pending a reconfiguration. |
| 207 | * We hold a refcount over the call to ->make_request. By the time that |
| 208 | * call has finished, the bio has been linked into some internal structure |
| 209 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 210 | */ |
| 211 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 213 | mddev_t *mddev = q->queuedata; |
| 214 | int rv; |
| 215 | if (mddev == NULL || mddev->pers == NULL) { |
| 216 | bio_io_error(bio); |
| 217 | return 0; |
| 218 | } |
| 219 | rcu_read_lock(); |
| 220 | if (mddev->suspended) { |
| 221 | DEFINE_WAIT(__wait); |
| 222 | for (;;) { |
| 223 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 224 | TASK_UNINTERRUPTIBLE); |
| 225 | if (!mddev->suspended) |
| 226 | break; |
| 227 | rcu_read_unlock(); |
| 228 | schedule(); |
| 229 | rcu_read_lock(); |
| 230 | } |
| 231 | finish_wait(&mddev->sb_wait, &__wait); |
| 232 | } |
| 233 | atomic_inc(&mddev->active_io); |
| 234 | rcu_read_unlock(); |
| 235 | rv = mddev->pers->make_request(q, bio); |
| 236 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 237 | wake_up(&mddev->sb_wait); |
| 238 | |
| 239 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 242 | static void mddev_suspend(mddev_t *mddev) |
| 243 | { |
| 244 | BUG_ON(mddev->suspended); |
| 245 | mddev->suspended = 1; |
| 246 | synchronize_rcu(); |
| 247 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 248 | mddev->pers->quiesce(mddev, 1); |
| 249 | md_unregister_thread(mddev->thread); |
| 250 | mddev->thread = NULL; |
| 251 | /* we now know that no code is executing in the personality module, |
| 252 | * except possibly the tail end of a ->bi_end_io function, but that |
| 253 | * is certain to complete before the module has a chance to get |
| 254 | * unloaded |
| 255 | */ |
| 256 | } |
| 257 | |
| 258 | static void mddev_resume(mddev_t *mddev) |
| 259 | { |
| 260 | mddev->suspended = 0; |
| 261 | wake_up(&mddev->sb_wait); |
| 262 | mddev->pers->quiesce(mddev, 0); |
| 263 | } |
| 264 | |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 267 | { |
| 268 | atomic_inc(&mddev->active); |
| 269 | return mddev; |
| 270 | } |
| 271 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 272 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | static void mddev_put(mddev_t *mddev) |
| 275 | { |
| 276 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 277 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 278 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 279 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 281 | if (mddev->gendisk) { |
| 282 | /* we did a probe so need to clean up. |
| 283 | * Call schedule_work inside the spinlock |
| 284 | * so that flush_scheduled_work() after |
| 285 | * mddev_find will succeed in waiting for the |
| 286 | * work to be done. |
| 287 | */ |
| 288 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 289 | schedule_work(&mddev->del_work); |
| 290 | } else |
| 291 | kfree(mddev); |
| 292 | } |
| 293 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static mddev_t * mddev_find(dev_t unit) |
| 297 | { |
| 298 | mddev_t *mddev, *new = NULL; |
| 299 | |
| 300 | retry: |
| 301 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 303 | if (unit) { |
| 304 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 305 | if (mddev->unit == unit) { |
| 306 | mddev_get(mddev); |
| 307 | spin_unlock(&all_mddevs_lock); |
| 308 | kfree(new); |
| 309 | return mddev; |
| 310 | } |
| 311 | |
| 312 | if (new) { |
| 313 | list_add(&new->all_mddevs, &all_mddevs); |
| 314 | spin_unlock(&all_mddevs_lock); |
| 315 | new->hold_active = UNTIL_IOCTL; |
| 316 | return new; |
| 317 | } |
| 318 | } else if (new) { |
| 319 | /* find an unused unit number */ |
| 320 | static int next_minor = 512; |
| 321 | int start = next_minor; |
| 322 | int is_free = 0; |
| 323 | int dev = 0; |
| 324 | while (!is_free) { |
| 325 | dev = MKDEV(MD_MAJOR, next_minor); |
| 326 | next_minor++; |
| 327 | if (next_minor > MINORMASK) |
| 328 | next_minor = 0; |
| 329 | if (next_minor == start) { |
| 330 | /* Oh dear, all in use. */ |
| 331 | spin_unlock(&all_mddevs_lock); |
| 332 | kfree(new); |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | is_free = 1; |
| 337 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 338 | if (mddev->unit == dev) { |
| 339 | is_free = 0; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | new->unit = dev; |
| 344 | new->md_minor = MINOR(dev); |
| 345 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | list_add(&new->all_mddevs, &all_mddevs); |
| 347 | spin_unlock(&all_mddevs_lock); |
| 348 | return new; |
| 349 | } |
| 350 | spin_unlock(&all_mddevs_lock); |
| 351 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 352 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (!new) |
| 354 | return NULL; |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | new->unit = unit; |
| 357 | if (MAJOR(unit) == MD_MAJOR) |
| 358 | new->md_minor = MINOR(unit); |
| 359 | else |
| 360 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 361 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 362 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | INIT_LIST_HEAD(&new->disks); |
| 364 | INIT_LIST_HEAD(&new->all_mddevs); |
| 365 | init_timer(&new->safemode_timer); |
| 366 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 367 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 368 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 369 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 370 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 371 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 372 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 373 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 374 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 375 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | goto retry; |
| 378 | } |
| 379 | |
| 380 | static inline int mddev_lock(mddev_t * mddev) |
| 381 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 382 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 385 | static inline int mddev_is_locked(mddev_t *mddev) |
| 386 | { |
| 387 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 388 | } |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | static inline int mddev_trylock(mddev_t * mddev) |
| 391 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 392 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static inline void mddev_unlock(mddev_t * mddev) |
| 396 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 397 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 399 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 402 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 404 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 406 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | if (rdev->desc_nr == nr) |
| 408 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 414 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | mdk_rdev_t *rdev; |
| 416 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 417 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | if (rdev->bdev->bd_dev == dev) |
| 419 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return NULL; |
| 422 | } |
| 423 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 424 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 425 | { |
| 426 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 427 | list_for_each_entry(pers, &pers_list, list) { |
| 428 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 429 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 430 | if (strcmp(pers->name, clevel)==0) |
| 431 | return pers; |
| 432 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 433 | return NULL; |
| 434 | } |
| 435 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 436 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 437 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 439 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 440 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 444 | { |
| 445 | if (rdev->sb_page) |
| 446 | MD_BUG(); |
| 447 | |
| 448 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 449 | if (!rdev->sb_page) { |
| 450 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 451 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 458 | { |
| 459 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 460 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | rdev->sb_loaded = 0; |
| 462 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 463 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 464 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 469 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 470 | { |
| 471 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 472 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 473 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 474 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 475 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 476 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 477 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 478 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 479 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 480 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 481 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 482 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 483 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 484 | } |
| 485 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 486 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 487 | { |
| 488 | struct bio *bio2 = bio->bi_private; |
| 489 | mdk_rdev_t *rdev = bio2->bi_private; |
| 490 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 491 | |
| 492 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 493 | error == -EOPNOTSUPP) { |
| 494 | unsigned long flags; |
| 495 | /* barriers don't appear to be supported :-( */ |
| 496 | set_bit(BarriersNotsupp, &rdev->flags); |
| 497 | mddev->barriers_work = 0; |
| 498 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 499 | bio2->bi_next = mddev->biolist; |
| 500 | mddev->biolist = bio2; |
| 501 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 502 | wake_up(&mddev->sb_wait); |
| 503 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 504 | } else { |
| 505 | bio_put(bio2); |
| 506 | bio->bi_private = rdev; |
| 507 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 508 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 509 | } |
| 510 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 511 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 512 | sector_t sector, int size, struct page *page) |
| 513 | { |
| 514 | /* write first size bytes of page to sector of rdev |
| 515 | * Increment mddev->pending_writes before returning |
| 516 | * and decrement it on completion, waking up sb_wait |
| 517 | * if zero is reached. |
| 518 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 519 | * |
| 520 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 521 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 522 | */ |
| 523 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 524 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 525 | |
| 526 | bio->bi_bdev = rdev->bdev; |
| 527 | bio->bi_sector = sector; |
| 528 | bio_add_page(bio, page, size, 0); |
| 529 | bio->bi_private = rdev; |
| 530 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 531 | bio->bi_rw = rw; |
| 532 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 533 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 534 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 535 | struct bio *rbio; |
| 536 | rw |= (1<<BIO_RW_BARRIER); |
| 537 | rbio = bio_clone(bio, GFP_NOIO); |
| 538 | rbio->bi_private = bio; |
| 539 | rbio->bi_end_io = super_written_barrier; |
| 540 | submit_bio(rw, rbio); |
| 541 | } else |
| 542 | submit_bio(rw, bio); |
| 543 | } |
| 544 | |
| 545 | void md_super_wait(mddev_t *mddev) |
| 546 | { |
| 547 | /* wait for all superblock writes that were scheduled to complete. |
| 548 | * if any had to be retried (due to BARRIER problems), retry them |
| 549 | */ |
| 550 | DEFINE_WAIT(wq); |
| 551 | for(;;) { |
| 552 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 553 | if (atomic_read(&mddev->pending_writes)==0) |
| 554 | break; |
| 555 | while (mddev->biolist) { |
| 556 | struct bio *bio; |
| 557 | spin_lock_irq(&mddev->write_lock); |
| 558 | bio = mddev->biolist; |
| 559 | mddev->biolist = bio->bi_next ; |
| 560 | bio->bi_next = NULL; |
| 561 | spin_unlock_irq(&mddev->write_lock); |
| 562 | submit_bio(bio->bi_rw, bio); |
| 563 | } |
| 564 | schedule(); |
| 565 | } |
| 566 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 567 | } |
| 568 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 569 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 574 | 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] | 575 | struct page *page, int rw) |
| 576 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 577 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | struct completion event; |
| 579 | int ret; |
| 580 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 581 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | bio->bi_bdev = bdev; |
| 584 | bio->bi_sector = sector; |
| 585 | bio_add_page(bio, page, size, 0); |
| 586 | init_completion(&event); |
| 587 | bio->bi_private = &event; |
| 588 | bio->bi_end_io = bi_complete; |
| 589 | submit_bio(rw, bio); |
| 590 | wait_for_completion(&event); |
| 591 | |
| 592 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 593 | bio_put(bio); |
| 594 | return ret; |
| 595 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 596 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 598 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { |
| 600 | char b[BDEVNAME_SIZE]; |
| 601 | if (!rdev->sb_page) { |
| 602 | MD_BUG(); |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | if (rdev->sb_loaded) |
| 606 | return 0; |
| 607 | |
| 608 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 609 | if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | goto fail; |
| 611 | rdev->sb_loaded = 1; |
| 612 | return 0; |
| 613 | |
| 614 | fail: |
| 615 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 616 | bdevname(rdev->bdev,b)); |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | |
| 620 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 621 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 622 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 623 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 624 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 625 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 629 | { |
| 630 | int ret; |
| 631 | mdp_super_t *tmp1, *tmp2; |
| 632 | |
| 633 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 634 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 635 | |
| 636 | if (!tmp1 || !tmp2) { |
| 637 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 638 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | goto abort; |
| 640 | } |
| 641 | |
| 642 | *tmp1 = *sb1; |
| 643 | *tmp2 = *sb2; |
| 644 | |
| 645 | /* |
| 646 | * nr_disks is not constant |
| 647 | */ |
| 648 | tmp1->nr_disks = 0; |
| 649 | tmp2->nr_disks = 0; |
| 650 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 651 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 653 | kfree(tmp1); |
| 654 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 658 | |
| 659 | static u32 md_csum_fold(u32 csum) |
| 660 | { |
| 661 | csum = (csum & 0xffff) + (csum >> 16); |
| 662 | return (csum & 0xffff) + (csum >> 16); |
| 663 | } |
| 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 666 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 667 | u64 newcsum = 0; |
| 668 | u32 *sb32 = (u32*)sb; |
| 669 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | unsigned int disk_csum, csum; |
| 671 | |
| 672 | disk_csum = sb->sb_csum; |
| 673 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 674 | |
| 675 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 676 | newcsum += sb32[i]; |
| 677 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 678 | |
| 679 | |
| 680 | #ifdef CONFIG_ALPHA |
| 681 | /* This used to use csum_partial, which was wrong for several |
| 682 | * reasons including that different results are returned on |
| 683 | * different architectures. It isn't critical that we get exactly |
| 684 | * the same return value as before (we always csum_fold before |
| 685 | * testing, and that removes any differences). However as we |
| 686 | * know that csum_partial always returned a 16bit value on |
| 687 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 688 | */ |
| 689 | sb->sb_csum = md_csum_fold(disk_csum); |
| 690 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 692 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | return csum; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | /* |
| 698 | * Handle superblock details. |
| 699 | * We want to be able to handle multiple superblock formats |
| 700 | * so we have a common interface to them all, and an array of |
| 701 | * different handlers. |
| 702 | * We rely on user-space to write the initial superblock, and support |
| 703 | * reading and updating of superblocks. |
| 704 | * Interface methods are: |
| 705 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 706 | * loads and validates a superblock on dev. |
| 707 | * if refdev != NULL, compare superblocks on both devices |
| 708 | * Return: |
| 709 | * 0 - dev has a superblock that is compatible with refdev |
| 710 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 711 | * so dev should be used as the refdev in future |
| 712 | * -EINVAL superblock incompatible or invalid |
| 713 | * -othererror e.g. -EIO |
| 714 | * |
| 715 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 716 | * Verify that dev is acceptable into mddev. |
| 717 | * The first time, mddev->raid_disks will be 0, and data from |
| 718 | * dev should be merged in. Subsequent calls check that dev |
| 719 | * is new enough. Return 0 or -EINVAL |
| 720 | * |
| 721 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 722 | * Update the superblock for rdev with data in mddev |
| 723 | * This does not write to disc. |
| 724 | * |
| 725 | */ |
| 726 | |
| 727 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 728 | char *name; |
| 729 | struct module *owner; |
| 730 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 731 | int minor_version); |
| 732 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 733 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 734 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 735 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | }; |
| 737 | |
| 738 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 739 | * Check that the given mddev has no bitmap. |
| 740 | * |
| 741 | * This function is called from the run method of all personalities that do not |
| 742 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 743 | * has a bitmap. Otherwise, it returns 0. |
| 744 | * |
| 745 | */ |
| 746 | int md_check_no_bitmap(mddev_t *mddev) |
| 747 | { |
| 748 | if (!mddev->bitmap_file && !mddev->bitmap_offset) |
| 749 | return 0; |
| 750 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 751 | mdname(mddev), mddev->pers->name); |
| 752 | return 1; |
| 753 | } |
| 754 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 755 | |
| 756 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | * load_super for 0.90.0 |
| 758 | */ |
| 759 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 760 | { |
| 761 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 762 | mdp_super_t *sb; |
| 763 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 766 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | * it's at the end of the disk. |
| 768 | * |
| 769 | * It also happens to be a multiple of 4Kb. |
| 770 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 771 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 773 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | if (ret) return ret; |
| 775 | |
| 776 | ret = -EINVAL; |
| 777 | |
| 778 | bdevname(rdev->bdev, b); |
| 779 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 780 | |
| 781 | if (sb->md_magic != MD_SB_MAGIC) { |
| 782 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 783 | b); |
| 784 | goto abort; |
| 785 | } |
| 786 | |
| 787 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 788 | sb->minor_version < 90 || |
| 789 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 791 | sb->major_version, sb->minor_version, |
| 792 | b); |
| 793 | goto abort; |
| 794 | } |
| 795 | |
| 796 | if (sb->raid_disks <= 0) |
| 797 | goto abort; |
| 798 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 799 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 801 | b); |
| 802 | goto abort; |
| 803 | } |
| 804 | |
| 805 | rdev->preferred_minor = sb->md_minor; |
| 806 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 807 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | if (sb->level == LEVEL_MULTIPATH) |
| 810 | rdev->desc_nr = -1; |
| 811 | else |
| 812 | rdev->desc_nr = sb->this_disk.number; |
| 813 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 814 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 816 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | __u64 ev1, ev2; |
| 818 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 819 | if (!uuid_equal(refsb, sb)) { |
| 820 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 821 | b, bdevname(refdev->bdev,b2)); |
| 822 | goto abort; |
| 823 | } |
| 824 | if (!sb_equal(refsb, sb)) { |
| 825 | printk(KERN_WARNING "md: %s has same UUID" |
| 826 | " but different superblock to %s\n", |
| 827 | b, bdevname(refdev->bdev, b2)); |
| 828 | goto abort; |
| 829 | } |
| 830 | ev1 = md_event(sb); |
| 831 | ev2 = md_event(refsb); |
| 832 | if (ev1 > ev2) |
| 833 | ret = 1; |
| 834 | else |
| 835 | ret = 0; |
| 836 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 837 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 839 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 840 | /* "this cannot possibly happen" ... */ |
| 841 | ret = -EINVAL; |
| 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | abort: |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | /* |
| 848 | * validate_super for 0.90.0 |
| 849 | */ |
| 850 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 851 | { |
| 852 | mdp_disk_t *desc; |
| 853 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 854 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 856 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 857 | clear_bit(Faulty, &rdev->flags); |
| 858 | clear_bit(In_sync, &rdev->flags); |
| 859 | clear_bit(WriteMostly, &rdev->flags); |
| 860 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | if (mddev->raid_disks == 0) { |
| 863 | mddev->major_version = 0; |
| 864 | mddev->minor_version = sb->minor_version; |
| 865 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 866 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 867 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | mddev->ctime = sb->ctime; |
| 869 | mddev->utime = sb->utime; |
| 870 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 871 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | mddev->layout = sb->layout; |
| 873 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 874 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 875 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 876 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 877 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 879 | if (mddev->minor_version >= 91) { |
| 880 | mddev->reshape_position = sb->reshape_position; |
| 881 | mddev->delta_disks = sb->delta_disks; |
| 882 | mddev->new_level = sb->new_level; |
| 883 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 884 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 885 | } else { |
| 886 | mddev->reshape_position = MaxSector; |
| 887 | mddev->delta_disks = 0; |
| 888 | mddev->new_level = mddev->level; |
| 889 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 890 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 894 | mddev->recovery_cp = MaxSector; |
| 895 | else { |
| 896 | if (sb->events_hi == sb->cp_events_hi && |
| 897 | sb->events_lo == sb->cp_events_lo) { |
| 898 | mddev->recovery_cp = sb->recovery_cp; |
| 899 | } else |
| 900 | mddev->recovery_cp = 0; |
| 901 | } |
| 902 | |
| 903 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 904 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 905 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 906 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 907 | |
| 908 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 909 | |
| 910 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 911 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 912 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 913 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 914 | } else if (mddev->pers == NULL) { |
| 915 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | ++ev1; |
| 917 | if (ev1 < mddev->events) |
| 918 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 919 | } else if (mddev->bitmap) { |
| 920 | /* if adding to array with a bitmap, then we can accept an |
| 921 | * older device ... but not too old. |
| 922 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 923 | if (ev1 < mddev->bitmap->events_cleared) |
| 924 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 925 | } else { |
| 926 | if (ev1 < mddev->events) |
| 927 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 928 | return 0; |
| 929 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | desc = sb->disks + rdev->desc_nr; |
| 933 | |
| 934 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 935 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 936 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 937 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 938 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | rdev->raid_disk = desc->raid_disk; |
| 940 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 941 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 942 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 943 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 944 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * sync_super for 0.90.0 |
| 950 | */ |
| 951 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 952 | { |
| 953 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | mdk_rdev_t *rdev2; |
| 955 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
| 958 | /* make rdev->sb match mddev data.. |
| 959 | * |
| 960 | * 1/ zero out disks |
| 961 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 962 | * 3/ any empty disks < next_spare become removed |
| 963 | * |
| 964 | * disks[0] gets initialised to REMOVED because |
| 965 | * we cannot be sure from other fields if it has |
| 966 | * been initialised or not. |
| 967 | */ |
| 968 | int i; |
| 969 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 970 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 971 | rdev->sb_size = MD_SB_BYTES; |
| 972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 974 | |
| 975 | memset(sb, 0, sizeof(*sb)); |
| 976 | |
| 977 | sb->md_magic = MD_SB_MAGIC; |
| 978 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | sb->patch_version = mddev->patch_version; |
| 980 | sb->gvalid_words = 0; /* ignored */ |
| 981 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 982 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 983 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 984 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 985 | |
| 986 | sb->ctime = mddev->ctime; |
| 987 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 988 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | sb->raid_disks = mddev->raid_disks; |
| 990 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 991 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | sb->utime = mddev->utime; |
| 993 | sb->state = 0; |
| 994 | sb->events_hi = (mddev->events>>32); |
| 995 | sb->events_lo = (u32)mddev->events; |
| 996 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 997 | if (mddev->reshape_position == MaxSector) |
| 998 | sb->minor_version = 90; |
| 999 | else { |
| 1000 | sb->minor_version = 91; |
| 1001 | sb->reshape_position = mddev->reshape_position; |
| 1002 | sb->new_level = mddev->new_level; |
| 1003 | sb->delta_disks = mddev->delta_disks; |
| 1004 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1005 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1006 | } |
| 1007 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | if (mddev->in_sync) |
| 1009 | { |
| 1010 | sb->recovery_cp = mddev->recovery_cp; |
| 1011 | sb->cp_events_hi = (mddev->events>>32); |
| 1012 | sb->cp_events_lo = (u32)mddev->events; |
| 1013 | if (mddev->recovery_cp == MaxSector) |
| 1014 | sb->state = (1<< MD_SB_CLEAN); |
| 1015 | } else |
| 1016 | sb->recovery_cp = 0; |
| 1017 | |
| 1018 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1019 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1021 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 1022 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1025 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1027 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1028 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 1029 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1030 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1032 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1033 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | d = &sb->disks[rdev2->desc_nr]; |
| 1035 | nr_disks++; |
| 1036 | d->number = rdev2->desc_nr; |
| 1037 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1038 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1039 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 1040 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | d->raid_disk = rdev2->raid_disk; |
| 1042 | else |
| 1043 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1044 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1046 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | d->state = (1<<MD_DISK_ACTIVE); |
| 1048 | d->state |= (1<<MD_DISK_SYNC); |
| 1049 | active++; |
| 1050 | working++; |
| 1051 | } else { |
| 1052 | d->state = 0; |
| 1053 | spare++; |
| 1054 | working++; |
| 1055 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1056 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1057 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1060 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1061 | mdp_disk_t *d = &sb->disks[i]; |
| 1062 | if (d->state == 0 && d->number == 0) { |
| 1063 | d->number = i; |
| 1064 | d->raid_disk = i; |
| 1065 | d->state = (1<<MD_DISK_REMOVED); |
| 1066 | d->state |= (1<<MD_DISK_FAULTY); |
| 1067 | failed++; |
| 1068 | } |
| 1069 | } |
| 1070 | sb->nr_disks = nr_disks; |
| 1071 | sb->active_disks = active; |
| 1072 | sb->working_disks = working; |
| 1073 | sb->failed_disks = failed; |
| 1074 | sb->spare_disks = spare; |
| 1075 | |
| 1076 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1077 | sb->sb_csum = calc_sb_csum(sb); |
| 1078 | } |
| 1079 | |
| 1080 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1081 | * rdev_size_change for 0.90.0 |
| 1082 | */ |
| 1083 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1084 | super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1085 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1086 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1087 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1088 | if (rdev->mddev->bitmap_offset) |
| 1089 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1090 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1091 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1092 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1093 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1094 | rdev->sb_page); |
| 1095 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1096 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | * version 1 superblock |
| 1102 | */ |
| 1103 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1104 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1106 | __le32 disk_csum; |
| 1107 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | unsigned long long newcsum; |
| 1109 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1110 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | int i; |
| 1112 | |
| 1113 | disk_csum = sb->sb_csum; |
| 1114 | sb->sb_csum = 0; |
| 1115 | newcsum = 0; |
| 1116 | for (i=0; size>=4; size -= 4 ) |
| 1117 | newcsum += le32_to_cpu(*isuper++); |
| 1118 | |
| 1119 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1120 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
| 1122 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1123 | sb->sb_csum = disk_csum; |
| 1124 | return cpu_to_le32(csum); |
| 1125 | } |
| 1126 | |
| 1127 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1128 | { |
| 1129 | struct mdp_superblock_1 *sb; |
| 1130 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1131 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1133 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
| 1135 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1136 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | * It is always aligned to a 4K boundary and |
| 1138 | * depeding on minor_version, it can be: |
| 1139 | * 0: At least 8K, but less than 12K, from end of device |
| 1140 | * 1: At start of device |
| 1141 | * 2: 4K from start of device. |
| 1142 | */ |
| 1143 | switch(minor_version) { |
| 1144 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1145 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1146 | sb_start -= 8*2; |
| 1147 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | break; |
| 1149 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1150 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | break; |
| 1152 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1153 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | break; |
| 1155 | default: |
| 1156 | return -EINVAL; |
| 1157 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1158 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1160 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1161 | * and it is safe to read 4k, so we do that |
| 1162 | */ |
| 1163 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | if (ret) return ret; |
| 1165 | |
| 1166 | |
| 1167 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1168 | |
| 1169 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1170 | sb->major_version != cpu_to_le32(1) || |
| 1171 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1172 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1173 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | return -EINVAL; |
| 1175 | |
| 1176 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1177 | printk("md: invalid superblock checksum on %s\n", |
| 1178 | bdevname(rdev->bdev,b)); |
| 1179 | return -EINVAL; |
| 1180 | } |
| 1181 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1182 | printk("md: data_size too small on %s\n", |
| 1183 | bdevname(rdev->bdev,b)); |
| 1184 | return -EINVAL; |
| 1185 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | rdev->preferred_minor = 0xffff; |
| 1188 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1189 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1191 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1192 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1193 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1194 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1195 | |
| 1196 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1197 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1198 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1199 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1200 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1201 | rdev->desc_nr = -1; |
| 1202 | else |
| 1203 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1204 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1205 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1206 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1207 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | __u64 ev1, ev2; |
| 1209 | struct mdp_superblock_1 *refsb = |
| 1210 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1211 | |
| 1212 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1213 | sb->level != refsb->level || |
| 1214 | sb->layout != refsb->layout || |
| 1215 | sb->chunksize != refsb->chunksize) { |
| 1216 | printk(KERN_WARNING "md: %s has strangely different" |
| 1217 | " superblock to %s\n", |
| 1218 | bdevname(rdev->bdev,b), |
| 1219 | bdevname(refdev->bdev,b2)); |
| 1220 | return -EINVAL; |
| 1221 | } |
| 1222 | ev1 = le64_to_cpu(sb->events); |
| 1223 | ev2 = le64_to_cpu(refsb->events); |
| 1224 | |
| 1225 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1226 | ret = 1; |
| 1227 | else |
| 1228 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1230 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1231 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1232 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1234 | rdev->sectors = rdev->sb_start; |
| 1235 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1237 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1238 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1239 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1240 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1244 | { |
| 1245 | struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1246 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1248 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1249 | clear_bit(Faulty, &rdev->flags); |
| 1250 | clear_bit(In_sync, &rdev->flags); |
| 1251 | clear_bit(WriteMostly, &rdev->flags); |
| 1252 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | if (mddev->raid_disks == 0) { |
| 1255 | mddev->major_version = 1; |
| 1256 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1257 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1258 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1260 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1261 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1262 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | mddev->layout = le32_to_cpu(sb->layout); |
| 1264 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1265 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1266 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1267 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1268 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | |
| 1270 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1271 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1272 | |
| 1273 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1274 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1275 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1276 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1277 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1278 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1279 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1280 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1281 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1282 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1283 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1284 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1285 | } else { |
| 1286 | mddev->reshape_position = MaxSector; |
| 1287 | mddev->delta_disks = 0; |
| 1288 | mddev->new_level = mddev->level; |
| 1289 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1290 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1293 | } else if (mddev->pers == NULL) { |
| 1294 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | ++ev1; |
| 1296 | if (ev1 < mddev->events) |
| 1297 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1298 | } else if (mddev->bitmap) { |
| 1299 | /* If adding to array with a bitmap, then we can accept an |
| 1300 | * older device, but not too old. |
| 1301 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1302 | if (ev1 < mddev->bitmap->events_cleared) |
| 1303 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1304 | } else { |
| 1305 | if (ev1 < mddev->events) |
| 1306 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1307 | return 0; |
| 1308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1310 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1312 | switch(role) { |
| 1313 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | break; |
| 1315 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1316 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | break; |
| 1318 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1319 | if ((le32_to_cpu(sb->feature_map) & |
| 1320 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1321 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1322 | else |
| 1323 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | rdev->raid_disk = role; |
| 1325 | break; |
| 1326 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1327 | if (sb->devflags & WriteMostly1) |
| 1328 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1329 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1330 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1336 | { |
| 1337 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | mdk_rdev_t *rdev2; |
| 1339 | int max_dev, i; |
| 1340 | /* make rdev->sb match mddev and rdev data. */ |
| 1341 | |
| 1342 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1343 | |
| 1344 | sb->feature_map = 0; |
| 1345 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1346 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1348 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1349 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1350 | |
| 1351 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1352 | sb->events = cpu_to_le64(mddev->events); |
| 1353 | if (mddev->in_sync) |
| 1354 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1355 | else |
| 1356 | sb->resync_offset = cpu_to_le64(0); |
| 1357 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1358 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1359 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1360 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1361 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1362 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1363 | sb->level = cpu_to_le32(mddev->level); |
| 1364 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1365 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1366 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1367 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1368 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1369 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1370 | |
| 1371 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1372 | !test_bit(In_sync, &rdev->flags)) { |
| 1373 | if (mddev->curr_resync_completed > rdev->recovery_offset) |
| 1374 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 1375 | if (rdev->recovery_offset > 0) { |
| 1376 | sb->feature_map |= |
| 1377 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1378 | sb->recovery_offset = |
| 1379 | cpu_to_le64(rdev->recovery_offset); |
| 1380 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1383 | if (mddev->reshape_position != MaxSector) { |
| 1384 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1385 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1386 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1387 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1388 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1389 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1390 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1393 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | if (rdev2->desc_nr+1 > max_dev) |
| 1395 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1396 | |
| 1397 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1398 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | for (i=0; i<max_dev;i++) |
| 1400 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1401 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1402 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1404 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1406 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1408 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1409 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | else |
| 1411 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1412 | } |
| 1413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1415 | } |
| 1416 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1417 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1418 | super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1419 | { |
| 1420 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1421 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1422 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1423 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1424 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1425 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1426 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1427 | max_sectors -= rdev->data_offset; |
| 1428 | if (!num_sectors || num_sectors > max_sectors) |
| 1429 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1430 | } else if (rdev->mddev->bitmap_offset) { |
| 1431 | /* minor version 0 with bitmap we can't move */ |
| 1432 | return 0; |
| 1433 | } else { |
| 1434 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1435 | sector_t sb_start; |
| 1436 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1437 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1438 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1439 | if (!num_sectors || num_sectors > max_sectors) |
| 1440 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1441 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1442 | } |
| 1443 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1444 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1445 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1446 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1447 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1448 | rdev->sb_page); |
| 1449 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1450 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1453 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | [0] = { |
| 1455 | .name = "0.90.0", |
| 1456 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1457 | .load_super = super_90_load, |
| 1458 | .validate_super = super_90_validate, |
| 1459 | .sync_super = super_90_sync, |
| 1460 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | }, |
| 1462 | [1] = { |
| 1463 | .name = "md-1", |
| 1464 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1465 | .load_super = super_1_load, |
| 1466 | .validate_super = super_1_validate, |
| 1467 | .sync_super = super_1_sync, |
| 1468 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | }, |
| 1470 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
| 1472 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1473 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1474 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1476 | rcu_read_lock(); |
| 1477 | rdev_for_each_rcu(rdev, mddev1) |
| 1478 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1479 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1480 | rdev2->bdev->bd_contains) { |
| 1481 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1482 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1483 | } |
| 1484 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | static LIST_HEAD(pending_raid_disks); |
| 1489 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame^] | 1490 | /* |
| 1491 | * Try to register data integrity profile for an mddev |
| 1492 | * |
| 1493 | * This is called when an array is started and after a disk has been kicked |
| 1494 | * from the array. It only succeeds if all working and active component devices |
| 1495 | * are integrity capable with matching profiles. |
| 1496 | */ |
| 1497 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1498 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame^] | 1499 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1500 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame^] | 1501 | if (list_empty(&mddev->disks)) |
| 1502 | return 0; /* nothing to do */ |
| 1503 | if (blk_get_integrity(mddev->gendisk)) |
| 1504 | return 0; /* already registered */ |
| 1505 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1506 | /* skip spares and non-functional disks */ |
| 1507 | if (test_bit(Faulty, &rdev->flags)) |
| 1508 | continue; |
| 1509 | if (rdev->raid_disk < 0) |
| 1510 | continue; |
| 1511 | /* |
| 1512 | * If at least one rdev is not integrity capable, we can not |
| 1513 | * enable data integrity for the md device. |
| 1514 | */ |
| 1515 | if (!bdev_get_integrity(rdev->bdev)) |
| 1516 | return -EINVAL; |
| 1517 | if (!reference) { |
| 1518 | /* Use the first rdev as the reference */ |
| 1519 | reference = rdev; |
| 1520 | continue; |
| 1521 | } |
| 1522 | /* does this rdev's profile match the reference profile? */ |
| 1523 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1524 | rdev->bdev->bd_disk) < 0) |
| 1525 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1526 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame^] | 1527 | /* |
| 1528 | * All component devices are integrity capable and have matching |
| 1529 | * profiles, register the common profile for the md device. |
| 1530 | */ |
| 1531 | if (blk_integrity_register(mddev->gendisk, |
| 1532 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1533 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1534 | mdname(mddev)); |
| 1535 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1536 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame^] | 1537 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1538 | mdname(mddev)); |
| 1539 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1540 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame^] | 1541 | EXPORT_SYMBOL(md_integrity_register); |
| 1542 | |
| 1543 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1544 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1545 | { |
| 1546 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1547 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1548 | |
| 1549 | if (!bi_mddev) /* nothing to do */ |
| 1550 | return; |
| 1551 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1552 | return; |
| 1553 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1554 | rdev->bdev->bd_disk) >= 0) |
| 1555 | return; |
| 1556 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1557 | blk_integrity_unregister(mddev->gendisk); |
| 1558 | } |
| 1559 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1562 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1563 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1564 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1565 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1566 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
| 1568 | if (rdev->mddev) { |
| 1569 | MD_BUG(); |
| 1570 | return -EINVAL; |
| 1571 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1572 | |
| 1573 | /* prevent duplicates */ |
| 1574 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1575 | return -EEXIST; |
| 1576 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1577 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1578 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1579 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1580 | if (mddev->pers) { |
| 1581 | /* Cannot change size, so fail |
| 1582 | * If mddev->level <= 0, then we don't care |
| 1583 | * about aligning sizes (e.g. linear) |
| 1584 | */ |
| 1585 | if (mddev->level > 0) |
| 1586 | return -ENOSPC; |
| 1587 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1588 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
| 1591 | /* Verify rdev->desc_nr is unique. |
| 1592 | * If it is -1, assign a free number, else |
| 1593 | * check number is not in use |
| 1594 | */ |
| 1595 | if (rdev->desc_nr < 0) { |
| 1596 | int choice = 0; |
| 1597 | if (mddev->pers) choice = mddev->raid_disks; |
| 1598 | while (find_rdev_nr(mddev, choice)) |
| 1599 | choice++; |
| 1600 | rdev->desc_nr = choice; |
| 1601 | } else { |
| 1602 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1603 | return -EBUSY; |
| 1604 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1605 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1606 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1607 | mdname(mddev), mddev->max_disks); |
| 1608 | return -EBUSY; |
| 1609 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1610 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1611 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1612 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1615 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1616 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1617 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1618 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1619 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1620 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1621 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1622 | kobject_del(&rdev->kobj); |
| 1623 | goto fail; |
| 1624 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1625 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1626 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1627 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1628 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1629 | |
| 1630 | /* May as well allow recovery to be retried once */ |
| 1631 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1634 | |
| 1635 | fail: |
| 1636 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1637 | b, mdname(mddev)); |
| 1638 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1641 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1642 | { |
| 1643 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1644 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1645 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1649 | { |
| 1650 | char b[BDEVNAME_SIZE]; |
| 1651 | if (!rdev->mddev) { |
| 1652 | MD_BUG(); |
| 1653 | return; |
| 1654 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1655 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1656 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1658 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1659 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1660 | sysfs_put(rdev->sysfs_state); |
| 1661 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1662 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1663 | * writing to 'remove' to "dev/state". We also need |
| 1664 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1665 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1666 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1667 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1668 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1669 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * prevent the device from being mounted, repartitioned or |
| 1674 | * otherwise reused by a RAID array (or any other kernel |
| 1675 | * subsystem), by bd_claiming the device. |
| 1676 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1677 | static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | { |
| 1679 | int err = 0; |
| 1680 | struct block_device *bdev; |
| 1681 | char b[BDEVNAME_SIZE]; |
| 1682 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1683 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | if (IS_ERR(bdev)) { |
| 1685 | printk(KERN_ERR "md: could not open %s.\n", |
| 1686 | __bdevname(dev, b)); |
| 1687 | return PTR_ERR(bdev); |
| 1688 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1689 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | if (err) { |
| 1691 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1692 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1693 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | return err; |
| 1695 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1696 | if (!shared) |
| 1697 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | rdev->bdev = bdev; |
| 1699 | return err; |
| 1700 | } |
| 1701 | |
| 1702 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1703 | { |
| 1704 | struct block_device *bdev = rdev->bdev; |
| 1705 | rdev->bdev = NULL; |
| 1706 | if (!bdev) |
| 1707 | MD_BUG(); |
| 1708 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1709 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | void md_autodetect_dev(dev_t dev); |
| 1713 | |
| 1714 | static void export_rdev(mdk_rdev_t * rdev) |
| 1715 | { |
| 1716 | char b[BDEVNAME_SIZE]; |
| 1717 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1718 | bdevname(rdev->bdev,b)); |
| 1719 | if (rdev->mddev) |
| 1720 | MD_BUG(); |
| 1721 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1723 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1724 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | #endif |
| 1726 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1727 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1731 | { |
| 1732 | unbind_rdev_from_array(rdev); |
| 1733 | export_rdev(rdev); |
| 1734 | } |
| 1735 | |
| 1736 | static void export_array(mddev_t *mddev) |
| 1737 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1738 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1740 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | if (!rdev->mddev) { |
| 1742 | MD_BUG(); |
| 1743 | continue; |
| 1744 | } |
| 1745 | kick_rdev_from_array(rdev); |
| 1746 | } |
| 1747 | if (!list_empty(&mddev->disks)) |
| 1748 | MD_BUG(); |
| 1749 | mddev->raid_disks = 0; |
| 1750 | mddev->major_version = 0; |
| 1751 | } |
| 1752 | |
| 1753 | static void print_desc(mdp_disk_t *desc) |
| 1754 | { |
| 1755 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1756 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1757 | } |
| 1758 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1759 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | { |
| 1761 | int i; |
| 1762 | |
| 1763 | printk(KERN_INFO |
| 1764 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1765 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1766 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1767 | sb->ctime); |
| 1768 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1769 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1770 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1771 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1772 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1773 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1774 | sb->failed_disks, sb->spare_disks, |
| 1775 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1776 | |
| 1777 | printk(KERN_INFO); |
| 1778 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1779 | mdp_disk_t *desc; |
| 1780 | |
| 1781 | desc = sb->disks + i; |
| 1782 | if (desc->number || desc->major || desc->minor || |
| 1783 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1784 | printk(" D %2d: ", i); |
| 1785 | print_desc(desc); |
| 1786 | } |
| 1787 | } |
| 1788 | printk(KERN_INFO "md: THIS: "); |
| 1789 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1792 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1793 | { |
| 1794 | __u8 *uuid; |
| 1795 | |
| 1796 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1797 | printk(KERN_INFO |
| 1798 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1799 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1800 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1801 | le32_to_cpu(sb->major_version), |
| 1802 | le32_to_cpu(sb->feature_map), |
| 1803 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1804 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1805 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1806 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1807 | sb->set_name, |
| 1808 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1809 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1810 | |
| 1811 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1812 | printk(KERN_INFO |
| 1813 | "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1814 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1815 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1816 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1817 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1818 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1819 | le32_to_cpu(sb->level), |
| 1820 | (unsigned long long)le64_to_cpu(sb->size), |
| 1821 | le32_to_cpu(sb->raid_disks), |
| 1822 | le32_to_cpu(sb->layout), |
| 1823 | le32_to_cpu(sb->chunksize), |
| 1824 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1825 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1826 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1827 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1828 | le32_to_cpu(sb->dev_number), |
| 1829 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1830 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1831 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1832 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1833 | sb->devflags, |
| 1834 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1835 | (unsigned long long)le64_to_cpu(sb->events), |
| 1836 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1837 | le32_to_cpu(sb->sb_csum), |
| 1838 | le32_to_cpu(sb->max_dev) |
| 1839 | ); |
| 1840 | } |
| 1841 | |
| 1842 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | { |
| 1844 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1845 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1846 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1847 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1848 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1850 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1851 | switch (major_version) { |
| 1852 | case 0: |
| 1853 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1854 | break; |
| 1855 | case 1: |
| 1856 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1857 | break; |
| 1858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | } else |
| 1860 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1861 | } |
| 1862 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1863 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1865 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | mdk_rdev_t *rdev; |
| 1867 | mddev_t *mddev; |
| 1868 | char b[BDEVNAME_SIZE]; |
| 1869 | |
| 1870 | printk("\n"); |
| 1871 | printk("md: **********************************\n"); |
| 1872 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1873 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1874 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1876 | if (mddev->bitmap) |
| 1877 | bitmap_print_sb(mddev->bitmap); |
| 1878 | else |
| 1879 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1880 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1882 | printk("\n"); |
| 1883 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1884 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1885 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | } |
| 1887 | printk("md: **********************************\n"); |
| 1888 | printk("\n"); |
| 1889 | } |
| 1890 | |
| 1891 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1892 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1894 | /* Update each superblock (in-memory image), but |
| 1895 | * if we are allowed to, skip spares which already |
| 1896 | * have the right event counter, or have one earlier |
| 1897 | * (which would mean they aren't being marked as dirty |
| 1898 | * with the rest of the array) |
| 1899 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1902 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1903 | if (rdev->sb_events == mddev->events || |
| 1904 | (nospares && |
| 1905 | rdev->raid_disk < 0 && |
| 1906 | (rdev->sb_events&1)==0 && |
| 1907 | rdev->sb_events+1 == mddev->events)) { |
| 1908 | /* Don't update this superblock */ |
| 1909 | rdev->sb_loaded = 2; |
| 1910 | } else { |
| 1911 | super_types[mddev->major_version]. |
| 1912 | sync_super(mddev, rdev); |
| 1913 | rdev->sb_loaded = 1; |
| 1914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1918 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1921 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1922 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 1924 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1925 | if (mddev->external) |
| 1926 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1928 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1929 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1930 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1931 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1932 | force_change = 1; |
| 1933 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1934 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1935 | * though if events isn't the right even/odd, we will have to do |
| 1936 | * spares after all |
| 1937 | */ |
| 1938 | nospares = 1; |
| 1939 | if (force_change) |
| 1940 | nospares = 0; |
| 1941 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1942 | /* If the array is degraded, then skipping spares is both |
| 1943 | * dangerous and fairly pointless. |
| 1944 | * Dangerous because a device that was removed from the array |
| 1945 | * might have a event_count that still looks up-to-date, |
| 1946 | * so it can be re-added without a resync. |
| 1947 | * Pointless because if there are any spares to skip, |
| 1948 | * then a recovery will happen and soon that array won't |
| 1949 | * be degraded any more and the spare can go back to sleep then. |
| 1950 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1951 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1952 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1953 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1954 | |
| 1955 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1956 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1957 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1958 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1959 | && (mddev->events & 1) |
| 1960 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1961 | mddev->events--; |
| 1962 | else { |
| 1963 | /* otherwise we have to go forward and ... */ |
| 1964 | mddev->events ++; |
| 1965 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1966 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1967 | if ((mddev->events&1)==0) { |
| 1968 | mddev->events++; |
| 1969 | nospares = 0; |
| 1970 | } |
| 1971 | } else { |
| 1972 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1973 | if ((mddev->events&1)) { |
| 1974 | mddev->events++; |
| 1975 | nospares = 0; |
| 1976 | } |
| 1977 | } |
| 1978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
| 1980 | if (!mddev->events) { |
| 1981 | /* |
| 1982 | * oops, this 64-bit counter should never wrap. |
| 1983 | * Either we are in around ~1 trillion A.C., assuming |
| 1984 | * 1 reboot per second, or we have a bug: |
| 1985 | */ |
| 1986 | MD_BUG(); |
| 1987 | mddev->events --; |
| 1988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | |
| 1990 | /* |
| 1991 | * do not write anything to disk if using |
| 1992 | * nonpersistent superblocks |
| 1993 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1994 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1995 | if (!mddev->external) |
| 1996 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1997 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1998 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1999 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2001 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2002 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2003 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | |
| 2005 | dprintk(KERN_INFO |
| 2006 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2007 | mdname(mddev),mddev->in_sync); |
| 2008 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2009 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2010 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | char b[BDEVNAME_SIZE]; |
| 2012 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2013 | if (rdev->sb_loaded != 1) |
| 2014 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2015 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | dprintk("(skipping faulty "); |
| 2017 | |
| 2018 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2019 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2020 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2021 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2022 | rdev->sb_page); |
| 2023 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2024 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2025 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2026 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | } else |
| 2029 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2030 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | /* only need to write one superblock... */ |
| 2032 | break; |
| 2033 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2034 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2035 | /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */ |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2036 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2037 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2038 | if (mddev->in_sync != sync_req || |
| 2039 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2040 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2041 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2042 | goto repeat; |
| 2043 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2044 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2045 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2046 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2047 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2048 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2052 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2053 | * We want to accept with case. For this we use cmd_match. |
| 2054 | */ |
| 2055 | static int cmd_match(const char *cmd, const char *str) |
| 2056 | { |
| 2057 | /* See if cmd, written into a sysfs file, matches |
| 2058 | * str. They must either be the same, or cmd can |
| 2059 | * have a trailing newline |
| 2060 | */ |
| 2061 | while (*cmd && *str && *cmd == *str) { |
| 2062 | cmd++; |
| 2063 | str++; |
| 2064 | } |
| 2065 | if (*cmd == '\n') |
| 2066 | cmd++; |
| 2067 | if (*str || *cmd) |
| 2068 | return 0; |
| 2069 | return 1; |
| 2070 | } |
| 2071 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2072 | struct rdev_sysfs_entry { |
| 2073 | struct attribute attr; |
| 2074 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2075 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2076 | }; |
| 2077 | |
| 2078 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2079 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2080 | { |
| 2081 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2082 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2083 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2084 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2085 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2086 | sep = ","; |
| 2087 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2088 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2089 | len += sprintf(page+len, "%sin_sync",sep); |
| 2090 | sep = ","; |
| 2091 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2092 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2093 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2094 | sep = ","; |
| 2095 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2096 | if (test_bit(Blocked, &rdev->flags)) { |
| 2097 | len += sprintf(page+len, "%sblocked", sep); |
| 2098 | sep = ","; |
| 2099 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2100 | if (!test_bit(Faulty, &rdev->flags) && |
| 2101 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2102 | len += sprintf(page+len, "%sspare", sep); |
| 2103 | sep = ","; |
| 2104 | } |
| 2105 | return len+sprintf(page+len, "\n"); |
| 2106 | } |
| 2107 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2108 | static ssize_t |
| 2109 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2110 | { |
| 2111 | /* can write |
| 2112 | * faulty - simulates and error |
| 2113 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2114 | * writemostly - sets write_mostly |
| 2115 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2116 | * blocked - sets the Blocked flag |
| 2117 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2118 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2119 | */ |
| 2120 | int err = -EINVAL; |
| 2121 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2122 | md_error(rdev->mddev, rdev); |
| 2123 | err = 0; |
| 2124 | } else if (cmd_match(buf, "remove")) { |
| 2125 | if (rdev->raid_disk >= 0) |
| 2126 | err = -EBUSY; |
| 2127 | else { |
| 2128 | mddev_t *mddev = rdev->mddev; |
| 2129 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2130 | if (mddev->pers) |
| 2131 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2132 | md_new_event(mddev); |
| 2133 | err = 0; |
| 2134 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2135 | } else if (cmd_match(buf, "writemostly")) { |
| 2136 | set_bit(WriteMostly, &rdev->flags); |
| 2137 | err = 0; |
| 2138 | } else if (cmd_match(buf, "-writemostly")) { |
| 2139 | clear_bit(WriteMostly, &rdev->flags); |
| 2140 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2141 | } else if (cmd_match(buf, "blocked")) { |
| 2142 | set_bit(Blocked, &rdev->flags); |
| 2143 | err = 0; |
| 2144 | } else if (cmd_match(buf, "-blocked")) { |
| 2145 | clear_bit(Blocked, &rdev->flags); |
| 2146 | wake_up(&rdev->blocked_wait); |
| 2147 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2148 | md_wakeup_thread(rdev->mddev->thread); |
| 2149 | |
| 2150 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2151 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2152 | set_bit(In_sync, &rdev->flags); |
| 2153 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2154 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2155 | if (!err && rdev->sysfs_state) |
| 2156 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2157 | return err ? err : len; |
| 2158 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2159 | static struct rdev_sysfs_entry rdev_state = |
| 2160 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2161 | |
| 2162 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2163 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2164 | { |
| 2165 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2166 | } |
| 2167 | |
| 2168 | static ssize_t |
| 2169 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2170 | { |
| 2171 | char *e; |
| 2172 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2173 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2174 | atomic_set(&rdev->corrected_errors, n); |
| 2175 | return len; |
| 2176 | } |
| 2177 | return -EINVAL; |
| 2178 | } |
| 2179 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2180 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2181 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2182 | static ssize_t |
| 2183 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2184 | { |
| 2185 | if (rdev->raid_disk < 0) |
| 2186 | return sprintf(page, "none\n"); |
| 2187 | else |
| 2188 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2189 | } |
| 2190 | |
| 2191 | static ssize_t |
| 2192 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2193 | { |
| 2194 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2195 | int err; |
| 2196 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2197 | int slot = simple_strtoul(buf, &e, 10); |
| 2198 | if (strncmp(buf, "none", 4)==0) |
| 2199 | slot = -1; |
| 2200 | else if (e==buf || (*e && *e!= '\n')) |
| 2201 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2202 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2203 | /* Setting 'slot' on an active array requires also |
| 2204 | * updating the 'rd%d' link, and communicating |
| 2205 | * with the personality with ->hot_*_disk. |
| 2206 | * For now we only support removing |
| 2207 | * failed/spare devices. This normally happens automatically, |
| 2208 | * but not when the metadata is externally managed. |
| 2209 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2210 | if (rdev->raid_disk == -1) |
| 2211 | return -EEXIST; |
| 2212 | /* personality does all needed checks */ |
| 2213 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2214 | return -EINVAL; |
| 2215 | err = rdev->mddev->pers-> |
| 2216 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2217 | if (err) |
| 2218 | return err; |
| 2219 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2220 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2221 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2222 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2223 | } else if (rdev->mddev->pers) { |
| 2224 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2225 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2226 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2227 | */ |
| 2228 | |
| 2229 | if (rdev->raid_disk != -1) |
| 2230 | return -EBUSY; |
| 2231 | |
| 2232 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2233 | return -EINVAL; |
| 2234 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2235 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2236 | if (rdev2->raid_disk == slot) |
| 2237 | return -EEXIST; |
| 2238 | |
| 2239 | rdev->raid_disk = slot; |
| 2240 | if (test_bit(In_sync, &rdev->flags)) |
| 2241 | rdev->saved_raid_disk = slot; |
| 2242 | else |
| 2243 | rdev->saved_raid_disk = -1; |
| 2244 | err = rdev->mddev->pers-> |
| 2245 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2246 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2247 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2248 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2249 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2250 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2251 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2252 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2253 | printk(KERN_WARNING |
| 2254 | "md: cannot register " |
| 2255 | "%s for %s\n", |
| 2256 | nm, mdname(rdev->mddev)); |
| 2257 | |
| 2258 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2259 | } else { |
| 2260 | if (slot >= rdev->mddev->raid_disks) |
| 2261 | return -ENOSPC; |
| 2262 | rdev->raid_disk = slot; |
| 2263 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2264 | clear_bit(Faulty, &rdev->flags); |
| 2265 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2266 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2267 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2268 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2269 | return len; |
| 2270 | } |
| 2271 | |
| 2272 | |
| 2273 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2274 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2275 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2276 | static ssize_t |
| 2277 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2278 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2279 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | static ssize_t |
| 2283 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2284 | { |
| 2285 | char *e; |
| 2286 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2287 | if (e==buf || (*e && *e != '\n')) |
| 2288 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2289 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2290 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2291 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2292 | /* Must set offset before size, so overlap checks |
| 2293 | * can be sane */ |
| 2294 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2295 | rdev->data_offset = offset; |
| 2296 | return len; |
| 2297 | } |
| 2298 | |
| 2299 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2300 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2301 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2302 | static ssize_t |
| 2303 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2304 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2305 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2306 | } |
| 2307 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2308 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2309 | { |
| 2310 | /* check if two start/length pairs overlap */ |
| 2311 | if (s1+l1 <= s2) |
| 2312 | return 0; |
| 2313 | if (s2+l2 <= s1) |
| 2314 | return 0; |
| 2315 | return 1; |
| 2316 | } |
| 2317 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2318 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2319 | { |
| 2320 | unsigned long long blocks; |
| 2321 | sector_t new; |
| 2322 | |
| 2323 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2324 | return -EINVAL; |
| 2325 | |
| 2326 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2327 | return -EINVAL; /* sector conversion overflow */ |
| 2328 | |
| 2329 | new = blocks * 2; |
| 2330 | if (new != blocks * 2) |
| 2331 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2332 | |
| 2333 | *sectors = new; |
| 2334 | return 0; |
| 2335 | } |
| 2336 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2337 | static ssize_t |
| 2338 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2339 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2340 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2341 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2342 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2343 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2344 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2345 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2346 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2347 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2348 | sectors = super_types[my_mddev->major_version]. |
| 2349 | rdev_size_change(rdev, sectors); |
| 2350 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2351 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2352 | } else if (!sectors) |
| 2353 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2354 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2355 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2356 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2357 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2358 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2359 | rdev->sectors = sectors; |
| 2360 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2361 | /* need to check that all other rdevs with the same ->bdev |
| 2362 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2363 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2364 | * we have to change it back, we will have the lock again. |
| 2365 | */ |
| 2366 | mddev_t *mddev; |
| 2367 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2368 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2369 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2370 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2371 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2372 | mdk_rdev_t *rdev2; |
| 2373 | |
| 2374 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2375 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2376 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2377 | (rdev->bdev == rdev2->bdev && |
| 2378 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2379 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2380 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2381 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2382 | overlap = 1; |
| 2383 | break; |
| 2384 | } |
| 2385 | mddev_unlock(mddev); |
| 2386 | if (overlap) { |
| 2387 | mddev_put(mddev); |
| 2388 | break; |
| 2389 | } |
| 2390 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2391 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2392 | if (overlap) { |
| 2393 | /* Someone else could have slipped in a size |
| 2394 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2395 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2396 | * safe, and trust userspace not to race with |
| 2397 | * itself |
| 2398 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2399 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2400 | return -EBUSY; |
| 2401 | } |
| 2402 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2403 | return len; |
| 2404 | } |
| 2405 | |
| 2406 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2407 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2408 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2409 | static struct attribute *rdev_default_attrs[] = { |
| 2410 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2411 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2412 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2413 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2414 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2415 | NULL, |
| 2416 | }; |
| 2417 | static ssize_t |
| 2418 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2419 | { |
| 2420 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2421 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2422 | mddev_t *mddev = rdev->mddev; |
| 2423 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2424 | |
| 2425 | if (!entry->show) |
| 2426 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2427 | |
| 2428 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2429 | if (!rv) { |
| 2430 | if (rdev->mddev == NULL) |
| 2431 | rv = -EBUSY; |
| 2432 | else |
| 2433 | rv = entry->show(rdev, page); |
| 2434 | mddev_unlock(mddev); |
| 2435 | } |
| 2436 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | static ssize_t |
| 2440 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2441 | const char *page, size_t length) |
| 2442 | { |
| 2443 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2444 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2445 | ssize_t rv; |
| 2446 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2447 | |
| 2448 | if (!entry->store) |
| 2449 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2450 | if (!capable(CAP_SYS_ADMIN)) |
| 2451 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2452 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2453 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2454 | if (rdev->mddev == NULL) |
| 2455 | rv = -EBUSY; |
| 2456 | else |
| 2457 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2458 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2459 | } |
| 2460 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | static void rdev_free(struct kobject *ko) |
| 2464 | { |
| 2465 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2466 | kfree(rdev); |
| 2467 | } |
| 2468 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2469 | .show = rdev_attr_show, |
| 2470 | .store = rdev_attr_store, |
| 2471 | }; |
| 2472 | static struct kobj_type rdev_ktype = { |
| 2473 | .release = rdev_free, |
| 2474 | .sysfs_ops = &rdev_sysfs_ops, |
| 2475 | .default_attrs = rdev_default_attrs, |
| 2476 | }; |
| 2477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | /* |
| 2479 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2480 | * |
| 2481 | * mark the device faulty if: |
| 2482 | * |
| 2483 | * - the device is nonexistent (zero size) |
| 2484 | * - the device has no valid superblock |
| 2485 | * |
| 2486 | * a faulty rdev _never_ has rdev->sb set. |
| 2487 | */ |
| 2488 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2489 | { |
| 2490 | char b[BDEVNAME_SIZE]; |
| 2491 | int err; |
| 2492 | mdk_rdev_t *rdev; |
| 2493 | sector_t size; |
| 2494 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2495 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | if (!rdev) { |
| 2497 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2498 | return ERR_PTR(-ENOMEM); |
| 2499 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | |
| 2501 | if ((err = alloc_disk_sb(rdev))) |
| 2502 | goto abort_free; |
| 2503 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2504 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | if (err) |
| 2506 | goto abort_free; |
| 2507 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2508 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2511 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2512 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2513 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2515 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2517 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2518 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | |
| 2520 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2521 | if (!size) { |
| 2522 | printk(KERN_WARNING |
| 2523 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2524 | bdevname(rdev->bdev,b)); |
| 2525 | err = -EINVAL; |
| 2526 | goto abort_free; |
| 2527 | } |
| 2528 | |
| 2529 | if (super_format >= 0) { |
| 2530 | err = super_types[super_format]. |
| 2531 | load_super(rdev, NULL, super_minor); |
| 2532 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2533 | printk(KERN_WARNING |
| 2534 | "md: %s does not have a valid v%d.%d " |
| 2535 | "superblock, not importing!\n", |
| 2536 | bdevname(rdev->bdev,b), |
| 2537 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | goto abort_free; |
| 2539 | } |
| 2540 | if (err < 0) { |
| 2541 | printk(KERN_WARNING |
| 2542 | "md: could not read %s's sb, not importing!\n", |
| 2543 | bdevname(rdev->bdev,b)); |
| 2544 | goto abort_free; |
| 2545 | } |
| 2546 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2549 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | |
| 2551 | return rdev; |
| 2552 | |
| 2553 | abort_free: |
| 2554 | if (rdev->sb_page) { |
| 2555 | if (rdev->bdev) |
| 2556 | unlock_rdev(rdev); |
| 2557 | free_disk_sb(rdev); |
| 2558 | } |
| 2559 | kfree(rdev); |
| 2560 | return ERR_PTR(err); |
| 2561 | } |
| 2562 | |
| 2563 | /* |
| 2564 | * Check a full RAID array for plausibility |
| 2565 | */ |
| 2566 | |
| 2567 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2568 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | { |
| 2570 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2571 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | char b[BDEVNAME_SIZE]; |
| 2573 | |
| 2574 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2575 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | switch (super_types[mddev->major_version]. |
| 2577 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2578 | case 1: |
| 2579 | freshest = rdev; |
| 2580 | break; |
| 2581 | case 0: |
| 2582 | break; |
| 2583 | default: |
| 2584 | printk( KERN_ERR \ |
| 2585 | "md: fatal superblock inconsistency in %s" |
| 2586 | " -- removing from array\n", |
| 2587 | bdevname(rdev->bdev,b)); |
| 2588 | kick_rdev_from_array(rdev); |
| 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | super_types[mddev->major_version]. |
| 2593 | validate_super(mddev, freshest); |
| 2594 | |
| 2595 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2596 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2597 | if (rdev->desc_nr >= mddev->max_disks || |
| 2598 | i > mddev->max_disks) { |
| 2599 | printk(KERN_WARNING |
| 2600 | "md: %s: %s: only %d devices permitted\n", |
| 2601 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2602 | mddev->max_disks); |
| 2603 | kick_rdev_from_array(rdev); |
| 2604 | continue; |
| 2605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | if (rdev != freshest) |
| 2607 | if (super_types[mddev->major_version]. |
| 2608 | validate_super(mddev, rdev)) { |
| 2609 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2610 | " from array!\n", |
| 2611 | bdevname(rdev->bdev,b)); |
| 2612 | kick_rdev_from_array(rdev); |
| 2613 | continue; |
| 2614 | } |
| 2615 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2616 | rdev->desc_nr = i++; |
| 2617 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2618 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2619 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2620 | rdev->raid_disk = -1; |
| 2621 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | } |
| 2623 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2626 | static void md_safemode_timeout(unsigned long data); |
| 2627 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2628 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2629 | safe_delay_show(mddev_t *mddev, char *page) |
| 2630 | { |
| 2631 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2632 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2633 | } |
| 2634 | static ssize_t |
| 2635 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2636 | { |
| 2637 | int scale=1; |
| 2638 | int dot=0; |
| 2639 | int i; |
| 2640 | unsigned long msec; |
| 2641 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2642 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2643 | /* remove a period, and count digits after it */ |
| 2644 | if (len >= sizeof(buf)) |
| 2645 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2646 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2647 | for (i=0; i<len; i++) { |
| 2648 | if (dot) { |
| 2649 | if (isdigit(buf[i])) { |
| 2650 | buf[i-1] = buf[i]; |
| 2651 | scale *= 10; |
| 2652 | } |
| 2653 | buf[i] = 0; |
| 2654 | } else if (buf[i] == '.') { |
| 2655 | dot=1; |
| 2656 | buf[i] = 0; |
| 2657 | } |
| 2658 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2659 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2660 | return -EINVAL; |
| 2661 | msec = (msec * 1000) / scale; |
| 2662 | if (msec == 0) |
| 2663 | mddev->safemode_delay = 0; |
| 2664 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2665 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2666 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2667 | if (mddev->safemode_delay == 0) |
| 2668 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2669 | if (mddev->safemode_delay < old_delay) |
| 2670 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2671 | } |
| 2672 | return len; |
| 2673 | } |
| 2674 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2675 | __ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2676 | |
| 2677 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2678 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2679 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2680 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2681 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2682 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2683 | else if (mddev->clevel[0]) |
| 2684 | return sprintf(page, "%s\n", mddev->clevel); |
| 2685 | else if (mddev->level != LEVEL_NONE) |
| 2686 | return sprintf(page, "%d\n", mddev->level); |
| 2687 | else |
| 2688 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2689 | } |
| 2690 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2691 | static ssize_t |
| 2692 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2693 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2694 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2695 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2696 | struct mdk_personality *pers; |
| 2697 | void *priv; |
| 2698 | |
| 2699 | if (mddev->pers == NULL) { |
| 2700 | if (len == 0) |
| 2701 | return 0; |
| 2702 | if (len >= sizeof(mddev->clevel)) |
| 2703 | return -ENOSPC; |
| 2704 | strncpy(mddev->clevel, buf, len); |
| 2705 | if (mddev->clevel[len-1] == '\n') |
| 2706 | len--; |
| 2707 | mddev->clevel[len] = 0; |
| 2708 | mddev->level = LEVEL_NONE; |
| 2709 | return rv; |
| 2710 | } |
| 2711 | |
| 2712 | /* request to change the personality. Need to ensure: |
| 2713 | * - array is not engaged in resync/recovery/reshape |
| 2714 | * - old personality can be suspended |
| 2715 | * - new personality will access other array. |
| 2716 | */ |
| 2717 | |
| 2718 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2719 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2720 | |
| 2721 | if (!mddev->pers->quiesce) { |
| 2722 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2723 | mdname(mddev), mddev->pers->name); |
| 2724 | return -EINVAL; |
| 2725 | } |
| 2726 | |
| 2727 | /* Now find the new personality */ |
| 2728 | if (len == 0 || len >= sizeof(level)) |
| 2729 | return -EINVAL; |
| 2730 | strncpy(level, buf, len); |
| 2731 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2732 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2733 | level[len] = 0; |
| 2734 | |
| 2735 | request_module("md-%s", level); |
| 2736 | spin_lock(&pers_lock); |
| 2737 | pers = find_pers(LEVEL_NONE, level); |
| 2738 | if (!pers || !try_module_get(pers->owner)) { |
| 2739 | spin_unlock(&pers_lock); |
| 2740 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2741 | return -EINVAL; |
| 2742 | } |
| 2743 | spin_unlock(&pers_lock); |
| 2744 | |
| 2745 | if (pers == mddev->pers) { |
| 2746 | /* Nothing to do! */ |
| 2747 | module_put(pers->owner); |
| 2748 | return rv; |
| 2749 | } |
| 2750 | if (!pers->takeover) { |
| 2751 | module_put(pers->owner); |
| 2752 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2753 | mdname(mddev), level); |
| 2754 | return -EINVAL; |
| 2755 | } |
| 2756 | |
| 2757 | /* ->takeover must set new_* and/or delta_disks |
| 2758 | * if it succeeds, and may set them when it fails. |
| 2759 | */ |
| 2760 | priv = pers->takeover(mddev); |
| 2761 | if (IS_ERR(priv)) { |
| 2762 | mddev->new_level = mddev->level; |
| 2763 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2764 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2765 | mddev->raid_disks -= mddev->delta_disks; |
| 2766 | mddev->delta_disks = 0; |
| 2767 | module_put(pers->owner); |
| 2768 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2769 | mdname(mddev), level); |
| 2770 | return PTR_ERR(priv); |
| 2771 | } |
| 2772 | |
| 2773 | /* Looks like we have a winner */ |
| 2774 | mddev_suspend(mddev); |
| 2775 | mddev->pers->stop(mddev); |
| 2776 | module_put(mddev->pers->owner); |
| 2777 | mddev->pers = pers; |
| 2778 | mddev->private = priv; |
| 2779 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2780 | mddev->level = mddev->new_level; |
| 2781 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2782 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2783 | mddev->delta_disks = 0; |
| 2784 | pers->run(mddev); |
| 2785 | mddev_resume(mddev); |
| 2786 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2787 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2788 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2789 | return rv; |
| 2790 | } |
| 2791 | |
| 2792 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2793 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2794 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2795 | |
| 2796 | static ssize_t |
| 2797 | layout_show(mddev_t *mddev, char *page) |
| 2798 | { |
| 2799 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2800 | if (mddev->reshape_position != MaxSector && |
| 2801 | mddev->layout != mddev->new_layout) |
| 2802 | return sprintf(page, "%d (%d)\n", |
| 2803 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2804 | return sprintf(page, "%d\n", mddev->layout); |
| 2805 | } |
| 2806 | |
| 2807 | static ssize_t |
| 2808 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2809 | { |
| 2810 | char *e; |
| 2811 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2812 | |
| 2813 | if (!*buf || (*e && *e != '\n')) |
| 2814 | return -EINVAL; |
| 2815 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2816 | if (mddev->pers) { |
| 2817 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2818 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2819 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2820 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2821 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2822 | if (err) { |
| 2823 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2824 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2825 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2826 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2827 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2828 | if (mddev->reshape_position == MaxSector) |
| 2829 | mddev->layout = n; |
| 2830 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2831 | return len; |
| 2832 | } |
| 2833 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2834 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2835 | |
| 2836 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2837 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2838 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2839 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2840 | if (mddev->raid_disks == 0) |
| 2841 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2842 | if (mddev->reshape_position != MaxSector && |
| 2843 | mddev->delta_disks != 0) |
| 2844 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2845 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2846 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2847 | } |
| 2848 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2849 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2850 | |
| 2851 | static ssize_t |
| 2852 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2853 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2854 | char *e; |
| 2855 | int rv = 0; |
| 2856 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2857 | |
| 2858 | if (!*buf || (*e && *e != '\n')) |
| 2859 | return -EINVAL; |
| 2860 | |
| 2861 | if (mddev->pers) |
| 2862 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2863 | else if (mddev->reshape_position != MaxSector) { |
| 2864 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2865 | mddev->delta_disks = n - olddisks; |
| 2866 | mddev->raid_disks = n; |
| 2867 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2868 | mddev->raid_disks = n; |
| 2869 | return rv ? rv : len; |
| 2870 | } |
| 2871 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2872 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2873 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2874 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2875 | chunk_size_show(mddev_t *mddev, char *page) |
| 2876 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2877 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2878 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 2879 | return sprintf(page, "%d (%d)\n", |
| 2880 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2881 | mddev->chunk_sectors << 9); |
| 2882 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | static ssize_t |
| 2886 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2887 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2888 | char *e; |
| 2889 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2890 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2891 | if (!*buf || (*e && *e != '\n')) |
| 2892 | return -EINVAL; |
| 2893 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2894 | if (mddev->pers) { |
| 2895 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2896 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2897 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2898 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2899 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2900 | if (err) { |
| 2901 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2902 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2903 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2904 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2905 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2906 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2907 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2908 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2909 | return len; |
| 2910 | } |
| 2911 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2912 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2913 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2914 | static ssize_t |
| 2915 | resync_start_show(mddev_t *mddev, char *page) |
| 2916 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 2917 | if (mddev->recovery_cp == MaxSector) |
| 2918 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2919 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2920 | } |
| 2921 | |
| 2922 | static ssize_t |
| 2923 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2924 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2925 | char *e; |
| 2926 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2927 | |
| 2928 | if (mddev->pers) |
| 2929 | return -EBUSY; |
| 2930 | if (!*buf || (*e && *e != '\n')) |
| 2931 | return -EINVAL; |
| 2932 | |
| 2933 | mddev->recovery_cp = n; |
| 2934 | return len; |
| 2935 | } |
| 2936 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2937 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2938 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2939 | /* |
| 2940 | * The array state can be: |
| 2941 | * |
| 2942 | * clear |
| 2943 | * No devices, no size, no level |
| 2944 | * Equivalent to STOP_ARRAY ioctl |
| 2945 | * inactive |
| 2946 | * May have some settings, but array is not active |
| 2947 | * all IO results in error |
| 2948 | * When written, doesn't tear down array, but just stops it |
| 2949 | * suspended (not supported yet) |
| 2950 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2951 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2952 | * readonly |
| 2953 | * no resync can happen. no superblocks get written. |
| 2954 | * write requests fail |
| 2955 | * read-auto |
| 2956 | * like readonly, but behaves like 'clean' on a write request. |
| 2957 | * |
| 2958 | * clean - no pending writes, but otherwise active. |
| 2959 | * When written to inactive array, starts without resync |
| 2960 | * If a write request arrives then |
| 2961 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2962 | * if not known, block and switch to write-pending |
| 2963 | * If written to an active array that has pending writes, then fails. |
| 2964 | * active |
| 2965 | * fully active: IO and resync can be happening. |
| 2966 | * When written to inactive array, starts with resync |
| 2967 | * |
| 2968 | * write-pending |
| 2969 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2970 | * |
| 2971 | * active-idle |
| 2972 | * like active, but no writes have been seen for a while (100msec). |
| 2973 | * |
| 2974 | */ |
| 2975 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2976 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2977 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2978 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2979 | "write-pending", "active-idle", NULL }; |
| 2980 | |
| 2981 | static int match_word(const char *word, char **list) |
| 2982 | { |
| 2983 | int n; |
| 2984 | for (n=0; list[n]; n++) |
| 2985 | if (cmd_match(word, list[n])) |
| 2986 | break; |
| 2987 | return n; |
| 2988 | } |
| 2989 | |
| 2990 | static ssize_t |
| 2991 | array_state_show(mddev_t *mddev, char *page) |
| 2992 | { |
| 2993 | enum array_state st = inactive; |
| 2994 | |
| 2995 | if (mddev->pers) |
| 2996 | switch(mddev->ro) { |
| 2997 | case 1: |
| 2998 | st = readonly; |
| 2999 | break; |
| 3000 | case 2: |
| 3001 | st = read_auto; |
| 3002 | break; |
| 3003 | case 0: |
| 3004 | if (mddev->in_sync) |
| 3005 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3006 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3007 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3008 | else if (mddev->safemode) |
| 3009 | st = active_idle; |
| 3010 | else |
| 3011 | st = active; |
| 3012 | } |
| 3013 | else { |
| 3014 | if (list_empty(&mddev->disks) && |
| 3015 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3016 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3017 | st = clear; |
| 3018 | else |
| 3019 | st = inactive; |
| 3020 | } |
| 3021 | return sprintf(page, "%s\n", array_states[st]); |
| 3022 | } |
| 3023 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3024 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3025 | static int do_md_run(mddev_t * mddev); |
| 3026 | static int restart_array(mddev_t *mddev); |
| 3027 | |
| 3028 | static ssize_t |
| 3029 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3030 | { |
| 3031 | int err = -EINVAL; |
| 3032 | enum array_state st = match_word(buf, array_states); |
| 3033 | switch(st) { |
| 3034 | case bad_word: |
| 3035 | break; |
| 3036 | case clear: |
| 3037 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3038 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3039 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3040 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3041 | break; |
| 3042 | case inactive: |
| 3043 | /* stopping an active array */ |
| 3044 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3045 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3046 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3047 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3048 | } else |
| 3049 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3050 | break; |
| 3051 | case suspended: |
| 3052 | break; /* not supported yet */ |
| 3053 | case readonly: |
| 3054 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3055 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3056 | else { |
| 3057 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3058 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3059 | err = do_md_run(mddev); |
| 3060 | } |
| 3061 | break; |
| 3062 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3063 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3064 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3065 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3066 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3067 | err = restart_array(mddev); |
| 3068 | if (err == 0) { |
| 3069 | mddev->ro = 2; |
| 3070 | set_disk_ro(mddev->gendisk, 0); |
| 3071 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3072 | } else { |
| 3073 | mddev->ro = 2; |
| 3074 | err = do_md_run(mddev); |
| 3075 | } |
| 3076 | break; |
| 3077 | case clean: |
| 3078 | if (mddev->pers) { |
| 3079 | restart_array(mddev); |
| 3080 | spin_lock_irq(&mddev->write_lock); |
| 3081 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3082 | if (mddev->in_sync == 0) { |
| 3083 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3084 | if (mddev->safemode == 1) |
| 3085 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3086 | if (mddev->persistent) |
| 3087 | set_bit(MD_CHANGE_CLEAN, |
| 3088 | &mddev->flags); |
| 3089 | } |
| 3090 | err = 0; |
| 3091 | } else |
| 3092 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3093 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3094 | } else |
| 3095 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3096 | break; |
| 3097 | case active: |
| 3098 | if (mddev->pers) { |
| 3099 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3100 | if (mddev->external) |
| 3101 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3102 | wake_up(&mddev->sb_wait); |
| 3103 | err = 0; |
| 3104 | } else { |
| 3105 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3106 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3107 | err = do_md_run(mddev); |
| 3108 | } |
| 3109 | break; |
| 3110 | case write_pending: |
| 3111 | case active_idle: |
| 3112 | /* these cannot be set */ |
| 3113 | break; |
| 3114 | } |
| 3115 | if (err) |
| 3116 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3117 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3118 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3119 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3120 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3121 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3122 | static struct md_sysfs_entry md_array_state = |
| 3123 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3124 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3125 | static ssize_t |
| 3126 | null_show(mddev_t *mddev, char *page) |
| 3127 | { |
| 3128 | return -EINVAL; |
| 3129 | } |
| 3130 | |
| 3131 | static ssize_t |
| 3132 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3133 | { |
| 3134 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3135 | /* The new device is added to the array. |
| 3136 | * If the array has a persistent superblock, we read the |
| 3137 | * superblock to initialise info and check validity. |
| 3138 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3139 | * which mainly checks size. |
| 3140 | */ |
| 3141 | char *e; |
| 3142 | int major = simple_strtoul(buf, &e, 10); |
| 3143 | int minor; |
| 3144 | dev_t dev; |
| 3145 | mdk_rdev_t *rdev; |
| 3146 | int err; |
| 3147 | |
| 3148 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3149 | return -EINVAL; |
| 3150 | minor = simple_strtoul(e+1, &e, 10); |
| 3151 | if (*e && *e != '\n') |
| 3152 | return -EINVAL; |
| 3153 | dev = MKDEV(major, minor); |
| 3154 | if (major != MAJOR(dev) || |
| 3155 | minor != MINOR(dev)) |
| 3156 | return -EOVERFLOW; |
| 3157 | |
| 3158 | |
| 3159 | if (mddev->persistent) { |
| 3160 | rdev = md_import_device(dev, mddev->major_version, |
| 3161 | mddev->minor_version); |
| 3162 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3163 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3164 | mdk_rdev_t, same_set); |
| 3165 | err = super_types[mddev->major_version] |
| 3166 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3167 | if (err < 0) |
| 3168 | goto out; |
| 3169 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3170 | } else if (mddev->external) |
| 3171 | rdev = md_import_device(dev, -2, -1); |
| 3172 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3173 | rdev = md_import_device(dev, -1, -1); |
| 3174 | |
| 3175 | if (IS_ERR(rdev)) |
| 3176 | return PTR_ERR(rdev); |
| 3177 | err = bind_rdev_to_array(rdev, mddev); |
| 3178 | out: |
| 3179 | if (err) |
| 3180 | export_rdev(rdev); |
| 3181 | return err ? err : len; |
| 3182 | } |
| 3183 | |
| 3184 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3185 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3186 | |
| 3187 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3188 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3189 | { |
| 3190 | char *end; |
| 3191 | unsigned long chunk, end_chunk; |
| 3192 | |
| 3193 | if (!mddev->bitmap) |
| 3194 | goto out; |
| 3195 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3196 | while (*buf) { |
| 3197 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3198 | if (buf == end) break; |
| 3199 | if (*end == '-') { /* range */ |
| 3200 | buf = end + 1; |
| 3201 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3202 | if (buf == end) break; |
| 3203 | } |
| 3204 | if (*end && !isspace(*end)) break; |
| 3205 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3206 | buf = end; |
| 3207 | while (isspace(*buf)) buf++; |
| 3208 | } |
| 3209 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3210 | out: |
| 3211 | return len; |
| 3212 | } |
| 3213 | |
| 3214 | static struct md_sysfs_entry md_bitmap = |
| 3215 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3216 | |
| 3217 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3218 | size_show(mddev_t *mddev, char *page) |
| 3219 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3220 | return sprintf(page, "%llu\n", |
| 3221 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3222 | } |
| 3223 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3224 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3225 | |
| 3226 | static ssize_t |
| 3227 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3228 | { |
| 3229 | /* If array is inactive, we can reduce the component size, but |
| 3230 | * not increase it (except from 0). |
| 3231 | * If array is active, we can try an on-line resize |
| 3232 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3233 | sector_t sectors; |
| 3234 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3235 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3236 | if (err < 0) |
| 3237 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3238 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3239 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3240 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3241 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3242 | if (mddev->dev_sectors == 0 || |
| 3243 | mddev->dev_sectors > sectors) |
| 3244 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3245 | else |
| 3246 | err = -ENOSPC; |
| 3247 | } |
| 3248 | return err ? err : len; |
| 3249 | } |
| 3250 | |
| 3251 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3252 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3253 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3254 | |
| 3255 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3256 | * This is one of |
| 3257 | * 'none' for arrays with no metadata (good luck...) |
| 3258 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3259 | * or N.M for internally known formats |
| 3260 | */ |
| 3261 | static ssize_t |
| 3262 | metadata_show(mddev_t *mddev, char *page) |
| 3263 | { |
| 3264 | if (mddev->persistent) |
| 3265 | return sprintf(page, "%d.%d\n", |
| 3266 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3267 | else if (mddev->external) |
| 3268 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3269 | else |
| 3270 | return sprintf(page, "none\n"); |
| 3271 | } |
| 3272 | |
| 3273 | static ssize_t |
| 3274 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3275 | { |
| 3276 | int major, minor; |
| 3277 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3278 | /* Changing the details of 'external' metadata is |
| 3279 | * always permitted. Otherwise there must be |
| 3280 | * no devices attached to the array. |
| 3281 | */ |
| 3282 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3283 | ; |
| 3284 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3285 | return -EBUSY; |
| 3286 | |
| 3287 | if (cmd_match(buf, "none")) { |
| 3288 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3289 | mddev->external = 0; |
| 3290 | mddev->major_version = 0; |
| 3291 | mddev->minor_version = 90; |
| 3292 | return len; |
| 3293 | } |
| 3294 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3295 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3296 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3297 | namelen = sizeof(mddev->metadata_type)-1; |
| 3298 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3299 | mddev->metadata_type[namelen] = 0; |
| 3300 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3301 | mddev->metadata_type[--namelen] = 0; |
| 3302 | mddev->persistent = 0; |
| 3303 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3304 | mddev->major_version = 0; |
| 3305 | mddev->minor_version = 90; |
| 3306 | return len; |
| 3307 | } |
| 3308 | major = simple_strtoul(buf, &e, 10); |
| 3309 | if (e==buf || *e != '.') |
| 3310 | return -EINVAL; |
| 3311 | buf = e+1; |
| 3312 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3313 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3314 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3315 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3316 | return -ENOENT; |
| 3317 | mddev->major_version = major; |
| 3318 | mddev->minor_version = minor; |
| 3319 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3320 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3321 | return len; |
| 3322 | } |
| 3323 | |
| 3324 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3325 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3326 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3327 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3328 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3329 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3330 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3331 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3332 | type = "frozen"; |
| 3333 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3334 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3335 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3336 | type = "reshape"; |
| 3337 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3338 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3339 | type = "resync"; |
| 3340 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3341 | type = "check"; |
| 3342 | else |
| 3343 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3344 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3345 | type = "recover"; |
| 3346 | } |
| 3347 | return sprintf(page, "%s\n", type); |
| 3348 | } |
| 3349 | |
| 3350 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3351 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3352 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3353 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3354 | return -EINVAL; |
| 3355 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3356 | if (cmd_match(page, "frozen")) |
| 3357 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3358 | else |
| 3359 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3360 | |
| 3361 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3362 | if (mddev->sync_thread) { |
| 3363 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3364 | md_unregister_thread(mddev->sync_thread); |
| 3365 | mddev->sync_thread = NULL; |
| 3366 | mddev->recovery = 0; |
| 3367 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3368 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3369 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3370 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3371 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3372 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3373 | else if (cmd_match(page, "recover")) { |
| 3374 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3375 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3376 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3377 | int err; |
| 3378 | if (mddev->pers->start_reshape == NULL) |
| 3379 | return -EINVAL; |
| 3380 | err = mddev->pers->start_reshape(mddev); |
| 3381 | if (err) |
| 3382 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3383 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3384 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3385 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3386 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3387 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3388 | return -EINVAL; |
| 3389 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3390 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3391 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3392 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3393 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3394 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3395 | return len; |
| 3396 | } |
| 3397 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3398 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3399 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3400 | { |
| 3401 | return sprintf(page, "%llu\n", |
| 3402 | (unsigned long long) mddev->resync_mismatches); |
| 3403 | } |
| 3404 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3405 | static struct md_sysfs_entry md_scan_mode = |
| 3406 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3407 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3408 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3409 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3410 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3411 | static ssize_t |
| 3412 | sync_min_show(mddev_t *mddev, char *page) |
| 3413 | { |
| 3414 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3415 | mddev->sync_speed_min ? "local": "system"); |
| 3416 | } |
| 3417 | |
| 3418 | static ssize_t |
| 3419 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3420 | { |
| 3421 | int min; |
| 3422 | char *e; |
| 3423 | if (strncmp(buf, "system", 6)==0) { |
| 3424 | mddev->sync_speed_min = 0; |
| 3425 | return len; |
| 3426 | } |
| 3427 | min = simple_strtoul(buf, &e, 10); |
| 3428 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3429 | return -EINVAL; |
| 3430 | mddev->sync_speed_min = min; |
| 3431 | return len; |
| 3432 | } |
| 3433 | |
| 3434 | static struct md_sysfs_entry md_sync_min = |
| 3435 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3436 | |
| 3437 | static ssize_t |
| 3438 | sync_max_show(mddev_t *mddev, char *page) |
| 3439 | { |
| 3440 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3441 | mddev->sync_speed_max ? "local": "system"); |
| 3442 | } |
| 3443 | |
| 3444 | static ssize_t |
| 3445 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3446 | { |
| 3447 | int max; |
| 3448 | char *e; |
| 3449 | if (strncmp(buf, "system", 6)==0) { |
| 3450 | mddev->sync_speed_max = 0; |
| 3451 | return len; |
| 3452 | } |
| 3453 | max = simple_strtoul(buf, &e, 10); |
| 3454 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3455 | return -EINVAL; |
| 3456 | mddev->sync_speed_max = max; |
| 3457 | return len; |
| 3458 | } |
| 3459 | |
| 3460 | static struct md_sysfs_entry md_sync_max = |
| 3461 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3462 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3463 | static ssize_t |
| 3464 | degraded_show(mddev_t *mddev, char *page) |
| 3465 | { |
| 3466 | return sprintf(page, "%d\n", mddev->degraded); |
| 3467 | } |
| 3468 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3469 | |
| 3470 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3471 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3472 | { |
| 3473 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3474 | } |
| 3475 | |
| 3476 | static ssize_t |
| 3477 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3478 | { |
| 3479 | long n; |
| 3480 | |
| 3481 | if (strict_strtol(buf, 10, &n)) |
| 3482 | return -EINVAL; |
| 3483 | |
| 3484 | if (n != 0 && n != 1) |
| 3485 | return -EINVAL; |
| 3486 | |
| 3487 | mddev->parallel_resync = n; |
| 3488 | |
| 3489 | if (mddev->sync_thread) |
| 3490 | wake_up(&resync_wait); |
| 3491 | |
| 3492 | return len; |
| 3493 | } |
| 3494 | |
| 3495 | /* force parallel resync, even with shared block devices */ |
| 3496 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3497 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3498 | sync_force_parallel_show, sync_force_parallel_store); |
| 3499 | |
| 3500 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3501 | sync_speed_show(mddev_t *mddev, char *page) |
| 3502 | { |
| 3503 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3504 | if (mddev->curr_resync == 0) |
| 3505 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3506 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3507 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3508 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3509 | db = resync - mddev->resync_mark_cnt; |
| 3510 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3511 | } |
| 3512 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3513 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3514 | |
| 3515 | static ssize_t |
| 3516 | sync_completed_show(mddev_t *mddev, char *page) |
| 3517 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3518 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3519 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3520 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3521 | return sprintf(page, "none\n"); |
| 3522 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3523 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3524 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3525 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3526 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3527 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3528 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3529 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3530 | } |
| 3531 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3532 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3533 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3534 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3535 | min_sync_show(mddev_t *mddev, char *page) |
| 3536 | { |
| 3537 | return sprintf(page, "%llu\n", |
| 3538 | (unsigned long long)mddev->resync_min); |
| 3539 | } |
| 3540 | static ssize_t |
| 3541 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3542 | { |
| 3543 | unsigned long long min; |
| 3544 | if (strict_strtoull(buf, 10, &min)) |
| 3545 | return -EINVAL; |
| 3546 | if (min > mddev->resync_max) |
| 3547 | return -EINVAL; |
| 3548 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3549 | return -EBUSY; |
| 3550 | |
| 3551 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3552 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3553 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3554 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3555 | return -EINVAL; |
| 3556 | } |
| 3557 | mddev->resync_min = min; |
| 3558 | |
| 3559 | return len; |
| 3560 | } |
| 3561 | |
| 3562 | static struct md_sysfs_entry md_min_sync = |
| 3563 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3564 | |
| 3565 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3566 | max_sync_show(mddev_t *mddev, char *page) |
| 3567 | { |
| 3568 | if (mddev->resync_max == MaxSector) |
| 3569 | return sprintf(page, "max\n"); |
| 3570 | else |
| 3571 | return sprintf(page, "%llu\n", |
| 3572 | (unsigned long long)mddev->resync_max); |
| 3573 | } |
| 3574 | static ssize_t |
| 3575 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3576 | { |
| 3577 | if (strncmp(buf, "max", 3) == 0) |
| 3578 | mddev->resync_max = MaxSector; |
| 3579 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3580 | unsigned long long max; |
| 3581 | if (strict_strtoull(buf, 10, &max)) |
| 3582 | return -EINVAL; |
| 3583 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3584 | return -EINVAL; |
| 3585 | if (max < mddev->resync_max && |
| 3586 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3587 | return -EBUSY; |
| 3588 | |
| 3589 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3590 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3591 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3592 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3593 | return -EINVAL; |
| 3594 | } |
| 3595 | mddev->resync_max = max; |
| 3596 | } |
| 3597 | wake_up(&mddev->recovery_wait); |
| 3598 | return len; |
| 3599 | } |
| 3600 | |
| 3601 | static struct md_sysfs_entry md_max_sync = |
| 3602 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3603 | |
| 3604 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3605 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3606 | { |
| 3607 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3608 | } |
| 3609 | |
| 3610 | static ssize_t |
| 3611 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3612 | { |
| 3613 | char *e; |
| 3614 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3615 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3616 | if (mddev->pers == NULL || |
| 3617 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3618 | return -EINVAL; |
| 3619 | if (buf == e || (*e && *e != '\n')) |
| 3620 | return -EINVAL; |
| 3621 | if (new >= mddev->suspend_hi || |
| 3622 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3623 | mddev->suspend_lo = new; |
| 3624 | mddev->pers->quiesce(mddev, 2); |
| 3625 | return len; |
| 3626 | } else |
| 3627 | return -EINVAL; |
| 3628 | } |
| 3629 | static struct md_sysfs_entry md_suspend_lo = |
| 3630 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3631 | |
| 3632 | |
| 3633 | static ssize_t |
| 3634 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3635 | { |
| 3636 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3637 | } |
| 3638 | |
| 3639 | static ssize_t |
| 3640 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3641 | { |
| 3642 | char *e; |
| 3643 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3644 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3645 | if (mddev->pers == NULL || |
| 3646 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3647 | return -EINVAL; |
| 3648 | if (buf == e || (*e && *e != '\n')) |
| 3649 | return -EINVAL; |
| 3650 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3651 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3652 | mddev->suspend_hi = new; |
| 3653 | mddev->pers->quiesce(mddev, 1); |
| 3654 | mddev->pers->quiesce(mddev, 0); |
| 3655 | return len; |
| 3656 | } else |
| 3657 | return -EINVAL; |
| 3658 | } |
| 3659 | static struct md_sysfs_entry md_suspend_hi = |
| 3660 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3661 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3662 | static ssize_t |
| 3663 | reshape_position_show(mddev_t *mddev, char *page) |
| 3664 | { |
| 3665 | if (mddev->reshape_position != MaxSector) |
| 3666 | return sprintf(page, "%llu\n", |
| 3667 | (unsigned long long)mddev->reshape_position); |
| 3668 | strcpy(page, "none\n"); |
| 3669 | return 5; |
| 3670 | } |
| 3671 | |
| 3672 | static ssize_t |
| 3673 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3674 | { |
| 3675 | char *e; |
| 3676 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3677 | if (mddev->pers) |
| 3678 | return -EBUSY; |
| 3679 | if (buf == e || (*e && *e != '\n')) |
| 3680 | return -EINVAL; |
| 3681 | mddev->reshape_position = new; |
| 3682 | mddev->delta_disks = 0; |
| 3683 | mddev->new_level = mddev->level; |
| 3684 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3685 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3686 | return len; |
| 3687 | } |
| 3688 | |
| 3689 | static struct md_sysfs_entry md_reshape_position = |
| 3690 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3691 | reshape_position_store); |
| 3692 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3693 | static ssize_t |
| 3694 | array_size_show(mddev_t *mddev, char *page) |
| 3695 | { |
| 3696 | if (mddev->external_size) |
| 3697 | return sprintf(page, "%llu\n", |
| 3698 | (unsigned long long)mddev->array_sectors/2); |
| 3699 | else |
| 3700 | return sprintf(page, "default\n"); |
| 3701 | } |
| 3702 | |
| 3703 | static ssize_t |
| 3704 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3705 | { |
| 3706 | sector_t sectors; |
| 3707 | |
| 3708 | if (strncmp(buf, "default", 7) == 0) { |
| 3709 | if (mddev->pers) |
| 3710 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3711 | else |
| 3712 | sectors = mddev->array_sectors; |
| 3713 | |
| 3714 | mddev->external_size = 0; |
| 3715 | } else { |
| 3716 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3717 | return -EINVAL; |
| 3718 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3719 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3720 | |
| 3721 | mddev->external_size = 1; |
| 3722 | } |
| 3723 | |
| 3724 | mddev->array_sectors = sectors; |
| 3725 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 3726 | if (mddev->pers) { |
| 3727 | struct block_device *bdev = bdget_disk(mddev->gendisk, 0); |
| 3728 | |
| 3729 | if (bdev) { |
| 3730 | mutex_lock(&bdev->bd_inode->i_mutex); |
| 3731 | i_size_write(bdev->bd_inode, |
| 3732 | (loff_t)mddev->array_sectors << 9); |
| 3733 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 3734 | bdput(bdev); |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | return len; |
| 3739 | } |
| 3740 | |
| 3741 | static struct md_sysfs_entry md_array_size = |
| 3742 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3743 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3744 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3745 | static struct attribute *md_default_attrs[] = { |
| 3746 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3747 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3748 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3749 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3750 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3751 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3752 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3753 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3754 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3755 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3756 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3757 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3758 | NULL, |
| 3759 | }; |
| 3760 | |
| 3761 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3762 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3763 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3764 | &md_sync_min.attr, |
| 3765 | &md_sync_max.attr, |
| 3766 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3767 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3768 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3769 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3770 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3771 | &md_suspend_lo.attr, |
| 3772 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3773 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3774 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3775 | NULL, |
| 3776 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3777 | static struct attribute_group md_redundancy_group = { |
| 3778 | .name = NULL, |
| 3779 | .attrs = md_redundancy_attrs, |
| 3780 | }; |
| 3781 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3782 | |
| 3783 | static ssize_t |
| 3784 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3785 | { |
| 3786 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3787 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3788 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3789 | |
| 3790 | if (!entry->show) |
| 3791 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3792 | rv = mddev_lock(mddev); |
| 3793 | if (!rv) { |
| 3794 | rv = entry->show(mddev, page); |
| 3795 | mddev_unlock(mddev); |
| 3796 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3797 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | static ssize_t |
| 3801 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3802 | const char *page, size_t length) |
| 3803 | { |
| 3804 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3805 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3806 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3807 | |
| 3808 | if (!entry->store) |
| 3809 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3810 | if (!capable(CAP_SYS_ADMIN)) |
| 3811 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3812 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3813 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3814 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3815 | if (!rv) { |
| 3816 | rv = entry->store(mddev, page, length); |
| 3817 | mddev_unlock(mddev); |
| 3818 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3819 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | static void md_free(struct kobject *ko) |
| 3823 | { |
| 3824 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3825 | |
| 3826 | if (mddev->sysfs_state) |
| 3827 | sysfs_put(mddev->sysfs_state); |
| 3828 | |
| 3829 | if (mddev->gendisk) { |
| 3830 | del_gendisk(mddev->gendisk); |
| 3831 | put_disk(mddev->gendisk); |
| 3832 | } |
| 3833 | if (mddev->queue) |
| 3834 | blk_cleanup_queue(mddev->queue); |
| 3835 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3836 | kfree(mddev); |
| 3837 | } |
| 3838 | |
| 3839 | static struct sysfs_ops md_sysfs_ops = { |
| 3840 | .show = md_attr_show, |
| 3841 | .store = md_attr_store, |
| 3842 | }; |
| 3843 | static struct kobj_type md_ktype = { |
| 3844 | .release = md_free, |
| 3845 | .sysfs_ops = &md_sysfs_ops, |
| 3846 | .default_attrs = md_default_attrs, |
| 3847 | }; |
| 3848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | int mdp_major = 0; |
| 3850 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3851 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3852 | { |
| 3853 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3854 | |
| 3855 | if (mddev->private == &md_redundancy_group) { |
| 3856 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3857 | if (mddev->sysfs_action) |
| 3858 | sysfs_put(mddev->sysfs_action); |
| 3859 | mddev->sysfs_action = NULL; |
| 3860 | mddev->private = NULL; |
| 3861 | } |
| 3862 | kobject_del(&mddev->kobj); |
| 3863 | kobject_put(&mddev->kobj); |
| 3864 | } |
| 3865 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3866 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3868 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | mddev_t *mddev = mddev_find(dev); |
| 3870 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3871 | int partitioned; |
| 3872 | int shift; |
| 3873 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3874 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | |
| 3876 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3877 | return -ENODEV; |
| 3878 | |
| 3879 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3880 | shift = partitioned ? MdpMinorShift : 0; |
| 3881 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3883 | /* wait for any previous instance if this device |
| 3884 | * to be completed removed (mddev_delayed_delete). |
| 3885 | */ |
| 3886 | flush_scheduled_work(); |
| 3887 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3888 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3889 | error = -EEXIST; |
| 3890 | if (mddev->gendisk) |
| 3891 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3892 | |
| 3893 | if (name) { |
| 3894 | /* Need to ensure that 'name' is not a duplicate. |
| 3895 | */ |
| 3896 | mddev_t *mddev2; |
| 3897 | spin_lock(&all_mddevs_lock); |
| 3898 | |
| 3899 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3900 | if (mddev2->gendisk && |
| 3901 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3902 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3903 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3904 | } |
| 3905 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3907 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3908 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3909 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3910 | if (!mddev->queue) |
| 3911 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3912 | mddev->queue->queuedata = mddev; |
| 3913 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3914 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3915 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3916 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3917 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | disk = alloc_disk(1 << shift); |
| 3920 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3921 | blk_cleanup_queue(mddev->queue); |
| 3922 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3923 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3925 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3927 | if (name) |
| 3928 | strcpy(disk->disk_name, name); |
| 3929 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3931 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | disk->fops = &md_fops; |
| 3934 | disk->private_data = mddev; |
| 3935 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3936 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3937 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3938 | * remove it now. |
| 3939 | */ |
| 3940 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3941 | add_disk(disk); |
| 3942 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3943 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3944 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3945 | if (error) { |
| 3946 | /* This isn't possible, but as kobject_init_and_add is marked |
| 3947 | * __must_check, we must do something with the result |
| 3948 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3949 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3950 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3951 | error = 0; |
| 3952 | } |
| 3953 | abort: |
| 3954 | mutex_unlock(&disks_mutex); |
| 3955 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3956 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3957 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3958 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3959 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3960 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3964 | { |
| 3965 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | return NULL; |
| 3967 | } |
| 3968 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3969 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 3970 | { |
| 3971 | /* val must be "md_*" where * is not all digits. |
| 3972 | * We allocate an array with a large free minor number, and |
| 3973 | * set the name to val. val must not already be an active name. |
| 3974 | */ |
| 3975 | int len = strlen(val); |
| 3976 | char buf[DISK_NAME_LEN]; |
| 3977 | |
| 3978 | while (len && val[len-1] == '\n') |
| 3979 | len--; |
| 3980 | if (len >= DISK_NAME_LEN) |
| 3981 | return -E2BIG; |
| 3982 | strlcpy(buf, val, len+1); |
| 3983 | if (strncmp(buf, "md_", 3) != 0) |
| 3984 | return -EINVAL; |
| 3985 | return md_alloc(0, buf); |
| 3986 | } |
| 3987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | static void md_safemode_timeout(unsigned long data) |
| 3989 | { |
| 3990 | mddev_t *mddev = (mddev_t *) data; |
| 3991 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3992 | if (!atomic_read(&mddev->writes_pending)) { |
| 3993 | mddev->safemode = 1; |
| 3994 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3995 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | md_wakeup_thread(mddev->thread); |
| 3998 | } |
| 3999 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4000 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | |
| 4002 | static int do_md_run(mddev_t * mddev) |
| 4003 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4004 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | mdk_rdev_t *rdev; |
| 4006 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4007 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4009 | if (list_empty(&mddev->disks)) |
| 4010 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
| 4013 | if (mddev->pers) |
| 4014 | return -EBUSY; |
| 4015 | |
| 4016 | /* |
| 4017 | * Analyze all RAID superblock(s) |
| 4018 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4019 | if (!mddev->raid_disks) { |
| 4020 | if (!mddev->persistent) |
| 4021 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4022 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4023 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4025 | if (mddev->level != LEVEL_NONE) |
| 4026 | request_module("md-level-%d", mddev->level); |
| 4027 | else if (mddev->clevel[0]) |
| 4028 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | |
| 4030 | /* |
| 4031 | * Drop all container device buffers, from now on |
| 4032 | * the only valid external interface is through the md |
| 4033 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4035 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4036 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | continue; |
| 4038 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4039 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4040 | |
| 4041 | /* perform some consistency tests on the device. |
| 4042 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4043 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4044 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4045 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4046 | if (mddev->dev_sectors && |
| 4047 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4048 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4049 | printk("md: %s: data overlaps metadata\n", |
| 4050 | mdname(mddev)); |
| 4051 | return -EINVAL; |
| 4052 | } |
| 4053 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4054 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4055 | > rdev->data_offset) { |
| 4056 | printk("md: %s: metadata overlaps data\n", |
| 4057 | mdname(mddev)); |
| 4058 | return -EINVAL; |
| 4059 | } |
| 4060 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4061 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | md_probe(mddev->unit, NULL, NULL); |
| 4065 | disk = mddev->gendisk; |
| 4066 | if (!disk) |
| 4067 | return -ENOMEM; |
| 4068 | |
| 4069 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4070 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4071 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4073 | if (mddev->level != LEVEL_NONE) |
| 4074 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4075 | mddev->level); |
| 4076 | else |
| 4077 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4078 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | return -EINVAL; |
| 4080 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4081 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4083 | if (mddev->level != pers->level) { |
| 4084 | mddev->level = pers->level; |
| 4085 | mddev->new_level = pers->level; |
| 4086 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4087 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4089 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4090 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4091 | /* This personality cannot handle reshaping... */ |
| 4092 | mddev->pers = NULL; |
| 4093 | module_put(pers->owner); |
| 4094 | return -EINVAL; |
| 4095 | } |
| 4096 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4097 | if (pers->sync_request) { |
| 4098 | /* Warn if this is a potentially silly |
| 4099 | * configuration. |
| 4100 | */ |
| 4101 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4102 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4103 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4104 | |
| 4105 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4106 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4107 | if (rdev < rdev2 && |
| 4108 | rdev->bdev->bd_contains == |
| 4109 | rdev2->bdev->bd_contains) { |
| 4110 | printk(KERN_WARNING |
| 4111 | "%s: WARNING: %s appears to be" |
| 4112 | " on the same physical disk as" |
| 4113 | " %s.\n", |
| 4114 | mdname(mddev), |
| 4115 | bdevname(rdev->bdev,b), |
| 4116 | bdevname(rdev2->bdev,b2)); |
| 4117 | warned = 1; |
| 4118 | } |
| 4119 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4120 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4121 | if (warned) |
| 4122 | printk(KERN_WARNING |
| 4123 | "True protection against single-disk" |
| 4124 | " failure might be compromised.\n"); |
| 4125 | } |
| 4126 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4127 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4128 | /* may be over-ridden by personality */ |
| 4129 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4130 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4131 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4132 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4133 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4134 | if (start_readonly) |
| 4135 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4136 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4137 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4138 | if (err) |
| 4139 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4140 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4141 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4142 | " but 'external_size' not in effect?\n", __func__); |
| 4143 | printk(KERN_ERR |
| 4144 | "md: invalid array_size %llu > default size %llu\n", |
| 4145 | (unsigned long long)mddev->array_sectors / 2, |
| 4146 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4147 | err = -EINVAL; |
| 4148 | mddev->pers->stop(mddev); |
| 4149 | } |
| 4150 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4151 | err = bitmap_create(mddev); |
| 4152 | if (err) { |
| 4153 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4154 | mdname(mddev), err); |
| 4155 | mddev->pers->stop(mddev); |
| 4156 | } |
| 4157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | module_put(mddev->pers->owner); |
| 4160 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4161 | bitmap_destroy(mddev); |
| 4162 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4164 | if (mddev->pers->sync_request) { |
| 4165 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4166 | printk(KERN_WARNING |
| 4167 | "md: cannot register extra attributes for %s\n", |
| 4168 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4169 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4170 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4171 | mddev->ro = 0; |
| 4172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4173 | atomic_set(&mddev->writes_pending,0); |
| 4174 | mddev->safemode = 0; |
| 4175 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4176 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4177 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4179 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4180 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4181 | if (rdev->raid_disk >= 0) { |
| 4182 | char nm[20]; |
| 4183 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4184 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4185 | printk("md: cannot register %s for %s\n", |
| 4186 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | |
| 4189 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4190 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4191 | if (mddev->flags) |
| 4192 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4194 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4196 | /* If there is a partially-recovered drive we need to |
| 4197 | * start recovery here. If we leave it to md_check_recovery, |
| 4198 | * it will remove the drives and not do the right thing |
| 4199 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4200 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4201 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4202 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4203 | if (rdev->raid_disk >= 0 && |
| 4204 | !test_bit(In_sync, &rdev->flags) && |
| 4205 | !test_bit(Faulty, &rdev->flags)) |
| 4206 | /* complete an interrupted recovery */ |
| 4207 | spares++; |
| 4208 | if (spares && mddev->pers->sync_request) { |
| 4209 | mddev->recovery = 0; |
| 4210 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4211 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4212 | mddev, |
| 4213 | "%s_resync"); |
| 4214 | if (!mddev->sync_thread) { |
| 4215 | printk(KERN_ERR "%s: could not start resync" |
| 4216 | " thread...\n", |
| 4217 | mdname(mddev)); |
| 4218 | /* leave the spares where they are, it shouldn't hurt */ |
| 4219 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4220 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4221 | } |
| 4222 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4223 | md_wakeup_thread(mddev->thread); |
| 4224 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4225 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4226 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4227 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4228 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4229 | if (mddev->sysfs_action) |
| 4230 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4231 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4232 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | return 0; |
| 4234 | } |
| 4235 | |
| 4236 | static int restart_array(mddev_t *mddev) |
| 4237 | { |
| 4238 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4240 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4242 | return -ENXIO; |
| 4243 | if (!mddev->pers) |
| 4244 | return -EINVAL; |
| 4245 | if (!mddev->ro) |
| 4246 | return -EBUSY; |
| 4247 | mddev->safemode = 0; |
| 4248 | mddev->ro = 0; |
| 4249 | set_disk_ro(disk, 0); |
| 4250 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4251 | mdname(mddev)); |
| 4252 | /* Kick recovery or resync if necessary */ |
| 4253 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4254 | md_wakeup_thread(mddev->thread); |
| 4255 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4256 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4257 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | } |
| 4259 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4260 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4261 | * to the file ourselves */ |
| 4262 | static int deny_bitmap_write_access(struct file * file) |
| 4263 | { |
| 4264 | struct inode *inode = file->f_mapping->host; |
| 4265 | |
| 4266 | spin_lock(&inode->i_lock); |
| 4267 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4268 | spin_unlock(&inode->i_lock); |
| 4269 | return -ETXTBSY; |
| 4270 | } |
| 4271 | atomic_set(&inode->i_writecount, -1); |
| 4272 | spin_unlock(&inode->i_lock); |
| 4273 | |
| 4274 | return 0; |
| 4275 | } |
| 4276 | |
| 4277 | static void restore_bitmap_write_access(struct file *file) |
| 4278 | { |
| 4279 | struct inode *inode = file->f_mapping->host; |
| 4280 | |
| 4281 | spin_lock(&inode->i_lock); |
| 4282 | atomic_set(&inode->i_writecount, 1); |
| 4283 | spin_unlock(&inode->i_lock); |
| 4284 | } |
| 4285 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4286 | /* mode: |
| 4287 | * 0 - completely stop and dis-assemble array |
| 4288 | * 1 - switch to readonly |
| 4289 | * 2 - stop but do not disassemble array |
| 4290 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4291 | static int do_md_stop(mddev_t * mddev, int mode, int is_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | { |
| 4293 | int err = 0; |
| 4294 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4295 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4297 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4298 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4299 | return -EBUSY; |
| 4300 | } |
| 4301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | |
| 4304 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4305 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4307 | md_unregister_thread(mddev->sync_thread); |
| 4308 | mddev->sync_thread = NULL; |
| 4309 | } |
| 4310 | |
| 4311 | del_timer_sync(&mddev->safemode_timer); |
| 4312 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4313 | switch(mode) { |
| 4314 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4316 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | goto out; |
| 4318 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4319 | break; |
| 4320 | case 0: /* disassemble */ |
| 4321 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4322 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4323 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | if (mddev->ro) |
| 4325 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4328 | mddev->queue->merge_bvec_fn = NULL; |
| 4329 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4330 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4332 | if (mddev->pers->sync_request) |
| 4333 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4335 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4336 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4337 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4338 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4339 | if (rdev->raid_disk >= 0) { |
| 4340 | char nm[20]; |
| 4341 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4342 | sysfs_remove_link(&mddev->kobj, nm); |
| 4343 | } |
| 4344 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4345 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4346 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | if (mddev->ro) |
| 4349 | mddev->ro = 0; |
| 4350 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4351 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | /* mark array as shutdown cleanly */ |
| 4353 | mddev->in_sync = 1; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4354 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4356 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4358 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | /* |
| 4362 | * Free resources if final stop |
| 4363 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4364 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4367 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4368 | bitmap_destroy(mddev); |
| 4369 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4370 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4371 | fput(mddev->bitmap_file); |
| 4372 | mddev->bitmap_file = NULL; |
| 4373 | } |
| 4374 | mddev->bitmap_offset = 0; |
| 4375 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4376 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4377 | flush_scheduled_work(); |
| 4378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | export_array(mddev); |
| 4380 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4381 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4382 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4383 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4384 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4385 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4386 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4387 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4388 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4389 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4390 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4391 | mddev->level = LEVEL_NONE; |
| 4392 | mddev->clevel[0] = 0; |
| 4393 | mddev->flags = 0; |
| 4394 | mddev->ro = 0; |
| 4395 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4396 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4397 | mddev->ctime = mddev->utime = 0; |
| 4398 | mddev->layout = 0; |
| 4399 | mddev->max_disks = 0; |
| 4400 | mddev->events = 0; |
| 4401 | mddev->delta_disks = 0; |
| 4402 | mddev->new_level = LEVEL_NONE; |
| 4403 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4404 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4405 | mddev->curr_resync = 0; |
| 4406 | mddev->resync_mismatches = 0; |
| 4407 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4408 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4409 | mddev->recovery = 0; |
| 4410 | mddev->in_sync = 0; |
| 4411 | mddev->changed = 0; |
| 4412 | mddev->degraded = 0; |
| 4413 | mddev->barriers_work = 0; |
| 4414 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4415 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4416 | if (mddev->hold_active == UNTIL_STOP) |
| 4417 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4418 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4419 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4421 | mdname(mddev)); |
| 4422 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4423 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4424 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4425 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4426 | out: |
| 4427 | return err; |
| 4428 | } |
| 4429 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4430 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | static void autorun_array(mddev_t *mddev) |
| 4432 | { |
| 4433 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | int err; |
| 4435 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4436 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | |
| 4439 | printk(KERN_INFO "md: running: "); |
| 4440 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4441 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | char b[BDEVNAME_SIZE]; |
| 4443 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4444 | } |
| 4445 | printk("\n"); |
| 4446 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4447 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | if (err) { |
| 4449 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4450 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4451 | } |
| 4452 | } |
| 4453 | |
| 4454 | /* |
| 4455 | * lets try to run arrays based on all disks that have arrived |
| 4456 | * until now. (those are in pending_raid_disks) |
| 4457 | * |
| 4458 | * the method: pick the first pending disk, collect all disks with |
| 4459 | * the same UUID, remove all from the pending list and put them into |
| 4460 | * the 'same_array' list. Then order this list based on superblock |
| 4461 | * update time (freshest comes first), kick out 'old' disks and |
| 4462 | * compare superblocks. If everything's fine then run it. |
| 4463 | * |
| 4464 | * If "unit" is allocated, then bump its reference count |
| 4465 | */ |
| 4466 | static void autorun_devices(int part) |
| 4467 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4468 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | mddev_t *mddev; |
| 4470 | char b[BDEVNAME_SIZE]; |
| 4471 | |
| 4472 | printk(KERN_INFO "md: autorun ...\n"); |
| 4473 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4474 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4476 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | rdev0 = list_entry(pending_raid_disks.next, |
| 4478 | mdk_rdev_t, same_set); |
| 4479 | |
| 4480 | printk(KERN_INFO "md: considering %s ...\n", |
| 4481 | bdevname(rdev0->bdev,b)); |
| 4482 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4483 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4485 | printk(KERN_INFO "md: adding %s ...\n", |
| 4486 | bdevname(rdev->bdev,b)); |
| 4487 | list_move(&rdev->same_set, &candidates); |
| 4488 | } |
| 4489 | /* |
| 4490 | * now we have a set of devices, with all of them having |
| 4491 | * mostly sane superblocks. It's time to allocate the |
| 4492 | * mddev. |
| 4493 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4494 | if (part) { |
| 4495 | dev = MKDEV(mdp_major, |
| 4496 | rdev0->preferred_minor << MdpMinorShift); |
| 4497 | unit = MINOR(dev) >> MdpMinorShift; |
| 4498 | } else { |
| 4499 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4500 | unit = MINOR(dev); |
| 4501 | } |
| 4502 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4504 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4505 | break; |
| 4506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | |
| 4508 | md_probe(dev, NULL, NULL); |
| 4509 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4510 | if (!mddev || !mddev->gendisk) { |
| 4511 | if (mddev) |
| 4512 | mddev_put(mddev); |
| 4513 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | "md: cannot allocate memory for md drive.\n"); |
| 4515 | break; |
| 4516 | } |
| 4517 | if (mddev_lock(mddev)) |
| 4518 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4519 | mdname(mddev)); |
| 4520 | else if (mddev->raid_disks || mddev->major_version |
| 4521 | || !list_empty(&mddev->disks)) { |
| 4522 | printk(KERN_WARNING |
| 4523 | "md: %s already running, cannot run %s\n", |
| 4524 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4525 | mddev_unlock(mddev); |
| 4526 | } else { |
| 4527 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4528 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4529 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | list_del_init(&rdev->same_set); |
| 4531 | if (bind_rdev_to_array(rdev, mddev)) |
| 4532 | export_rdev(rdev); |
| 4533 | } |
| 4534 | autorun_array(mddev); |
| 4535 | mddev_unlock(mddev); |
| 4536 | } |
| 4537 | /* on success, candidates will be empty, on error |
| 4538 | * it won't... |
| 4539 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4540 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4541 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | mddev_put(mddev); |
| 4545 | } |
| 4546 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4547 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4548 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | static int get_version(void __user * arg) |
| 4551 | { |
| 4552 | mdu_version_t ver; |
| 4553 | |
| 4554 | ver.major = MD_MAJOR_VERSION; |
| 4555 | ver.minor = MD_MINOR_VERSION; |
| 4556 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4557 | |
| 4558 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4559 | return -EFAULT; |
| 4560 | |
| 4561 | return 0; |
| 4562 | } |
| 4563 | |
| 4564 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4565 | { |
| 4566 | mdu_array_info_t info; |
| 4567 | int nr,working,active,failed,spare; |
| 4568 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | |
| 4570 | nr=working=active=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4571 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4573 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4574 | failed++; |
| 4575 | else { |
| 4576 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4577 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | active++; |
| 4579 | else |
| 4580 | spare++; |
| 4581 | } |
| 4582 | } |
| 4583 | |
| 4584 | info.major_version = mddev->major_version; |
| 4585 | info.minor_version = mddev->minor_version; |
| 4586 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4587 | info.ctime = mddev->ctime; |
| 4588 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4589 | info.size = mddev->dev_sectors / 2; |
| 4590 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4591 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | info.nr_disks = nr; |
| 4593 | info.raid_disks = mddev->raid_disks; |
| 4594 | info.md_minor = mddev->md_minor; |
| 4595 | info.not_persistent= !mddev->persistent; |
| 4596 | |
| 4597 | info.utime = mddev->utime; |
| 4598 | info.state = 0; |
| 4599 | if (mddev->in_sync) |
| 4600 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4601 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4602 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4603 | info.active_disks = active; |
| 4604 | info.working_disks = working; |
| 4605 | info.failed_disks = failed; |
| 4606 | info.spare_disks = spare; |
| 4607 | |
| 4608 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4609 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | |
| 4611 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4612 | return -EFAULT; |
| 4613 | |
| 4614 | return 0; |
| 4615 | } |
| 4616 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4617 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4618 | { |
| 4619 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4620 | char *ptr, *buf = NULL; |
| 4621 | int err = -ENOMEM; |
| 4622 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4623 | if (md_allow_write(mddev)) |
| 4624 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4625 | else |
| 4626 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4627 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4628 | if (!file) |
| 4629 | goto out; |
| 4630 | |
| 4631 | /* bitmap disabled, zero the first byte and copy out */ |
| 4632 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4633 | file->pathname[0] = '\0'; |
| 4634 | goto copy_out; |
| 4635 | } |
| 4636 | |
| 4637 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4638 | if (!buf) |
| 4639 | goto out; |
| 4640 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4641 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4642 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4643 | goto out; |
| 4644 | |
| 4645 | strcpy(file->pathname, ptr); |
| 4646 | |
| 4647 | copy_out: |
| 4648 | err = 0; |
| 4649 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4650 | err = -EFAULT; |
| 4651 | out: |
| 4652 | kfree(buf); |
| 4653 | kfree(file); |
| 4654 | return err; |
| 4655 | } |
| 4656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4658 | { |
| 4659 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | mdk_rdev_t *rdev; |
| 4661 | |
| 4662 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4663 | return -EFAULT; |
| 4664 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4665 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | if (rdev) { |
| 4667 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4668 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4669 | info.raid_disk = rdev->raid_disk; |
| 4670 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4671 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4673 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4675 | info.state |= (1<<MD_DISK_SYNC); |
| 4676 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4677 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4678 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | } else { |
| 4680 | info.major = info.minor = 0; |
| 4681 | info.raid_disk = -1; |
| 4682 | info.state = (1<<MD_DISK_REMOVED); |
| 4683 | } |
| 4684 | |
| 4685 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4686 | return -EFAULT; |
| 4687 | |
| 4688 | return 0; |
| 4689 | } |
| 4690 | |
| 4691 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4692 | { |
| 4693 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4694 | mdk_rdev_t *rdev; |
| 4695 | dev_t dev = MKDEV(info->major,info->minor); |
| 4696 | |
| 4697 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4698 | return -EOVERFLOW; |
| 4699 | |
| 4700 | if (!mddev->raid_disks) { |
| 4701 | int err; |
| 4702 | /* expecting a device which has a superblock */ |
| 4703 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4704 | if (IS_ERR(rdev)) { |
| 4705 | printk(KERN_WARNING |
| 4706 | "md: md_import_device returned %ld\n", |
| 4707 | PTR_ERR(rdev)); |
| 4708 | return PTR_ERR(rdev); |
| 4709 | } |
| 4710 | if (!list_empty(&mddev->disks)) { |
| 4711 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4712 | mdk_rdev_t, same_set); |
| 4713 | int err = super_types[mddev->major_version] |
| 4714 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4715 | if (err < 0) { |
| 4716 | printk(KERN_WARNING |
| 4717 | "md: %s has different UUID to %s\n", |
| 4718 | bdevname(rdev->bdev,b), |
| 4719 | bdevname(rdev0->bdev,b2)); |
| 4720 | export_rdev(rdev); |
| 4721 | return -EINVAL; |
| 4722 | } |
| 4723 | } |
| 4724 | err = bind_rdev_to_array(rdev, mddev); |
| 4725 | if (err) |
| 4726 | export_rdev(rdev); |
| 4727 | return err; |
| 4728 | } |
| 4729 | |
| 4730 | /* |
| 4731 | * add_new_disk can be used once the array is assembled |
| 4732 | * to add "hot spares". They must already have a superblock |
| 4733 | * written |
| 4734 | */ |
| 4735 | if (mddev->pers) { |
| 4736 | int err; |
| 4737 | if (!mddev->pers->hot_add_disk) { |
| 4738 | printk(KERN_WARNING |
| 4739 | "%s: personality does not support diskops!\n", |
| 4740 | mdname(mddev)); |
| 4741 | return -EINVAL; |
| 4742 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4743 | if (mddev->persistent) |
| 4744 | rdev = md_import_device(dev, mddev->major_version, |
| 4745 | mddev->minor_version); |
| 4746 | else |
| 4747 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | if (IS_ERR(rdev)) { |
| 4749 | printk(KERN_WARNING |
| 4750 | "md: md_import_device returned %ld\n", |
| 4751 | PTR_ERR(rdev)); |
| 4752 | return PTR_ERR(rdev); |
| 4753 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4754 | /* set save_raid_disk if appropriate */ |
| 4755 | if (!mddev->persistent) { |
| 4756 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4757 | info->raid_disk < mddev->raid_disks) |
| 4758 | rdev->raid_disk = info->raid_disk; |
| 4759 | else |
| 4760 | rdev->raid_disk = -1; |
| 4761 | } else |
| 4762 | super_types[mddev->major_version]. |
| 4763 | validate_super(mddev, rdev); |
| 4764 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4765 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4766 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4767 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4768 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4769 | else |
| 4770 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | rdev->raid_disk = -1; |
| 4773 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4774 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4775 | /* If there is hot_add_disk but no hot_remove_disk |
| 4776 | * then added disks for geometry changes, |
| 4777 | * and should be added immediately. |
| 4778 | */ |
| 4779 | super_types[mddev->major_version]. |
| 4780 | validate_super(mddev, rdev); |
| 4781 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4782 | if (err) |
| 4783 | unbind_rdev_from_array(rdev); |
| 4784 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | if (err) |
| 4786 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4787 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4788 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4789 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4790 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4791 | if (mddev->degraded) |
| 4792 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4793 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4794 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4795 | return err; |
| 4796 | } |
| 4797 | |
| 4798 | /* otherwise, add_new_disk is only allowed |
| 4799 | * for major_version==0 superblocks |
| 4800 | */ |
| 4801 | if (mddev->major_version != 0) { |
| 4802 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4803 | mdname(mddev)); |
| 4804 | return -EINVAL; |
| 4805 | } |
| 4806 | |
| 4807 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4808 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4809 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | if (IS_ERR(rdev)) { |
| 4811 | printk(KERN_WARNING |
| 4812 | "md: error, md_import_device() returned %ld\n", |
| 4813 | PTR_ERR(rdev)); |
| 4814 | return PTR_ERR(rdev); |
| 4815 | } |
| 4816 | rdev->desc_nr = info->number; |
| 4817 | if (info->raid_disk < mddev->raid_disks) |
| 4818 | rdev->raid_disk = info->raid_disk; |
| 4819 | else |
| 4820 | rdev->raid_disk = -1; |
| 4821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4823 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4824 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4826 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4827 | set_bit(WriteMostly, &rdev->flags); |
| 4828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | if (!mddev->persistent) { |
| 4830 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4831 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4833 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4834 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4836 | err = bind_rdev_to_array(rdev, mddev); |
| 4837 | if (err) { |
| 4838 | export_rdev(rdev); |
| 4839 | return err; |
| 4840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | return 0; |
| 4844 | } |
| 4845 | |
| 4846 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4847 | { |
| 4848 | char b[BDEVNAME_SIZE]; |
| 4849 | mdk_rdev_t *rdev; |
| 4850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4851 | rdev = find_rdev(mddev, dev); |
| 4852 | if (!rdev) |
| 4853 | return -ENXIO; |
| 4854 | |
| 4855 | if (rdev->raid_disk >= 0) |
| 4856 | goto busy; |
| 4857 | |
| 4858 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4859 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4860 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | |
| 4862 | return 0; |
| 4863 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4864 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4866 | return -EBUSY; |
| 4867 | } |
| 4868 | |
| 4869 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4870 | { |
| 4871 | char b[BDEVNAME_SIZE]; |
| 4872 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | mdk_rdev_t *rdev; |
| 4874 | |
| 4875 | if (!mddev->pers) |
| 4876 | return -ENODEV; |
| 4877 | |
| 4878 | if (mddev->major_version != 0) { |
| 4879 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4880 | " version-0 superblocks.\n", |
| 4881 | mdname(mddev)); |
| 4882 | return -EINVAL; |
| 4883 | } |
| 4884 | if (!mddev->pers->hot_add_disk) { |
| 4885 | printk(KERN_WARNING |
| 4886 | "%s: personality does not support diskops!\n", |
| 4887 | mdname(mddev)); |
| 4888 | return -EINVAL; |
| 4889 | } |
| 4890 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4891 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4892 | if (IS_ERR(rdev)) { |
| 4893 | printk(KERN_WARNING |
| 4894 | "md: error, md_import_device() returned %ld\n", |
| 4895 | PTR_ERR(rdev)); |
| 4896 | return -EINVAL; |
| 4897 | } |
| 4898 | |
| 4899 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4900 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4902 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4904 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4905 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4906 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | printk(KERN_WARNING |
| 4908 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4909 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4910 | err = -EINVAL; |
| 4911 | goto abort_export; |
| 4912 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4913 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4915 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4916 | err = bind_rdev_to_array(rdev, mddev); |
| 4917 | if (err) |
| 4918 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | |
| 4920 | /* |
| 4921 | * The rest should better be atomic, we can have disk failures |
| 4922 | * noticed in interrupt contexts ... |
| 4923 | */ |
| 4924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | rdev->raid_disk = -1; |
| 4926 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4927 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | |
| 4929 | /* |
| 4930 | * Kick recovery, maybe this spare has to be added to the |
| 4931 | * array immediately. |
| 4932 | */ |
| 4933 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4934 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4935 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | return 0; |
| 4937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | abort_export: |
| 4939 | export_rdev(rdev); |
| 4940 | return err; |
| 4941 | } |
| 4942 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4943 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4944 | { |
| 4945 | int err; |
| 4946 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4947 | if (mddev->pers) { |
| 4948 | if (!mddev->pers->quiesce) |
| 4949 | return -EBUSY; |
| 4950 | if (mddev->recovery || mddev->sync_thread) |
| 4951 | return -EBUSY; |
| 4952 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4953 | } |
| 4954 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4955 | |
| 4956 | if (fd >= 0) { |
| 4957 | if (mddev->bitmap) |
| 4958 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4959 | mddev->bitmap_file = fget(fd); |
| 4960 | |
| 4961 | if (mddev->bitmap_file == NULL) { |
| 4962 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4963 | mdname(mddev)); |
| 4964 | return -EBADF; |
| 4965 | } |
| 4966 | |
| 4967 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4968 | if (err) { |
| 4969 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4970 | mdname(mddev)); |
| 4971 | fput(mddev->bitmap_file); |
| 4972 | mddev->bitmap_file = NULL; |
| 4973 | return err; |
| 4974 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4975 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4976 | } else if (mddev->bitmap == NULL) |
| 4977 | return -ENOENT; /* cannot remove what isn't there */ |
| 4978 | err = 0; |
| 4979 | if (mddev->pers) { |
| 4980 | mddev->pers->quiesce(mddev, 1); |
| 4981 | if (fd >= 0) |
| 4982 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4983 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4984 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4985 | fd = -1; /* make sure to put the file */ |
| 4986 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4987 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4988 | } |
| 4989 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4990 | if (mddev->bitmap_file) { |
| 4991 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4992 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4993 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4994 | mddev->bitmap_file = NULL; |
| 4995 | } |
| 4996 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4997 | return err; |
| 4998 | } |
| 4999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | /* |
| 5001 | * set_array_info is used two different ways |
| 5002 | * The original usage is when creating a new array. |
| 5003 | * In this usage, raid_disks is > 0 and it together with |
| 5004 | * level, size, not_persistent,layout,chunksize determine the |
| 5005 | * shape of the array. |
| 5006 | * This will always create an array with a type-0.90.0 superblock. |
| 5007 | * The newer usage is when assembling an array. |
| 5008 | * In this case raid_disks will be 0, and the major_version field is |
| 5009 | * use to determine which style super-blocks are to be found on the devices. |
| 5010 | * The minor and patch _version numbers are also kept incase the |
| 5011 | * super_block handler wishes to interpret them. |
| 5012 | */ |
| 5013 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5014 | { |
| 5015 | |
| 5016 | if (info->raid_disks == 0) { |
| 5017 | /* just setting version number for superblock loading */ |
| 5018 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5019 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | super_types[info->major_version].name == NULL) { |
| 5021 | /* maybe try to auto-load a module? */ |
| 5022 | printk(KERN_INFO |
| 5023 | "md: superblock version %d not known\n", |
| 5024 | info->major_version); |
| 5025 | return -EINVAL; |
| 5026 | } |
| 5027 | mddev->major_version = info->major_version; |
| 5028 | mddev->minor_version = info->minor_version; |
| 5029 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5030 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | return 0; |
| 5032 | } |
| 5033 | mddev->major_version = MD_MAJOR_VERSION; |
| 5034 | mddev->minor_version = MD_MINOR_VERSION; |
| 5035 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5036 | mddev->ctime = get_seconds(); |
| 5037 | |
| 5038 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5039 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5040 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | mddev->raid_disks = info->raid_disks; |
| 5042 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5043 | * openned |
| 5044 | */ |
| 5045 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5046 | mddev->recovery_cp = MaxSector; |
| 5047 | else |
| 5048 | mddev->recovery_cp = 0; |
| 5049 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5050 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | |
| 5052 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5053 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | |
| 5055 | mddev->max_disks = MD_SB_DISKS; |
| 5056 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5057 | if (mddev->persistent) |
| 5058 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5059 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5060 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5061 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 5062 | mddev->bitmap_offset = 0; |
| 5063 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5064 | mddev->reshape_position = MaxSector; |
| 5065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | /* |
| 5067 | * Generate a 128 bit UUID |
| 5068 | */ |
| 5069 | get_random_bytes(mddev->uuid, 16); |
| 5070 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5071 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5072 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5073 | mddev->new_layout = mddev->layout; |
| 5074 | mddev->delta_disks = 0; |
| 5075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | return 0; |
| 5077 | } |
| 5078 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5079 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5080 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5081 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5082 | |
| 5083 | if (mddev->external_size) |
| 5084 | return; |
| 5085 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5086 | mddev->array_sectors = array_sectors; |
| 5087 | } |
| 5088 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5089 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5090 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5091 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5092 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5093 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5094 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5095 | |
| 5096 | if (mddev->pers->resize == NULL) |
| 5097 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5098 | /* The "num_sectors" is the number of sectors of each device that |
| 5099 | * is used. This can only make sense for arrays with redundancy. |
| 5100 | * linear and raid0 always use whatever space is available. We can only |
| 5101 | * consider changing this number if no resync or reconstruction is |
| 5102 | * happening, and if the new size is acceptable. It must fit before the |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5103 | * sb_start or, if that is <data_offset, it must fit before the size |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5104 | * of each device. If num_sectors is zero, we find the largest size |
| 5105 | * that fits. |
| 5106 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5107 | */ |
| 5108 | if (mddev->sync_thread) |
| 5109 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5110 | if (mddev->bitmap) |
| 5111 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5112 | * grow, and re-add. |
| 5113 | */ |
| 5114 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5115 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5116 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5117 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5118 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5119 | num_sectors = avail; |
| 5120 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5121 | return -ENOSPC; |
| 5122 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5123 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5124 | if (!rv) { |
| 5125 | struct block_device *bdev; |
| 5126 | |
| 5127 | bdev = bdget_disk(mddev->gendisk, 0); |
| 5128 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5129 | mutex_lock(&bdev->bd_inode->i_mutex); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5130 | i_size_write(bdev->bd_inode, |
| 5131 | (loff_t)mddev->array_sectors << 9); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5132 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5133 | bdput(bdev); |
| 5134 | } |
| 5135 | } |
| 5136 | return rv; |
| 5137 | } |
| 5138 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5139 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5140 | { |
| 5141 | int rv; |
| 5142 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5143 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5144 | return -EINVAL; |
| 5145 | if (raid_disks <= 0 || |
| 5146 | raid_disks >= mddev->max_disks) |
| 5147 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5148 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5149 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5150 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5151 | |
| 5152 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5153 | return rv; |
| 5154 | } |
| 5155 | |
| 5156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | /* |
| 5158 | * update_array_info is used to change the configuration of an |
| 5159 | * on-line array. |
| 5160 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5161 | * fields in the info are checked against the array. |
| 5162 | * Any differences that cannot be handled will cause an error. |
| 5163 | * Normally, only one change can be managed at a time. |
| 5164 | */ |
| 5165 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5166 | { |
| 5167 | int rv = 0; |
| 5168 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5169 | int state = 0; |
| 5170 | |
| 5171 | /* calculate expected state,ignoring low bits */ |
| 5172 | if (mddev->bitmap && mddev->bitmap_offset) |
| 5173 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | |
| 5175 | if (mddev->major_version != info->major_version || |
| 5176 | mddev->minor_version != info->minor_version || |
| 5177 | /* mddev->patch_version != info->patch_version || */ |
| 5178 | mddev->ctime != info->ctime || |
| 5179 | mddev->level != info->level || |
| 5180 | /* mddev->layout != info->layout || */ |
| 5181 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5182 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5183 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5184 | ((state^info->state) & 0xfffffe00) |
| 5185 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | return -EINVAL; |
| 5187 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5188 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5189 | cnt++; |
| 5190 | if (mddev->raid_disks != info->raid_disks) |
| 5191 | cnt++; |
| 5192 | if (mddev->layout != info->layout) |
| 5193 | cnt++; |
| 5194 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5195 | cnt++; |
| 5196 | if (cnt == 0) |
| 5197 | return 0; |
| 5198 | if (cnt > 1) |
| 5199 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5200 | |
| 5201 | if (mddev->layout != info->layout) { |
| 5202 | /* Change layout |
| 5203 | * we don't need to do anything at the md level, the |
| 5204 | * personality will take care of it all. |
| 5205 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5206 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5208 | else { |
| 5209 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5210 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5211 | if (rv) |
| 5212 | mddev->new_layout = mddev->layout; |
| 5213 | return rv; |
| 5214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5216 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5217 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5219 | if (mddev->raid_disks != info->raid_disks) |
| 5220 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5221 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5222 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5223 | if (mddev->pers->quiesce == NULL) |
| 5224 | return -EINVAL; |
| 5225 | if (mddev->recovery || mddev->sync_thread) |
| 5226 | return -EBUSY; |
| 5227 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5228 | /* add the bitmap */ |
| 5229 | if (mddev->bitmap) |
| 5230 | return -EEXIST; |
| 5231 | if (mddev->default_bitmap_offset == 0) |
| 5232 | return -EINVAL; |
| 5233 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 5234 | mddev->pers->quiesce(mddev, 1); |
| 5235 | rv = bitmap_create(mddev); |
| 5236 | if (rv) |
| 5237 | bitmap_destroy(mddev); |
| 5238 | mddev->pers->quiesce(mddev, 0); |
| 5239 | } else { |
| 5240 | /* remove the bitmap */ |
| 5241 | if (!mddev->bitmap) |
| 5242 | return -ENOENT; |
| 5243 | if (mddev->bitmap->file) |
| 5244 | return -EINVAL; |
| 5245 | mddev->pers->quiesce(mddev, 1); |
| 5246 | bitmap_destroy(mddev); |
| 5247 | mddev->pers->quiesce(mddev, 0); |
| 5248 | mddev->bitmap_offset = 0; |
| 5249 | } |
| 5250 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5251 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | return rv; |
| 5253 | } |
| 5254 | |
| 5255 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5256 | { |
| 5257 | mdk_rdev_t *rdev; |
| 5258 | |
| 5259 | if (mddev->pers == NULL) |
| 5260 | return -ENODEV; |
| 5261 | |
| 5262 | rdev = find_rdev(mddev, dev); |
| 5263 | if (!rdev) |
| 5264 | return -ENODEV; |
| 5265 | |
| 5266 | md_error(mddev, rdev); |
| 5267 | return 0; |
| 5268 | } |
| 5269 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5270 | /* |
| 5271 | * We have a problem here : there is no easy way to give a CHS |
| 5272 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5273 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5274 | * dosfs just mad... ;-) |
| 5275 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5276 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5277 | { |
| 5278 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5279 | |
| 5280 | geo->heads = 2; |
| 5281 | geo->sectors = 4; |
| 5282 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5283 | return 0; |
| 5284 | } |
| 5285 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5286 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | unsigned int cmd, unsigned long arg) |
| 5288 | { |
| 5289 | int err = 0; |
| 5290 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | mddev_t *mddev = NULL; |
| 5292 | |
| 5293 | if (!capable(CAP_SYS_ADMIN)) |
| 5294 | return -EACCES; |
| 5295 | |
| 5296 | /* |
| 5297 | * Commands dealing with the RAID driver but not any |
| 5298 | * particular array: |
| 5299 | */ |
| 5300 | switch (cmd) |
| 5301 | { |
| 5302 | case RAID_VERSION: |
| 5303 | err = get_version(argp); |
| 5304 | goto done; |
| 5305 | |
| 5306 | case PRINT_RAID_DEBUG: |
| 5307 | err = 0; |
| 5308 | md_print_devices(); |
| 5309 | goto done; |
| 5310 | |
| 5311 | #ifndef MODULE |
| 5312 | case RAID_AUTORUN: |
| 5313 | err = 0; |
| 5314 | autostart_arrays(arg); |
| 5315 | goto done; |
| 5316 | #endif |
| 5317 | default:; |
| 5318 | } |
| 5319 | |
| 5320 | /* |
| 5321 | * Commands creating/starting a new array: |
| 5322 | */ |
| 5323 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5324 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | |
| 5326 | if (!mddev) { |
| 5327 | BUG(); |
| 5328 | goto abort; |
| 5329 | } |
| 5330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | err = mddev_lock(mddev); |
| 5332 | if (err) { |
| 5333 | printk(KERN_INFO |
| 5334 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5335 | err, cmd); |
| 5336 | goto abort; |
| 5337 | } |
| 5338 | |
| 5339 | switch (cmd) |
| 5340 | { |
| 5341 | case SET_ARRAY_INFO: |
| 5342 | { |
| 5343 | mdu_array_info_t info; |
| 5344 | if (!arg) |
| 5345 | memset(&info, 0, sizeof(info)); |
| 5346 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5347 | err = -EFAULT; |
| 5348 | goto abort_unlock; |
| 5349 | } |
| 5350 | if (mddev->pers) { |
| 5351 | err = update_array_info(mddev, &info); |
| 5352 | if (err) { |
| 5353 | printk(KERN_WARNING "md: couldn't update" |
| 5354 | " array info. %d\n", err); |
| 5355 | goto abort_unlock; |
| 5356 | } |
| 5357 | goto done_unlock; |
| 5358 | } |
| 5359 | if (!list_empty(&mddev->disks)) { |
| 5360 | printk(KERN_WARNING |
| 5361 | "md: array %s already has disks!\n", |
| 5362 | mdname(mddev)); |
| 5363 | err = -EBUSY; |
| 5364 | goto abort_unlock; |
| 5365 | } |
| 5366 | if (mddev->raid_disks) { |
| 5367 | printk(KERN_WARNING |
| 5368 | "md: array %s already initialised!\n", |
| 5369 | mdname(mddev)); |
| 5370 | err = -EBUSY; |
| 5371 | goto abort_unlock; |
| 5372 | } |
| 5373 | err = set_array_info(mddev, &info); |
| 5374 | if (err) { |
| 5375 | printk(KERN_WARNING "md: couldn't set" |
| 5376 | " array info. %d\n", err); |
| 5377 | goto abort_unlock; |
| 5378 | } |
| 5379 | } |
| 5380 | goto done_unlock; |
| 5381 | |
| 5382 | default:; |
| 5383 | } |
| 5384 | |
| 5385 | /* |
| 5386 | * Commands querying/configuring an existing array: |
| 5387 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5388 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5389 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5390 | if ((!mddev->raid_disks && !mddev->external) |
| 5391 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5392 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5393 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | err = -ENODEV; |
| 5395 | goto abort_unlock; |
| 5396 | } |
| 5397 | |
| 5398 | /* |
| 5399 | * Commands even a read-only array can execute: |
| 5400 | */ |
| 5401 | switch (cmd) |
| 5402 | { |
| 5403 | case GET_ARRAY_INFO: |
| 5404 | err = get_array_info(mddev, argp); |
| 5405 | goto done_unlock; |
| 5406 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5407 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5408 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5409 | goto done_unlock; |
| 5410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | case GET_DISK_INFO: |
| 5412 | err = get_disk_info(mddev, argp); |
| 5413 | goto done_unlock; |
| 5414 | |
| 5415 | case RESTART_ARRAY_RW: |
| 5416 | err = restart_array(mddev); |
| 5417 | goto done_unlock; |
| 5418 | |
| 5419 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5420 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | goto done_unlock; |
| 5422 | |
| 5423 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5424 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | goto done_unlock; |
| 5426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | /* |
| 5430 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5431 | * superblock, so we do not allow them on read-only arrays. |
| 5432 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5433 | * here and hit the 'default' below, so only disallow |
| 5434 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5435 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5436 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5437 | if (mddev->ro == 2) { |
| 5438 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5439 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5440 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5441 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5442 | } else { |
| 5443 | err = -EROFS; |
| 5444 | goto abort_unlock; |
| 5445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | switch (cmd) |
| 5449 | { |
| 5450 | case ADD_NEW_DISK: |
| 5451 | { |
| 5452 | mdu_disk_info_t info; |
| 5453 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5454 | err = -EFAULT; |
| 5455 | else |
| 5456 | err = add_new_disk(mddev, &info); |
| 5457 | goto done_unlock; |
| 5458 | } |
| 5459 | |
| 5460 | case HOT_REMOVE_DISK: |
| 5461 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5462 | goto done_unlock; |
| 5463 | |
| 5464 | case HOT_ADD_DISK: |
| 5465 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5466 | goto done_unlock; |
| 5467 | |
| 5468 | case SET_DISK_FAULTY: |
| 5469 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5470 | goto done_unlock; |
| 5471 | |
| 5472 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5473 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | goto done_unlock; |
| 5475 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5476 | case SET_BITMAP_FILE: |
| 5477 | err = set_bitmap_file(mddev, (int)arg); |
| 5478 | goto done_unlock; |
| 5479 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | err = -EINVAL; |
| 5482 | goto abort_unlock; |
| 5483 | } |
| 5484 | |
| 5485 | done_unlock: |
| 5486 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5487 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5488 | err != -EINVAL) |
| 5489 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | mddev_unlock(mddev); |
| 5491 | |
| 5492 | return err; |
| 5493 | done: |
| 5494 | if (err) |
| 5495 | MD_BUG(); |
| 5496 | abort: |
| 5497 | return err; |
| 5498 | } |
| 5499 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5500 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | { |
| 5502 | /* |
| 5503 | * Succeed if we can lock the mddev, which confirms that |
| 5504 | * it isn't being stopped right now. |
| 5505 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5506 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5507 | int err; |
| 5508 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5509 | if (mddev->gendisk != bdev->bd_disk) { |
| 5510 | /* we are racing with mddev_put which is discarding this |
| 5511 | * bd_disk. |
| 5512 | */ |
| 5513 | mddev_put(mddev); |
| 5514 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5515 | flush_scheduled_work(); |
| 5516 | /* Then retry the open from the top */ |
| 5517 | return -ERESTARTSYS; |
| 5518 | } |
| 5519 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5520 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5521 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | goto out; |
| 5523 | |
| 5524 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5525 | atomic_inc(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | mddev_unlock(mddev); |
| 5527 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5528 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | out: |
| 5530 | return err; |
| 5531 | } |
| 5532 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5533 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5535 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5537 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5538 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | mddev_put(mddev); |
| 5540 | |
| 5541 | return 0; |
| 5542 | } |
| 5543 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5544 | static int md_media_changed(struct gendisk *disk) |
| 5545 | { |
| 5546 | mddev_t *mddev = disk->private_data; |
| 5547 | |
| 5548 | return mddev->changed; |
| 5549 | } |
| 5550 | |
| 5551 | static int md_revalidate(struct gendisk *disk) |
| 5552 | { |
| 5553 | mddev_t *mddev = disk->private_data; |
| 5554 | |
| 5555 | mddev->changed = 0; |
| 5556 | return 0; |
| 5557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5558 | static struct block_device_operations md_fops = |
| 5559 | { |
| 5560 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5561 | .open = md_open, |
| 5562 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5563 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5564 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5565 | .media_changed = md_media_changed, |
| 5566 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | }; |
| 5568 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5569 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | { |
| 5571 | mdk_thread_t *thread = arg; |
| 5572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | /* |
| 5574 | * md_thread is a 'system-thread', it's priority should be very |
| 5575 | * high. We avoid resource deadlocks individually in each |
| 5576 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5577 | * the very same RT priority as kswapd, thus we will never get |
| 5578 | * into a priority inversion deadlock. |
| 5579 | * |
| 5580 | * we definitely have to have equal or higher priority than |
| 5581 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5582 | * many dirty RAID5 blocks. |
| 5583 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5585 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5586 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5588 | /* We need to wait INTERRUPTIBLE so that |
| 5589 | * we don't add to the load-average. |
| 5590 | * That means we need to be sure no signals are |
| 5591 | * pending |
| 5592 | */ |
| 5593 | if (signal_pending(current)) |
| 5594 | flush_signals(current); |
| 5595 | |
| 5596 | wait_event_interruptible_timeout |
| 5597 | (thread->wqueue, |
| 5598 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5599 | || kthread_should_stop(), |
| 5600 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | |
| 5602 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5603 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5604 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | return 0; |
| 5608 | } |
| 5609 | |
| 5610 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5611 | { |
| 5612 | if (thread) { |
| 5613 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5614 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5615 | wake_up(&thread->wqueue); |
| 5616 | } |
| 5617 | } |
| 5618 | |
| 5619 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5620 | const char *name) |
| 5621 | { |
| 5622 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5624 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | if (!thread) |
| 5626 | return NULL; |
| 5627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5628 | init_waitqueue_head(&thread->wqueue); |
| 5629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | thread->run = run; |
| 5631 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5632 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5633 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5634 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | kfree(thread); |
| 5636 | return NULL; |
| 5637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | return thread; |
| 5639 | } |
| 5640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | void md_unregister_thread(mdk_thread_t *thread) |
| 5642 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5643 | if (!thread) |
| 5644 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5645 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5646 | |
| 5647 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | kfree(thread); |
| 5649 | } |
| 5650 | |
| 5651 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5652 | { |
| 5653 | if (!mddev) { |
| 5654 | MD_BUG(); |
| 5655 | return; |
| 5656 | } |
| 5657 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5658 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5660 | |
| 5661 | if (mddev->external) |
| 5662 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5663 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5665 | mdname(mddev), |
| 5666 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5667 | __builtin_return_address(0),__builtin_return_address(1), |
| 5668 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5669 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5670 | if (!mddev->pers) |
| 5671 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5672 | if (!mddev->pers->error_handler) |
| 5673 | return; |
| 5674 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5675 | if (mddev->degraded) |
| 5676 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5677 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5679 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5680 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5681 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | /* seq_file implementation /proc/mdstat */ |
| 5685 | |
| 5686 | static void status_unused(struct seq_file *seq) |
| 5687 | { |
| 5688 | int i = 0; |
| 5689 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | |
| 5691 | seq_printf(seq, "unused devices: "); |
| 5692 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5693 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | char b[BDEVNAME_SIZE]; |
| 5695 | i++; |
| 5696 | seq_printf(seq, "%s ", |
| 5697 | bdevname(rdev->bdev,b)); |
| 5698 | } |
| 5699 | if (!i) |
| 5700 | seq_printf(seq, "<none>"); |
| 5701 | |
| 5702 | seq_printf(seq, "\n"); |
| 5703 | } |
| 5704 | |
| 5705 | |
| 5706 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5707 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5708 | sector_t max_sectors, resync, res; |
| 5709 | unsigned long dt, db; |
| 5710 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5711 | int scale; |
| 5712 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5714 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | |
| 5716 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5717 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5719 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | |
| 5721 | /* |
| 5722 | * Should not happen. |
| 5723 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5724 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | MD_BUG(); |
| 5726 | return; |
| 5727 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5728 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5729 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5730 | * u32, as those are the requirements for sector_div. |
| 5731 | * Thus 'scale' must be at least 10 |
| 5732 | */ |
| 5733 | scale = 10; |
| 5734 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5735 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5736 | scale++; |
| 5737 | } |
| 5738 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5739 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5740 | |
| 5741 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5743 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | seq_printf(seq, "["); |
| 5745 | for (i = 0; i < x; i++) |
| 5746 | seq_printf(seq, "="); |
| 5747 | seq_printf(seq, ">"); |
| 5748 | for (i = 0; i < y; i++) |
| 5749 | seq_printf(seq, "."); |
| 5750 | seq_printf(seq, "] "); |
| 5751 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5752 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5753 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5754 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5755 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5756 | "check" : |
| 5757 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5758 | "resync" : "recovery"))), |
| 5759 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5760 | (unsigned long long) resync/2, |
| 5761 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | |
| 5763 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | * dt: time from mark until now |
| 5765 | * db: blocks written from mark until now |
| 5766 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5767 | * |
| 5768 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5769 | * So we divide before multiply in case it is 32bit and close |
| 5770 | * to the limit. |
| 5771 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5772 | * near the end of resync when the number of remaining sectors |
| 5773 | * is close to 'db'. |
| 5774 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5775 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | */ |
| 5777 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5778 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5779 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5780 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5782 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5783 | sector_div(rt, db/32+1); |
| 5784 | rt *= dt; |
| 5785 | rt >>= 5; |
| 5786 | |
| 5787 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5788 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5790 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | } |
| 5792 | |
| 5793 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5794 | { |
| 5795 | struct list_head *tmp; |
| 5796 | loff_t l = *pos; |
| 5797 | mddev_t *mddev; |
| 5798 | |
| 5799 | if (l >= 0x10000) |
| 5800 | return NULL; |
| 5801 | if (!l--) |
| 5802 | /* header */ |
| 5803 | return (void*)1; |
| 5804 | |
| 5805 | spin_lock(&all_mddevs_lock); |
| 5806 | list_for_each(tmp,&all_mddevs) |
| 5807 | if (!l--) { |
| 5808 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5809 | mddev_get(mddev); |
| 5810 | spin_unlock(&all_mddevs_lock); |
| 5811 | return mddev; |
| 5812 | } |
| 5813 | spin_unlock(&all_mddevs_lock); |
| 5814 | if (!l--) |
| 5815 | return (void*)2;/* tail */ |
| 5816 | return NULL; |
| 5817 | } |
| 5818 | |
| 5819 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5820 | { |
| 5821 | struct list_head *tmp; |
| 5822 | mddev_t *next_mddev, *mddev = v; |
| 5823 | |
| 5824 | ++*pos; |
| 5825 | if (v == (void*)2) |
| 5826 | return NULL; |
| 5827 | |
| 5828 | spin_lock(&all_mddevs_lock); |
| 5829 | if (v == (void*)1) |
| 5830 | tmp = all_mddevs.next; |
| 5831 | else |
| 5832 | tmp = mddev->all_mddevs.next; |
| 5833 | if (tmp != &all_mddevs) |
| 5834 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5835 | else { |
| 5836 | next_mddev = (void*)2; |
| 5837 | *pos = 0x10000; |
| 5838 | } |
| 5839 | spin_unlock(&all_mddevs_lock); |
| 5840 | |
| 5841 | if (v != (void*)1) |
| 5842 | mddev_put(mddev); |
| 5843 | return next_mddev; |
| 5844 | |
| 5845 | } |
| 5846 | |
| 5847 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5848 | { |
| 5849 | mddev_t *mddev = v; |
| 5850 | |
| 5851 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5852 | mddev_put(mddev); |
| 5853 | } |
| 5854 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5855 | struct mdstat_info { |
| 5856 | int event; |
| 5857 | }; |
| 5858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5860 | { |
| 5861 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5862 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5864 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5865 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | |
| 5867 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5868 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | seq_printf(seq, "Personalities : "); |
| 5870 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5871 | list_for_each_entry(pers, &pers_list, list) |
| 5872 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | |
| 5874 | spin_unlock(&pers_lock); |
| 5875 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5876 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | return 0; |
| 5878 | } |
| 5879 | if (v == (void*)2) { |
| 5880 | status_unused(seq); |
| 5881 | return 0; |
| 5882 | } |
| 5883 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5884 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5888 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5889 | mddev->pers ? "" : "in"); |
| 5890 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5891 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5893 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5894 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | seq_printf(seq, " %s", mddev->pers->name); |
| 5896 | } |
| 5897 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5898 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5899 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | char b[BDEVNAME_SIZE]; |
| 5901 | seq_printf(seq, " %s[%d]", |
| 5902 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5903 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5904 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5905 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | seq_printf(seq, "(F)"); |
| 5907 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5908 | } else if (rdev->raid_disk < 0) |
| 5909 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5910 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | } |
| 5912 | |
| 5913 | if (!list_empty(&mddev->disks)) { |
| 5914 | if (mddev->pers) |
| 5915 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5916 | (unsigned long long) |
| 5917 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | else |
| 5919 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5920 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5922 | if (mddev->persistent) { |
| 5923 | if (mddev->major_version != 0 || |
| 5924 | mddev->minor_version != 90) { |
| 5925 | seq_printf(seq," super %d.%d", |
| 5926 | mddev->major_version, |
| 5927 | mddev->minor_version); |
| 5928 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5929 | } else if (mddev->external) |
| 5930 | seq_printf(seq, " super external:%s", |
| 5931 | mddev->metadata_type); |
| 5932 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5933 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | |
| 5935 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5936 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5938 | if (mddev->pers->sync_request) { |
| 5939 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5940 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5941 | seq_printf(seq, "\n "); |
| 5942 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5943 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5944 | else if (mddev->recovery_cp < MaxSector) |
| 5945 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5946 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5947 | } else |
| 5948 | seq_printf(seq, "\n "); |
| 5949 | |
| 5950 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5951 | unsigned long chunk_kb; |
| 5952 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5953 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5954 | chunk_kb = bitmap->chunksize >> 10; |
| 5955 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5956 | "%lu%s chunk", |
| 5957 | bitmap->pages - bitmap->missing_pages, |
| 5958 | bitmap->pages, |
| 5959 | (bitmap->pages - bitmap->missing_pages) |
| 5960 | << (PAGE_SHIFT - 10), |
| 5961 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5962 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5963 | if (bitmap->file) { |
| 5964 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5965 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5966 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5967 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5968 | seq_printf(seq, "\n"); |
| 5969 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | seq_printf(seq, "\n"); |
| 5973 | } |
| 5974 | mddev_unlock(mddev); |
| 5975 | |
| 5976 | return 0; |
| 5977 | } |
| 5978 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 5979 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | .start = md_seq_start, |
| 5981 | .next = md_seq_next, |
| 5982 | .stop = md_seq_stop, |
| 5983 | .show = md_seq_show, |
| 5984 | }; |
| 5985 | |
| 5986 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5987 | { |
| 5988 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5989 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5990 | if (mi == NULL) |
| 5991 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | |
| 5993 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5994 | if (error) |
| 5995 | kfree(mi); |
| 5996 | else { |
| 5997 | struct seq_file *p = file->private_data; |
| 5998 | p->private = mi; |
| 5999 | mi->event = atomic_read(&md_event_count); |
| 6000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | return error; |
| 6002 | } |
| 6003 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6004 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6005 | { |
| 6006 | struct seq_file *m = filp->private_data; |
| 6007 | struct mdstat_info *mi = m->private; |
| 6008 | int mask; |
| 6009 | |
| 6010 | poll_wait(filp, &md_event_waiters, wait); |
| 6011 | |
| 6012 | /* always allow read */ |
| 6013 | mask = POLLIN | POLLRDNORM; |
| 6014 | |
| 6015 | if (mi->event != atomic_read(&md_event_count)) |
| 6016 | mask |= POLLERR | POLLPRI; |
| 6017 | return mask; |
| 6018 | } |
| 6019 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6020 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6021 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | .open = md_seq_open, |
| 6023 | .read = seq_read, |
| 6024 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6025 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6026 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6027 | }; |
| 6028 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6029 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6031 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6032 | list_add_tail(&p->list, &pers_list); |
| 6033 | 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] | 6034 | spin_unlock(&pers_lock); |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6038 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6039 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6040 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6042 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | spin_unlock(&pers_lock); |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6047 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | { |
| 6049 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6050 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6051 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6052 | |
| 6053 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6054 | rcu_read_lock(); |
| 6055 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6057 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6058 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6059 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6060 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6061 | * as sync_io is counted when a request starts, and |
| 6062 | * disk_stats is counted when it completes. |
| 6063 | * So resync activity will cause curr_events to be smaller than |
| 6064 | * when there was no such activity. |
| 6065 | * non-sync IO will cause disk_stat to increase without |
| 6066 | * increasing sync_io so curr_events will (eventually) |
| 6067 | * be larger than it was before. Once it becomes |
| 6068 | * substantially larger, the test below will cause |
| 6069 | * the array to appear non-idle, and resync will slow |
| 6070 | * down. |
| 6071 | * If there is a lot of outstanding resync activity when |
| 6072 | * we set last_event to curr_events, then all that activity |
| 6073 | * completing might cause the array to appear non-idle |
| 6074 | * and resync will be slowed down even though there might |
| 6075 | * not have been non-resync activity. This will only |
| 6076 | * happen once though. 'last_events' will soon reflect |
| 6077 | * the state where there is little or no outstanding |
| 6078 | * resync requests, and further resync activity will |
| 6079 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6080 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6082 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | rdev->last_events = curr_events; |
| 6084 | idle = 0; |
| 6085 | } |
| 6086 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6087 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6088 | return idle; |
| 6089 | } |
| 6090 | |
| 6091 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6092 | { |
| 6093 | /* another "blocks" (512byte) blocks have been synced */ |
| 6094 | atomic_sub(blocks, &mddev->recovery_active); |
| 6095 | wake_up(&mddev->recovery_wait); |
| 6096 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6097 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | md_wakeup_thread(mddev->thread); |
| 6099 | // stop recovery, signal do_sync .... |
| 6100 | } |
| 6101 | } |
| 6102 | |
| 6103 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6104 | /* md_write_start(mddev, bi) |
| 6105 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6106 | * in superblock) before writing, schedule a superblock update |
| 6107 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6108 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6109 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6111 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6112 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6113 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6114 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6115 | BUG_ON(mddev->ro == 1); |
| 6116 | if (mddev->ro == 2) { |
| 6117 | /* need to switch to read/write */ |
| 6118 | mddev->ro = 0; |
| 6119 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6120 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6121 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6122 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6123 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6124 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6125 | if (mddev->safemode == 1) |
| 6126 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6127 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6128 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6129 | if (mddev->in_sync) { |
| 6130 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6131 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6132 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6133 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6134 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6135 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6136 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6137 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6138 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6139 | wait_event(mddev->sb_wait, |
| 6140 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6141 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | void md_write_end(mddev_t *mddev) |
| 6145 | { |
| 6146 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6147 | if (mddev->safemode == 2) |
| 6148 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6149 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6150 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6151 | } |
| 6152 | } |
| 6153 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6154 | /* md_allow_write(mddev) |
| 6155 | * Calling this ensures that the array is marked 'active' so that writes |
| 6156 | * may proceed without blocking. It is important to call this before |
| 6157 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6158 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6159 | * |
| 6160 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6161 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6162 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6163 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6164 | { |
| 6165 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6166 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6167 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6168 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6169 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6170 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6171 | |
| 6172 | spin_lock_irq(&mddev->write_lock); |
| 6173 | if (mddev->in_sync) { |
| 6174 | mddev->in_sync = 0; |
| 6175 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6176 | if (mddev->safemode_delay && |
| 6177 | mddev->safemode == 0) |
| 6178 | mddev->safemode = 1; |
| 6179 | spin_unlock_irq(&mddev->write_lock); |
| 6180 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6181 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6182 | } else |
| 6183 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6184 | |
| 6185 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6186 | return -EAGAIN; |
| 6187 | else |
| 6188 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6189 | } |
| 6190 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | #define SYNC_MARKS 10 |
| 6193 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6194 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | { |
| 6196 | mddev_t *mddev2; |
| 6197 | unsigned int currspeed = 0, |
| 6198 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6199 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | unsigned long mark[SYNC_MARKS]; |
| 6201 | sector_t mark_cnt[SYNC_MARKS]; |
| 6202 | int last_mark,m; |
| 6203 | struct list_head *tmp; |
| 6204 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6205 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6206 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6207 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | |
| 6209 | /* just incase thread restarts... */ |
| 6210 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6211 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6212 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6213 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6214 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6215 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6216 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6217 | desc = "data-check"; |
| 6218 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6219 | desc = "requested-resync"; |
| 6220 | else |
| 6221 | desc = "resync"; |
| 6222 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6223 | desc = "reshape"; |
| 6224 | else |
| 6225 | desc = "recovery"; |
| 6226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | /* we overload curr_resync somewhat here. |
| 6228 | * 0 == not engaged in resync at all |
| 6229 | * 2 == checking that there is no conflict with another sync |
| 6230 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6231 | * commense |
| 6232 | * other == active in resync - this many blocks |
| 6233 | * |
| 6234 | * Before starting a resync we must have set curr_resync to |
| 6235 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6236 | * less than ours. When we find one that is the same or higher |
| 6237 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6238 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6239 | * This will mean we have to start checking from the beginning again. |
| 6240 | * |
| 6241 | */ |
| 6242 | |
| 6243 | do { |
| 6244 | mddev->curr_resync = 2; |
| 6245 | |
| 6246 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6247 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6248 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | goto skip; |
| 6250 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6251 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6252 | if (mddev2 == mddev) |
| 6253 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6254 | if (!mddev->parallel_resync |
| 6255 | && mddev2->curr_resync |
| 6256 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | DEFINE_WAIT(wq); |
| 6258 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6259 | /* arbitrarily yield */ |
| 6260 | mddev->curr_resync = 1; |
| 6261 | wake_up(&resync_wait); |
| 6262 | } |
| 6263 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6264 | /* no need to wait here, we can wait the next |
| 6265 | * time 'round when curr_resync == 2 |
| 6266 | */ |
| 6267 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6268 | /* We need to wait 'interruptible' so as not to |
| 6269 | * contribute to the load average, and not to |
| 6270 | * be caught by 'softlockup' |
| 6271 | */ |
| 6272 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6273 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6274 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6275 | printk(KERN_INFO "md: delaying %s of %s" |
| 6276 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6278 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6280 | if (signal_pending(current)) |
| 6281 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | schedule(); |
| 6283 | finish_wait(&resync_wait, &wq); |
| 6284 | goto try_again; |
| 6285 | } |
| 6286 | finish_wait(&resync_wait, &wq); |
| 6287 | } |
| 6288 | } |
| 6289 | } while (mddev->curr_resync < 2); |
| 6290 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6291 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6292 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6294 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | */ |
| 6296 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6297 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6298 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6299 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6300 | j = mddev->resync_min; |
| 6301 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6302 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6303 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6304 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6305 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6306 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6307 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6308 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6309 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6310 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6311 | if (rdev->raid_disk >= 0 && |
| 6312 | !test_bit(Faulty, &rdev->flags) && |
| 6313 | !test_bit(In_sync, &rdev->flags) && |
| 6314 | rdev->recovery_offset < j) |
| 6315 | j = rdev->recovery_offset; |
| 6316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6318 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6319 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6320 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6321 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6322 | "(but not more than %d KB/sec) for %s.\n", |
| 6323 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6325 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6326 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6327 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6329 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6330 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6331 | } |
| 6332 | last_mark = 0; |
| 6333 | mddev->resync_mark = mark[last_mark]; |
| 6334 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6335 | |
| 6336 | /* |
| 6337 | * Tune reconstruction: |
| 6338 | */ |
| 6339 | window = 32*(PAGE_SIZE/512); |
| 6340 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6341 | window/2,(unsigned long long) max_sectors/2); |
| 6342 | |
| 6343 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | last_check = 0; |
| 6345 | |
| 6346 | if (j>2) { |
| 6347 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6348 | "md: resuming %s of %s from checkpoint.\n", |
| 6349 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6350 | mddev->curr_resync = j; |
| 6351 | } |
| 6352 | |
| 6353 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6354 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6355 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6356 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6357 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6358 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6359 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6360 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6361 | > (max_sectors >> 4)) || |
| 6362 | (j - mddev->curr_resync_completed)*2 |
| 6363 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6364 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6365 | /* time to update curr_resync_completed */ |
| 6366 | blk_unplug(mddev->queue); |
| 6367 | wait_event(mddev->recovery_wait, |
| 6368 | atomic_read(&mddev->recovery_active) == 0); |
| 6369 | mddev->curr_resync_completed = |
| 6370 | mddev->curr_resync; |
| 6371 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6372 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6373 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6374 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6375 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6376 | /* As this condition is controlled by user-space, |
| 6377 | * we can block indefinitely, so use '_interruptible' |
| 6378 | * to avoid triggering warnings. |
| 6379 | */ |
| 6380 | flush_signals(current); /* just in case */ |
| 6381 | wait_event_interruptible(mddev->recovery_wait, |
| 6382 | mddev->resync_max > j |
| 6383 | || kthread_should_stop()); |
| 6384 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6385 | |
| 6386 | if (kthread_should_stop()) |
| 6387 | goto interrupted; |
| 6388 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6389 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6390 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6391 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6392 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | goto out; |
| 6394 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6395 | |
| 6396 | if (!skipped) { /* actual IO requested */ |
| 6397 | io_sectors += sectors; |
| 6398 | atomic_add(sectors, &mddev->recovery_active); |
| 6399 | } |
| 6400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | j += sectors; |
| 6402 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6403 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6404 | if (last_check == 0) |
| 6405 | /* this is the earliers that rebuilt will be |
| 6406 | * visible in /proc/mdstat |
| 6407 | */ |
| 6408 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6409 | |
| 6410 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6411 | continue; |
| 6412 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6413 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6415 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | break; |
| 6417 | |
| 6418 | repeat: |
| 6419 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6420 | /* step marks */ |
| 6421 | int next = (last_mark+1) % SYNC_MARKS; |
| 6422 | |
| 6423 | mddev->resync_mark = mark[next]; |
| 6424 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6425 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6426 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | last_mark = next; |
| 6428 | } |
| 6429 | |
| 6430 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6431 | if (kthread_should_stop()) |
| 6432 | goto interrupted; |
| 6433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | |
| 6435 | /* |
| 6436 | * this loop exits only if either when we are slower than |
| 6437 | * the 'hard' speed limit, or the system was IO-idle for |
| 6438 | * a jiffy. |
| 6439 | * the system might be non-idle CPU-wise, but we only care |
| 6440 | * about not overloading the IO subsystem. (things like an |
| 6441 | * e2fsck being done on the RAID array should execute fast) |
| 6442 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6443 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | cond_resched(); |
| 6445 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6446 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6447 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6448 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6449 | if (currspeed > speed_min(mddev)) { |
| 6450 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6451 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6452 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | goto repeat; |
| 6454 | } |
| 6455 | } |
| 6456 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6457 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | /* |
| 6459 | * this also signals 'finished resyncing' to md_stop |
| 6460 | */ |
| 6461 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6462 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | |
| 6464 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6465 | |
| 6466 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6467 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6468 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6469 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6470 | mddev->curr_resync > 2) { |
| 6471 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6472 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6473 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6474 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6475 | "md: checkpointing %s of %s.\n", |
| 6476 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6477 | mddev->recovery_cp = mddev->curr_resync; |
| 6478 | } |
| 6479 | } else |
| 6480 | mddev->recovery_cp = MaxSector; |
| 6481 | } else { |
| 6482 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6483 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6484 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6485 | if (rdev->raid_disk >= 0 && |
| 6486 | !test_bit(Faulty, &rdev->flags) && |
| 6487 | !test_bit(In_sync, &rdev->flags) && |
| 6488 | rdev->recovery_offset < mddev->curr_resync) |
| 6489 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6492 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | skip: |
| 6495 | mddev->curr_resync = 0; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6496 | mddev->curr_resync_completed = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6497 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6498 | mddev->resync_max = MaxSector; |
| 6499 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | wake_up(&resync_wait); |
| 6501 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6502 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6503 | return; |
| 6504 | |
| 6505 | interrupted: |
| 6506 | /* |
| 6507 | * got a signal, exit. |
| 6508 | */ |
| 6509 | printk(KERN_INFO |
| 6510 | "md: md_do_sync() got signal ... exiting\n"); |
| 6511 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6512 | goto out; |
| 6513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6515 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6516 | |
| 6517 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6518 | static int remove_and_add_spares(mddev_t *mddev) |
| 6519 | { |
| 6520 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6521 | int spares = 0; |
| 6522 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6523 | mddev->curr_resync_completed = 0; |
| 6524 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6525 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6526 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6527 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6528 | (test_bit(Faulty, &rdev->flags) || |
| 6529 | ! test_bit(In_sync, &rdev->flags)) && |
| 6530 | atomic_read(&rdev->nr_pending)==0) { |
| 6531 | if (mddev->pers->hot_remove_disk( |
| 6532 | mddev, rdev->raid_disk)==0) { |
| 6533 | char nm[20]; |
| 6534 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6535 | sysfs_remove_link(&mddev->kobj, nm); |
| 6536 | rdev->raid_disk = -1; |
| 6537 | } |
| 6538 | } |
| 6539 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6540 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6541 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6542 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6543 | !test_bit(In_sync, &rdev->flags) && |
| 6544 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6545 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6546 | if (rdev->raid_disk < 0 |
| 6547 | && !test_bit(Faulty, &rdev->flags)) { |
| 6548 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6549 | if (mddev->pers-> |
| 6550 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6551 | char nm[20]; |
| 6552 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6553 | if (sysfs_create_link(&mddev->kobj, |
| 6554 | &rdev->kobj, nm)) |
| 6555 | printk(KERN_WARNING |
| 6556 | "md: cannot register " |
| 6557 | "%s for %s\n", |
| 6558 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6559 | spares++; |
| 6560 | md_new_event(mddev); |
| 6561 | } else |
| 6562 | break; |
| 6563 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6564 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6565 | } |
| 6566 | return spares; |
| 6567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | /* |
| 6569 | * This routine is regularly called by all per-raid-array threads to |
| 6570 | * deal with generic issues like resync and super-block update. |
| 6571 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6572 | * need this as they never do any recovery or update the superblock. |
| 6573 | * |
| 6574 | * It does not do any resync itself, but rather "forks" off other threads |
| 6575 | * to do that as needed. |
| 6576 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6577 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6578 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | * and wakeups up this thread which will reap the thread and finish up. |
| 6580 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6581 | * |
| 6582 | * The overall approach is: |
| 6583 | * 1/ if the superblock needs updating, update it. |
| 6584 | * 2/ If a recovery thread is running, don't do anything else. |
| 6585 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6586 | * 4/ If there are any faulty devices, remove them. |
| 6587 | * 5/ If array is degraded, try to add spares devices |
| 6588 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6589 | */ |
| 6590 | void md_check_recovery(mddev_t *mddev) |
| 6591 | { |
| 6592 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6593 | |
| 6594 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6595 | if (mddev->bitmap) |
| 6596 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | |
| 6598 | if (mddev->ro) |
| 6599 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6600 | |
| 6601 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6602 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6603 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6604 | mdname(mddev)); |
| 6605 | mddev->safemode = 2; |
| 6606 | } |
| 6607 | flush_signals(current); |
| 6608 | } |
| 6609 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6610 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6611 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6613 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6615 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6616 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6617 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6618 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6619 | )) |
| 6620 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6621 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6622 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6623 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6624 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6625 | if (mddev->ro) { |
| 6626 | /* Only thing we do on a ro array is remove |
| 6627 | * failed devices. |
| 6628 | */ |
| 6629 | remove_and_add_spares(mddev); |
| 6630 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6631 | goto unlock; |
| 6632 | } |
| 6633 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6634 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6635 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6636 | spin_lock_irq(&mddev->write_lock); |
| 6637 | if (mddev->safemode && |
| 6638 | !atomic_read(&mddev->writes_pending) && |
| 6639 | !mddev->in_sync && |
| 6640 | mddev->recovery_cp == MaxSector) { |
| 6641 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6642 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6643 | if (mddev->persistent) |
| 6644 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6645 | } |
| 6646 | if (mddev->safemode == 1) |
| 6647 | mddev->safemode = 0; |
| 6648 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6649 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6650 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6651 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6652 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6653 | if (mddev->flags) |
| 6654 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6655 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6656 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6657 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6658 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6659 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6661 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6662 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6663 | /* resync/recovery still happening */ |
| 6664 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6665 | goto unlock; |
| 6666 | } |
| 6667 | if (mddev->sync_thread) { |
| 6668 | /* resync has finished, collect result */ |
| 6669 | md_unregister_thread(mddev->sync_thread); |
| 6670 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6671 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6672 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6673 | /* success...*/ |
| 6674 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6675 | if (mddev->pers->spare_active(mddev)) |
| 6676 | sysfs_notify(&mddev->kobj, NULL, |
| 6677 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6679 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6680 | mddev->pers->finish_reshape) |
| 6681 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6682 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6683 | |
| 6684 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6685 | * information must be scrapped |
| 6686 | */ |
| 6687 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6688 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6689 | rdev->saved_raid_disk = -1; |
| 6690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6691 | mddev->recovery = 0; |
| 6692 | /* flag recovery needed just to double check */ |
| 6693 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6694 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6695 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6696 | goto unlock; |
| 6697 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6698 | /* Set RUNNING before clearing NEEDED to avoid |
| 6699 | * any transients in the value of "sync_action". |
| 6700 | */ |
| 6701 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6702 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6703 | /* Clear some bits that don't mean anything, but |
| 6704 | * might be left set |
| 6705 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6706 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6707 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6709 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6710 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | /* no recovery is running. |
| 6712 | * remove any failed drives, then |
| 6713 | * add spares if possible. |
| 6714 | * Spare are also removed and re-added, to allow |
| 6715 | * the personality to fail the re-add. |
| 6716 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6718 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6719 | if (mddev->pers->check_reshape == NULL || |
| 6720 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6721 | /* Cannot proceed */ |
| 6722 | goto unlock; |
| 6723 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6724 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6725 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6726 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6727 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6728 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6729 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6730 | } else if (mddev->recovery_cp < MaxSector) { |
| 6731 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6732 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6733 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6734 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6738 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6739 | /* We are adding a device or devices to an array |
| 6740 | * which has the bitmap stored on all devices. |
| 6741 | * So make sure all bitmap pages get written |
| 6742 | */ |
| 6743 | bitmap_write_all(mddev->bitmap); |
| 6744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6745 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6746 | mddev, |
| 6747 | "%s_resync"); |
| 6748 | if (!mddev->sync_thread) { |
| 6749 | printk(KERN_ERR "%s: could not start resync" |
| 6750 | " thread...\n", |
| 6751 | mdname(mddev)); |
| 6752 | /* leave the spares where they are, it shouldn't hurt */ |
| 6753 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6754 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6755 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6756 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6757 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | } |
| 6759 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6760 | if (!mddev->sync_thread) { |
| 6761 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6762 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6763 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6764 | if (mddev->sysfs_action) |
| 6765 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | mddev_unlock(mddev); |
| 6768 | } |
| 6769 | } |
| 6770 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6771 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6772 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6773 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6774 | wait_event_timeout(rdev->blocked_wait, |
| 6775 | !test_bit(Blocked, &rdev->flags), |
| 6776 | msecs_to_jiffies(5000)); |
| 6777 | rdev_dec_pending(rdev, mddev); |
| 6778 | } |
| 6779 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6780 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6781 | static int md_notify_reboot(struct notifier_block *this, |
| 6782 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | { |
| 6784 | struct list_head *tmp; |
| 6785 | mddev_t *mddev; |
| 6786 | |
| 6787 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6788 | |
| 6789 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6790 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6791 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6792 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6793 | /* Force a switch to readonly even array |
| 6794 | * appears to still be in use. Hence |
| 6795 | * the '100'. |
| 6796 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6797 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6798 | mddev_unlock(mddev); |
| 6799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6800 | /* |
| 6801 | * certain more exotic SCSI devices are known to be |
| 6802 | * volatile wrt too early system reboots. While the |
| 6803 | * right place to handle this issue is the given |
| 6804 | * driver, we do want to have a safe RAID driver ... |
| 6805 | */ |
| 6806 | mdelay(1000*1); |
| 6807 | } |
| 6808 | return NOTIFY_DONE; |
| 6809 | } |
| 6810 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6811 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | .notifier_call = md_notify_reboot, |
| 6813 | .next = NULL, |
| 6814 | .priority = INT_MAX, /* before any real devices */ |
| 6815 | }; |
| 6816 | |
| 6817 | static void md_geninit(void) |
| 6818 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6820 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6821 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | } |
| 6823 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6824 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6826 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6827 | return -1; |
| 6828 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6829 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | return -1; |
| 6831 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6832 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6833 | md_probe, NULL, NULL); |
| 6834 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | md_probe, NULL, NULL); |
| 6836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6838 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | |
| 6840 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6841 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | |
| 6845 | #ifndef MODULE |
| 6846 | |
| 6847 | /* |
| 6848 | * Searches all registered partitions for autorun RAID arrays |
| 6849 | * at boot time. |
| 6850 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6851 | |
| 6852 | static LIST_HEAD(all_detected_devices); |
| 6853 | struct detected_devices_node { |
| 6854 | struct list_head list; |
| 6855 | dev_t dev; |
| 6856 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | |
| 6858 | void md_autodetect_dev(dev_t dev) |
| 6859 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6860 | struct detected_devices_node *node_detected_dev; |
| 6861 | |
| 6862 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6863 | if (node_detected_dev) { |
| 6864 | node_detected_dev->dev = dev; |
| 6865 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6866 | } else { |
| 6867 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6868 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6870 | } |
| 6871 | |
| 6872 | |
| 6873 | static void autostart_arrays(int part) |
| 6874 | { |
| 6875 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6876 | struct detected_devices_node *node_detected_dev; |
| 6877 | dev_t dev; |
| 6878 | int i_scanned, i_passed; |
| 6879 | |
| 6880 | i_scanned = 0; |
| 6881 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | |
| 6883 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6884 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6885 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6886 | i_scanned++; |
| 6887 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6888 | struct detected_devices_node, list); |
| 6889 | list_del(&node_detected_dev->list); |
| 6890 | dev = node_detected_dev->dev; |
| 6891 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6892 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | if (IS_ERR(rdev)) |
| 6894 | continue; |
| 6895 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6896 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6897 | MD_BUG(); |
| 6898 | continue; |
| 6899 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6900 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6902 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6904 | |
| 6905 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6906 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | |
| 6908 | autorun_devices(part); |
| 6909 | } |
| 6910 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6911 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | |
| 6913 | static __exit void md_exit(void) |
| 6914 | { |
| 6915 | mddev_t *mddev; |
| 6916 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6917 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6918 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6919 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6921 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6922 | unregister_blkdev(mdp_major, "mdp"); |
| 6923 | unregister_reboot_notifier(&md_notifier); |
| 6924 | unregister_sysctl_table(raid_table_header); |
| 6925 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6926 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6927 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6928 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | } |
| 6930 | } |
| 6931 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6932 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | module_exit(md_exit) |
| 6934 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6935 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6936 | { |
| 6937 | return sprintf(buffer, "%d", start_readonly); |
| 6938 | } |
| 6939 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6940 | { |
| 6941 | char *e; |
| 6942 | int num = simple_strtoul(val, &e, 10); |
| 6943 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6944 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6945 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6946 | } |
| 6947 | return -EINVAL; |
| 6948 | } |
| 6949 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6950 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6951 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6952 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6953 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | EXPORT_SYMBOL(register_md_personality); |
| 6956 | EXPORT_SYMBOL(unregister_md_personality); |
| 6957 | EXPORT_SYMBOL(md_error); |
| 6958 | EXPORT_SYMBOL(md_done_sync); |
| 6959 | EXPORT_SYMBOL(md_write_start); |
| 6960 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6961 | EXPORT_SYMBOL(md_register_thread); |
| 6962 | EXPORT_SYMBOL(md_unregister_thread); |
| 6963 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | EXPORT_SYMBOL(md_check_recovery); |
| 6965 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6966 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6967 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |