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; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1311 | if (rdev->desc_nr < 0 || |
| 1312 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1313 | role = 0xffff; |
| 1314 | rdev->desc_nr = -1; |
| 1315 | } else |
| 1316 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | switch(role) { |
| 1318 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | break; |
| 1320 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1321 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | break; |
| 1323 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1324 | if ((le32_to_cpu(sb->feature_map) & |
| 1325 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1326 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1327 | else |
| 1328 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | rdev->raid_disk = role; |
| 1330 | break; |
| 1331 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1332 | if (sb->devflags & WriteMostly1) |
| 1333 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1334 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1335 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1341 | { |
| 1342 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | mdk_rdev_t *rdev2; |
| 1344 | int max_dev, i; |
| 1345 | /* make rdev->sb match mddev and rdev data. */ |
| 1346 | |
| 1347 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1348 | |
| 1349 | sb->feature_map = 0; |
| 1350 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1351 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1353 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1354 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1355 | |
| 1356 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1357 | sb->events = cpu_to_le64(mddev->events); |
| 1358 | if (mddev->in_sync) |
| 1359 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1360 | else |
| 1361 | sb->resync_offset = cpu_to_le64(0); |
| 1362 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1363 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1364 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1365 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1366 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1367 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1368 | sb->level = cpu_to_le32(mddev->level); |
| 1369 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1370 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1371 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1372 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1373 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1374 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1375 | |
| 1376 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1377 | !test_bit(In_sync, &rdev->flags)) { |
| 1378 | if (mddev->curr_resync_completed > rdev->recovery_offset) |
| 1379 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 1380 | if (rdev->recovery_offset > 0) { |
| 1381 | sb->feature_map |= |
| 1382 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1383 | sb->recovery_offset = |
| 1384 | cpu_to_le64(rdev->recovery_offset); |
| 1385 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1388 | if (mddev->reshape_position != MaxSector) { |
| 1389 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1390 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1391 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1392 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1393 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1394 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1395 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1398 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | if (rdev2->desc_nr+1 > max_dev) |
| 1400 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1401 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame^] | 1402 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1403 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1404 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame^] | 1405 | rdev->sb_size = max_dev * 2 + 256; |
| 1406 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1407 | if (rdev->sb_size & bmask) |
| 1408 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | for (i=0; i<max_dev;i++) |
| 1411 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1412 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1413 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1415 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1417 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1419 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1420 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | else |
| 1422 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1423 | } |
| 1424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1426 | } |
| 1427 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1428 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1429 | 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] | 1430 | { |
| 1431 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1432 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1433 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1434 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1435 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1436 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1437 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1438 | max_sectors -= rdev->data_offset; |
| 1439 | if (!num_sectors || num_sectors > max_sectors) |
| 1440 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1441 | } else if (rdev->mddev->bitmap_offset) { |
| 1442 | /* minor version 0 with bitmap we can't move */ |
| 1443 | return 0; |
| 1444 | } else { |
| 1445 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1446 | sector_t sb_start; |
| 1447 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1448 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1449 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1450 | if (!num_sectors || num_sectors > max_sectors) |
| 1451 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1452 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1453 | } |
| 1454 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1455 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1456 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1457 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1458 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1459 | rdev->sb_page); |
| 1460 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1461 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1464 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | [0] = { |
| 1466 | .name = "0.90.0", |
| 1467 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1468 | .load_super = super_90_load, |
| 1469 | .validate_super = super_90_validate, |
| 1470 | .sync_super = super_90_sync, |
| 1471 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | }, |
| 1473 | [1] = { |
| 1474 | .name = "md-1", |
| 1475 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1476 | .load_super = super_1_load, |
| 1477 | .validate_super = super_1_validate, |
| 1478 | .sync_super = super_1_sync, |
| 1479 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | }, |
| 1481 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
| 1483 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1484 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1485 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1487 | rcu_read_lock(); |
| 1488 | rdev_for_each_rcu(rdev, mddev1) |
| 1489 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1490 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1491 | rdev2->bdev->bd_contains) { |
| 1492 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1493 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1494 | } |
| 1495 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static LIST_HEAD(pending_raid_disks); |
| 1500 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1501 | /* |
| 1502 | * Try to register data integrity profile for an mddev |
| 1503 | * |
| 1504 | * This is called when an array is started and after a disk has been kicked |
| 1505 | * from the array. It only succeeds if all working and active component devices |
| 1506 | * are integrity capable with matching profiles. |
| 1507 | */ |
| 1508 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1509 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1510 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1511 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1512 | if (list_empty(&mddev->disks)) |
| 1513 | return 0; /* nothing to do */ |
| 1514 | if (blk_get_integrity(mddev->gendisk)) |
| 1515 | return 0; /* already registered */ |
| 1516 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1517 | /* skip spares and non-functional disks */ |
| 1518 | if (test_bit(Faulty, &rdev->flags)) |
| 1519 | continue; |
| 1520 | if (rdev->raid_disk < 0) |
| 1521 | continue; |
| 1522 | /* |
| 1523 | * If at least one rdev is not integrity capable, we can not |
| 1524 | * enable data integrity for the md device. |
| 1525 | */ |
| 1526 | if (!bdev_get_integrity(rdev->bdev)) |
| 1527 | return -EINVAL; |
| 1528 | if (!reference) { |
| 1529 | /* Use the first rdev as the reference */ |
| 1530 | reference = rdev; |
| 1531 | continue; |
| 1532 | } |
| 1533 | /* does this rdev's profile match the reference profile? */ |
| 1534 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1535 | rdev->bdev->bd_disk) < 0) |
| 1536 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1537 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1538 | /* |
| 1539 | * All component devices are integrity capable and have matching |
| 1540 | * profiles, register the common profile for the md device. |
| 1541 | */ |
| 1542 | if (blk_integrity_register(mddev->gendisk, |
| 1543 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1544 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1545 | mdname(mddev)); |
| 1546 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1547 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1548 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1549 | mdname(mddev)); |
| 1550 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1551 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1552 | EXPORT_SYMBOL(md_integrity_register); |
| 1553 | |
| 1554 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1555 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1556 | { |
| 1557 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1558 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1559 | |
| 1560 | if (!bi_mddev) /* nothing to do */ |
| 1561 | return; |
| 1562 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1563 | return; |
| 1564 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1565 | rdev->bdev->bd_disk) >= 0) |
| 1566 | return; |
| 1567 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1568 | blk_integrity_unregister(mddev->gendisk); |
| 1569 | } |
| 1570 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1573 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1574 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1575 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1576 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1577 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | |
| 1579 | if (rdev->mddev) { |
| 1580 | MD_BUG(); |
| 1581 | return -EINVAL; |
| 1582 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1583 | |
| 1584 | /* prevent duplicates */ |
| 1585 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1586 | return -EEXIST; |
| 1587 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1588 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1589 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1590 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1591 | if (mddev->pers) { |
| 1592 | /* Cannot change size, so fail |
| 1593 | * If mddev->level <= 0, then we don't care |
| 1594 | * about aligning sizes (e.g. linear) |
| 1595 | */ |
| 1596 | if (mddev->level > 0) |
| 1597 | return -ENOSPC; |
| 1598 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1599 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
| 1602 | /* Verify rdev->desc_nr is unique. |
| 1603 | * If it is -1, assign a free number, else |
| 1604 | * check number is not in use |
| 1605 | */ |
| 1606 | if (rdev->desc_nr < 0) { |
| 1607 | int choice = 0; |
| 1608 | if (mddev->pers) choice = mddev->raid_disks; |
| 1609 | while (find_rdev_nr(mddev, choice)) |
| 1610 | choice++; |
| 1611 | rdev->desc_nr = choice; |
| 1612 | } else { |
| 1613 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1614 | return -EBUSY; |
| 1615 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1616 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1617 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1618 | mdname(mddev), mddev->max_disks); |
| 1619 | return -EBUSY; |
| 1620 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1621 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1622 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1623 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1626 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1627 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1628 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1629 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1630 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1631 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1632 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1633 | kobject_del(&rdev->kobj); |
| 1634 | goto fail; |
| 1635 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1636 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1637 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1638 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1639 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1640 | |
| 1641 | /* May as well allow recovery to be retried once */ |
| 1642 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1645 | |
| 1646 | fail: |
| 1647 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1648 | b, mdname(mddev)); |
| 1649 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1652 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1653 | { |
| 1654 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1655 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1656 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1660 | { |
| 1661 | char b[BDEVNAME_SIZE]; |
| 1662 | if (!rdev->mddev) { |
| 1663 | MD_BUG(); |
| 1664 | return; |
| 1665 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1666 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1667 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1669 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1670 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1671 | sysfs_put(rdev->sysfs_state); |
| 1672 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1673 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1674 | * writing to 'remove' to "dev/state". We also need |
| 1675 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1676 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1677 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1678 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1679 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1680 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* |
| 1684 | * prevent the device from being mounted, repartitioned or |
| 1685 | * otherwise reused by a RAID array (or any other kernel |
| 1686 | * subsystem), by bd_claiming the device. |
| 1687 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1688 | 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] | 1689 | { |
| 1690 | int err = 0; |
| 1691 | struct block_device *bdev; |
| 1692 | char b[BDEVNAME_SIZE]; |
| 1693 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1694 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | if (IS_ERR(bdev)) { |
| 1696 | printk(KERN_ERR "md: could not open %s.\n", |
| 1697 | __bdevname(dev, b)); |
| 1698 | return PTR_ERR(bdev); |
| 1699 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1700 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | if (err) { |
| 1702 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1703 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1704 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | return err; |
| 1706 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1707 | if (!shared) |
| 1708 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | rdev->bdev = bdev; |
| 1710 | return err; |
| 1711 | } |
| 1712 | |
| 1713 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1714 | { |
| 1715 | struct block_device *bdev = rdev->bdev; |
| 1716 | rdev->bdev = NULL; |
| 1717 | if (!bdev) |
| 1718 | MD_BUG(); |
| 1719 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1720 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | void md_autodetect_dev(dev_t dev); |
| 1724 | |
| 1725 | static void export_rdev(mdk_rdev_t * rdev) |
| 1726 | { |
| 1727 | char b[BDEVNAME_SIZE]; |
| 1728 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1729 | bdevname(rdev->bdev,b)); |
| 1730 | if (rdev->mddev) |
| 1731 | MD_BUG(); |
| 1732 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1734 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1735 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | #endif |
| 1737 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1738 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1742 | { |
| 1743 | unbind_rdev_from_array(rdev); |
| 1744 | export_rdev(rdev); |
| 1745 | } |
| 1746 | |
| 1747 | static void export_array(mddev_t *mddev) |
| 1748 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1749 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1751 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | if (!rdev->mddev) { |
| 1753 | MD_BUG(); |
| 1754 | continue; |
| 1755 | } |
| 1756 | kick_rdev_from_array(rdev); |
| 1757 | } |
| 1758 | if (!list_empty(&mddev->disks)) |
| 1759 | MD_BUG(); |
| 1760 | mddev->raid_disks = 0; |
| 1761 | mddev->major_version = 0; |
| 1762 | } |
| 1763 | |
| 1764 | static void print_desc(mdp_disk_t *desc) |
| 1765 | { |
| 1766 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1767 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1768 | } |
| 1769 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1770 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | { |
| 1772 | int i; |
| 1773 | |
| 1774 | printk(KERN_INFO |
| 1775 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1776 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1777 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1778 | sb->ctime); |
| 1779 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1780 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1781 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1782 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1783 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1784 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1785 | sb->failed_disks, sb->spare_disks, |
| 1786 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1787 | |
| 1788 | printk(KERN_INFO); |
| 1789 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1790 | mdp_disk_t *desc; |
| 1791 | |
| 1792 | desc = sb->disks + i; |
| 1793 | if (desc->number || desc->major || desc->minor || |
| 1794 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1795 | printk(" D %2d: ", i); |
| 1796 | print_desc(desc); |
| 1797 | } |
| 1798 | } |
| 1799 | printk(KERN_INFO "md: THIS: "); |
| 1800 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1803 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1804 | { |
| 1805 | __u8 *uuid; |
| 1806 | |
| 1807 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1808 | printk(KERN_INFO |
| 1809 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1810 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1811 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1812 | le32_to_cpu(sb->major_version), |
| 1813 | le32_to_cpu(sb->feature_map), |
| 1814 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1815 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1816 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1817 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1818 | sb->set_name, |
| 1819 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1820 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1821 | |
| 1822 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1823 | printk(KERN_INFO |
| 1824 | "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] | 1825 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1826 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1827 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1828 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1829 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1830 | le32_to_cpu(sb->level), |
| 1831 | (unsigned long long)le64_to_cpu(sb->size), |
| 1832 | le32_to_cpu(sb->raid_disks), |
| 1833 | le32_to_cpu(sb->layout), |
| 1834 | le32_to_cpu(sb->chunksize), |
| 1835 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1836 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1837 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1838 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1839 | le32_to_cpu(sb->dev_number), |
| 1840 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1841 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1842 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1843 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1844 | sb->devflags, |
| 1845 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1846 | (unsigned long long)le64_to_cpu(sb->events), |
| 1847 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1848 | le32_to_cpu(sb->sb_csum), |
| 1849 | le32_to_cpu(sb->max_dev) |
| 1850 | ); |
| 1851 | } |
| 1852 | |
| 1853 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | { |
| 1855 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1856 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1857 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1858 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1859 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1861 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1862 | switch (major_version) { |
| 1863 | case 0: |
| 1864 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1865 | break; |
| 1866 | case 1: |
| 1867 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1868 | break; |
| 1869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } else |
| 1871 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1872 | } |
| 1873 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1874 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1876 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | mdk_rdev_t *rdev; |
| 1878 | mddev_t *mddev; |
| 1879 | char b[BDEVNAME_SIZE]; |
| 1880 | |
| 1881 | printk("\n"); |
| 1882 | printk("md: **********************************\n"); |
| 1883 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1884 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1885 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1887 | if (mddev->bitmap) |
| 1888 | bitmap_print_sb(mddev->bitmap); |
| 1889 | else |
| 1890 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1891 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1893 | printk("\n"); |
| 1894 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1895 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1896 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | } |
| 1898 | printk("md: **********************************\n"); |
| 1899 | printk("\n"); |
| 1900 | } |
| 1901 | |
| 1902 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1903 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1905 | /* Update each superblock (in-memory image), but |
| 1906 | * if we are allowed to, skip spares which already |
| 1907 | * have the right event counter, or have one earlier |
| 1908 | * (which would mean they aren't being marked as dirty |
| 1909 | * with the rest of the array) |
| 1910 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1913 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1914 | if (rdev->sb_events == mddev->events || |
| 1915 | (nospares && |
| 1916 | rdev->raid_disk < 0 && |
| 1917 | (rdev->sb_events&1)==0 && |
| 1918 | rdev->sb_events+1 == mddev->events)) { |
| 1919 | /* Don't update this superblock */ |
| 1920 | rdev->sb_loaded = 2; |
| 1921 | } else { |
| 1922 | super_types[mddev->major_version]. |
| 1923 | sync_super(mddev, rdev); |
| 1924 | rdev->sb_loaded = 1; |
| 1925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1929 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1930 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1932 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1933 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 1935 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1936 | if (mddev->external) |
| 1937 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1939 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1940 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1941 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1942 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1943 | force_change = 1; |
| 1944 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1945 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1946 | * though if events isn't the right even/odd, we will have to do |
| 1947 | * spares after all |
| 1948 | */ |
| 1949 | nospares = 1; |
| 1950 | if (force_change) |
| 1951 | nospares = 0; |
| 1952 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1953 | /* If the array is degraded, then skipping spares is both |
| 1954 | * dangerous and fairly pointless. |
| 1955 | * Dangerous because a device that was removed from the array |
| 1956 | * might have a event_count that still looks up-to-date, |
| 1957 | * so it can be re-added without a resync. |
| 1958 | * Pointless because if there are any spares to skip, |
| 1959 | * then a recovery will happen and soon that array won't |
| 1960 | * be degraded any more and the spare can go back to sleep then. |
| 1961 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1962 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1963 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1964 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1965 | |
| 1966 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1967 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1968 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1969 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1970 | && (mddev->events & 1) |
| 1971 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1972 | mddev->events--; |
| 1973 | else { |
| 1974 | /* otherwise we have to go forward and ... */ |
| 1975 | mddev->events ++; |
| 1976 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1977 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1978 | if ((mddev->events&1)==0) { |
| 1979 | mddev->events++; |
| 1980 | nospares = 0; |
| 1981 | } |
| 1982 | } else { |
| 1983 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1984 | if ((mddev->events&1)) { |
| 1985 | mddev->events++; |
| 1986 | nospares = 0; |
| 1987 | } |
| 1988 | } |
| 1989 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | |
| 1991 | if (!mddev->events) { |
| 1992 | /* |
| 1993 | * oops, this 64-bit counter should never wrap. |
| 1994 | * Either we are in around ~1 trillion A.C., assuming |
| 1995 | * 1 reboot per second, or we have a bug: |
| 1996 | */ |
| 1997 | MD_BUG(); |
| 1998 | mddev->events --; |
| 1999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | |
| 2001 | /* |
| 2002 | * do not write anything to disk if using |
| 2003 | * nonpersistent superblocks |
| 2004 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2005 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2006 | if (!mddev->external) |
| 2007 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2008 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2009 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2010 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2012 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2013 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2014 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | |
| 2016 | dprintk(KERN_INFO |
| 2017 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2018 | mdname(mddev),mddev->in_sync); |
| 2019 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2020 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2021 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | char b[BDEVNAME_SIZE]; |
| 2023 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2024 | if (rdev->sb_loaded != 1) |
| 2025 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2026 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | dprintk("(skipping faulty "); |
| 2028 | |
| 2029 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2030 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2031 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2032 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2033 | rdev->sb_page); |
| 2034 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2035 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2036 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2037 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | } else |
| 2040 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2041 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | /* only need to write one superblock... */ |
| 2043 | break; |
| 2044 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2045 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2046 | /* 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] | 2047 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2048 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2049 | if (mddev->in_sync != sync_req || |
| 2050 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2051 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2052 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2053 | goto repeat; |
| 2054 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2055 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2056 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2057 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2058 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2059 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2063 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2064 | * We want to accept with case. For this we use cmd_match. |
| 2065 | */ |
| 2066 | static int cmd_match(const char *cmd, const char *str) |
| 2067 | { |
| 2068 | /* See if cmd, written into a sysfs file, matches |
| 2069 | * str. They must either be the same, or cmd can |
| 2070 | * have a trailing newline |
| 2071 | */ |
| 2072 | while (*cmd && *str && *cmd == *str) { |
| 2073 | cmd++; |
| 2074 | str++; |
| 2075 | } |
| 2076 | if (*cmd == '\n') |
| 2077 | cmd++; |
| 2078 | if (*str || *cmd) |
| 2079 | return 0; |
| 2080 | return 1; |
| 2081 | } |
| 2082 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2083 | struct rdev_sysfs_entry { |
| 2084 | struct attribute attr; |
| 2085 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2086 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2087 | }; |
| 2088 | |
| 2089 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2090 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2091 | { |
| 2092 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2093 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2094 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2095 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2096 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2097 | sep = ","; |
| 2098 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2099 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2100 | len += sprintf(page+len, "%sin_sync",sep); |
| 2101 | sep = ","; |
| 2102 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2103 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2104 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2105 | sep = ","; |
| 2106 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2107 | if (test_bit(Blocked, &rdev->flags)) { |
| 2108 | len += sprintf(page+len, "%sblocked", sep); |
| 2109 | sep = ","; |
| 2110 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2111 | if (!test_bit(Faulty, &rdev->flags) && |
| 2112 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2113 | len += sprintf(page+len, "%sspare", sep); |
| 2114 | sep = ","; |
| 2115 | } |
| 2116 | return len+sprintf(page+len, "\n"); |
| 2117 | } |
| 2118 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2119 | static ssize_t |
| 2120 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2121 | { |
| 2122 | /* can write |
| 2123 | * faulty - simulates and error |
| 2124 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2125 | * writemostly - sets write_mostly |
| 2126 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2127 | * blocked - sets the Blocked flag |
| 2128 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2129 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2130 | */ |
| 2131 | int err = -EINVAL; |
| 2132 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2133 | md_error(rdev->mddev, rdev); |
| 2134 | err = 0; |
| 2135 | } else if (cmd_match(buf, "remove")) { |
| 2136 | if (rdev->raid_disk >= 0) |
| 2137 | err = -EBUSY; |
| 2138 | else { |
| 2139 | mddev_t *mddev = rdev->mddev; |
| 2140 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2141 | if (mddev->pers) |
| 2142 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2143 | md_new_event(mddev); |
| 2144 | err = 0; |
| 2145 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2146 | } else if (cmd_match(buf, "writemostly")) { |
| 2147 | set_bit(WriteMostly, &rdev->flags); |
| 2148 | err = 0; |
| 2149 | } else if (cmd_match(buf, "-writemostly")) { |
| 2150 | clear_bit(WriteMostly, &rdev->flags); |
| 2151 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2152 | } else if (cmd_match(buf, "blocked")) { |
| 2153 | set_bit(Blocked, &rdev->flags); |
| 2154 | err = 0; |
| 2155 | } else if (cmd_match(buf, "-blocked")) { |
| 2156 | clear_bit(Blocked, &rdev->flags); |
| 2157 | wake_up(&rdev->blocked_wait); |
| 2158 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2159 | md_wakeup_thread(rdev->mddev->thread); |
| 2160 | |
| 2161 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2162 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2163 | set_bit(In_sync, &rdev->flags); |
| 2164 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2165 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2166 | if (!err && rdev->sysfs_state) |
| 2167 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2168 | return err ? err : len; |
| 2169 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2170 | static struct rdev_sysfs_entry rdev_state = |
| 2171 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2172 | |
| 2173 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2174 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2175 | { |
| 2176 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2177 | } |
| 2178 | |
| 2179 | static ssize_t |
| 2180 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2181 | { |
| 2182 | char *e; |
| 2183 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2184 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2185 | atomic_set(&rdev->corrected_errors, n); |
| 2186 | return len; |
| 2187 | } |
| 2188 | return -EINVAL; |
| 2189 | } |
| 2190 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2191 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2192 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2193 | static ssize_t |
| 2194 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2195 | { |
| 2196 | if (rdev->raid_disk < 0) |
| 2197 | return sprintf(page, "none\n"); |
| 2198 | else |
| 2199 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2200 | } |
| 2201 | |
| 2202 | static ssize_t |
| 2203 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2204 | { |
| 2205 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2206 | int err; |
| 2207 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2208 | int slot = simple_strtoul(buf, &e, 10); |
| 2209 | if (strncmp(buf, "none", 4)==0) |
| 2210 | slot = -1; |
| 2211 | else if (e==buf || (*e && *e!= '\n')) |
| 2212 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2213 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2214 | /* Setting 'slot' on an active array requires also |
| 2215 | * updating the 'rd%d' link, and communicating |
| 2216 | * with the personality with ->hot_*_disk. |
| 2217 | * For now we only support removing |
| 2218 | * failed/spare devices. This normally happens automatically, |
| 2219 | * but not when the metadata is externally managed. |
| 2220 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2221 | if (rdev->raid_disk == -1) |
| 2222 | return -EEXIST; |
| 2223 | /* personality does all needed checks */ |
| 2224 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2225 | return -EINVAL; |
| 2226 | err = rdev->mddev->pers-> |
| 2227 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2228 | if (err) |
| 2229 | return err; |
| 2230 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2231 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2232 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2233 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2234 | } else if (rdev->mddev->pers) { |
| 2235 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2236 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2237 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2238 | */ |
| 2239 | |
| 2240 | if (rdev->raid_disk != -1) |
| 2241 | return -EBUSY; |
| 2242 | |
| 2243 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2244 | return -EINVAL; |
| 2245 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2246 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2247 | if (rdev2->raid_disk == slot) |
| 2248 | return -EEXIST; |
| 2249 | |
| 2250 | rdev->raid_disk = slot; |
| 2251 | if (test_bit(In_sync, &rdev->flags)) |
| 2252 | rdev->saved_raid_disk = slot; |
| 2253 | else |
| 2254 | rdev->saved_raid_disk = -1; |
| 2255 | err = rdev->mddev->pers-> |
| 2256 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2257 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2258 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2259 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2260 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2261 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2262 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2263 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2264 | printk(KERN_WARNING |
| 2265 | "md: cannot register " |
| 2266 | "%s for %s\n", |
| 2267 | nm, mdname(rdev->mddev)); |
| 2268 | |
| 2269 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2270 | } else { |
| 2271 | if (slot >= rdev->mddev->raid_disks) |
| 2272 | return -ENOSPC; |
| 2273 | rdev->raid_disk = slot; |
| 2274 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2275 | clear_bit(Faulty, &rdev->flags); |
| 2276 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2277 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2278 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2279 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2280 | return len; |
| 2281 | } |
| 2282 | |
| 2283 | |
| 2284 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2285 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2286 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2287 | static ssize_t |
| 2288 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2289 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2290 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | static ssize_t |
| 2294 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2295 | { |
| 2296 | char *e; |
| 2297 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2298 | if (e==buf || (*e && *e != '\n')) |
| 2299 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2300 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2301 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2302 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2303 | /* Must set offset before size, so overlap checks |
| 2304 | * can be sane */ |
| 2305 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2306 | rdev->data_offset = offset; |
| 2307 | return len; |
| 2308 | } |
| 2309 | |
| 2310 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2311 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2312 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2313 | static ssize_t |
| 2314 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2315 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2316 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2317 | } |
| 2318 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2319 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2320 | { |
| 2321 | /* check if two start/length pairs overlap */ |
| 2322 | if (s1+l1 <= s2) |
| 2323 | return 0; |
| 2324 | if (s2+l2 <= s1) |
| 2325 | return 0; |
| 2326 | return 1; |
| 2327 | } |
| 2328 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2329 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2330 | { |
| 2331 | unsigned long long blocks; |
| 2332 | sector_t new; |
| 2333 | |
| 2334 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2335 | return -EINVAL; |
| 2336 | |
| 2337 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2338 | return -EINVAL; /* sector conversion overflow */ |
| 2339 | |
| 2340 | new = blocks * 2; |
| 2341 | if (new != blocks * 2) |
| 2342 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2343 | |
| 2344 | *sectors = new; |
| 2345 | return 0; |
| 2346 | } |
| 2347 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2348 | static ssize_t |
| 2349 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2350 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2351 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2352 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2353 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2354 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2355 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2356 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2357 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2358 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2359 | sectors = super_types[my_mddev->major_version]. |
| 2360 | rdev_size_change(rdev, sectors); |
| 2361 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2362 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2363 | } else if (!sectors) |
| 2364 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2365 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2366 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2367 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2368 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2369 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2370 | rdev->sectors = sectors; |
| 2371 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2372 | /* need to check that all other rdevs with the same ->bdev |
| 2373 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2374 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2375 | * we have to change it back, we will have the lock again. |
| 2376 | */ |
| 2377 | mddev_t *mddev; |
| 2378 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2379 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2380 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2381 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2382 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2383 | mdk_rdev_t *rdev2; |
| 2384 | |
| 2385 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2386 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2387 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2388 | (rdev->bdev == rdev2->bdev && |
| 2389 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2390 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2391 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2392 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2393 | overlap = 1; |
| 2394 | break; |
| 2395 | } |
| 2396 | mddev_unlock(mddev); |
| 2397 | if (overlap) { |
| 2398 | mddev_put(mddev); |
| 2399 | break; |
| 2400 | } |
| 2401 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2402 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2403 | if (overlap) { |
| 2404 | /* Someone else could have slipped in a size |
| 2405 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2406 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2407 | * safe, and trust userspace not to race with |
| 2408 | * itself |
| 2409 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2410 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2411 | return -EBUSY; |
| 2412 | } |
| 2413 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2414 | return len; |
| 2415 | } |
| 2416 | |
| 2417 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2418 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2419 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2420 | static struct attribute *rdev_default_attrs[] = { |
| 2421 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2422 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2423 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2424 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2425 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2426 | NULL, |
| 2427 | }; |
| 2428 | static ssize_t |
| 2429 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2430 | { |
| 2431 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2432 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2433 | mddev_t *mddev = rdev->mddev; |
| 2434 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2435 | |
| 2436 | if (!entry->show) |
| 2437 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2438 | |
| 2439 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2440 | if (!rv) { |
| 2441 | if (rdev->mddev == NULL) |
| 2442 | rv = -EBUSY; |
| 2443 | else |
| 2444 | rv = entry->show(rdev, page); |
| 2445 | mddev_unlock(mddev); |
| 2446 | } |
| 2447 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | static ssize_t |
| 2451 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2452 | const char *page, size_t length) |
| 2453 | { |
| 2454 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2455 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2456 | ssize_t rv; |
| 2457 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2458 | |
| 2459 | if (!entry->store) |
| 2460 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2461 | if (!capable(CAP_SYS_ADMIN)) |
| 2462 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2463 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2464 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2465 | if (rdev->mddev == NULL) |
| 2466 | rv = -EBUSY; |
| 2467 | else |
| 2468 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2469 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2470 | } |
| 2471 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | static void rdev_free(struct kobject *ko) |
| 2475 | { |
| 2476 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2477 | kfree(rdev); |
| 2478 | } |
| 2479 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2480 | .show = rdev_attr_show, |
| 2481 | .store = rdev_attr_store, |
| 2482 | }; |
| 2483 | static struct kobj_type rdev_ktype = { |
| 2484 | .release = rdev_free, |
| 2485 | .sysfs_ops = &rdev_sysfs_ops, |
| 2486 | .default_attrs = rdev_default_attrs, |
| 2487 | }; |
| 2488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | /* |
| 2490 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2491 | * |
| 2492 | * mark the device faulty if: |
| 2493 | * |
| 2494 | * - the device is nonexistent (zero size) |
| 2495 | * - the device has no valid superblock |
| 2496 | * |
| 2497 | * a faulty rdev _never_ has rdev->sb set. |
| 2498 | */ |
| 2499 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2500 | { |
| 2501 | char b[BDEVNAME_SIZE]; |
| 2502 | int err; |
| 2503 | mdk_rdev_t *rdev; |
| 2504 | sector_t size; |
| 2505 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2506 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | if (!rdev) { |
| 2508 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2509 | return ERR_PTR(-ENOMEM); |
| 2510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | |
| 2512 | if ((err = alloc_disk_sb(rdev))) |
| 2513 | goto abort_free; |
| 2514 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2515 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | if (err) |
| 2517 | goto abort_free; |
| 2518 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2519 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2522 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2523 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2524 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2526 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2528 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2529 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | |
| 2531 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2532 | if (!size) { |
| 2533 | printk(KERN_WARNING |
| 2534 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2535 | bdevname(rdev->bdev,b)); |
| 2536 | err = -EINVAL; |
| 2537 | goto abort_free; |
| 2538 | } |
| 2539 | |
| 2540 | if (super_format >= 0) { |
| 2541 | err = super_types[super_format]. |
| 2542 | load_super(rdev, NULL, super_minor); |
| 2543 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2544 | printk(KERN_WARNING |
| 2545 | "md: %s does not have a valid v%d.%d " |
| 2546 | "superblock, not importing!\n", |
| 2547 | bdevname(rdev->bdev,b), |
| 2548 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | goto abort_free; |
| 2550 | } |
| 2551 | if (err < 0) { |
| 2552 | printk(KERN_WARNING |
| 2553 | "md: could not read %s's sb, not importing!\n", |
| 2554 | bdevname(rdev->bdev,b)); |
| 2555 | goto abort_free; |
| 2556 | } |
| 2557 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2560 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | |
| 2562 | return rdev; |
| 2563 | |
| 2564 | abort_free: |
| 2565 | if (rdev->sb_page) { |
| 2566 | if (rdev->bdev) |
| 2567 | unlock_rdev(rdev); |
| 2568 | free_disk_sb(rdev); |
| 2569 | } |
| 2570 | kfree(rdev); |
| 2571 | return ERR_PTR(err); |
| 2572 | } |
| 2573 | |
| 2574 | /* |
| 2575 | * Check a full RAID array for plausibility |
| 2576 | */ |
| 2577 | |
| 2578 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2579 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | { |
| 2581 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2582 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | char b[BDEVNAME_SIZE]; |
| 2584 | |
| 2585 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2586 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | switch (super_types[mddev->major_version]. |
| 2588 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2589 | case 1: |
| 2590 | freshest = rdev; |
| 2591 | break; |
| 2592 | case 0: |
| 2593 | break; |
| 2594 | default: |
| 2595 | printk( KERN_ERR \ |
| 2596 | "md: fatal superblock inconsistency in %s" |
| 2597 | " -- removing from array\n", |
| 2598 | bdevname(rdev->bdev,b)); |
| 2599 | kick_rdev_from_array(rdev); |
| 2600 | } |
| 2601 | |
| 2602 | |
| 2603 | super_types[mddev->major_version]. |
| 2604 | validate_super(mddev, freshest); |
| 2605 | |
| 2606 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2607 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2608 | if (rdev->desc_nr >= mddev->max_disks || |
| 2609 | i > mddev->max_disks) { |
| 2610 | printk(KERN_WARNING |
| 2611 | "md: %s: %s: only %d devices permitted\n", |
| 2612 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2613 | mddev->max_disks); |
| 2614 | kick_rdev_from_array(rdev); |
| 2615 | continue; |
| 2616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | if (rdev != freshest) |
| 2618 | if (super_types[mddev->major_version]. |
| 2619 | validate_super(mddev, rdev)) { |
| 2620 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2621 | " from array!\n", |
| 2622 | bdevname(rdev->bdev,b)); |
| 2623 | kick_rdev_from_array(rdev); |
| 2624 | continue; |
| 2625 | } |
| 2626 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2627 | rdev->desc_nr = i++; |
| 2628 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2629 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2630 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2631 | rdev->raid_disk = -1; |
| 2632 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | } |
| 2634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2637 | static void md_safemode_timeout(unsigned long data); |
| 2638 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2639 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2640 | safe_delay_show(mddev_t *mddev, char *page) |
| 2641 | { |
| 2642 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2643 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2644 | } |
| 2645 | static ssize_t |
| 2646 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2647 | { |
| 2648 | int scale=1; |
| 2649 | int dot=0; |
| 2650 | int i; |
| 2651 | unsigned long msec; |
| 2652 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2653 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2654 | /* remove a period, and count digits after it */ |
| 2655 | if (len >= sizeof(buf)) |
| 2656 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2657 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2658 | for (i=0; i<len; i++) { |
| 2659 | if (dot) { |
| 2660 | if (isdigit(buf[i])) { |
| 2661 | buf[i-1] = buf[i]; |
| 2662 | scale *= 10; |
| 2663 | } |
| 2664 | buf[i] = 0; |
| 2665 | } else if (buf[i] == '.') { |
| 2666 | dot=1; |
| 2667 | buf[i] = 0; |
| 2668 | } |
| 2669 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2670 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2671 | return -EINVAL; |
| 2672 | msec = (msec * 1000) / scale; |
| 2673 | if (msec == 0) |
| 2674 | mddev->safemode_delay = 0; |
| 2675 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2676 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2677 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2678 | if (mddev->safemode_delay == 0) |
| 2679 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2680 | if (mddev->safemode_delay < old_delay) |
| 2681 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2682 | } |
| 2683 | return len; |
| 2684 | } |
| 2685 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2686 | __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] | 2687 | |
| 2688 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2689 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2690 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2691 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2692 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2693 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2694 | else if (mddev->clevel[0]) |
| 2695 | return sprintf(page, "%s\n", mddev->clevel); |
| 2696 | else if (mddev->level != LEVEL_NONE) |
| 2697 | return sprintf(page, "%d\n", mddev->level); |
| 2698 | else |
| 2699 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2700 | } |
| 2701 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2702 | static ssize_t |
| 2703 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2704 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2705 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2706 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2707 | struct mdk_personality *pers; |
| 2708 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2709 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2710 | |
| 2711 | if (mddev->pers == NULL) { |
| 2712 | if (len == 0) |
| 2713 | return 0; |
| 2714 | if (len >= sizeof(mddev->clevel)) |
| 2715 | return -ENOSPC; |
| 2716 | strncpy(mddev->clevel, buf, len); |
| 2717 | if (mddev->clevel[len-1] == '\n') |
| 2718 | len--; |
| 2719 | mddev->clevel[len] = 0; |
| 2720 | mddev->level = LEVEL_NONE; |
| 2721 | return rv; |
| 2722 | } |
| 2723 | |
| 2724 | /* request to change the personality. Need to ensure: |
| 2725 | * - array is not engaged in resync/recovery/reshape |
| 2726 | * - old personality can be suspended |
| 2727 | * - new personality will access other array. |
| 2728 | */ |
| 2729 | |
| 2730 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2731 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2732 | |
| 2733 | if (!mddev->pers->quiesce) { |
| 2734 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2735 | mdname(mddev), mddev->pers->name); |
| 2736 | return -EINVAL; |
| 2737 | } |
| 2738 | |
| 2739 | /* Now find the new personality */ |
| 2740 | if (len == 0 || len >= sizeof(level)) |
| 2741 | return -EINVAL; |
| 2742 | strncpy(level, buf, len); |
| 2743 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2744 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2745 | level[len] = 0; |
| 2746 | |
| 2747 | request_module("md-%s", level); |
| 2748 | spin_lock(&pers_lock); |
| 2749 | pers = find_pers(LEVEL_NONE, level); |
| 2750 | if (!pers || !try_module_get(pers->owner)) { |
| 2751 | spin_unlock(&pers_lock); |
| 2752 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2753 | return -EINVAL; |
| 2754 | } |
| 2755 | spin_unlock(&pers_lock); |
| 2756 | |
| 2757 | if (pers == mddev->pers) { |
| 2758 | /* Nothing to do! */ |
| 2759 | module_put(pers->owner); |
| 2760 | return rv; |
| 2761 | } |
| 2762 | if (!pers->takeover) { |
| 2763 | module_put(pers->owner); |
| 2764 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2765 | mdname(mddev), level); |
| 2766 | return -EINVAL; |
| 2767 | } |
| 2768 | |
| 2769 | /* ->takeover must set new_* and/or delta_disks |
| 2770 | * if it succeeds, and may set them when it fails. |
| 2771 | */ |
| 2772 | priv = pers->takeover(mddev); |
| 2773 | if (IS_ERR(priv)) { |
| 2774 | mddev->new_level = mddev->level; |
| 2775 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2776 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2777 | mddev->raid_disks -= mddev->delta_disks; |
| 2778 | mddev->delta_disks = 0; |
| 2779 | module_put(pers->owner); |
| 2780 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2781 | mdname(mddev), level); |
| 2782 | return PTR_ERR(priv); |
| 2783 | } |
| 2784 | |
| 2785 | /* Looks like we have a winner */ |
| 2786 | mddev_suspend(mddev); |
| 2787 | mddev->pers->stop(mddev); |
| 2788 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2789 | /* Invalidate devices that are now superfluous */ |
| 2790 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2791 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2792 | rdev->raid_disk = -1; |
| 2793 | clear_bit(In_sync, &rdev->flags); |
| 2794 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2795 | mddev->pers = pers; |
| 2796 | mddev->private = priv; |
| 2797 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2798 | mddev->level = mddev->new_level; |
| 2799 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2800 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2801 | mddev->delta_disks = 0; |
| 2802 | pers->run(mddev); |
| 2803 | mddev_resume(mddev); |
| 2804 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2805 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2806 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2807 | return rv; |
| 2808 | } |
| 2809 | |
| 2810 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2811 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2812 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2813 | |
| 2814 | static ssize_t |
| 2815 | layout_show(mddev_t *mddev, char *page) |
| 2816 | { |
| 2817 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2818 | if (mddev->reshape_position != MaxSector && |
| 2819 | mddev->layout != mddev->new_layout) |
| 2820 | return sprintf(page, "%d (%d)\n", |
| 2821 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2822 | return sprintf(page, "%d\n", mddev->layout); |
| 2823 | } |
| 2824 | |
| 2825 | static ssize_t |
| 2826 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2827 | { |
| 2828 | char *e; |
| 2829 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2830 | |
| 2831 | if (!*buf || (*e && *e != '\n')) |
| 2832 | return -EINVAL; |
| 2833 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2834 | if (mddev->pers) { |
| 2835 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2836 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2837 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2838 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2839 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2840 | if (err) { |
| 2841 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2842 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2843 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2844 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2845 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2846 | if (mddev->reshape_position == MaxSector) |
| 2847 | mddev->layout = n; |
| 2848 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2849 | return len; |
| 2850 | } |
| 2851 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2852 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2853 | |
| 2854 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2855 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2856 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2857 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2858 | if (mddev->raid_disks == 0) |
| 2859 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2860 | if (mddev->reshape_position != MaxSector && |
| 2861 | mddev->delta_disks != 0) |
| 2862 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2863 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2864 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2865 | } |
| 2866 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2867 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2868 | |
| 2869 | static ssize_t |
| 2870 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2871 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2872 | char *e; |
| 2873 | int rv = 0; |
| 2874 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2875 | |
| 2876 | if (!*buf || (*e && *e != '\n')) |
| 2877 | return -EINVAL; |
| 2878 | |
| 2879 | if (mddev->pers) |
| 2880 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2881 | else if (mddev->reshape_position != MaxSector) { |
| 2882 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2883 | mddev->delta_disks = n - olddisks; |
| 2884 | mddev->raid_disks = n; |
| 2885 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2886 | mddev->raid_disks = n; |
| 2887 | return rv ? rv : len; |
| 2888 | } |
| 2889 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2890 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2891 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2892 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2893 | chunk_size_show(mddev_t *mddev, char *page) |
| 2894 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2895 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2896 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 2897 | return sprintf(page, "%d (%d)\n", |
| 2898 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2899 | mddev->chunk_sectors << 9); |
| 2900 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | static ssize_t |
| 2904 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2905 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2906 | char *e; |
| 2907 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2908 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2909 | if (!*buf || (*e && *e != '\n')) |
| 2910 | return -EINVAL; |
| 2911 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2912 | if (mddev->pers) { |
| 2913 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2914 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2915 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2916 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2917 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2918 | if (err) { |
| 2919 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2920 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2921 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2922 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2923 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2924 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2925 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2926 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2927 | return len; |
| 2928 | } |
| 2929 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2930 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2931 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2932 | static ssize_t |
| 2933 | resync_start_show(mddev_t *mddev, char *page) |
| 2934 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 2935 | if (mddev->recovery_cp == MaxSector) |
| 2936 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2937 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2938 | } |
| 2939 | |
| 2940 | static ssize_t |
| 2941 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2942 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2943 | char *e; |
| 2944 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2945 | |
| 2946 | if (mddev->pers) |
| 2947 | return -EBUSY; |
| 2948 | if (!*buf || (*e && *e != '\n')) |
| 2949 | return -EINVAL; |
| 2950 | |
| 2951 | mddev->recovery_cp = n; |
| 2952 | return len; |
| 2953 | } |
| 2954 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2955 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2956 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2957 | /* |
| 2958 | * The array state can be: |
| 2959 | * |
| 2960 | * clear |
| 2961 | * No devices, no size, no level |
| 2962 | * Equivalent to STOP_ARRAY ioctl |
| 2963 | * inactive |
| 2964 | * May have some settings, but array is not active |
| 2965 | * all IO results in error |
| 2966 | * When written, doesn't tear down array, but just stops it |
| 2967 | * suspended (not supported yet) |
| 2968 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2969 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2970 | * readonly |
| 2971 | * no resync can happen. no superblocks get written. |
| 2972 | * write requests fail |
| 2973 | * read-auto |
| 2974 | * like readonly, but behaves like 'clean' on a write request. |
| 2975 | * |
| 2976 | * clean - no pending writes, but otherwise active. |
| 2977 | * When written to inactive array, starts without resync |
| 2978 | * If a write request arrives then |
| 2979 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2980 | * if not known, block and switch to write-pending |
| 2981 | * If written to an active array that has pending writes, then fails. |
| 2982 | * active |
| 2983 | * fully active: IO and resync can be happening. |
| 2984 | * When written to inactive array, starts with resync |
| 2985 | * |
| 2986 | * write-pending |
| 2987 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2988 | * |
| 2989 | * active-idle |
| 2990 | * like active, but no writes have been seen for a while (100msec). |
| 2991 | * |
| 2992 | */ |
| 2993 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2994 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2995 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2996 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2997 | "write-pending", "active-idle", NULL }; |
| 2998 | |
| 2999 | static int match_word(const char *word, char **list) |
| 3000 | { |
| 3001 | int n; |
| 3002 | for (n=0; list[n]; n++) |
| 3003 | if (cmd_match(word, list[n])) |
| 3004 | break; |
| 3005 | return n; |
| 3006 | } |
| 3007 | |
| 3008 | static ssize_t |
| 3009 | array_state_show(mddev_t *mddev, char *page) |
| 3010 | { |
| 3011 | enum array_state st = inactive; |
| 3012 | |
| 3013 | if (mddev->pers) |
| 3014 | switch(mddev->ro) { |
| 3015 | case 1: |
| 3016 | st = readonly; |
| 3017 | break; |
| 3018 | case 2: |
| 3019 | st = read_auto; |
| 3020 | break; |
| 3021 | case 0: |
| 3022 | if (mddev->in_sync) |
| 3023 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3024 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3025 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3026 | else if (mddev->safemode) |
| 3027 | st = active_idle; |
| 3028 | else |
| 3029 | st = active; |
| 3030 | } |
| 3031 | else { |
| 3032 | if (list_empty(&mddev->disks) && |
| 3033 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3034 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3035 | st = clear; |
| 3036 | else |
| 3037 | st = inactive; |
| 3038 | } |
| 3039 | return sprintf(page, "%s\n", array_states[st]); |
| 3040 | } |
| 3041 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3042 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3043 | static int do_md_run(mddev_t * mddev); |
| 3044 | static int restart_array(mddev_t *mddev); |
| 3045 | |
| 3046 | static ssize_t |
| 3047 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3048 | { |
| 3049 | int err = -EINVAL; |
| 3050 | enum array_state st = match_word(buf, array_states); |
| 3051 | switch(st) { |
| 3052 | case bad_word: |
| 3053 | break; |
| 3054 | case clear: |
| 3055 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3056 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3057 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3058 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3059 | break; |
| 3060 | case inactive: |
| 3061 | /* stopping an active array */ |
| 3062 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3063 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3064 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3065 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3066 | } else |
| 3067 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3068 | break; |
| 3069 | case suspended: |
| 3070 | break; /* not supported yet */ |
| 3071 | case readonly: |
| 3072 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3073 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3074 | else { |
| 3075 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3076 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3077 | err = do_md_run(mddev); |
| 3078 | } |
| 3079 | break; |
| 3080 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3081 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3082 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3083 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3084 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3085 | err = restart_array(mddev); |
| 3086 | if (err == 0) { |
| 3087 | mddev->ro = 2; |
| 3088 | set_disk_ro(mddev->gendisk, 0); |
| 3089 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3090 | } else { |
| 3091 | mddev->ro = 2; |
| 3092 | err = do_md_run(mddev); |
| 3093 | } |
| 3094 | break; |
| 3095 | case clean: |
| 3096 | if (mddev->pers) { |
| 3097 | restart_array(mddev); |
| 3098 | spin_lock_irq(&mddev->write_lock); |
| 3099 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3100 | if (mddev->in_sync == 0) { |
| 3101 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3102 | if (mddev->safemode == 1) |
| 3103 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3104 | if (mddev->persistent) |
| 3105 | set_bit(MD_CHANGE_CLEAN, |
| 3106 | &mddev->flags); |
| 3107 | } |
| 3108 | err = 0; |
| 3109 | } else |
| 3110 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3111 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3112 | } else |
| 3113 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3114 | break; |
| 3115 | case active: |
| 3116 | if (mddev->pers) { |
| 3117 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3118 | if (mddev->external) |
| 3119 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3120 | wake_up(&mddev->sb_wait); |
| 3121 | err = 0; |
| 3122 | } else { |
| 3123 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3124 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3125 | err = do_md_run(mddev); |
| 3126 | } |
| 3127 | break; |
| 3128 | case write_pending: |
| 3129 | case active_idle: |
| 3130 | /* these cannot be set */ |
| 3131 | break; |
| 3132 | } |
| 3133 | if (err) |
| 3134 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3135 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3136 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3137 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3138 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3139 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3140 | static struct md_sysfs_entry md_array_state = |
| 3141 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3142 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3143 | static ssize_t |
| 3144 | null_show(mddev_t *mddev, char *page) |
| 3145 | { |
| 3146 | return -EINVAL; |
| 3147 | } |
| 3148 | |
| 3149 | static ssize_t |
| 3150 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3151 | { |
| 3152 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3153 | /* The new device is added to the array. |
| 3154 | * If the array has a persistent superblock, we read the |
| 3155 | * superblock to initialise info and check validity. |
| 3156 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3157 | * which mainly checks size. |
| 3158 | */ |
| 3159 | char *e; |
| 3160 | int major = simple_strtoul(buf, &e, 10); |
| 3161 | int minor; |
| 3162 | dev_t dev; |
| 3163 | mdk_rdev_t *rdev; |
| 3164 | int err; |
| 3165 | |
| 3166 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3167 | return -EINVAL; |
| 3168 | minor = simple_strtoul(e+1, &e, 10); |
| 3169 | if (*e && *e != '\n') |
| 3170 | return -EINVAL; |
| 3171 | dev = MKDEV(major, minor); |
| 3172 | if (major != MAJOR(dev) || |
| 3173 | minor != MINOR(dev)) |
| 3174 | return -EOVERFLOW; |
| 3175 | |
| 3176 | |
| 3177 | if (mddev->persistent) { |
| 3178 | rdev = md_import_device(dev, mddev->major_version, |
| 3179 | mddev->minor_version); |
| 3180 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3181 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3182 | mdk_rdev_t, same_set); |
| 3183 | err = super_types[mddev->major_version] |
| 3184 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3185 | if (err < 0) |
| 3186 | goto out; |
| 3187 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3188 | } else if (mddev->external) |
| 3189 | rdev = md_import_device(dev, -2, -1); |
| 3190 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3191 | rdev = md_import_device(dev, -1, -1); |
| 3192 | |
| 3193 | if (IS_ERR(rdev)) |
| 3194 | return PTR_ERR(rdev); |
| 3195 | err = bind_rdev_to_array(rdev, mddev); |
| 3196 | out: |
| 3197 | if (err) |
| 3198 | export_rdev(rdev); |
| 3199 | return err ? err : len; |
| 3200 | } |
| 3201 | |
| 3202 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3203 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3204 | |
| 3205 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3206 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3207 | { |
| 3208 | char *end; |
| 3209 | unsigned long chunk, end_chunk; |
| 3210 | |
| 3211 | if (!mddev->bitmap) |
| 3212 | goto out; |
| 3213 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3214 | while (*buf) { |
| 3215 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3216 | if (buf == end) break; |
| 3217 | if (*end == '-') { /* range */ |
| 3218 | buf = end + 1; |
| 3219 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3220 | if (buf == end) break; |
| 3221 | } |
| 3222 | if (*end && !isspace(*end)) break; |
| 3223 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3224 | buf = end; |
| 3225 | while (isspace(*buf)) buf++; |
| 3226 | } |
| 3227 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3228 | out: |
| 3229 | return len; |
| 3230 | } |
| 3231 | |
| 3232 | static struct md_sysfs_entry md_bitmap = |
| 3233 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3234 | |
| 3235 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3236 | size_show(mddev_t *mddev, char *page) |
| 3237 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3238 | return sprintf(page, "%llu\n", |
| 3239 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3240 | } |
| 3241 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3242 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3243 | |
| 3244 | static ssize_t |
| 3245 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3246 | { |
| 3247 | /* If array is inactive, we can reduce the component size, but |
| 3248 | * not increase it (except from 0). |
| 3249 | * If array is active, we can try an on-line resize |
| 3250 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3251 | sector_t sectors; |
| 3252 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3253 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3254 | if (err < 0) |
| 3255 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3256 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3257 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3258 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3259 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3260 | if (mddev->dev_sectors == 0 || |
| 3261 | mddev->dev_sectors > sectors) |
| 3262 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3263 | else |
| 3264 | err = -ENOSPC; |
| 3265 | } |
| 3266 | return err ? err : len; |
| 3267 | } |
| 3268 | |
| 3269 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3270 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3271 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3272 | |
| 3273 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3274 | * This is one of |
| 3275 | * 'none' for arrays with no metadata (good luck...) |
| 3276 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3277 | * or N.M for internally known formats |
| 3278 | */ |
| 3279 | static ssize_t |
| 3280 | metadata_show(mddev_t *mddev, char *page) |
| 3281 | { |
| 3282 | if (mddev->persistent) |
| 3283 | return sprintf(page, "%d.%d\n", |
| 3284 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3285 | else if (mddev->external) |
| 3286 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3287 | else |
| 3288 | return sprintf(page, "none\n"); |
| 3289 | } |
| 3290 | |
| 3291 | static ssize_t |
| 3292 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3293 | { |
| 3294 | int major, minor; |
| 3295 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3296 | /* Changing the details of 'external' metadata is |
| 3297 | * always permitted. Otherwise there must be |
| 3298 | * no devices attached to the array. |
| 3299 | */ |
| 3300 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3301 | ; |
| 3302 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3303 | return -EBUSY; |
| 3304 | |
| 3305 | if (cmd_match(buf, "none")) { |
| 3306 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3307 | mddev->external = 0; |
| 3308 | mddev->major_version = 0; |
| 3309 | mddev->minor_version = 90; |
| 3310 | return len; |
| 3311 | } |
| 3312 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3313 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3314 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3315 | namelen = sizeof(mddev->metadata_type)-1; |
| 3316 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3317 | mddev->metadata_type[namelen] = 0; |
| 3318 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3319 | mddev->metadata_type[--namelen] = 0; |
| 3320 | mddev->persistent = 0; |
| 3321 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3322 | mddev->major_version = 0; |
| 3323 | mddev->minor_version = 90; |
| 3324 | return len; |
| 3325 | } |
| 3326 | major = simple_strtoul(buf, &e, 10); |
| 3327 | if (e==buf || *e != '.') |
| 3328 | return -EINVAL; |
| 3329 | buf = e+1; |
| 3330 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3331 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3332 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3333 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3334 | return -ENOENT; |
| 3335 | mddev->major_version = major; |
| 3336 | mddev->minor_version = minor; |
| 3337 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3338 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3339 | return len; |
| 3340 | } |
| 3341 | |
| 3342 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3343 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3344 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3345 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3346 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3347 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3348 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3349 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3350 | type = "frozen"; |
| 3351 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3352 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3353 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3354 | type = "reshape"; |
| 3355 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3356 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3357 | type = "resync"; |
| 3358 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3359 | type = "check"; |
| 3360 | else |
| 3361 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3362 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3363 | type = "recover"; |
| 3364 | } |
| 3365 | return sprintf(page, "%s\n", type); |
| 3366 | } |
| 3367 | |
| 3368 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3369 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3370 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3371 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3372 | return -EINVAL; |
| 3373 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3374 | if (cmd_match(page, "frozen")) |
| 3375 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3376 | else |
| 3377 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3378 | |
| 3379 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3380 | if (mddev->sync_thread) { |
| 3381 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3382 | md_unregister_thread(mddev->sync_thread); |
| 3383 | mddev->sync_thread = NULL; |
| 3384 | mddev->recovery = 0; |
| 3385 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3386 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3387 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3388 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3389 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3390 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3391 | else if (cmd_match(page, "recover")) { |
| 3392 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3393 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3394 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3395 | int err; |
| 3396 | if (mddev->pers->start_reshape == NULL) |
| 3397 | return -EINVAL; |
| 3398 | err = mddev->pers->start_reshape(mddev); |
| 3399 | if (err) |
| 3400 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3401 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3402 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3403 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3404 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3405 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3406 | return -EINVAL; |
| 3407 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3408 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3409 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3410 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3411 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3412 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3413 | return len; |
| 3414 | } |
| 3415 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3416 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3417 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3418 | { |
| 3419 | return sprintf(page, "%llu\n", |
| 3420 | (unsigned long long) mddev->resync_mismatches); |
| 3421 | } |
| 3422 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3423 | static struct md_sysfs_entry md_scan_mode = |
| 3424 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3425 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3426 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3427 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3428 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3429 | static ssize_t |
| 3430 | sync_min_show(mddev_t *mddev, char *page) |
| 3431 | { |
| 3432 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3433 | mddev->sync_speed_min ? "local": "system"); |
| 3434 | } |
| 3435 | |
| 3436 | static ssize_t |
| 3437 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3438 | { |
| 3439 | int min; |
| 3440 | char *e; |
| 3441 | if (strncmp(buf, "system", 6)==0) { |
| 3442 | mddev->sync_speed_min = 0; |
| 3443 | return len; |
| 3444 | } |
| 3445 | min = simple_strtoul(buf, &e, 10); |
| 3446 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3447 | return -EINVAL; |
| 3448 | mddev->sync_speed_min = min; |
| 3449 | return len; |
| 3450 | } |
| 3451 | |
| 3452 | static struct md_sysfs_entry md_sync_min = |
| 3453 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3454 | |
| 3455 | static ssize_t |
| 3456 | sync_max_show(mddev_t *mddev, char *page) |
| 3457 | { |
| 3458 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3459 | mddev->sync_speed_max ? "local": "system"); |
| 3460 | } |
| 3461 | |
| 3462 | static ssize_t |
| 3463 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3464 | { |
| 3465 | int max; |
| 3466 | char *e; |
| 3467 | if (strncmp(buf, "system", 6)==0) { |
| 3468 | mddev->sync_speed_max = 0; |
| 3469 | return len; |
| 3470 | } |
| 3471 | max = simple_strtoul(buf, &e, 10); |
| 3472 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3473 | return -EINVAL; |
| 3474 | mddev->sync_speed_max = max; |
| 3475 | return len; |
| 3476 | } |
| 3477 | |
| 3478 | static struct md_sysfs_entry md_sync_max = |
| 3479 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3480 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3481 | static ssize_t |
| 3482 | degraded_show(mddev_t *mddev, char *page) |
| 3483 | { |
| 3484 | return sprintf(page, "%d\n", mddev->degraded); |
| 3485 | } |
| 3486 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3487 | |
| 3488 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3489 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3490 | { |
| 3491 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3492 | } |
| 3493 | |
| 3494 | static ssize_t |
| 3495 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3496 | { |
| 3497 | long n; |
| 3498 | |
| 3499 | if (strict_strtol(buf, 10, &n)) |
| 3500 | return -EINVAL; |
| 3501 | |
| 3502 | if (n != 0 && n != 1) |
| 3503 | return -EINVAL; |
| 3504 | |
| 3505 | mddev->parallel_resync = n; |
| 3506 | |
| 3507 | if (mddev->sync_thread) |
| 3508 | wake_up(&resync_wait); |
| 3509 | |
| 3510 | return len; |
| 3511 | } |
| 3512 | |
| 3513 | /* force parallel resync, even with shared block devices */ |
| 3514 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3515 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3516 | sync_force_parallel_show, sync_force_parallel_store); |
| 3517 | |
| 3518 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3519 | sync_speed_show(mddev_t *mddev, char *page) |
| 3520 | { |
| 3521 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3522 | if (mddev->curr_resync == 0) |
| 3523 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3524 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3525 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3526 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3527 | db = resync - mddev->resync_mark_cnt; |
| 3528 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3531 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3532 | |
| 3533 | static ssize_t |
| 3534 | sync_completed_show(mddev_t *mddev, char *page) |
| 3535 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3536 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3537 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3538 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3539 | return sprintf(page, "none\n"); |
| 3540 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3541 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3542 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3543 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3544 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3545 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3546 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3547 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3548 | } |
| 3549 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3550 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3551 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3552 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3553 | min_sync_show(mddev_t *mddev, char *page) |
| 3554 | { |
| 3555 | return sprintf(page, "%llu\n", |
| 3556 | (unsigned long long)mddev->resync_min); |
| 3557 | } |
| 3558 | static ssize_t |
| 3559 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3560 | { |
| 3561 | unsigned long long min; |
| 3562 | if (strict_strtoull(buf, 10, &min)) |
| 3563 | return -EINVAL; |
| 3564 | if (min > mddev->resync_max) |
| 3565 | return -EINVAL; |
| 3566 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3567 | return -EBUSY; |
| 3568 | |
| 3569 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3570 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3571 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3572 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3573 | return -EINVAL; |
| 3574 | } |
| 3575 | mddev->resync_min = min; |
| 3576 | |
| 3577 | return len; |
| 3578 | } |
| 3579 | |
| 3580 | static struct md_sysfs_entry md_min_sync = |
| 3581 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3582 | |
| 3583 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3584 | max_sync_show(mddev_t *mddev, char *page) |
| 3585 | { |
| 3586 | if (mddev->resync_max == MaxSector) |
| 3587 | return sprintf(page, "max\n"); |
| 3588 | else |
| 3589 | return sprintf(page, "%llu\n", |
| 3590 | (unsigned long long)mddev->resync_max); |
| 3591 | } |
| 3592 | static ssize_t |
| 3593 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3594 | { |
| 3595 | if (strncmp(buf, "max", 3) == 0) |
| 3596 | mddev->resync_max = MaxSector; |
| 3597 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3598 | unsigned long long max; |
| 3599 | if (strict_strtoull(buf, 10, &max)) |
| 3600 | return -EINVAL; |
| 3601 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3602 | return -EINVAL; |
| 3603 | if (max < mddev->resync_max && |
| 3604 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3605 | return -EBUSY; |
| 3606 | |
| 3607 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3608 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3609 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3610 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3611 | return -EINVAL; |
| 3612 | } |
| 3613 | mddev->resync_max = max; |
| 3614 | } |
| 3615 | wake_up(&mddev->recovery_wait); |
| 3616 | return len; |
| 3617 | } |
| 3618 | |
| 3619 | static struct md_sysfs_entry md_max_sync = |
| 3620 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3621 | |
| 3622 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3623 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3624 | { |
| 3625 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3626 | } |
| 3627 | |
| 3628 | static ssize_t |
| 3629 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3630 | { |
| 3631 | char *e; |
| 3632 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3633 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3634 | if (mddev->pers == NULL || |
| 3635 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3636 | return -EINVAL; |
| 3637 | if (buf == e || (*e && *e != '\n')) |
| 3638 | return -EINVAL; |
| 3639 | if (new >= mddev->suspend_hi || |
| 3640 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3641 | mddev->suspend_lo = new; |
| 3642 | mddev->pers->quiesce(mddev, 2); |
| 3643 | return len; |
| 3644 | } else |
| 3645 | return -EINVAL; |
| 3646 | } |
| 3647 | static struct md_sysfs_entry md_suspend_lo = |
| 3648 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3649 | |
| 3650 | |
| 3651 | static ssize_t |
| 3652 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3653 | { |
| 3654 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3655 | } |
| 3656 | |
| 3657 | static ssize_t |
| 3658 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3659 | { |
| 3660 | char *e; |
| 3661 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3662 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3663 | if (mddev->pers == NULL || |
| 3664 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3665 | return -EINVAL; |
| 3666 | if (buf == e || (*e && *e != '\n')) |
| 3667 | return -EINVAL; |
| 3668 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3669 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3670 | mddev->suspend_hi = new; |
| 3671 | mddev->pers->quiesce(mddev, 1); |
| 3672 | mddev->pers->quiesce(mddev, 0); |
| 3673 | return len; |
| 3674 | } else |
| 3675 | return -EINVAL; |
| 3676 | } |
| 3677 | static struct md_sysfs_entry md_suspend_hi = |
| 3678 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3679 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3680 | static ssize_t |
| 3681 | reshape_position_show(mddev_t *mddev, char *page) |
| 3682 | { |
| 3683 | if (mddev->reshape_position != MaxSector) |
| 3684 | return sprintf(page, "%llu\n", |
| 3685 | (unsigned long long)mddev->reshape_position); |
| 3686 | strcpy(page, "none\n"); |
| 3687 | return 5; |
| 3688 | } |
| 3689 | |
| 3690 | static ssize_t |
| 3691 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3692 | { |
| 3693 | char *e; |
| 3694 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3695 | if (mddev->pers) |
| 3696 | return -EBUSY; |
| 3697 | if (buf == e || (*e && *e != '\n')) |
| 3698 | return -EINVAL; |
| 3699 | mddev->reshape_position = new; |
| 3700 | mddev->delta_disks = 0; |
| 3701 | mddev->new_level = mddev->level; |
| 3702 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3703 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3704 | return len; |
| 3705 | } |
| 3706 | |
| 3707 | static struct md_sysfs_entry md_reshape_position = |
| 3708 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3709 | reshape_position_store); |
| 3710 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3711 | static ssize_t |
| 3712 | array_size_show(mddev_t *mddev, char *page) |
| 3713 | { |
| 3714 | if (mddev->external_size) |
| 3715 | return sprintf(page, "%llu\n", |
| 3716 | (unsigned long long)mddev->array_sectors/2); |
| 3717 | else |
| 3718 | return sprintf(page, "default\n"); |
| 3719 | } |
| 3720 | |
| 3721 | static ssize_t |
| 3722 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3723 | { |
| 3724 | sector_t sectors; |
| 3725 | |
| 3726 | if (strncmp(buf, "default", 7) == 0) { |
| 3727 | if (mddev->pers) |
| 3728 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3729 | else |
| 3730 | sectors = mddev->array_sectors; |
| 3731 | |
| 3732 | mddev->external_size = 0; |
| 3733 | } else { |
| 3734 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3735 | return -EINVAL; |
| 3736 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3737 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3738 | |
| 3739 | mddev->external_size = 1; |
| 3740 | } |
| 3741 | |
| 3742 | mddev->array_sectors = sectors; |
| 3743 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 3744 | if (mddev->pers) { |
| 3745 | struct block_device *bdev = bdget_disk(mddev->gendisk, 0); |
| 3746 | |
| 3747 | if (bdev) { |
| 3748 | mutex_lock(&bdev->bd_inode->i_mutex); |
| 3749 | i_size_write(bdev->bd_inode, |
| 3750 | (loff_t)mddev->array_sectors << 9); |
| 3751 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 3752 | bdput(bdev); |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | return len; |
| 3757 | } |
| 3758 | |
| 3759 | static struct md_sysfs_entry md_array_size = |
| 3760 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3761 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3762 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3763 | static struct attribute *md_default_attrs[] = { |
| 3764 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3765 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3766 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3767 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3768 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3769 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3770 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3771 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3772 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3773 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3774 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3775 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3776 | NULL, |
| 3777 | }; |
| 3778 | |
| 3779 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3780 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3781 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3782 | &md_sync_min.attr, |
| 3783 | &md_sync_max.attr, |
| 3784 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3785 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3786 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3787 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3788 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3789 | &md_suspend_lo.attr, |
| 3790 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3791 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3792 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3793 | NULL, |
| 3794 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3795 | static struct attribute_group md_redundancy_group = { |
| 3796 | .name = NULL, |
| 3797 | .attrs = md_redundancy_attrs, |
| 3798 | }; |
| 3799 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3800 | |
| 3801 | static ssize_t |
| 3802 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 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->show) |
| 3809 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3810 | rv = mddev_lock(mddev); |
| 3811 | if (!rv) { |
| 3812 | rv = entry->show(mddev, page); |
| 3813 | mddev_unlock(mddev); |
| 3814 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3815 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | static ssize_t |
| 3819 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3820 | const char *page, size_t length) |
| 3821 | { |
| 3822 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3823 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3824 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3825 | |
| 3826 | if (!entry->store) |
| 3827 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3828 | if (!capable(CAP_SYS_ADMIN)) |
| 3829 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3830 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3831 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3832 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3833 | if (!rv) { |
| 3834 | rv = entry->store(mddev, page, length); |
| 3835 | mddev_unlock(mddev); |
| 3836 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3837 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | static void md_free(struct kobject *ko) |
| 3841 | { |
| 3842 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3843 | |
| 3844 | if (mddev->sysfs_state) |
| 3845 | sysfs_put(mddev->sysfs_state); |
| 3846 | |
| 3847 | if (mddev->gendisk) { |
| 3848 | del_gendisk(mddev->gendisk); |
| 3849 | put_disk(mddev->gendisk); |
| 3850 | } |
| 3851 | if (mddev->queue) |
| 3852 | blk_cleanup_queue(mddev->queue); |
| 3853 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3854 | kfree(mddev); |
| 3855 | } |
| 3856 | |
| 3857 | static struct sysfs_ops md_sysfs_ops = { |
| 3858 | .show = md_attr_show, |
| 3859 | .store = md_attr_store, |
| 3860 | }; |
| 3861 | static struct kobj_type md_ktype = { |
| 3862 | .release = md_free, |
| 3863 | .sysfs_ops = &md_sysfs_ops, |
| 3864 | .default_attrs = md_default_attrs, |
| 3865 | }; |
| 3866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | int mdp_major = 0; |
| 3868 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3869 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3870 | { |
| 3871 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3872 | |
| 3873 | if (mddev->private == &md_redundancy_group) { |
| 3874 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3875 | if (mddev->sysfs_action) |
| 3876 | sysfs_put(mddev->sysfs_action); |
| 3877 | mddev->sysfs_action = NULL; |
| 3878 | mddev->private = NULL; |
| 3879 | } |
| 3880 | kobject_del(&mddev->kobj); |
| 3881 | kobject_put(&mddev->kobj); |
| 3882 | } |
| 3883 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3884 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3886 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3887 | mddev_t *mddev = mddev_find(dev); |
| 3888 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3889 | int partitioned; |
| 3890 | int shift; |
| 3891 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3892 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | |
| 3894 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3895 | return -ENODEV; |
| 3896 | |
| 3897 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3898 | shift = partitioned ? MdpMinorShift : 0; |
| 3899 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3901 | /* wait for any previous instance if this device |
| 3902 | * to be completed removed (mddev_delayed_delete). |
| 3903 | */ |
| 3904 | flush_scheduled_work(); |
| 3905 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3906 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3907 | error = -EEXIST; |
| 3908 | if (mddev->gendisk) |
| 3909 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3910 | |
| 3911 | if (name) { |
| 3912 | /* Need to ensure that 'name' is not a duplicate. |
| 3913 | */ |
| 3914 | mddev_t *mddev2; |
| 3915 | spin_lock(&all_mddevs_lock); |
| 3916 | |
| 3917 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3918 | if (mddev2->gendisk && |
| 3919 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3920 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3921 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3922 | } |
| 3923 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3925 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3926 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3927 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3928 | if (!mddev->queue) |
| 3929 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3930 | mddev->queue->queuedata = mddev; |
| 3931 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3932 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3933 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3934 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3935 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | disk = alloc_disk(1 << shift); |
| 3938 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3939 | blk_cleanup_queue(mddev->queue); |
| 3940 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3941 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3943 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3945 | if (name) |
| 3946 | strcpy(disk->disk_name, name); |
| 3947 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3949 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | disk->fops = &md_fops; |
| 3952 | disk->private_data = mddev; |
| 3953 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3954 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3955 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3956 | * remove it now. |
| 3957 | */ |
| 3958 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | add_disk(disk); |
| 3960 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3961 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3962 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3963 | if (error) { |
| 3964 | /* This isn't possible, but as kobject_init_and_add is marked |
| 3965 | * __must_check, we must do something with the result |
| 3966 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3967 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3968 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3969 | error = 0; |
| 3970 | } |
| 3971 | abort: |
| 3972 | mutex_unlock(&disks_mutex); |
| 3973 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3974 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3975 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3976 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3977 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3978 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3982 | { |
| 3983 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | return NULL; |
| 3985 | } |
| 3986 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3987 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 3988 | { |
| 3989 | /* val must be "md_*" where * is not all digits. |
| 3990 | * We allocate an array with a large free minor number, and |
| 3991 | * set the name to val. val must not already be an active name. |
| 3992 | */ |
| 3993 | int len = strlen(val); |
| 3994 | char buf[DISK_NAME_LEN]; |
| 3995 | |
| 3996 | while (len && val[len-1] == '\n') |
| 3997 | len--; |
| 3998 | if (len >= DISK_NAME_LEN) |
| 3999 | return -E2BIG; |
| 4000 | strlcpy(buf, val, len+1); |
| 4001 | if (strncmp(buf, "md_", 3) != 0) |
| 4002 | return -EINVAL; |
| 4003 | return md_alloc(0, buf); |
| 4004 | } |
| 4005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | static void md_safemode_timeout(unsigned long data) |
| 4007 | { |
| 4008 | mddev_t *mddev = (mddev_t *) data; |
| 4009 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4010 | if (!atomic_read(&mddev->writes_pending)) { |
| 4011 | mddev->safemode = 1; |
| 4012 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4013 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4014 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | md_wakeup_thread(mddev->thread); |
| 4016 | } |
| 4017 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4018 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | |
| 4020 | static int do_md_run(mddev_t * mddev) |
| 4021 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4022 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | mdk_rdev_t *rdev; |
| 4024 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4025 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4027 | if (list_empty(&mddev->disks)) |
| 4028 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | |
| 4031 | if (mddev->pers) |
| 4032 | return -EBUSY; |
| 4033 | |
| 4034 | /* |
| 4035 | * Analyze all RAID superblock(s) |
| 4036 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4037 | if (!mddev->raid_disks) { |
| 4038 | if (!mddev->persistent) |
| 4039 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4040 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4043 | if (mddev->level != LEVEL_NONE) |
| 4044 | request_module("md-level-%d", mddev->level); |
| 4045 | else if (mddev->clevel[0]) |
| 4046 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | |
| 4048 | /* |
| 4049 | * Drop all container device buffers, from now on |
| 4050 | * the only valid external interface is through the md |
| 4051 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4053 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4054 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | continue; |
| 4056 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4057 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4058 | |
| 4059 | /* perform some consistency tests on the device. |
| 4060 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4061 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4062 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4063 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4064 | if (mddev->dev_sectors && |
| 4065 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4066 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4067 | printk("md: %s: data overlaps metadata\n", |
| 4068 | mdname(mddev)); |
| 4069 | return -EINVAL; |
| 4070 | } |
| 4071 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4072 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4073 | > rdev->data_offset) { |
| 4074 | printk("md: %s: metadata overlaps data\n", |
| 4075 | mdname(mddev)); |
| 4076 | return -EINVAL; |
| 4077 | } |
| 4078 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4079 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | md_probe(mddev->unit, NULL, NULL); |
| 4083 | disk = mddev->gendisk; |
| 4084 | if (!disk) |
| 4085 | return -ENOMEM; |
| 4086 | |
| 4087 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4088 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4089 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4091 | if (mddev->level != LEVEL_NONE) |
| 4092 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4093 | mddev->level); |
| 4094 | else |
| 4095 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4096 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | return -EINVAL; |
| 4098 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4099 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4101 | if (mddev->level != pers->level) { |
| 4102 | mddev->level = pers->level; |
| 4103 | mddev->new_level = pers->level; |
| 4104 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4105 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4107 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4108 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4109 | /* This personality cannot handle reshaping... */ |
| 4110 | mddev->pers = NULL; |
| 4111 | module_put(pers->owner); |
| 4112 | return -EINVAL; |
| 4113 | } |
| 4114 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4115 | if (pers->sync_request) { |
| 4116 | /* Warn if this is a potentially silly |
| 4117 | * configuration. |
| 4118 | */ |
| 4119 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4120 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4121 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4122 | |
| 4123 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4124 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4125 | if (rdev < rdev2 && |
| 4126 | rdev->bdev->bd_contains == |
| 4127 | rdev2->bdev->bd_contains) { |
| 4128 | printk(KERN_WARNING |
| 4129 | "%s: WARNING: %s appears to be" |
| 4130 | " on the same physical disk as" |
| 4131 | " %s.\n", |
| 4132 | mdname(mddev), |
| 4133 | bdevname(rdev->bdev,b), |
| 4134 | bdevname(rdev2->bdev,b2)); |
| 4135 | warned = 1; |
| 4136 | } |
| 4137 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4138 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4139 | if (warned) |
| 4140 | printk(KERN_WARNING |
| 4141 | "True protection against single-disk" |
| 4142 | " failure might be compromised.\n"); |
| 4143 | } |
| 4144 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4145 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4146 | /* may be over-ridden by personality */ |
| 4147 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4148 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4149 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4150 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4152 | if (start_readonly) |
| 4153 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4154 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4155 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4156 | if (err) |
| 4157 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4158 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4159 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4160 | " but 'external_size' not in effect?\n", __func__); |
| 4161 | printk(KERN_ERR |
| 4162 | "md: invalid array_size %llu > default size %llu\n", |
| 4163 | (unsigned long long)mddev->array_sectors / 2, |
| 4164 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4165 | err = -EINVAL; |
| 4166 | mddev->pers->stop(mddev); |
| 4167 | } |
| 4168 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4169 | err = bitmap_create(mddev); |
| 4170 | if (err) { |
| 4171 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4172 | mdname(mddev), err); |
| 4173 | mddev->pers->stop(mddev); |
| 4174 | } |
| 4175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | module_put(mddev->pers->owner); |
| 4178 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4179 | bitmap_destroy(mddev); |
| 4180 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4182 | if (mddev->pers->sync_request) { |
| 4183 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4184 | printk(KERN_WARNING |
| 4185 | "md: cannot register extra attributes for %s\n", |
| 4186 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4187 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4188 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4189 | mddev->ro = 0; |
| 4190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | atomic_set(&mddev->writes_pending,0); |
| 4192 | mddev->safemode = 0; |
| 4193 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4194 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4195 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4197 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4198 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4199 | if (rdev->raid_disk >= 0) { |
| 4200 | char nm[20]; |
| 4201 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4202 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4203 | printk("md: cannot register %s for %s\n", |
| 4204 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | |
| 4207 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4208 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4209 | if (mddev->flags) |
| 4210 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4212 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4214 | /* If there is a partially-recovered drive we need to |
| 4215 | * start recovery here. If we leave it to md_check_recovery, |
| 4216 | * it will remove the drives and not do the right thing |
| 4217 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4218 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4219 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4220 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4221 | if (rdev->raid_disk >= 0 && |
| 4222 | !test_bit(In_sync, &rdev->flags) && |
| 4223 | !test_bit(Faulty, &rdev->flags)) |
| 4224 | /* complete an interrupted recovery */ |
| 4225 | spares++; |
| 4226 | if (spares && mddev->pers->sync_request) { |
| 4227 | mddev->recovery = 0; |
| 4228 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4229 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4230 | mddev, |
| 4231 | "%s_resync"); |
| 4232 | if (!mddev->sync_thread) { |
| 4233 | printk(KERN_ERR "%s: could not start resync" |
| 4234 | " thread...\n", |
| 4235 | mdname(mddev)); |
| 4236 | /* leave the spares where they are, it shouldn't hurt */ |
| 4237 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4238 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4239 | } |
| 4240 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4241 | md_wakeup_thread(mddev->thread); |
| 4242 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4243 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4244 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4245 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4246 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4247 | if (mddev->sysfs_action) |
| 4248 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4249 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4250 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | return 0; |
| 4252 | } |
| 4253 | |
| 4254 | static int restart_array(mddev_t *mddev) |
| 4255 | { |
| 4256 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4258 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4260 | return -ENXIO; |
| 4261 | if (!mddev->pers) |
| 4262 | return -EINVAL; |
| 4263 | if (!mddev->ro) |
| 4264 | return -EBUSY; |
| 4265 | mddev->safemode = 0; |
| 4266 | mddev->ro = 0; |
| 4267 | set_disk_ro(disk, 0); |
| 4268 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4269 | mdname(mddev)); |
| 4270 | /* Kick recovery or resync if necessary */ |
| 4271 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4272 | md_wakeup_thread(mddev->thread); |
| 4273 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4274 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4275 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4278 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4279 | * to the file ourselves */ |
| 4280 | static int deny_bitmap_write_access(struct file * file) |
| 4281 | { |
| 4282 | struct inode *inode = file->f_mapping->host; |
| 4283 | |
| 4284 | spin_lock(&inode->i_lock); |
| 4285 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4286 | spin_unlock(&inode->i_lock); |
| 4287 | return -ETXTBSY; |
| 4288 | } |
| 4289 | atomic_set(&inode->i_writecount, -1); |
| 4290 | spin_unlock(&inode->i_lock); |
| 4291 | |
| 4292 | return 0; |
| 4293 | } |
| 4294 | |
| 4295 | static void restore_bitmap_write_access(struct file *file) |
| 4296 | { |
| 4297 | struct inode *inode = file->f_mapping->host; |
| 4298 | |
| 4299 | spin_lock(&inode->i_lock); |
| 4300 | atomic_set(&inode->i_writecount, 1); |
| 4301 | spin_unlock(&inode->i_lock); |
| 4302 | } |
| 4303 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4304 | /* mode: |
| 4305 | * 0 - completely stop and dis-assemble array |
| 4306 | * 1 - switch to readonly |
| 4307 | * 2 - stop but do not disassemble array |
| 4308 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4309 | 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] | 4310 | { |
| 4311 | int err = 0; |
| 4312 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4313 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4315 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4316 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4317 | return -EBUSY; |
| 4318 | } |
| 4319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | |
| 4322 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4323 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4324 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4325 | md_unregister_thread(mddev->sync_thread); |
| 4326 | mddev->sync_thread = NULL; |
| 4327 | } |
| 4328 | |
| 4329 | del_timer_sync(&mddev->safemode_timer); |
| 4330 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4331 | switch(mode) { |
| 4332 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4334 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | goto out; |
| 4336 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4337 | break; |
| 4338 | case 0: /* disassemble */ |
| 4339 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4340 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4341 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | if (mddev->ro) |
| 4343 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4346 | mddev->queue->merge_bvec_fn = NULL; |
| 4347 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4348 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4350 | if (mddev->pers->sync_request) |
| 4351 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4353 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4354 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4355 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4356 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4357 | if (rdev->raid_disk >= 0) { |
| 4358 | char nm[20]; |
| 4359 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4360 | sysfs_remove_link(&mddev->kobj, nm); |
| 4361 | } |
| 4362 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4363 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4364 | mddev->changed = 1; |
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 | if (mddev->ro) |
| 4367 | mddev->ro = 0; |
| 4368 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4369 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | /* mark array as shutdown cleanly */ |
| 4371 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4372 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4374 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4376 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | /* |
| 4380 | * Free resources if final stop |
| 4381 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4382 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4385 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4386 | bitmap_destroy(mddev); |
| 4387 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4388 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4389 | fput(mddev->bitmap_file); |
| 4390 | mddev->bitmap_file = NULL; |
| 4391 | } |
| 4392 | mddev->bitmap_offset = 0; |
| 4393 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4394 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4395 | flush_scheduled_work(); |
| 4396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | export_array(mddev); |
| 4398 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4399 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4400 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4401 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4402 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4403 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4404 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4405 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4406 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4407 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4408 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4409 | mddev->level = LEVEL_NONE; |
| 4410 | mddev->clevel[0] = 0; |
| 4411 | mddev->flags = 0; |
| 4412 | mddev->ro = 0; |
| 4413 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4414 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4415 | mddev->ctime = mddev->utime = 0; |
| 4416 | mddev->layout = 0; |
| 4417 | mddev->max_disks = 0; |
| 4418 | mddev->events = 0; |
| 4419 | mddev->delta_disks = 0; |
| 4420 | mddev->new_level = LEVEL_NONE; |
| 4421 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4422 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4423 | mddev->curr_resync = 0; |
| 4424 | mddev->resync_mismatches = 0; |
| 4425 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4426 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4427 | mddev->recovery = 0; |
| 4428 | mddev->in_sync = 0; |
| 4429 | mddev->changed = 0; |
| 4430 | mddev->degraded = 0; |
| 4431 | mddev->barriers_work = 0; |
| 4432 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4433 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4434 | if (mddev->hold_active == UNTIL_STOP) |
| 4435 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4436 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4437 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4439 | mdname(mddev)); |
| 4440 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4441 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4442 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4443 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | out: |
| 4445 | return err; |
| 4446 | } |
| 4447 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4448 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | static void autorun_array(mddev_t *mddev) |
| 4450 | { |
| 4451 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | int err; |
| 4453 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4454 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4456 | |
| 4457 | printk(KERN_INFO "md: running: "); |
| 4458 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4459 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | char b[BDEVNAME_SIZE]; |
| 4461 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4462 | } |
| 4463 | printk("\n"); |
| 4464 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4465 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | if (err) { |
| 4467 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4468 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | } |
| 4470 | } |
| 4471 | |
| 4472 | /* |
| 4473 | * lets try to run arrays based on all disks that have arrived |
| 4474 | * until now. (those are in pending_raid_disks) |
| 4475 | * |
| 4476 | * the method: pick the first pending disk, collect all disks with |
| 4477 | * the same UUID, remove all from the pending list and put them into |
| 4478 | * the 'same_array' list. Then order this list based on superblock |
| 4479 | * update time (freshest comes first), kick out 'old' disks and |
| 4480 | * compare superblocks. If everything's fine then run it. |
| 4481 | * |
| 4482 | * If "unit" is allocated, then bump its reference count |
| 4483 | */ |
| 4484 | static void autorun_devices(int part) |
| 4485 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4486 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | mddev_t *mddev; |
| 4488 | char b[BDEVNAME_SIZE]; |
| 4489 | |
| 4490 | printk(KERN_INFO "md: autorun ...\n"); |
| 4491 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4492 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4494 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | rdev0 = list_entry(pending_raid_disks.next, |
| 4496 | mdk_rdev_t, same_set); |
| 4497 | |
| 4498 | printk(KERN_INFO "md: considering %s ...\n", |
| 4499 | bdevname(rdev0->bdev,b)); |
| 4500 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4501 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4503 | printk(KERN_INFO "md: adding %s ...\n", |
| 4504 | bdevname(rdev->bdev,b)); |
| 4505 | list_move(&rdev->same_set, &candidates); |
| 4506 | } |
| 4507 | /* |
| 4508 | * now we have a set of devices, with all of them having |
| 4509 | * mostly sane superblocks. It's time to allocate the |
| 4510 | * mddev. |
| 4511 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4512 | if (part) { |
| 4513 | dev = MKDEV(mdp_major, |
| 4514 | rdev0->preferred_minor << MdpMinorShift); |
| 4515 | unit = MINOR(dev) >> MdpMinorShift; |
| 4516 | } else { |
| 4517 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4518 | unit = MINOR(dev); |
| 4519 | } |
| 4520 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4521 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4522 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4523 | break; |
| 4524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | |
| 4526 | md_probe(dev, NULL, NULL); |
| 4527 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4528 | if (!mddev || !mddev->gendisk) { |
| 4529 | if (mddev) |
| 4530 | mddev_put(mddev); |
| 4531 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | "md: cannot allocate memory for md drive.\n"); |
| 4533 | break; |
| 4534 | } |
| 4535 | if (mddev_lock(mddev)) |
| 4536 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4537 | mdname(mddev)); |
| 4538 | else if (mddev->raid_disks || mddev->major_version |
| 4539 | || !list_empty(&mddev->disks)) { |
| 4540 | printk(KERN_WARNING |
| 4541 | "md: %s already running, cannot run %s\n", |
| 4542 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4543 | mddev_unlock(mddev); |
| 4544 | } else { |
| 4545 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4546 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4547 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | list_del_init(&rdev->same_set); |
| 4549 | if (bind_rdev_to_array(rdev, mddev)) |
| 4550 | export_rdev(rdev); |
| 4551 | } |
| 4552 | autorun_array(mddev); |
| 4553 | mddev_unlock(mddev); |
| 4554 | } |
| 4555 | /* on success, candidates will be empty, on error |
| 4556 | * it won't... |
| 4557 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4558 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4559 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | mddev_put(mddev); |
| 4563 | } |
| 4564 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4565 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4566 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | static int get_version(void __user * arg) |
| 4569 | { |
| 4570 | mdu_version_t ver; |
| 4571 | |
| 4572 | ver.major = MD_MAJOR_VERSION; |
| 4573 | ver.minor = MD_MINOR_VERSION; |
| 4574 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4575 | |
| 4576 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4577 | return -EFAULT; |
| 4578 | |
| 4579 | return 0; |
| 4580 | } |
| 4581 | |
| 4582 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4583 | { |
| 4584 | mdu_array_info_t info; |
| 4585 | int nr,working,active,failed,spare; |
| 4586 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | |
| 4588 | nr=working=active=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4589 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4591 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | failed++; |
| 4593 | else { |
| 4594 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4595 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | active++; |
| 4597 | else |
| 4598 | spare++; |
| 4599 | } |
| 4600 | } |
| 4601 | |
| 4602 | info.major_version = mddev->major_version; |
| 4603 | info.minor_version = mddev->minor_version; |
| 4604 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4605 | info.ctime = mddev->ctime; |
| 4606 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4607 | info.size = mddev->dev_sectors / 2; |
| 4608 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4609 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | info.nr_disks = nr; |
| 4611 | info.raid_disks = mddev->raid_disks; |
| 4612 | info.md_minor = mddev->md_minor; |
| 4613 | info.not_persistent= !mddev->persistent; |
| 4614 | |
| 4615 | info.utime = mddev->utime; |
| 4616 | info.state = 0; |
| 4617 | if (mddev->in_sync) |
| 4618 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4619 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4620 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | info.active_disks = active; |
| 4622 | info.working_disks = working; |
| 4623 | info.failed_disks = failed; |
| 4624 | info.spare_disks = spare; |
| 4625 | |
| 4626 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4627 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | |
| 4629 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4630 | return -EFAULT; |
| 4631 | |
| 4632 | return 0; |
| 4633 | } |
| 4634 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4635 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4636 | { |
| 4637 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4638 | char *ptr, *buf = NULL; |
| 4639 | int err = -ENOMEM; |
| 4640 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4641 | if (md_allow_write(mddev)) |
| 4642 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4643 | else |
| 4644 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4645 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4646 | if (!file) |
| 4647 | goto out; |
| 4648 | |
| 4649 | /* bitmap disabled, zero the first byte and copy out */ |
| 4650 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4651 | file->pathname[0] = '\0'; |
| 4652 | goto copy_out; |
| 4653 | } |
| 4654 | |
| 4655 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4656 | if (!buf) |
| 4657 | goto out; |
| 4658 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4659 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4660 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4661 | goto out; |
| 4662 | |
| 4663 | strcpy(file->pathname, ptr); |
| 4664 | |
| 4665 | copy_out: |
| 4666 | err = 0; |
| 4667 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4668 | err = -EFAULT; |
| 4669 | out: |
| 4670 | kfree(buf); |
| 4671 | kfree(file); |
| 4672 | return err; |
| 4673 | } |
| 4674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4676 | { |
| 4677 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | mdk_rdev_t *rdev; |
| 4679 | |
| 4680 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4681 | return -EFAULT; |
| 4682 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4683 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | if (rdev) { |
| 4685 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4686 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4687 | info.raid_disk = rdev->raid_disk; |
| 4688 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4689 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4691 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4693 | info.state |= (1<<MD_DISK_SYNC); |
| 4694 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4695 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4696 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | } else { |
| 4698 | info.major = info.minor = 0; |
| 4699 | info.raid_disk = -1; |
| 4700 | info.state = (1<<MD_DISK_REMOVED); |
| 4701 | } |
| 4702 | |
| 4703 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4704 | return -EFAULT; |
| 4705 | |
| 4706 | return 0; |
| 4707 | } |
| 4708 | |
| 4709 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4710 | { |
| 4711 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4712 | mdk_rdev_t *rdev; |
| 4713 | dev_t dev = MKDEV(info->major,info->minor); |
| 4714 | |
| 4715 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4716 | return -EOVERFLOW; |
| 4717 | |
| 4718 | if (!mddev->raid_disks) { |
| 4719 | int err; |
| 4720 | /* expecting a device which has a superblock */ |
| 4721 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4722 | if (IS_ERR(rdev)) { |
| 4723 | printk(KERN_WARNING |
| 4724 | "md: md_import_device returned %ld\n", |
| 4725 | PTR_ERR(rdev)); |
| 4726 | return PTR_ERR(rdev); |
| 4727 | } |
| 4728 | if (!list_empty(&mddev->disks)) { |
| 4729 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4730 | mdk_rdev_t, same_set); |
| 4731 | int err = super_types[mddev->major_version] |
| 4732 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4733 | if (err < 0) { |
| 4734 | printk(KERN_WARNING |
| 4735 | "md: %s has different UUID to %s\n", |
| 4736 | bdevname(rdev->bdev,b), |
| 4737 | bdevname(rdev0->bdev,b2)); |
| 4738 | export_rdev(rdev); |
| 4739 | return -EINVAL; |
| 4740 | } |
| 4741 | } |
| 4742 | err = bind_rdev_to_array(rdev, mddev); |
| 4743 | if (err) |
| 4744 | export_rdev(rdev); |
| 4745 | return err; |
| 4746 | } |
| 4747 | |
| 4748 | /* |
| 4749 | * add_new_disk can be used once the array is assembled |
| 4750 | * to add "hot spares". They must already have a superblock |
| 4751 | * written |
| 4752 | */ |
| 4753 | if (mddev->pers) { |
| 4754 | int err; |
| 4755 | if (!mddev->pers->hot_add_disk) { |
| 4756 | printk(KERN_WARNING |
| 4757 | "%s: personality does not support diskops!\n", |
| 4758 | mdname(mddev)); |
| 4759 | return -EINVAL; |
| 4760 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4761 | if (mddev->persistent) |
| 4762 | rdev = md_import_device(dev, mddev->major_version, |
| 4763 | mddev->minor_version); |
| 4764 | else |
| 4765 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | if (IS_ERR(rdev)) { |
| 4767 | printk(KERN_WARNING |
| 4768 | "md: md_import_device returned %ld\n", |
| 4769 | PTR_ERR(rdev)); |
| 4770 | return PTR_ERR(rdev); |
| 4771 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4772 | /* set save_raid_disk if appropriate */ |
| 4773 | if (!mddev->persistent) { |
| 4774 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4775 | info->raid_disk < mddev->raid_disks) |
| 4776 | rdev->raid_disk = info->raid_disk; |
| 4777 | else |
| 4778 | rdev->raid_disk = -1; |
| 4779 | } else |
| 4780 | super_types[mddev->major_version]. |
| 4781 | validate_super(mddev, rdev); |
| 4782 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4783 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4784 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4785 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4786 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4787 | else |
| 4788 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | rdev->raid_disk = -1; |
| 4791 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4792 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4793 | /* If there is hot_add_disk but no hot_remove_disk |
| 4794 | * then added disks for geometry changes, |
| 4795 | * and should be added immediately. |
| 4796 | */ |
| 4797 | super_types[mddev->major_version]. |
| 4798 | validate_super(mddev, rdev); |
| 4799 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4800 | if (err) |
| 4801 | unbind_rdev_from_array(rdev); |
| 4802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | if (err) |
| 4804 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4805 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4806 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4807 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4808 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4809 | if (mddev->degraded) |
| 4810 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4811 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4812 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | return err; |
| 4814 | } |
| 4815 | |
| 4816 | /* otherwise, add_new_disk is only allowed |
| 4817 | * for major_version==0 superblocks |
| 4818 | */ |
| 4819 | if (mddev->major_version != 0) { |
| 4820 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4821 | mdname(mddev)); |
| 4822 | return -EINVAL; |
| 4823 | } |
| 4824 | |
| 4825 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4826 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4827 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4828 | if (IS_ERR(rdev)) { |
| 4829 | printk(KERN_WARNING |
| 4830 | "md: error, md_import_device() returned %ld\n", |
| 4831 | PTR_ERR(rdev)); |
| 4832 | return PTR_ERR(rdev); |
| 4833 | } |
| 4834 | rdev->desc_nr = info->number; |
| 4835 | if (info->raid_disk < mddev->raid_disks) |
| 4836 | rdev->raid_disk = info->raid_disk; |
| 4837 | else |
| 4838 | rdev->raid_disk = -1; |
| 4839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4840 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4841 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4842 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4843 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4844 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4845 | set_bit(WriteMostly, &rdev->flags); |
| 4846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4847 | if (!mddev->persistent) { |
| 4848 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4849 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4851 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4852 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4854 | err = bind_rdev_to_array(rdev, mddev); |
| 4855 | if (err) { |
| 4856 | export_rdev(rdev); |
| 4857 | return err; |
| 4858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | return 0; |
| 4862 | } |
| 4863 | |
| 4864 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4865 | { |
| 4866 | char b[BDEVNAME_SIZE]; |
| 4867 | mdk_rdev_t *rdev; |
| 4868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | rdev = find_rdev(mddev, dev); |
| 4870 | if (!rdev) |
| 4871 | return -ENXIO; |
| 4872 | |
| 4873 | if (rdev->raid_disk >= 0) |
| 4874 | goto busy; |
| 4875 | |
| 4876 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4877 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4878 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | |
| 4880 | return 0; |
| 4881 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4882 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4884 | return -EBUSY; |
| 4885 | } |
| 4886 | |
| 4887 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4888 | { |
| 4889 | char b[BDEVNAME_SIZE]; |
| 4890 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | mdk_rdev_t *rdev; |
| 4892 | |
| 4893 | if (!mddev->pers) |
| 4894 | return -ENODEV; |
| 4895 | |
| 4896 | if (mddev->major_version != 0) { |
| 4897 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4898 | " version-0 superblocks.\n", |
| 4899 | mdname(mddev)); |
| 4900 | return -EINVAL; |
| 4901 | } |
| 4902 | if (!mddev->pers->hot_add_disk) { |
| 4903 | printk(KERN_WARNING |
| 4904 | "%s: personality does not support diskops!\n", |
| 4905 | mdname(mddev)); |
| 4906 | return -EINVAL; |
| 4907 | } |
| 4908 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4909 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4910 | if (IS_ERR(rdev)) { |
| 4911 | printk(KERN_WARNING |
| 4912 | "md: error, md_import_device() returned %ld\n", |
| 4913 | PTR_ERR(rdev)); |
| 4914 | return -EINVAL; |
| 4915 | } |
| 4916 | |
| 4917 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4918 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4920 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4922 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4924 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4925 | printk(KERN_WARNING |
| 4926 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4927 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4928 | err = -EINVAL; |
| 4929 | goto abort_export; |
| 4930 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4931 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4933 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4934 | err = bind_rdev_to_array(rdev, mddev); |
| 4935 | if (err) |
| 4936 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | |
| 4938 | /* |
| 4939 | * The rest should better be atomic, we can have disk failures |
| 4940 | * noticed in interrupt contexts ... |
| 4941 | */ |
| 4942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | rdev->raid_disk = -1; |
| 4944 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4945 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | |
| 4947 | /* |
| 4948 | * Kick recovery, maybe this spare has to be added to the |
| 4949 | * array immediately. |
| 4950 | */ |
| 4951 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4952 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4953 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | return 0; |
| 4955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | abort_export: |
| 4957 | export_rdev(rdev); |
| 4958 | return err; |
| 4959 | } |
| 4960 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4961 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4962 | { |
| 4963 | int err; |
| 4964 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4965 | if (mddev->pers) { |
| 4966 | if (!mddev->pers->quiesce) |
| 4967 | return -EBUSY; |
| 4968 | if (mddev->recovery || mddev->sync_thread) |
| 4969 | return -EBUSY; |
| 4970 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4971 | } |
| 4972 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4973 | |
| 4974 | if (fd >= 0) { |
| 4975 | if (mddev->bitmap) |
| 4976 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4977 | mddev->bitmap_file = fget(fd); |
| 4978 | |
| 4979 | if (mddev->bitmap_file == NULL) { |
| 4980 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4981 | mdname(mddev)); |
| 4982 | return -EBADF; |
| 4983 | } |
| 4984 | |
| 4985 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4986 | if (err) { |
| 4987 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4988 | mdname(mddev)); |
| 4989 | fput(mddev->bitmap_file); |
| 4990 | mddev->bitmap_file = NULL; |
| 4991 | return err; |
| 4992 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4993 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4994 | } else if (mddev->bitmap == NULL) |
| 4995 | return -ENOENT; /* cannot remove what isn't there */ |
| 4996 | err = 0; |
| 4997 | if (mddev->pers) { |
| 4998 | mddev->pers->quiesce(mddev, 1); |
| 4999 | if (fd >= 0) |
| 5000 | err = bitmap_create(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5001 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5002 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5003 | fd = -1; /* make sure to put the file */ |
| 5004 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5005 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5006 | } |
| 5007 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5008 | if (mddev->bitmap_file) { |
| 5009 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5010 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5011 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5012 | mddev->bitmap_file = NULL; |
| 5013 | } |
| 5014 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5015 | return err; |
| 5016 | } |
| 5017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | /* |
| 5019 | * set_array_info is used two different ways |
| 5020 | * The original usage is when creating a new array. |
| 5021 | * In this usage, raid_disks is > 0 and it together with |
| 5022 | * level, size, not_persistent,layout,chunksize determine the |
| 5023 | * shape of the array. |
| 5024 | * This will always create an array with a type-0.90.0 superblock. |
| 5025 | * The newer usage is when assembling an array. |
| 5026 | * In this case raid_disks will be 0, and the major_version field is |
| 5027 | * use to determine which style super-blocks are to be found on the devices. |
| 5028 | * The minor and patch _version numbers are also kept incase the |
| 5029 | * super_block handler wishes to interpret them. |
| 5030 | */ |
| 5031 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5032 | { |
| 5033 | |
| 5034 | if (info->raid_disks == 0) { |
| 5035 | /* just setting version number for superblock loading */ |
| 5036 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5037 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | super_types[info->major_version].name == NULL) { |
| 5039 | /* maybe try to auto-load a module? */ |
| 5040 | printk(KERN_INFO |
| 5041 | "md: superblock version %d not known\n", |
| 5042 | info->major_version); |
| 5043 | return -EINVAL; |
| 5044 | } |
| 5045 | mddev->major_version = info->major_version; |
| 5046 | mddev->minor_version = info->minor_version; |
| 5047 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5048 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5049 | return 0; |
| 5050 | } |
| 5051 | mddev->major_version = MD_MAJOR_VERSION; |
| 5052 | mddev->minor_version = MD_MINOR_VERSION; |
| 5053 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5054 | mddev->ctime = get_seconds(); |
| 5055 | |
| 5056 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5057 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5058 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | mddev->raid_disks = info->raid_disks; |
| 5060 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5061 | * openned |
| 5062 | */ |
| 5063 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5064 | mddev->recovery_cp = MaxSector; |
| 5065 | else |
| 5066 | mddev->recovery_cp = 0; |
| 5067 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5068 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | |
| 5070 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5071 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5072 | |
| 5073 | mddev->max_disks = MD_SB_DISKS; |
| 5074 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5075 | if (mddev->persistent) |
| 5076 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5077 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5079 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 5080 | mddev->bitmap_offset = 0; |
| 5081 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5082 | mddev->reshape_position = MaxSector; |
| 5083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | /* |
| 5085 | * Generate a 128 bit UUID |
| 5086 | */ |
| 5087 | get_random_bytes(mddev->uuid, 16); |
| 5088 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5089 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5090 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5091 | mddev->new_layout = mddev->layout; |
| 5092 | mddev->delta_disks = 0; |
| 5093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | return 0; |
| 5095 | } |
| 5096 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5097 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5098 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5099 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5100 | |
| 5101 | if (mddev->external_size) |
| 5102 | return; |
| 5103 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5104 | mddev->array_sectors = array_sectors; |
| 5105 | } |
| 5106 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5107 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5108 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5109 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5110 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5111 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5112 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5113 | |
| 5114 | if (mddev->pers->resize == NULL) |
| 5115 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5116 | /* The "num_sectors" is the number of sectors of each device that |
| 5117 | * is used. This can only make sense for arrays with redundancy. |
| 5118 | * linear and raid0 always use whatever space is available. We can only |
| 5119 | * consider changing this number if no resync or reconstruction is |
| 5120 | * 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] | 5121 | * 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] | 5122 | * of each device. If num_sectors is zero, we find the largest size |
| 5123 | * that fits. |
| 5124 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5125 | */ |
| 5126 | if (mddev->sync_thread) |
| 5127 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5128 | if (mddev->bitmap) |
| 5129 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5130 | * grow, and re-add. |
| 5131 | */ |
| 5132 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5133 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5134 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5135 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5136 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5137 | num_sectors = avail; |
| 5138 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5139 | return -ENOSPC; |
| 5140 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5141 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5142 | if (!rv) { |
| 5143 | struct block_device *bdev; |
| 5144 | |
| 5145 | bdev = bdget_disk(mddev->gendisk, 0); |
| 5146 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5147 | mutex_lock(&bdev->bd_inode->i_mutex); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5148 | i_size_write(bdev->bd_inode, |
| 5149 | (loff_t)mddev->array_sectors << 9); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5150 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5151 | bdput(bdev); |
| 5152 | } |
| 5153 | } |
| 5154 | return rv; |
| 5155 | } |
| 5156 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5157 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5158 | { |
| 5159 | int rv; |
| 5160 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5161 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5162 | return -EINVAL; |
| 5163 | if (raid_disks <= 0 || |
| 5164 | raid_disks >= mddev->max_disks) |
| 5165 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5166 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5167 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5168 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5169 | |
| 5170 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5171 | return rv; |
| 5172 | } |
| 5173 | |
| 5174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5175 | /* |
| 5176 | * update_array_info is used to change the configuration of an |
| 5177 | * on-line array. |
| 5178 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5179 | * fields in the info are checked against the array. |
| 5180 | * Any differences that cannot be handled will cause an error. |
| 5181 | * Normally, only one change can be managed at a time. |
| 5182 | */ |
| 5183 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5184 | { |
| 5185 | int rv = 0; |
| 5186 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5187 | int state = 0; |
| 5188 | |
| 5189 | /* calculate expected state,ignoring low bits */ |
| 5190 | if (mddev->bitmap && mddev->bitmap_offset) |
| 5191 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | |
| 5193 | if (mddev->major_version != info->major_version || |
| 5194 | mddev->minor_version != info->minor_version || |
| 5195 | /* mddev->patch_version != info->patch_version || */ |
| 5196 | mddev->ctime != info->ctime || |
| 5197 | mddev->level != info->level || |
| 5198 | /* mddev->layout != info->layout || */ |
| 5199 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5200 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5201 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5202 | ((state^info->state) & 0xfffffe00) |
| 5203 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | return -EINVAL; |
| 5205 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5206 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5207 | cnt++; |
| 5208 | if (mddev->raid_disks != info->raid_disks) |
| 5209 | cnt++; |
| 5210 | if (mddev->layout != info->layout) |
| 5211 | cnt++; |
| 5212 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5213 | cnt++; |
| 5214 | if (cnt == 0) |
| 5215 | return 0; |
| 5216 | if (cnt > 1) |
| 5217 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | |
| 5219 | if (mddev->layout != info->layout) { |
| 5220 | /* Change layout |
| 5221 | * we don't need to do anything at the md level, the |
| 5222 | * personality will take care of it all. |
| 5223 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5224 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5226 | else { |
| 5227 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5228 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5229 | if (rv) |
| 5230 | mddev->new_layout = mddev->layout; |
| 5231 | return rv; |
| 5232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5234 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5235 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5237 | if (mddev->raid_disks != info->raid_disks) |
| 5238 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5240 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5241 | if (mddev->pers->quiesce == NULL) |
| 5242 | return -EINVAL; |
| 5243 | if (mddev->recovery || mddev->sync_thread) |
| 5244 | return -EBUSY; |
| 5245 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5246 | /* add the bitmap */ |
| 5247 | if (mddev->bitmap) |
| 5248 | return -EEXIST; |
| 5249 | if (mddev->default_bitmap_offset == 0) |
| 5250 | return -EINVAL; |
| 5251 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 5252 | mddev->pers->quiesce(mddev, 1); |
| 5253 | rv = bitmap_create(mddev); |
| 5254 | if (rv) |
| 5255 | bitmap_destroy(mddev); |
| 5256 | mddev->pers->quiesce(mddev, 0); |
| 5257 | } else { |
| 5258 | /* remove the bitmap */ |
| 5259 | if (!mddev->bitmap) |
| 5260 | return -ENOENT; |
| 5261 | if (mddev->bitmap->file) |
| 5262 | return -EINVAL; |
| 5263 | mddev->pers->quiesce(mddev, 1); |
| 5264 | bitmap_destroy(mddev); |
| 5265 | mddev->pers->quiesce(mddev, 0); |
| 5266 | mddev->bitmap_offset = 0; |
| 5267 | } |
| 5268 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5269 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | return rv; |
| 5271 | } |
| 5272 | |
| 5273 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5274 | { |
| 5275 | mdk_rdev_t *rdev; |
| 5276 | |
| 5277 | if (mddev->pers == NULL) |
| 5278 | return -ENODEV; |
| 5279 | |
| 5280 | rdev = find_rdev(mddev, dev); |
| 5281 | if (!rdev) |
| 5282 | return -ENODEV; |
| 5283 | |
| 5284 | md_error(mddev, rdev); |
| 5285 | return 0; |
| 5286 | } |
| 5287 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5288 | /* |
| 5289 | * We have a problem here : there is no easy way to give a CHS |
| 5290 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5291 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5292 | * dosfs just mad... ;-) |
| 5293 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5294 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5295 | { |
| 5296 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5297 | |
| 5298 | geo->heads = 2; |
| 5299 | geo->sectors = 4; |
| 5300 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5301 | return 0; |
| 5302 | } |
| 5303 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5304 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | unsigned int cmd, unsigned long arg) |
| 5306 | { |
| 5307 | int err = 0; |
| 5308 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | mddev_t *mddev = NULL; |
| 5310 | |
| 5311 | if (!capable(CAP_SYS_ADMIN)) |
| 5312 | return -EACCES; |
| 5313 | |
| 5314 | /* |
| 5315 | * Commands dealing with the RAID driver but not any |
| 5316 | * particular array: |
| 5317 | */ |
| 5318 | switch (cmd) |
| 5319 | { |
| 5320 | case RAID_VERSION: |
| 5321 | err = get_version(argp); |
| 5322 | goto done; |
| 5323 | |
| 5324 | case PRINT_RAID_DEBUG: |
| 5325 | err = 0; |
| 5326 | md_print_devices(); |
| 5327 | goto done; |
| 5328 | |
| 5329 | #ifndef MODULE |
| 5330 | case RAID_AUTORUN: |
| 5331 | err = 0; |
| 5332 | autostart_arrays(arg); |
| 5333 | goto done; |
| 5334 | #endif |
| 5335 | default:; |
| 5336 | } |
| 5337 | |
| 5338 | /* |
| 5339 | * Commands creating/starting a new array: |
| 5340 | */ |
| 5341 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5342 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | |
| 5344 | if (!mddev) { |
| 5345 | BUG(); |
| 5346 | goto abort; |
| 5347 | } |
| 5348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | err = mddev_lock(mddev); |
| 5350 | if (err) { |
| 5351 | printk(KERN_INFO |
| 5352 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5353 | err, cmd); |
| 5354 | goto abort; |
| 5355 | } |
| 5356 | |
| 5357 | switch (cmd) |
| 5358 | { |
| 5359 | case SET_ARRAY_INFO: |
| 5360 | { |
| 5361 | mdu_array_info_t info; |
| 5362 | if (!arg) |
| 5363 | memset(&info, 0, sizeof(info)); |
| 5364 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5365 | err = -EFAULT; |
| 5366 | goto abort_unlock; |
| 5367 | } |
| 5368 | if (mddev->pers) { |
| 5369 | err = update_array_info(mddev, &info); |
| 5370 | if (err) { |
| 5371 | printk(KERN_WARNING "md: couldn't update" |
| 5372 | " array info. %d\n", err); |
| 5373 | goto abort_unlock; |
| 5374 | } |
| 5375 | goto done_unlock; |
| 5376 | } |
| 5377 | if (!list_empty(&mddev->disks)) { |
| 5378 | printk(KERN_WARNING |
| 5379 | "md: array %s already has disks!\n", |
| 5380 | mdname(mddev)); |
| 5381 | err = -EBUSY; |
| 5382 | goto abort_unlock; |
| 5383 | } |
| 5384 | if (mddev->raid_disks) { |
| 5385 | printk(KERN_WARNING |
| 5386 | "md: array %s already initialised!\n", |
| 5387 | mdname(mddev)); |
| 5388 | err = -EBUSY; |
| 5389 | goto abort_unlock; |
| 5390 | } |
| 5391 | err = set_array_info(mddev, &info); |
| 5392 | if (err) { |
| 5393 | printk(KERN_WARNING "md: couldn't set" |
| 5394 | " array info. %d\n", err); |
| 5395 | goto abort_unlock; |
| 5396 | } |
| 5397 | } |
| 5398 | goto done_unlock; |
| 5399 | |
| 5400 | default:; |
| 5401 | } |
| 5402 | |
| 5403 | /* |
| 5404 | * Commands querying/configuring an existing array: |
| 5405 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5406 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5407 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5408 | if ((!mddev->raid_disks && !mddev->external) |
| 5409 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5410 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5411 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | err = -ENODEV; |
| 5413 | goto abort_unlock; |
| 5414 | } |
| 5415 | |
| 5416 | /* |
| 5417 | * Commands even a read-only array can execute: |
| 5418 | */ |
| 5419 | switch (cmd) |
| 5420 | { |
| 5421 | case GET_ARRAY_INFO: |
| 5422 | err = get_array_info(mddev, argp); |
| 5423 | goto done_unlock; |
| 5424 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5425 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5426 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5427 | goto done_unlock; |
| 5428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | case GET_DISK_INFO: |
| 5430 | err = get_disk_info(mddev, argp); |
| 5431 | goto done_unlock; |
| 5432 | |
| 5433 | case RESTART_ARRAY_RW: |
| 5434 | err = restart_array(mddev); |
| 5435 | goto done_unlock; |
| 5436 | |
| 5437 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5438 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | goto done_unlock; |
| 5440 | |
| 5441 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5442 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | goto done_unlock; |
| 5444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | /* |
| 5448 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5449 | * superblock, so we do not allow them on read-only arrays. |
| 5450 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5451 | * here and hit the 'default' below, so only disallow |
| 5452 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5454 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5455 | if (mddev->ro == 2) { |
| 5456 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5457 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5458 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5459 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5460 | } else { |
| 5461 | err = -EROFS; |
| 5462 | goto abort_unlock; |
| 5463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | switch (cmd) |
| 5467 | { |
| 5468 | case ADD_NEW_DISK: |
| 5469 | { |
| 5470 | mdu_disk_info_t info; |
| 5471 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5472 | err = -EFAULT; |
| 5473 | else |
| 5474 | err = add_new_disk(mddev, &info); |
| 5475 | goto done_unlock; |
| 5476 | } |
| 5477 | |
| 5478 | case HOT_REMOVE_DISK: |
| 5479 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5480 | goto done_unlock; |
| 5481 | |
| 5482 | case HOT_ADD_DISK: |
| 5483 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5484 | goto done_unlock; |
| 5485 | |
| 5486 | case SET_DISK_FAULTY: |
| 5487 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5488 | goto done_unlock; |
| 5489 | |
| 5490 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5491 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | goto done_unlock; |
| 5493 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5494 | case SET_BITMAP_FILE: |
| 5495 | err = set_bitmap_file(mddev, (int)arg); |
| 5496 | goto done_unlock; |
| 5497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5498 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | err = -EINVAL; |
| 5500 | goto abort_unlock; |
| 5501 | } |
| 5502 | |
| 5503 | done_unlock: |
| 5504 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5505 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5506 | err != -EINVAL) |
| 5507 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | mddev_unlock(mddev); |
| 5509 | |
| 5510 | return err; |
| 5511 | done: |
| 5512 | if (err) |
| 5513 | MD_BUG(); |
| 5514 | abort: |
| 5515 | return err; |
| 5516 | } |
| 5517 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5518 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | { |
| 5520 | /* |
| 5521 | * Succeed if we can lock the mddev, which confirms that |
| 5522 | * it isn't being stopped right now. |
| 5523 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5524 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5525 | int err; |
| 5526 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5527 | if (mddev->gendisk != bdev->bd_disk) { |
| 5528 | /* we are racing with mddev_put which is discarding this |
| 5529 | * bd_disk. |
| 5530 | */ |
| 5531 | mddev_put(mddev); |
| 5532 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5533 | flush_scheduled_work(); |
| 5534 | /* Then retry the open from the top */ |
| 5535 | return -ERESTARTSYS; |
| 5536 | } |
| 5537 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5538 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5539 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5540 | goto out; |
| 5541 | |
| 5542 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5543 | atomic_inc(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5544 | mddev_unlock(mddev); |
| 5545 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5546 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | out: |
| 5548 | return err; |
| 5549 | } |
| 5550 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5551 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5553 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5555 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5556 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5557 | mddev_put(mddev); |
| 5558 | |
| 5559 | return 0; |
| 5560 | } |
| 5561 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5562 | static int md_media_changed(struct gendisk *disk) |
| 5563 | { |
| 5564 | mddev_t *mddev = disk->private_data; |
| 5565 | |
| 5566 | return mddev->changed; |
| 5567 | } |
| 5568 | |
| 5569 | static int md_revalidate(struct gendisk *disk) |
| 5570 | { |
| 5571 | mddev_t *mddev = disk->private_data; |
| 5572 | |
| 5573 | mddev->changed = 0; |
| 5574 | return 0; |
| 5575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | static struct block_device_operations md_fops = |
| 5577 | { |
| 5578 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5579 | .open = md_open, |
| 5580 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5581 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5582 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5583 | .media_changed = md_media_changed, |
| 5584 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5585 | }; |
| 5586 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5587 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | { |
| 5589 | mdk_thread_t *thread = arg; |
| 5590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | /* |
| 5592 | * md_thread is a 'system-thread', it's priority should be very |
| 5593 | * high. We avoid resource deadlocks individually in each |
| 5594 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5595 | * the very same RT priority as kswapd, thus we will never get |
| 5596 | * into a priority inversion deadlock. |
| 5597 | * |
| 5598 | * we definitely have to have equal or higher priority than |
| 5599 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5600 | * many dirty RAID5 blocks. |
| 5601 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5603 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5604 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5606 | /* We need to wait INTERRUPTIBLE so that |
| 5607 | * we don't add to the load-average. |
| 5608 | * That means we need to be sure no signals are |
| 5609 | * pending |
| 5610 | */ |
| 5611 | if (signal_pending(current)) |
| 5612 | flush_signals(current); |
| 5613 | |
| 5614 | wait_event_interruptible_timeout |
| 5615 | (thread->wqueue, |
| 5616 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5617 | || kthread_should_stop(), |
| 5618 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | |
| 5620 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5621 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5622 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | return 0; |
| 5626 | } |
| 5627 | |
| 5628 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5629 | { |
| 5630 | if (thread) { |
| 5631 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5632 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5633 | wake_up(&thread->wqueue); |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5638 | const char *name) |
| 5639 | { |
| 5640 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5642 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | if (!thread) |
| 5644 | return NULL; |
| 5645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | init_waitqueue_head(&thread->wqueue); |
| 5647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | thread->run = run; |
| 5649 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5650 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5651 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5652 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | kfree(thread); |
| 5654 | return NULL; |
| 5655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | return thread; |
| 5657 | } |
| 5658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5659 | void md_unregister_thread(mdk_thread_t *thread) |
| 5660 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5661 | if (!thread) |
| 5662 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5663 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5664 | |
| 5665 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | kfree(thread); |
| 5667 | } |
| 5668 | |
| 5669 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5670 | { |
| 5671 | if (!mddev) { |
| 5672 | MD_BUG(); |
| 5673 | return; |
| 5674 | } |
| 5675 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5676 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5677 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5678 | |
| 5679 | if (mddev->external) |
| 5680 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5681 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5683 | mdname(mddev), |
| 5684 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5685 | __builtin_return_address(0),__builtin_return_address(1), |
| 5686 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5687 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5688 | if (!mddev->pers) |
| 5689 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | if (!mddev->pers->error_handler) |
| 5691 | return; |
| 5692 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5693 | if (mddev->degraded) |
| 5694 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5695 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5697 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5698 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5699 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | /* seq_file implementation /proc/mdstat */ |
| 5703 | |
| 5704 | static void status_unused(struct seq_file *seq) |
| 5705 | { |
| 5706 | int i = 0; |
| 5707 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | |
| 5709 | seq_printf(seq, "unused devices: "); |
| 5710 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5711 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | char b[BDEVNAME_SIZE]; |
| 5713 | i++; |
| 5714 | seq_printf(seq, "%s ", |
| 5715 | bdevname(rdev->bdev,b)); |
| 5716 | } |
| 5717 | if (!i) |
| 5718 | seq_printf(seq, "<none>"); |
| 5719 | |
| 5720 | seq_printf(seq, "\n"); |
| 5721 | } |
| 5722 | |
| 5723 | |
| 5724 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5725 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5726 | sector_t max_sectors, resync, res; |
| 5727 | unsigned long dt, db; |
| 5728 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5729 | int scale; |
| 5730 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5732 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | |
| 5734 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5735 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5737 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | |
| 5739 | /* |
| 5740 | * Should not happen. |
| 5741 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5742 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | MD_BUG(); |
| 5744 | return; |
| 5745 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5746 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5747 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5748 | * u32, as those are the requirements for sector_div. |
| 5749 | * Thus 'scale' must be at least 10 |
| 5750 | */ |
| 5751 | scale = 10; |
| 5752 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5753 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5754 | scale++; |
| 5755 | } |
| 5756 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5757 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5758 | |
| 5759 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5761 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | seq_printf(seq, "["); |
| 5763 | for (i = 0; i < x; i++) |
| 5764 | seq_printf(seq, "="); |
| 5765 | seq_printf(seq, ">"); |
| 5766 | for (i = 0; i < y; i++) |
| 5767 | seq_printf(seq, "."); |
| 5768 | seq_printf(seq, "] "); |
| 5769 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5770 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5771 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5772 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5773 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5774 | "check" : |
| 5775 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5776 | "resync" : "recovery"))), |
| 5777 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5778 | (unsigned long long) resync/2, |
| 5779 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | |
| 5781 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | * dt: time from mark until now |
| 5783 | * db: blocks written from mark until now |
| 5784 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5785 | * |
| 5786 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5787 | * So we divide before multiply in case it is 32bit and close |
| 5788 | * to the limit. |
| 5789 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5790 | * near the end of resync when the number of remaining sectors |
| 5791 | * is close to 'db'. |
| 5792 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5793 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | */ |
| 5795 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5796 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5797 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5798 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5800 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5801 | sector_div(rt, db/32+1); |
| 5802 | rt *= dt; |
| 5803 | rt >>= 5; |
| 5804 | |
| 5805 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5806 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5808 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | } |
| 5810 | |
| 5811 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5812 | { |
| 5813 | struct list_head *tmp; |
| 5814 | loff_t l = *pos; |
| 5815 | mddev_t *mddev; |
| 5816 | |
| 5817 | if (l >= 0x10000) |
| 5818 | return NULL; |
| 5819 | if (!l--) |
| 5820 | /* header */ |
| 5821 | return (void*)1; |
| 5822 | |
| 5823 | spin_lock(&all_mddevs_lock); |
| 5824 | list_for_each(tmp,&all_mddevs) |
| 5825 | if (!l--) { |
| 5826 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5827 | mddev_get(mddev); |
| 5828 | spin_unlock(&all_mddevs_lock); |
| 5829 | return mddev; |
| 5830 | } |
| 5831 | spin_unlock(&all_mddevs_lock); |
| 5832 | if (!l--) |
| 5833 | return (void*)2;/* tail */ |
| 5834 | return NULL; |
| 5835 | } |
| 5836 | |
| 5837 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5838 | { |
| 5839 | struct list_head *tmp; |
| 5840 | mddev_t *next_mddev, *mddev = v; |
| 5841 | |
| 5842 | ++*pos; |
| 5843 | if (v == (void*)2) |
| 5844 | return NULL; |
| 5845 | |
| 5846 | spin_lock(&all_mddevs_lock); |
| 5847 | if (v == (void*)1) |
| 5848 | tmp = all_mddevs.next; |
| 5849 | else |
| 5850 | tmp = mddev->all_mddevs.next; |
| 5851 | if (tmp != &all_mddevs) |
| 5852 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5853 | else { |
| 5854 | next_mddev = (void*)2; |
| 5855 | *pos = 0x10000; |
| 5856 | } |
| 5857 | spin_unlock(&all_mddevs_lock); |
| 5858 | |
| 5859 | if (v != (void*)1) |
| 5860 | mddev_put(mddev); |
| 5861 | return next_mddev; |
| 5862 | |
| 5863 | } |
| 5864 | |
| 5865 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5866 | { |
| 5867 | mddev_t *mddev = v; |
| 5868 | |
| 5869 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5870 | mddev_put(mddev); |
| 5871 | } |
| 5872 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5873 | struct mdstat_info { |
| 5874 | int event; |
| 5875 | }; |
| 5876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5878 | { |
| 5879 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5880 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5882 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5883 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | |
| 5885 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5886 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | seq_printf(seq, "Personalities : "); |
| 5888 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5889 | list_for_each_entry(pers, &pers_list, list) |
| 5890 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5891 | |
| 5892 | spin_unlock(&pers_lock); |
| 5893 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5894 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | return 0; |
| 5896 | } |
| 5897 | if (v == (void*)2) { |
| 5898 | status_unused(seq); |
| 5899 | return 0; |
| 5900 | } |
| 5901 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5902 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5906 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5907 | mddev->pers ? "" : "in"); |
| 5908 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5909 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5911 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5912 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | seq_printf(seq, " %s", mddev->pers->name); |
| 5914 | } |
| 5915 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5916 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5917 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | char b[BDEVNAME_SIZE]; |
| 5919 | seq_printf(seq, " %s[%d]", |
| 5920 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5921 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5922 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5923 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | seq_printf(seq, "(F)"); |
| 5925 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5926 | } else if (rdev->raid_disk < 0) |
| 5927 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5928 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | } |
| 5930 | |
| 5931 | if (!list_empty(&mddev->disks)) { |
| 5932 | if (mddev->pers) |
| 5933 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5934 | (unsigned long long) |
| 5935 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | else |
| 5937 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5938 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5940 | if (mddev->persistent) { |
| 5941 | if (mddev->major_version != 0 || |
| 5942 | mddev->minor_version != 90) { |
| 5943 | seq_printf(seq," super %d.%d", |
| 5944 | mddev->major_version, |
| 5945 | mddev->minor_version); |
| 5946 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5947 | } else if (mddev->external) |
| 5948 | seq_printf(seq, " super external:%s", |
| 5949 | mddev->metadata_type); |
| 5950 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5951 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | |
| 5953 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5954 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5956 | if (mddev->pers->sync_request) { |
| 5957 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5958 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5959 | seq_printf(seq, "\n "); |
| 5960 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5961 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5962 | else if (mddev->recovery_cp < MaxSector) |
| 5963 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5964 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5965 | } else |
| 5966 | seq_printf(seq, "\n "); |
| 5967 | |
| 5968 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5969 | unsigned long chunk_kb; |
| 5970 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5971 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5972 | chunk_kb = bitmap->chunksize >> 10; |
| 5973 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5974 | "%lu%s chunk", |
| 5975 | bitmap->pages - bitmap->missing_pages, |
| 5976 | bitmap->pages, |
| 5977 | (bitmap->pages - bitmap->missing_pages) |
| 5978 | << (PAGE_SHIFT - 10), |
| 5979 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5980 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5981 | if (bitmap->file) { |
| 5982 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5983 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5984 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5985 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5986 | seq_printf(seq, "\n"); |
| 5987 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | } |
| 5989 | |
| 5990 | seq_printf(seq, "\n"); |
| 5991 | } |
| 5992 | mddev_unlock(mddev); |
| 5993 | |
| 5994 | return 0; |
| 5995 | } |
| 5996 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 5997 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | .start = md_seq_start, |
| 5999 | .next = md_seq_next, |
| 6000 | .stop = md_seq_stop, |
| 6001 | .show = md_seq_show, |
| 6002 | }; |
| 6003 | |
| 6004 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6005 | { |
| 6006 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6007 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6008 | if (mi == NULL) |
| 6009 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | |
| 6011 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6012 | if (error) |
| 6013 | kfree(mi); |
| 6014 | else { |
| 6015 | struct seq_file *p = file->private_data; |
| 6016 | p->private = mi; |
| 6017 | mi->event = atomic_read(&md_event_count); |
| 6018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | return error; |
| 6020 | } |
| 6021 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6022 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6023 | { |
| 6024 | struct seq_file *m = filp->private_data; |
| 6025 | struct mdstat_info *mi = m->private; |
| 6026 | int mask; |
| 6027 | |
| 6028 | poll_wait(filp, &md_event_waiters, wait); |
| 6029 | |
| 6030 | /* always allow read */ |
| 6031 | mask = POLLIN | POLLRDNORM; |
| 6032 | |
| 6033 | if (mi->event != atomic_read(&md_event_count)) |
| 6034 | mask |= POLLERR | POLLPRI; |
| 6035 | return mask; |
| 6036 | } |
| 6037 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6038 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6039 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | .open = md_seq_open, |
| 6041 | .read = seq_read, |
| 6042 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6043 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6044 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | }; |
| 6046 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6047 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6050 | list_add_tail(&p->list, &pers_list); |
| 6051 | 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] | 6052 | spin_unlock(&pers_lock); |
| 6053 | return 0; |
| 6054 | } |
| 6055 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6056 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6058 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6060 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | spin_unlock(&pers_lock); |
| 6062 | return 0; |
| 6063 | } |
| 6064 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6065 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | { |
| 6067 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6069 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | |
| 6071 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6072 | rcu_read_lock(); |
| 6073 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6075 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6076 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6077 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6078 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6079 | * as sync_io is counted when a request starts, and |
| 6080 | * disk_stats is counted when it completes. |
| 6081 | * So resync activity will cause curr_events to be smaller than |
| 6082 | * when there was no such activity. |
| 6083 | * non-sync IO will cause disk_stat to increase without |
| 6084 | * increasing sync_io so curr_events will (eventually) |
| 6085 | * be larger than it was before. Once it becomes |
| 6086 | * substantially larger, the test below will cause |
| 6087 | * the array to appear non-idle, and resync will slow |
| 6088 | * down. |
| 6089 | * If there is a lot of outstanding resync activity when |
| 6090 | * we set last_event to curr_events, then all that activity |
| 6091 | * completing might cause the array to appear non-idle |
| 6092 | * and resync will be slowed down even though there might |
| 6093 | * not have been non-resync activity. This will only |
| 6094 | * happen once though. 'last_events' will soon reflect |
| 6095 | * the state where there is little or no outstanding |
| 6096 | * resync requests, and further resync activity will |
| 6097 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6098 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6100 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | rdev->last_events = curr_events; |
| 6102 | idle = 0; |
| 6103 | } |
| 6104 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6105 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | return idle; |
| 6107 | } |
| 6108 | |
| 6109 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6110 | { |
| 6111 | /* another "blocks" (512byte) blocks have been synced */ |
| 6112 | atomic_sub(blocks, &mddev->recovery_active); |
| 6113 | wake_up(&mddev->recovery_wait); |
| 6114 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6115 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | md_wakeup_thread(mddev->thread); |
| 6117 | // stop recovery, signal do_sync .... |
| 6118 | } |
| 6119 | } |
| 6120 | |
| 6121 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6122 | /* md_write_start(mddev, bi) |
| 6123 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6124 | * in superblock) before writing, schedule a superblock update |
| 6125 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6126 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6127 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6129 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6130 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6131 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6132 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6133 | BUG_ON(mddev->ro == 1); |
| 6134 | if (mddev->ro == 2) { |
| 6135 | /* need to switch to read/write */ |
| 6136 | mddev->ro = 0; |
| 6137 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6138 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6139 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6140 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6141 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6142 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6143 | if (mddev->safemode == 1) |
| 6144 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6145 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6146 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6147 | if (mddev->in_sync) { |
| 6148 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6149 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6150 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6151 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6152 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6153 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6154 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6155 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6156 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6157 | wait_event(mddev->sb_wait, |
| 6158 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6159 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | void md_write_end(mddev_t *mddev) |
| 6163 | { |
| 6164 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6165 | if (mddev->safemode == 2) |
| 6166 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6167 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6169 | } |
| 6170 | } |
| 6171 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6172 | /* md_allow_write(mddev) |
| 6173 | * Calling this ensures that the array is marked 'active' so that writes |
| 6174 | * may proceed without blocking. It is important to call this before |
| 6175 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6176 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6177 | * |
| 6178 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6179 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6180 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6181 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6182 | { |
| 6183 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6184 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6185 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6186 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6187 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6188 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6189 | |
| 6190 | spin_lock_irq(&mddev->write_lock); |
| 6191 | if (mddev->in_sync) { |
| 6192 | mddev->in_sync = 0; |
| 6193 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6194 | if (mddev->safemode_delay && |
| 6195 | mddev->safemode == 0) |
| 6196 | mddev->safemode = 1; |
| 6197 | spin_unlock_irq(&mddev->write_lock); |
| 6198 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6199 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6200 | } else |
| 6201 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6202 | |
| 6203 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6204 | return -EAGAIN; |
| 6205 | else |
| 6206 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6207 | } |
| 6208 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | #define SYNC_MARKS 10 |
| 6211 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6212 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | { |
| 6214 | mddev_t *mddev2; |
| 6215 | unsigned int currspeed = 0, |
| 6216 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6217 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | unsigned long mark[SYNC_MARKS]; |
| 6219 | sector_t mark_cnt[SYNC_MARKS]; |
| 6220 | int last_mark,m; |
| 6221 | struct list_head *tmp; |
| 6222 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6223 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6224 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6225 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | |
| 6227 | /* just incase thread restarts... */ |
| 6228 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6229 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6230 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6231 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6232 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6233 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6234 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6235 | desc = "data-check"; |
| 6236 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6237 | desc = "requested-resync"; |
| 6238 | else |
| 6239 | desc = "resync"; |
| 6240 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6241 | desc = "reshape"; |
| 6242 | else |
| 6243 | desc = "recovery"; |
| 6244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | /* we overload curr_resync somewhat here. |
| 6246 | * 0 == not engaged in resync at all |
| 6247 | * 2 == checking that there is no conflict with another sync |
| 6248 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6249 | * commense |
| 6250 | * other == active in resync - this many blocks |
| 6251 | * |
| 6252 | * Before starting a resync we must have set curr_resync to |
| 6253 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6254 | * less than ours. When we find one that is the same or higher |
| 6255 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6256 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6257 | * This will mean we have to start checking from the beginning again. |
| 6258 | * |
| 6259 | */ |
| 6260 | |
| 6261 | do { |
| 6262 | mddev->curr_resync = 2; |
| 6263 | |
| 6264 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6265 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6266 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6267 | goto skip; |
| 6268 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6269 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | if (mddev2 == mddev) |
| 6271 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6272 | if (!mddev->parallel_resync |
| 6273 | && mddev2->curr_resync |
| 6274 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | DEFINE_WAIT(wq); |
| 6276 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6277 | /* arbitrarily yield */ |
| 6278 | mddev->curr_resync = 1; |
| 6279 | wake_up(&resync_wait); |
| 6280 | } |
| 6281 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6282 | /* no need to wait here, we can wait the next |
| 6283 | * time 'round when curr_resync == 2 |
| 6284 | */ |
| 6285 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6286 | /* We need to wait 'interruptible' so as not to |
| 6287 | * contribute to the load average, and not to |
| 6288 | * be caught by 'softlockup' |
| 6289 | */ |
| 6290 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6291 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6292 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6293 | printk(KERN_INFO "md: delaying %s of %s" |
| 6294 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6296 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6298 | if (signal_pending(current)) |
| 6299 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | schedule(); |
| 6301 | finish_wait(&resync_wait, &wq); |
| 6302 | goto try_again; |
| 6303 | } |
| 6304 | finish_wait(&resync_wait, &wq); |
| 6305 | } |
| 6306 | } |
| 6307 | } while (mddev->curr_resync < 2); |
| 6308 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6309 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6310 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6312 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | */ |
| 6314 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6315 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6316 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6317 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6318 | j = mddev->resync_min; |
| 6319 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6320 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6321 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6322 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6323 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6324 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6325 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6326 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6327 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6328 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6329 | if (rdev->raid_disk >= 0 && |
| 6330 | !test_bit(Faulty, &rdev->flags) && |
| 6331 | !test_bit(In_sync, &rdev->flags) && |
| 6332 | rdev->recovery_offset < j) |
| 6333 | j = rdev->recovery_offset; |
| 6334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6335 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6336 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6337 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6338 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6339 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6340 | "(but not more than %d KB/sec) for %s.\n", |
| 6341 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6343 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6344 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6345 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6347 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6348 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6349 | } |
| 6350 | last_mark = 0; |
| 6351 | mddev->resync_mark = mark[last_mark]; |
| 6352 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6353 | |
| 6354 | /* |
| 6355 | * Tune reconstruction: |
| 6356 | */ |
| 6357 | window = 32*(PAGE_SIZE/512); |
| 6358 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6359 | window/2,(unsigned long long) max_sectors/2); |
| 6360 | |
| 6361 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | last_check = 0; |
| 6363 | |
| 6364 | if (j>2) { |
| 6365 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6366 | "md: resuming %s of %s from checkpoint.\n", |
| 6367 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | mddev->curr_resync = j; |
| 6369 | } |
| 6370 | |
| 6371 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6372 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6374 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6375 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6376 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6377 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6378 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6379 | > (max_sectors >> 4)) || |
| 6380 | (j - mddev->curr_resync_completed)*2 |
| 6381 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6382 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6383 | /* time to update curr_resync_completed */ |
| 6384 | blk_unplug(mddev->queue); |
| 6385 | wait_event(mddev->recovery_wait, |
| 6386 | atomic_read(&mddev->recovery_active) == 0); |
| 6387 | mddev->curr_resync_completed = |
| 6388 | mddev->curr_resync; |
| 6389 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6390 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6391 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6392 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6393 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6394 | /* As this condition is controlled by user-space, |
| 6395 | * we can block indefinitely, so use '_interruptible' |
| 6396 | * to avoid triggering warnings. |
| 6397 | */ |
| 6398 | flush_signals(current); /* just in case */ |
| 6399 | wait_event_interruptible(mddev->recovery_wait, |
| 6400 | mddev->resync_max > j |
| 6401 | || kthread_should_stop()); |
| 6402 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6403 | |
| 6404 | if (kthread_should_stop()) |
| 6405 | goto interrupted; |
| 6406 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6407 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6408 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6409 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6410 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6411 | goto out; |
| 6412 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6413 | |
| 6414 | if (!skipped) { /* actual IO requested */ |
| 6415 | io_sectors += sectors; |
| 6416 | atomic_add(sectors, &mddev->recovery_active); |
| 6417 | } |
| 6418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | j += sectors; |
| 6420 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6421 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6422 | if (last_check == 0) |
| 6423 | /* this is the earliers that rebuilt will be |
| 6424 | * visible in /proc/mdstat |
| 6425 | */ |
| 6426 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6427 | |
| 6428 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6429 | continue; |
| 6430 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6431 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6433 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | break; |
| 6435 | |
| 6436 | repeat: |
| 6437 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6438 | /* step marks */ |
| 6439 | int next = (last_mark+1) % SYNC_MARKS; |
| 6440 | |
| 6441 | mddev->resync_mark = mark[next]; |
| 6442 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6443 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6444 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6445 | last_mark = next; |
| 6446 | } |
| 6447 | |
| 6448 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6449 | if (kthread_should_stop()) |
| 6450 | goto interrupted; |
| 6451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | |
| 6453 | /* |
| 6454 | * this loop exits only if either when we are slower than |
| 6455 | * the 'hard' speed limit, or the system was IO-idle for |
| 6456 | * a jiffy. |
| 6457 | * the system might be non-idle CPU-wise, but we only care |
| 6458 | * about not overloading the IO subsystem. (things like an |
| 6459 | * e2fsck being done on the RAID array should execute fast) |
| 6460 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6461 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | cond_resched(); |
| 6463 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6464 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6465 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6467 | if (currspeed > speed_min(mddev)) { |
| 6468 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6469 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6470 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | goto repeat; |
| 6472 | } |
| 6473 | } |
| 6474 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6475 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | /* |
| 6477 | * this also signals 'finished resyncing' to md_stop |
| 6478 | */ |
| 6479 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6480 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | |
| 6482 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6483 | |
| 6484 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6485 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6487 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6488 | mddev->curr_resync > 2) { |
| 6489 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6490 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6491 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6492 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6493 | "md: checkpointing %s of %s.\n", |
| 6494 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6495 | mddev->recovery_cp = mddev->curr_resync; |
| 6496 | } |
| 6497 | } else |
| 6498 | mddev->recovery_cp = MaxSector; |
| 6499 | } else { |
| 6500 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6501 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6502 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6503 | if (rdev->raid_disk >= 0 && |
| 6504 | !test_bit(Faulty, &rdev->flags) && |
| 6505 | !test_bit(In_sync, &rdev->flags) && |
| 6506 | rdev->recovery_offset < mddev->curr_resync) |
| 6507 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6510 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6512 | skip: |
| 6513 | mddev->curr_resync = 0; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6514 | mddev->curr_resync_completed = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6515 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6516 | mddev->resync_max = MaxSector; |
| 6517 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | wake_up(&resync_wait); |
| 6519 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6520 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6521 | return; |
| 6522 | |
| 6523 | interrupted: |
| 6524 | /* |
| 6525 | * got a signal, exit. |
| 6526 | */ |
| 6527 | printk(KERN_INFO |
| 6528 | "md: md_do_sync() got signal ... exiting\n"); |
| 6529 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6530 | goto out; |
| 6531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6532 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6533 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | |
| 6535 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6536 | static int remove_and_add_spares(mddev_t *mddev) |
| 6537 | { |
| 6538 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6539 | int spares = 0; |
| 6540 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6541 | mddev->curr_resync_completed = 0; |
| 6542 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6543 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6544 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6545 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6546 | (test_bit(Faulty, &rdev->flags) || |
| 6547 | ! test_bit(In_sync, &rdev->flags)) && |
| 6548 | atomic_read(&rdev->nr_pending)==0) { |
| 6549 | if (mddev->pers->hot_remove_disk( |
| 6550 | mddev, rdev->raid_disk)==0) { |
| 6551 | char nm[20]; |
| 6552 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6553 | sysfs_remove_link(&mddev->kobj, nm); |
| 6554 | rdev->raid_disk = -1; |
| 6555 | } |
| 6556 | } |
| 6557 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6558 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6559 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6560 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6561 | !test_bit(In_sync, &rdev->flags) && |
| 6562 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6563 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6564 | if (rdev->raid_disk < 0 |
| 6565 | && !test_bit(Faulty, &rdev->flags)) { |
| 6566 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6567 | if (mddev->pers-> |
| 6568 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6569 | char nm[20]; |
| 6570 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6571 | if (sysfs_create_link(&mddev->kobj, |
| 6572 | &rdev->kobj, nm)) |
| 6573 | printk(KERN_WARNING |
| 6574 | "md: cannot register " |
| 6575 | "%s for %s\n", |
| 6576 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6577 | spares++; |
| 6578 | md_new_event(mddev); |
| 6579 | } else |
| 6580 | break; |
| 6581 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6582 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6583 | } |
| 6584 | return spares; |
| 6585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | /* |
| 6587 | * This routine is regularly called by all per-raid-array threads to |
| 6588 | * deal with generic issues like resync and super-block update. |
| 6589 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6590 | * need this as they never do any recovery or update the superblock. |
| 6591 | * |
| 6592 | * It does not do any resync itself, but rather "forks" off other threads |
| 6593 | * to do that as needed. |
| 6594 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6595 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6596 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | * and wakeups up this thread which will reap the thread and finish up. |
| 6598 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6599 | * |
| 6600 | * The overall approach is: |
| 6601 | * 1/ if the superblock needs updating, update it. |
| 6602 | * 2/ If a recovery thread is running, don't do anything else. |
| 6603 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6604 | * 4/ If there are any faulty devices, remove them. |
| 6605 | * 5/ If array is degraded, try to add spares devices |
| 6606 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6607 | */ |
| 6608 | void md_check_recovery(mddev_t *mddev) |
| 6609 | { |
| 6610 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | |
| 6612 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6613 | if (mddev->bitmap) |
| 6614 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | |
| 6616 | if (mddev->ro) |
| 6617 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6618 | |
| 6619 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6620 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6621 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6622 | mdname(mddev)); |
| 6623 | mddev->safemode = 2; |
| 6624 | } |
| 6625 | flush_signals(current); |
| 6626 | } |
| 6627 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6628 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6629 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6631 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6632 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6633 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6634 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6635 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6636 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | )) |
| 6638 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6639 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6640 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6641 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6642 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6643 | if (mddev->ro) { |
| 6644 | /* Only thing we do on a ro array is remove |
| 6645 | * failed devices. |
| 6646 | */ |
| 6647 | remove_and_add_spares(mddev); |
| 6648 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6649 | goto unlock; |
| 6650 | } |
| 6651 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6652 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6653 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6654 | spin_lock_irq(&mddev->write_lock); |
| 6655 | if (mddev->safemode && |
| 6656 | !atomic_read(&mddev->writes_pending) && |
| 6657 | !mddev->in_sync && |
| 6658 | mddev->recovery_cp == MaxSector) { |
| 6659 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6660 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6661 | if (mddev->persistent) |
| 6662 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6663 | } |
| 6664 | if (mddev->safemode == 1) |
| 6665 | mddev->safemode = 0; |
| 6666 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6667 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6668 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6669 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6670 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6671 | if (mddev->flags) |
| 6672 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6673 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6674 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6675 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6676 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6677 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6679 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6680 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6681 | /* resync/recovery still happening */ |
| 6682 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6683 | goto unlock; |
| 6684 | } |
| 6685 | if (mddev->sync_thread) { |
| 6686 | /* resync has finished, collect result */ |
| 6687 | md_unregister_thread(mddev->sync_thread); |
| 6688 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6689 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6690 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6691 | /* success...*/ |
| 6692 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6693 | if (mddev->pers->spare_active(mddev)) |
| 6694 | sysfs_notify(&mddev->kobj, NULL, |
| 6695 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6696 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6697 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6698 | mddev->pers->finish_reshape) |
| 6699 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6700 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6701 | |
| 6702 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6703 | * information must be scrapped |
| 6704 | */ |
| 6705 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6706 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6707 | rdev->saved_raid_disk = -1; |
| 6708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | mddev->recovery = 0; |
| 6710 | /* flag recovery needed just to double check */ |
| 6711 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6712 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6713 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | goto unlock; |
| 6715 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6716 | /* Set RUNNING before clearing NEEDED to avoid |
| 6717 | * any transients in the value of "sync_action". |
| 6718 | */ |
| 6719 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6720 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6721 | /* Clear some bits that don't mean anything, but |
| 6722 | * might be left set |
| 6723 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6724 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6725 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6726 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6727 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6728 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6729 | /* no recovery is running. |
| 6730 | * remove any failed drives, then |
| 6731 | * add spares if possible. |
| 6732 | * Spare are also removed and re-added, to allow |
| 6733 | * the personality to fail the re-add. |
| 6734 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6736 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6737 | if (mddev->pers->check_reshape == NULL || |
| 6738 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6739 | /* Cannot proceed */ |
| 6740 | goto unlock; |
| 6741 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6742 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6743 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6744 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6745 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6746 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6747 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6748 | } else if (mddev->recovery_cp < MaxSector) { |
| 6749 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6750 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6751 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6752 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6755 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6756 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6757 | /* We are adding a device or devices to an array |
| 6758 | * which has the bitmap stored on all devices. |
| 6759 | * So make sure all bitmap pages get written |
| 6760 | */ |
| 6761 | bitmap_write_all(mddev->bitmap); |
| 6762 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6763 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6764 | mddev, |
| 6765 | "%s_resync"); |
| 6766 | if (!mddev->sync_thread) { |
| 6767 | printk(KERN_ERR "%s: could not start resync" |
| 6768 | " thread...\n", |
| 6769 | mdname(mddev)); |
| 6770 | /* leave the spares where they are, it shouldn't hurt */ |
| 6771 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6772 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6774 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6775 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6776 | } |
| 6777 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6778 | if (!mddev->sync_thread) { |
| 6779 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6780 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6781 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6782 | if (mddev->sysfs_action) |
| 6783 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6784 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | mddev_unlock(mddev); |
| 6786 | } |
| 6787 | } |
| 6788 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6789 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6790 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6791 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6792 | wait_event_timeout(rdev->blocked_wait, |
| 6793 | !test_bit(Blocked, &rdev->flags), |
| 6794 | msecs_to_jiffies(5000)); |
| 6795 | rdev_dec_pending(rdev, mddev); |
| 6796 | } |
| 6797 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6798 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6799 | static int md_notify_reboot(struct notifier_block *this, |
| 6800 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | { |
| 6802 | struct list_head *tmp; |
| 6803 | mddev_t *mddev; |
| 6804 | |
| 6805 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6806 | |
| 6807 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6808 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6809 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6810 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6811 | /* Force a switch to readonly even array |
| 6812 | * appears to still be in use. Hence |
| 6813 | * the '100'. |
| 6814 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6815 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6816 | mddev_unlock(mddev); |
| 6817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | /* |
| 6819 | * certain more exotic SCSI devices are known to be |
| 6820 | * volatile wrt too early system reboots. While the |
| 6821 | * right place to handle this issue is the given |
| 6822 | * driver, we do want to have a safe RAID driver ... |
| 6823 | */ |
| 6824 | mdelay(1000*1); |
| 6825 | } |
| 6826 | return NOTIFY_DONE; |
| 6827 | } |
| 6828 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6829 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | .notifier_call = md_notify_reboot, |
| 6831 | .next = NULL, |
| 6832 | .priority = INT_MAX, /* before any real devices */ |
| 6833 | }; |
| 6834 | |
| 6835 | static void md_geninit(void) |
| 6836 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6838 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6839 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | } |
| 6841 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6842 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6844 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | return -1; |
| 6846 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6847 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | return -1; |
| 6849 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6850 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6851 | md_probe, NULL, NULL); |
| 6852 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | md_probe, NULL, NULL); |
| 6854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6856 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | |
| 6858 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6859 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | } |
| 6861 | |
| 6862 | |
| 6863 | #ifndef MODULE |
| 6864 | |
| 6865 | /* |
| 6866 | * Searches all registered partitions for autorun RAID arrays |
| 6867 | * at boot time. |
| 6868 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6869 | |
| 6870 | static LIST_HEAD(all_detected_devices); |
| 6871 | struct detected_devices_node { |
| 6872 | struct list_head list; |
| 6873 | dev_t dev; |
| 6874 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | |
| 6876 | void md_autodetect_dev(dev_t dev) |
| 6877 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6878 | struct detected_devices_node *node_detected_dev; |
| 6879 | |
| 6880 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6881 | if (node_detected_dev) { |
| 6882 | node_detected_dev->dev = dev; |
| 6883 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6884 | } else { |
| 6885 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6886 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6888 | } |
| 6889 | |
| 6890 | |
| 6891 | static void autostart_arrays(int part) |
| 6892 | { |
| 6893 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6894 | struct detected_devices_node *node_detected_dev; |
| 6895 | dev_t dev; |
| 6896 | int i_scanned, i_passed; |
| 6897 | |
| 6898 | i_scanned = 0; |
| 6899 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6900 | |
| 6901 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6902 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6903 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6904 | i_scanned++; |
| 6905 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6906 | struct detected_devices_node, list); |
| 6907 | list_del(&node_detected_dev->list); |
| 6908 | dev = node_detected_dev->dev; |
| 6909 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6910 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | if (IS_ERR(rdev)) |
| 6912 | continue; |
| 6913 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6914 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | MD_BUG(); |
| 6916 | continue; |
| 6917 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6918 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6919 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6920 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6922 | |
| 6923 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6924 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6925 | |
| 6926 | autorun_devices(part); |
| 6927 | } |
| 6928 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6929 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | |
| 6931 | static __exit void md_exit(void) |
| 6932 | { |
| 6933 | mddev_t *mddev; |
| 6934 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6935 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6936 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6937 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6938 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6939 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6940 | unregister_blkdev(mdp_major, "mdp"); |
| 6941 | unregister_reboot_notifier(&md_notifier); |
| 6942 | unregister_sysctl_table(raid_table_header); |
| 6943 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6944 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6945 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6946 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6947 | } |
| 6948 | } |
| 6949 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6950 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | module_exit(md_exit) |
| 6952 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6953 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6954 | { |
| 6955 | return sprintf(buffer, "%d", start_readonly); |
| 6956 | } |
| 6957 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6958 | { |
| 6959 | char *e; |
| 6960 | int num = simple_strtoul(val, &e, 10); |
| 6961 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6962 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6963 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6964 | } |
| 6965 | return -EINVAL; |
| 6966 | } |
| 6967 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6968 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6969 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6970 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6971 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6973 | EXPORT_SYMBOL(register_md_personality); |
| 6974 | EXPORT_SYMBOL(unregister_md_personality); |
| 6975 | EXPORT_SYMBOL(md_error); |
| 6976 | EXPORT_SYMBOL(md_done_sync); |
| 6977 | EXPORT_SYMBOL(md_write_start); |
| 6978 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | EXPORT_SYMBOL(md_register_thread); |
| 6980 | EXPORT_SYMBOL(md_unregister_thread); |
| 6981 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | EXPORT_SYMBOL(md_check_recovery); |
| 6983 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6984 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6985 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |