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 | /* |
| 739 | * load_super for 0.90.0 |
| 740 | */ |
| 741 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 742 | { |
| 743 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 744 | mdp_super_t *sb; |
| 745 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
| 747 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 748 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | * it's at the end of the disk. |
| 750 | * |
| 751 | * It also happens to be a multiple of 4Kb. |
| 752 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 753 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 755 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | if (ret) return ret; |
| 757 | |
| 758 | ret = -EINVAL; |
| 759 | |
| 760 | bdevname(rdev->bdev, b); |
| 761 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 762 | |
| 763 | if (sb->md_magic != MD_SB_MAGIC) { |
| 764 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 765 | b); |
| 766 | goto abort; |
| 767 | } |
| 768 | |
| 769 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 770 | sb->minor_version < 90 || |
| 771 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 773 | sb->major_version, sb->minor_version, |
| 774 | b); |
| 775 | goto abort; |
| 776 | } |
| 777 | |
| 778 | if (sb->raid_disks <= 0) |
| 779 | goto abort; |
| 780 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 781 | 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] | 782 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 783 | b); |
| 784 | goto abort; |
| 785 | } |
| 786 | |
| 787 | rdev->preferred_minor = sb->md_minor; |
| 788 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 789 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 791 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 792 | if (sb->level != 1 && sb->level != 4 |
| 793 | && sb->level != 5 && sb->level != 6 |
| 794 | && sb->level != 10) { |
| 795 | /* FIXME use a better test */ |
| 796 | printk(KERN_WARNING |
| 797 | "md: bitmaps not supported for this level.\n"); |
| 798 | goto abort; |
| 799 | } |
| 800 | } |
| 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | if (sb->level == LEVEL_MULTIPATH) |
| 803 | rdev->desc_nr = -1; |
| 804 | else |
| 805 | rdev->desc_nr = sb->this_disk.number; |
| 806 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 807 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 809 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | __u64 ev1, ev2; |
| 811 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 812 | if (!uuid_equal(refsb, sb)) { |
| 813 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 814 | b, bdevname(refdev->bdev,b2)); |
| 815 | goto abort; |
| 816 | } |
| 817 | if (!sb_equal(refsb, sb)) { |
| 818 | printk(KERN_WARNING "md: %s has same UUID" |
| 819 | " but different superblock to %s\n", |
| 820 | b, bdevname(refdev->bdev, b2)); |
| 821 | goto abort; |
| 822 | } |
| 823 | ev1 = md_event(sb); |
| 824 | ev2 = md_event(refsb); |
| 825 | if (ev1 > ev2) |
| 826 | ret = 1; |
| 827 | else |
| 828 | ret = 0; |
| 829 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame^] | 830 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 832 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 833 | /* "this cannot possibly happen" ... */ |
| 834 | ret = -EINVAL; |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | abort: |
| 837 | return ret; |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * validate_super for 0.90.0 |
| 842 | */ |
| 843 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 844 | { |
| 845 | mdp_disk_t *desc; |
| 846 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 847 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 849 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 850 | clear_bit(Faulty, &rdev->flags); |
| 851 | clear_bit(In_sync, &rdev->flags); |
| 852 | clear_bit(WriteMostly, &rdev->flags); |
| 853 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | if (mddev->raid_disks == 0) { |
| 856 | mddev->major_version = 0; |
| 857 | mddev->minor_version = sb->minor_version; |
| 858 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 859 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 860 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | mddev->ctime = sb->ctime; |
| 862 | mddev->utime = sb->utime; |
| 863 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 864 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | mddev->layout = sb->layout; |
| 866 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 867 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 868 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 869 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 870 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 872 | if (mddev->minor_version >= 91) { |
| 873 | mddev->reshape_position = sb->reshape_position; |
| 874 | mddev->delta_disks = sb->delta_disks; |
| 875 | mddev->new_level = sb->new_level; |
| 876 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 877 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 878 | } else { |
| 879 | mddev->reshape_position = MaxSector; |
| 880 | mddev->delta_disks = 0; |
| 881 | mddev->new_level = mddev->level; |
| 882 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 883 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 887 | mddev->recovery_cp = MaxSector; |
| 888 | else { |
| 889 | if (sb->events_hi == sb->cp_events_hi && |
| 890 | sb->events_lo == sb->cp_events_lo) { |
| 891 | mddev->recovery_cp = sb->recovery_cp; |
| 892 | } else |
| 893 | mddev->recovery_cp = 0; |
| 894 | } |
| 895 | |
| 896 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 897 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 898 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 899 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 900 | |
| 901 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 902 | |
| 903 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 904 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 905 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 906 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 907 | } else if (mddev->pers == NULL) { |
| 908 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | ++ev1; |
| 910 | if (ev1 < mddev->events) |
| 911 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 912 | } else if (mddev->bitmap) { |
| 913 | /* if adding to array with a bitmap, then we can accept an |
| 914 | * older device ... but not too old. |
| 915 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 916 | if (ev1 < mddev->bitmap->events_cleared) |
| 917 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 918 | } else { |
| 919 | if (ev1 < mddev->events) |
| 920 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 921 | return 0; |
| 922 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | desc = sb->disks + rdev->desc_nr; |
| 926 | |
| 927 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 928 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 929 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 930 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 931 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | rdev->raid_disk = desc->raid_disk; |
| 933 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 934 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 935 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 936 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 937 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * sync_super for 0.90.0 |
| 943 | */ |
| 944 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 945 | { |
| 946 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | mdk_rdev_t *rdev2; |
| 948 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
| 951 | /* make rdev->sb match mddev data.. |
| 952 | * |
| 953 | * 1/ zero out disks |
| 954 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 955 | * 3/ any empty disks < next_spare become removed |
| 956 | * |
| 957 | * disks[0] gets initialised to REMOVED because |
| 958 | * we cannot be sure from other fields if it has |
| 959 | * been initialised or not. |
| 960 | */ |
| 961 | int i; |
| 962 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 963 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 964 | rdev->sb_size = MD_SB_BYTES; |
| 965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 967 | |
| 968 | memset(sb, 0, sizeof(*sb)); |
| 969 | |
| 970 | sb->md_magic = MD_SB_MAGIC; |
| 971 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | sb->patch_version = mddev->patch_version; |
| 973 | sb->gvalid_words = 0; /* ignored */ |
| 974 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 975 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 976 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 977 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 978 | |
| 979 | sb->ctime = mddev->ctime; |
| 980 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 981 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | sb->raid_disks = mddev->raid_disks; |
| 983 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 984 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | sb->utime = mddev->utime; |
| 986 | sb->state = 0; |
| 987 | sb->events_hi = (mddev->events>>32); |
| 988 | sb->events_lo = (u32)mddev->events; |
| 989 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 990 | if (mddev->reshape_position == MaxSector) |
| 991 | sb->minor_version = 90; |
| 992 | else { |
| 993 | sb->minor_version = 91; |
| 994 | sb->reshape_position = mddev->reshape_position; |
| 995 | sb->new_level = mddev->new_level; |
| 996 | sb->delta_disks = mddev->delta_disks; |
| 997 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 998 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 999 | } |
| 1000 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | if (mddev->in_sync) |
| 1002 | { |
| 1003 | sb->recovery_cp = mddev->recovery_cp; |
| 1004 | sb->cp_events_hi = (mddev->events>>32); |
| 1005 | sb->cp_events_lo = (u32)mddev->events; |
| 1006 | if (mddev->recovery_cp == MaxSector) |
| 1007 | sb->state = (1<< MD_SB_CLEAN); |
| 1008 | } else |
| 1009 | sb->recovery_cp = 0; |
| 1010 | |
| 1011 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1012 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1014 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 1015 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1018 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1020 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1021 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 1022 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1023 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1025 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1026 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | d = &sb->disks[rdev2->desc_nr]; |
| 1028 | nr_disks++; |
| 1029 | d->number = rdev2->desc_nr; |
| 1030 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1031 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1032 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 1033 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | d->raid_disk = rdev2->raid_disk; |
| 1035 | else |
| 1036 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1037 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1039 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | d->state = (1<<MD_DISK_ACTIVE); |
| 1041 | d->state |= (1<<MD_DISK_SYNC); |
| 1042 | active++; |
| 1043 | working++; |
| 1044 | } else { |
| 1045 | d->state = 0; |
| 1046 | spare++; |
| 1047 | working++; |
| 1048 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1049 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1050 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1053 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1054 | mdp_disk_t *d = &sb->disks[i]; |
| 1055 | if (d->state == 0 && d->number == 0) { |
| 1056 | d->number = i; |
| 1057 | d->raid_disk = i; |
| 1058 | d->state = (1<<MD_DISK_REMOVED); |
| 1059 | d->state |= (1<<MD_DISK_FAULTY); |
| 1060 | failed++; |
| 1061 | } |
| 1062 | } |
| 1063 | sb->nr_disks = nr_disks; |
| 1064 | sb->active_disks = active; |
| 1065 | sb->working_disks = working; |
| 1066 | sb->failed_disks = failed; |
| 1067 | sb->spare_disks = spare; |
| 1068 | |
| 1069 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1070 | sb->sb_csum = calc_sb_csum(sb); |
| 1071 | } |
| 1072 | |
| 1073 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1074 | * rdev_size_change for 0.90.0 |
| 1075 | */ |
| 1076 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1077 | 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] | 1078 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1079 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1080 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1081 | if (rdev->mddev->bitmap_offset) |
| 1082 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1083 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1084 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1085 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1086 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1087 | rdev->sb_page); |
| 1088 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1089 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | * version 1 superblock |
| 1095 | */ |
| 1096 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1097 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1099 | __le32 disk_csum; |
| 1100 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | unsigned long long newcsum; |
| 1102 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1103 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | int i; |
| 1105 | |
| 1106 | disk_csum = sb->sb_csum; |
| 1107 | sb->sb_csum = 0; |
| 1108 | newcsum = 0; |
| 1109 | for (i=0; size>=4; size -= 4 ) |
| 1110 | newcsum += le32_to_cpu(*isuper++); |
| 1111 | |
| 1112 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1113 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
| 1115 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1116 | sb->sb_csum = disk_csum; |
| 1117 | return cpu_to_le32(csum); |
| 1118 | } |
| 1119 | |
| 1120 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1121 | { |
| 1122 | struct mdp_superblock_1 *sb; |
| 1123 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1124 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1126 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
| 1128 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1129 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | * It is always aligned to a 4K boundary and |
| 1131 | * depeding on minor_version, it can be: |
| 1132 | * 0: At least 8K, but less than 12K, from end of device |
| 1133 | * 1: At start of device |
| 1134 | * 2: 4K from start of device. |
| 1135 | */ |
| 1136 | switch(minor_version) { |
| 1137 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1138 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1139 | sb_start -= 8*2; |
| 1140 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | break; |
| 1142 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1143 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | break; |
| 1145 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1146 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | break; |
| 1148 | default: |
| 1149 | return -EINVAL; |
| 1150 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1151 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1153 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1154 | * and it is safe to read 4k, so we do that |
| 1155 | */ |
| 1156 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | if (ret) return ret; |
| 1158 | |
| 1159 | |
| 1160 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1161 | |
| 1162 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1163 | sb->major_version != cpu_to_le32(1) || |
| 1164 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1165 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1166 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | return -EINVAL; |
| 1168 | |
| 1169 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1170 | printk("md: invalid superblock checksum on %s\n", |
| 1171 | bdevname(rdev->bdev,b)); |
| 1172 | return -EINVAL; |
| 1173 | } |
| 1174 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1175 | printk("md: data_size too small on %s\n", |
| 1176 | bdevname(rdev->bdev,b)); |
| 1177 | return -EINVAL; |
| 1178 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1179 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) { |
| 1180 | if (sb->level != cpu_to_le32(1) && |
| 1181 | sb->level != cpu_to_le32(4) && |
| 1182 | sb->level != cpu_to_le32(5) && |
| 1183 | sb->level != cpu_to_le32(6) && |
| 1184 | sb->level != cpu_to_le32(10)) { |
| 1185 | printk(KERN_WARNING |
| 1186 | "md: bitmaps not supported for this level.\n"); |
| 1187 | return -EINVAL; |
| 1188 | } |
| 1189 | } |
| 1190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | rdev->preferred_minor = 0xffff; |
| 1192 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1193 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1195 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1196 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1197 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1198 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1199 | |
| 1200 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1201 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1202 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1203 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1204 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1205 | rdev->desc_nr = -1; |
| 1206 | else |
| 1207 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1208 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1209 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1210 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1211 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | __u64 ev1, ev2; |
| 1213 | struct mdp_superblock_1 *refsb = |
| 1214 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1215 | |
| 1216 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1217 | sb->level != refsb->level || |
| 1218 | sb->layout != refsb->layout || |
| 1219 | sb->chunksize != refsb->chunksize) { |
| 1220 | printk(KERN_WARNING "md: %s has strangely different" |
| 1221 | " superblock to %s\n", |
| 1222 | bdevname(rdev->bdev,b), |
| 1223 | bdevname(refdev->bdev,b2)); |
| 1224 | return -EINVAL; |
| 1225 | } |
| 1226 | ev1 = le64_to_cpu(sb->events); |
| 1227 | ev2 = le64_to_cpu(refsb->events); |
| 1228 | |
| 1229 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1230 | ret = 1; |
| 1231 | else |
| 1232 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1234 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1235 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1236 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1238 | rdev->sectors = rdev->sb_start; |
| 1239 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1241 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1242 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1243 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1244 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1248 | { |
| 1249 | 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] | 1250 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1252 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1253 | clear_bit(Faulty, &rdev->flags); |
| 1254 | clear_bit(In_sync, &rdev->flags); |
| 1255 | clear_bit(WriteMostly, &rdev->flags); |
| 1256 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | if (mddev->raid_disks == 0) { |
| 1259 | mddev->major_version = 1; |
| 1260 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1261 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1262 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1264 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1265 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1266 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | mddev->layout = le32_to_cpu(sb->layout); |
| 1268 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1269 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1270 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1271 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1272 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
| 1274 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1275 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1276 | |
| 1277 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1278 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1279 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1280 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1281 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1282 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1283 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1284 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1285 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1286 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1287 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1288 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1289 | } else { |
| 1290 | mddev->reshape_position = MaxSector; |
| 1291 | mddev->delta_disks = 0; |
| 1292 | mddev->new_level = mddev->level; |
| 1293 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1294 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1295 | } |
| 1296 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1297 | } else if (mddev->pers == NULL) { |
| 1298 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | ++ev1; |
| 1300 | if (ev1 < mddev->events) |
| 1301 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1302 | } else if (mddev->bitmap) { |
| 1303 | /* If adding to array with a bitmap, then we can accept an |
| 1304 | * older device, but not too old. |
| 1305 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1306 | if (ev1 < mddev->bitmap->events_cleared) |
| 1307 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1308 | } else { |
| 1309 | if (ev1 < mddev->events) |
| 1310 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1311 | return 0; |
| 1312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1314 | int role; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 1316 | switch(role) { |
| 1317 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | break; |
| 1319 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1320 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | break; |
| 1322 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1323 | if ((le32_to_cpu(sb->feature_map) & |
| 1324 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1325 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1326 | else |
| 1327 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | rdev->raid_disk = role; |
| 1329 | break; |
| 1330 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1331 | if (sb->devflags & WriteMostly1) |
| 1332 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1333 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1334 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | return 0; |
| 1337 | } |
| 1338 | |
| 1339 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1340 | { |
| 1341 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | mdk_rdev_t *rdev2; |
| 1343 | int max_dev, i; |
| 1344 | /* make rdev->sb match mddev and rdev data. */ |
| 1345 | |
| 1346 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1347 | |
| 1348 | sb->feature_map = 0; |
| 1349 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1350 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1352 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1353 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1354 | |
| 1355 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1356 | sb->events = cpu_to_le64(mddev->events); |
| 1357 | if (mddev->in_sync) |
| 1358 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1359 | else |
| 1360 | sb->resync_offset = cpu_to_le64(0); |
| 1361 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1362 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1363 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1364 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1365 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1366 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1367 | sb->level = cpu_to_le32(mddev->level); |
| 1368 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1369 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1370 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1371 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1372 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1373 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1374 | |
| 1375 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1376 | !test_bit(In_sync, &rdev->flags)) { |
| 1377 | if (mddev->curr_resync_completed > rdev->recovery_offset) |
| 1378 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 1379 | if (rdev->recovery_offset > 0) { |
| 1380 | sb->feature_map |= |
| 1381 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1382 | sb->recovery_offset = |
| 1383 | cpu_to_le64(rdev->recovery_offset); |
| 1384 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1387 | if (mddev->reshape_position != MaxSector) { |
| 1388 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1389 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1390 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1391 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1392 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1393 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1394 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1397 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | if (rdev2->desc_nr+1 > max_dev) |
| 1399 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1400 | |
| 1401 | if (max_dev > le32_to_cpu(sb->max_dev)) |
| 1402 | sb->max_dev = cpu_to_le32(max_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | for (i=0; i<max_dev;i++) |
| 1404 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1405 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1406 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1408 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1410 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1412 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1413 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | else |
| 1415 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1416 | } |
| 1417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1419 | } |
| 1420 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1421 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1422 | 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] | 1423 | { |
| 1424 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1425 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1426 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1427 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1428 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1429 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1430 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1431 | max_sectors -= rdev->data_offset; |
| 1432 | if (!num_sectors || num_sectors > max_sectors) |
| 1433 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1434 | } else if (rdev->mddev->bitmap_offset) { |
| 1435 | /* minor version 0 with bitmap we can't move */ |
| 1436 | return 0; |
| 1437 | } else { |
| 1438 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1439 | sector_t sb_start; |
| 1440 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1441 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1442 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1443 | if (!num_sectors || num_sectors > max_sectors) |
| 1444 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1445 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1446 | } |
| 1447 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1448 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1449 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1450 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1451 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1452 | rdev->sb_page); |
| 1453 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1454 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1457 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | [0] = { |
| 1459 | .name = "0.90.0", |
| 1460 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1461 | .load_super = super_90_load, |
| 1462 | .validate_super = super_90_validate, |
| 1463 | .sync_super = super_90_sync, |
| 1464 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | }, |
| 1466 | [1] = { |
| 1467 | .name = "md-1", |
| 1468 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1469 | .load_super = super_1_load, |
| 1470 | .validate_super = super_1_validate, |
| 1471 | .sync_super = super_1_sync, |
| 1472 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | }, |
| 1474 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | |
| 1476 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1477 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1478 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1480 | rcu_read_lock(); |
| 1481 | rdev_for_each_rcu(rdev, mddev1) |
| 1482 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1483 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1484 | rdev2->bdev->bd_contains) { |
| 1485 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1486 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1487 | } |
| 1488 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | static LIST_HEAD(pending_raid_disks); |
| 1493 | |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1494 | static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1495 | { |
| 1496 | struct mdk_personality *pers = mddev->pers; |
| 1497 | struct gendisk *disk = mddev->gendisk; |
| 1498 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1499 | struct blk_integrity *bi_mddev = blk_get_integrity(disk); |
| 1500 | |
| 1501 | /* Data integrity passthrough not supported on RAID 4, 5 and 6 */ |
| 1502 | if (pers && pers->level >= 4 && pers->level <= 6) |
| 1503 | return; |
| 1504 | |
| 1505 | /* If rdev is integrity capable, register profile for mddev */ |
| 1506 | if (!bi_mddev && bi_rdev) { |
| 1507 | if (blk_integrity_register(disk, bi_rdev)) |
| 1508 | printk(KERN_ERR "%s: %s Could not register integrity!\n", |
| 1509 | __func__, disk->disk_name); |
| 1510 | else |
| 1511 | printk(KERN_NOTICE "Enabling data integrity on %s\n", |
| 1512 | disk->disk_name); |
| 1513 | return; |
| 1514 | } |
| 1515 | |
| 1516 | /* Check that mddev and rdev have matching profiles */ |
| 1517 | if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) { |
| 1518 | printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__, |
| 1519 | disk->disk_name, rdev->bdev->bd_disk->disk_name); |
| 1520 | printk(KERN_NOTICE "Disabling data integrity on %s\n", |
| 1521 | disk->disk_name); |
| 1522 | blk_integrity_unregister(disk); |
| 1523 | } |
| 1524 | } |
| 1525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1527 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1528 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1529 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1530 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1531 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
| 1533 | if (rdev->mddev) { |
| 1534 | MD_BUG(); |
| 1535 | return -EINVAL; |
| 1536 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1537 | |
| 1538 | /* prevent duplicates */ |
| 1539 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1540 | return -EEXIST; |
| 1541 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1542 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1543 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1544 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1545 | if (mddev->pers) { |
| 1546 | /* Cannot change size, so fail |
| 1547 | * If mddev->level <= 0, then we don't care |
| 1548 | * about aligning sizes (e.g. linear) |
| 1549 | */ |
| 1550 | if (mddev->level > 0) |
| 1551 | return -ENOSPC; |
| 1552 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1553 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
| 1556 | /* Verify rdev->desc_nr is unique. |
| 1557 | * If it is -1, assign a free number, else |
| 1558 | * check number is not in use |
| 1559 | */ |
| 1560 | if (rdev->desc_nr < 0) { |
| 1561 | int choice = 0; |
| 1562 | if (mddev->pers) choice = mddev->raid_disks; |
| 1563 | while (find_rdev_nr(mddev, choice)) |
| 1564 | choice++; |
| 1565 | rdev->desc_nr = choice; |
| 1566 | } else { |
| 1567 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1568 | return -EBUSY; |
| 1569 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1570 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1571 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1572 | mdname(mddev), mddev->max_disks); |
| 1573 | return -EBUSY; |
| 1574 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1575 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1576 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1577 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1580 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1581 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1582 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1583 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1584 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1585 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1586 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1587 | kobject_del(&rdev->kobj); |
| 1588 | goto fail; |
| 1589 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1590 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1591 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1592 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1593 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1594 | |
| 1595 | /* May as well allow recovery to be retried once */ |
| 1596 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1597 | |
| 1598 | md_integrity_check(rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1600 | |
| 1601 | fail: |
| 1602 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1603 | b, mdname(mddev)); |
| 1604 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | } |
| 1606 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1607 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1608 | { |
| 1609 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1610 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1611 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1615 | { |
| 1616 | char b[BDEVNAME_SIZE]; |
| 1617 | if (!rdev->mddev) { |
| 1618 | MD_BUG(); |
| 1619 | return; |
| 1620 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1621 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1622 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1624 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1625 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1626 | sysfs_put(rdev->sysfs_state); |
| 1627 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1628 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1629 | * writing to 'remove' to "dev/state". We also need |
| 1630 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1631 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1632 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1633 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1634 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1635 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * prevent the device from being mounted, repartitioned or |
| 1640 | * otherwise reused by a RAID array (or any other kernel |
| 1641 | * subsystem), by bd_claiming the device. |
| 1642 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1643 | 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] | 1644 | { |
| 1645 | int err = 0; |
| 1646 | struct block_device *bdev; |
| 1647 | char b[BDEVNAME_SIZE]; |
| 1648 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1649 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | if (IS_ERR(bdev)) { |
| 1651 | printk(KERN_ERR "md: could not open %s.\n", |
| 1652 | __bdevname(dev, b)); |
| 1653 | return PTR_ERR(bdev); |
| 1654 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1655 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | if (err) { |
| 1657 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1658 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1659 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | return err; |
| 1661 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1662 | if (!shared) |
| 1663 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | rdev->bdev = bdev; |
| 1665 | return err; |
| 1666 | } |
| 1667 | |
| 1668 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1669 | { |
| 1670 | struct block_device *bdev = rdev->bdev; |
| 1671 | rdev->bdev = NULL; |
| 1672 | if (!bdev) |
| 1673 | MD_BUG(); |
| 1674 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1675 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | void md_autodetect_dev(dev_t dev); |
| 1679 | |
| 1680 | static void export_rdev(mdk_rdev_t * rdev) |
| 1681 | { |
| 1682 | char b[BDEVNAME_SIZE]; |
| 1683 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1684 | bdevname(rdev->bdev,b)); |
| 1685 | if (rdev->mddev) |
| 1686 | MD_BUG(); |
| 1687 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1689 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1690 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | #endif |
| 1692 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1693 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1697 | { |
| 1698 | unbind_rdev_from_array(rdev); |
| 1699 | export_rdev(rdev); |
| 1700 | } |
| 1701 | |
| 1702 | static void export_array(mddev_t *mddev) |
| 1703 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1704 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1706 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | if (!rdev->mddev) { |
| 1708 | MD_BUG(); |
| 1709 | continue; |
| 1710 | } |
| 1711 | kick_rdev_from_array(rdev); |
| 1712 | } |
| 1713 | if (!list_empty(&mddev->disks)) |
| 1714 | MD_BUG(); |
| 1715 | mddev->raid_disks = 0; |
| 1716 | mddev->major_version = 0; |
| 1717 | } |
| 1718 | |
| 1719 | static void print_desc(mdp_disk_t *desc) |
| 1720 | { |
| 1721 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1722 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1723 | } |
| 1724 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1725 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | { |
| 1727 | int i; |
| 1728 | |
| 1729 | printk(KERN_INFO |
| 1730 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1731 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1732 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1733 | sb->ctime); |
| 1734 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1735 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1736 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1737 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1738 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1739 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1740 | sb->failed_disks, sb->spare_disks, |
| 1741 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1742 | |
| 1743 | printk(KERN_INFO); |
| 1744 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1745 | mdp_disk_t *desc; |
| 1746 | |
| 1747 | desc = sb->disks + i; |
| 1748 | if (desc->number || desc->major || desc->minor || |
| 1749 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1750 | printk(" D %2d: ", i); |
| 1751 | print_desc(desc); |
| 1752 | } |
| 1753 | } |
| 1754 | printk(KERN_INFO "md: THIS: "); |
| 1755 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1758 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1759 | { |
| 1760 | __u8 *uuid; |
| 1761 | |
| 1762 | uuid = sb->set_uuid; |
| 1763 | printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1764 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1765 | KERN_INFO "md: Name: \"%s\" CT:%llu\n", |
| 1766 | le32_to_cpu(sb->major_version), |
| 1767 | le32_to_cpu(sb->feature_map), |
| 1768 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1769 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1770 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1771 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1772 | sb->set_name, |
| 1773 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1774 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1775 | |
| 1776 | uuid = sb->device_uuid; |
| 1777 | printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" |
| 1778 | " RO:%llu\n" |
| 1779 | KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1780 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1781 | KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1782 | KERN_INFO "md: (MaxDev:%u) \n", |
| 1783 | le32_to_cpu(sb->level), |
| 1784 | (unsigned long long)le64_to_cpu(sb->size), |
| 1785 | le32_to_cpu(sb->raid_disks), |
| 1786 | le32_to_cpu(sb->layout), |
| 1787 | le32_to_cpu(sb->chunksize), |
| 1788 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1789 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1790 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1791 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1792 | le32_to_cpu(sb->dev_number), |
| 1793 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1794 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1795 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1796 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1797 | sb->devflags, |
| 1798 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1799 | (unsigned long long)le64_to_cpu(sb->events), |
| 1800 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1801 | le32_to_cpu(sb->sb_csum), |
| 1802 | le32_to_cpu(sb->max_dev) |
| 1803 | ); |
| 1804 | } |
| 1805 | |
| 1806 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | { |
| 1808 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1809 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1810 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1811 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1812 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1814 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1815 | switch (major_version) { |
| 1816 | case 0: |
| 1817 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1818 | break; |
| 1819 | case 1: |
| 1820 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1821 | break; |
| 1822 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | } else |
| 1824 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1825 | } |
| 1826 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1827 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1829 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | mdk_rdev_t *rdev; |
| 1831 | mddev_t *mddev; |
| 1832 | char b[BDEVNAME_SIZE]; |
| 1833 | |
| 1834 | printk("\n"); |
| 1835 | printk("md: **********************************\n"); |
| 1836 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1837 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1838 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1840 | if (mddev->bitmap) |
| 1841 | bitmap_print_sb(mddev->bitmap); |
| 1842 | else |
| 1843 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1844 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1846 | printk("\n"); |
| 1847 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1848 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1849 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | } |
| 1851 | printk("md: **********************************\n"); |
| 1852 | printk("\n"); |
| 1853 | } |
| 1854 | |
| 1855 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1856 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1858 | /* Update each superblock (in-memory image), but |
| 1859 | * if we are allowed to, skip spares which already |
| 1860 | * have the right event counter, or have one earlier |
| 1861 | * (which would mean they aren't being marked as dirty |
| 1862 | * with the rest of the array) |
| 1863 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1866 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1867 | if (rdev->sb_events == mddev->events || |
| 1868 | (nospares && |
| 1869 | rdev->raid_disk < 0 && |
| 1870 | (rdev->sb_events&1)==0 && |
| 1871 | rdev->sb_events+1 == mddev->events)) { |
| 1872 | /* Don't update this superblock */ |
| 1873 | rdev->sb_loaded = 2; |
| 1874 | } else { |
| 1875 | super_types[mddev->major_version]. |
| 1876 | sync_super(mddev, rdev); |
| 1877 | rdev->sb_loaded = 1; |
| 1878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1882 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1885 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1886 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 1888 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1889 | if (mddev->external) |
| 1890 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1892 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1893 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1894 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1895 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1896 | force_change = 1; |
| 1897 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1898 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1899 | * though if events isn't the right even/odd, we will have to do |
| 1900 | * spares after all |
| 1901 | */ |
| 1902 | nospares = 1; |
| 1903 | if (force_change) |
| 1904 | nospares = 0; |
| 1905 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1906 | /* If the array is degraded, then skipping spares is both |
| 1907 | * dangerous and fairly pointless. |
| 1908 | * Dangerous because a device that was removed from the array |
| 1909 | * might have a event_count that still looks up-to-date, |
| 1910 | * so it can be re-added without a resync. |
| 1911 | * Pointless because if there are any spares to skip, |
| 1912 | * then a recovery will happen and soon that array won't |
| 1913 | * be degraded any more and the spare can go back to sleep then. |
| 1914 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1915 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1916 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1917 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1918 | |
| 1919 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1920 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1921 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1922 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1923 | && (mddev->events & 1) |
| 1924 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1925 | mddev->events--; |
| 1926 | else { |
| 1927 | /* otherwise we have to go forward and ... */ |
| 1928 | mddev->events ++; |
| 1929 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
| 1930 | /* .. if the array isn't clean, insist on an odd 'events' */ |
| 1931 | if ((mddev->events&1)==0) { |
| 1932 | mddev->events++; |
| 1933 | nospares = 0; |
| 1934 | } |
| 1935 | } else { |
| 1936 | /* otherwise insist on an even 'events' (for clean states) */ |
| 1937 | if ((mddev->events&1)) { |
| 1938 | mddev->events++; |
| 1939 | nospares = 0; |
| 1940 | } |
| 1941 | } |
| 1942 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | |
| 1944 | if (!mddev->events) { |
| 1945 | /* |
| 1946 | * oops, this 64-bit counter should never wrap. |
| 1947 | * Either we are in around ~1 trillion A.C., assuming |
| 1948 | * 1 reboot per second, or we have a bug: |
| 1949 | */ |
| 1950 | MD_BUG(); |
| 1951 | mddev->events --; |
| 1952 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | |
| 1954 | /* |
| 1955 | * do not write anything to disk if using |
| 1956 | * nonpersistent superblocks |
| 1957 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1958 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1959 | if (!mddev->external) |
| 1960 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1961 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1962 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1963 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1965 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1966 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1967 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | |
| 1969 | dprintk(KERN_INFO |
| 1970 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 1971 | mdname(mddev),mddev->in_sync); |
| 1972 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 1973 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1974 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | char b[BDEVNAME_SIZE]; |
| 1976 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1977 | if (rdev->sb_loaded != 1) |
| 1978 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1979 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | dprintk("(skipping faulty "); |
| 1981 | |
| 1982 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1983 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1984 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1985 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1986 | rdev->sb_page); |
| 1987 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 1988 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1989 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1990 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | } else |
| 1993 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1994 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | /* only need to write one superblock... */ |
| 1996 | break; |
| 1997 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1998 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1999 | /* 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] | 2000 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2001 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2002 | if (mddev->in_sync != sync_req || |
| 2003 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2004 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2005 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2006 | goto repeat; |
| 2007 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2008 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
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); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2011 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2012 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2016 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2017 | * We want to accept with case. For this we use cmd_match. |
| 2018 | */ |
| 2019 | static int cmd_match(const char *cmd, const char *str) |
| 2020 | { |
| 2021 | /* See if cmd, written into a sysfs file, matches |
| 2022 | * str. They must either be the same, or cmd can |
| 2023 | * have a trailing newline |
| 2024 | */ |
| 2025 | while (*cmd && *str && *cmd == *str) { |
| 2026 | cmd++; |
| 2027 | str++; |
| 2028 | } |
| 2029 | if (*cmd == '\n') |
| 2030 | cmd++; |
| 2031 | if (*str || *cmd) |
| 2032 | return 0; |
| 2033 | return 1; |
| 2034 | } |
| 2035 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2036 | struct rdev_sysfs_entry { |
| 2037 | struct attribute attr; |
| 2038 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2039 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2040 | }; |
| 2041 | |
| 2042 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2043 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2044 | { |
| 2045 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2046 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2047 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2048 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2049 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2050 | sep = ","; |
| 2051 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2052 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2053 | len += sprintf(page+len, "%sin_sync",sep); |
| 2054 | sep = ","; |
| 2055 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2056 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2057 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2058 | sep = ","; |
| 2059 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2060 | if (test_bit(Blocked, &rdev->flags)) { |
| 2061 | len += sprintf(page+len, "%sblocked", sep); |
| 2062 | sep = ","; |
| 2063 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2064 | if (!test_bit(Faulty, &rdev->flags) && |
| 2065 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2066 | len += sprintf(page+len, "%sspare", sep); |
| 2067 | sep = ","; |
| 2068 | } |
| 2069 | return len+sprintf(page+len, "\n"); |
| 2070 | } |
| 2071 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2072 | static ssize_t |
| 2073 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2074 | { |
| 2075 | /* can write |
| 2076 | * faulty - simulates and error |
| 2077 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2078 | * writemostly - sets write_mostly |
| 2079 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2080 | * blocked - sets the Blocked flag |
| 2081 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2082 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2083 | */ |
| 2084 | int err = -EINVAL; |
| 2085 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2086 | md_error(rdev->mddev, rdev); |
| 2087 | err = 0; |
| 2088 | } else if (cmd_match(buf, "remove")) { |
| 2089 | if (rdev->raid_disk >= 0) |
| 2090 | err = -EBUSY; |
| 2091 | else { |
| 2092 | mddev_t *mddev = rdev->mddev; |
| 2093 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2094 | if (mddev->pers) |
| 2095 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2096 | md_new_event(mddev); |
| 2097 | err = 0; |
| 2098 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2099 | } else if (cmd_match(buf, "writemostly")) { |
| 2100 | set_bit(WriteMostly, &rdev->flags); |
| 2101 | err = 0; |
| 2102 | } else if (cmd_match(buf, "-writemostly")) { |
| 2103 | clear_bit(WriteMostly, &rdev->flags); |
| 2104 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2105 | } else if (cmd_match(buf, "blocked")) { |
| 2106 | set_bit(Blocked, &rdev->flags); |
| 2107 | err = 0; |
| 2108 | } else if (cmd_match(buf, "-blocked")) { |
| 2109 | clear_bit(Blocked, &rdev->flags); |
| 2110 | wake_up(&rdev->blocked_wait); |
| 2111 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2112 | md_wakeup_thread(rdev->mddev->thread); |
| 2113 | |
| 2114 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2115 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2116 | set_bit(In_sync, &rdev->flags); |
| 2117 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2118 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2119 | if (!err && rdev->sysfs_state) |
| 2120 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2121 | return err ? err : len; |
| 2122 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2123 | static struct rdev_sysfs_entry rdev_state = |
| 2124 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2125 | |
| 2126 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2127 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2128 | { |
| 2129 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2130 | } |
| 2131 | |
| 2132 | static ssize_t |
| 2133 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2134 | { |
| 2135 | char *e; |
| 2136 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2137 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2138 | atomic_set(&rdev->corrected_errors, n); |
| 2139 | return len; |
| 2140 | } |
| 2141 | return -EINVAL; |
| 2142 | } |
| 2143 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2144 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2145 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2146 | static ssize_t |
| 2147 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2148 | { |
| 2149 | if (rdev->raid_disk < 0) |
| 2150 | return sprintf(page, "none\n"); |
| 2151 | else |
| 2152 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2153 | } |
| 2154 | |
| 2155 | static ssize_t |
| 2156 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2157 | { |
| 2158 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2159 | int err; |
| 2160 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2161 | int slot = simple_strtoul(buf, &e, 10); |
| 2162 | if (strncmp(buf, "none", 4)==0) |
| 2163 | slot = -1; |
| 2164 | else if (e==buf || (*e && *e!= '\n')) |
| 2165 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2166 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2167 | /* Setting 'slot' on an active array requires also |
| 2168 | * updating the 'rd%d' link, and communicating |
| 2169 | * with the personality with ->hot_*_disk. |
| 2170 | * For now we only support removing |
| 2171 | * failed/spare devices. This normally happens automatically, |
| 2172 | * but not when the metadata is externally managed. |
| 2173 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2174 | if (rdev->raid_disk == -1) |
| 2175 | return -EEXIST; |
| 2176 | /* personality does all needed checks */ |
| 2177 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2178 | return -EINVAL; |
| 2179 | err = rdev->mddev->pers-> |
| 2180 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2181 | if (err) |
| 2182 | return err; |
| 2183 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2184 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2185 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2186 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2187 | } else if (rdev->mddev->pers) { |
| 2188 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2189 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2190 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2191 | */ |
| 2192 | |
| 2193 | if (rdev->raid_disk != -1) |
| 2194 | return -EBUSY; |
| 2195 | |
| 2196 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2197 | return -EINVAL; |
| 2198 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2199 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2200 | if (rdev2->raid_disk == slot) |
| 2201 | return -EEXIST; |
| 2202 | |
| 2203 | rdev->raid_disk = slot; |
| 2204 | if (test_bit(In_sync, &rdev->flags)) |
| 2205 | rdev->saved_raid_disk = slot; |
| 2206 | else |
| 2207 | rdev->saved_raid_disk = -1; |
| 2208 | err = rdev->mddev->pers-> |
| 2209 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2210 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2211 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2212 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2213 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2214 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2215 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2216 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2217 | printk(KERN_WARNING |
| 2218 | "md: cannot register " |
| 2219 | "%s for %s\n", |
| 2220 | nm, mdname(rdev->mddev)); |
| 2221 | |
| 2222 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2223 | } else { |
| 2224 | if (slot >= rdev->mddev->raid_disks) |
| 2225 | return -ENOSPC; |
| 2226 | rdev->raid_disk = slot; |
| 2227 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2228 | clear_bit(Faulty, &rdev->flags); |
| 2229 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2230 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2231 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2232 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2233 | return len; |
| 2234 | } |
| 2235 | |
| 2236 | |
| 2237 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2238 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2239 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2240 | static ssize_t |
| 2241 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2242 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2243 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | static ssize_t |
| 2247 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2248 | { |
| 2249 | char *e; |
| 2250 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2251 | if (e==buf || (*e && *e != '\n')) |
| 2252 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2253 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2254 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2255 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2256 | /* Must set offset before size, so overlap checks |
| 2257 | * can be sane */ |
| 2258 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2259 | rdev->data_offset = offset; |
| 2260 | return len; |
| 2261 | } |
| 2262 | |
| 2263 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2264 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2265 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2266 | static ssize_t |
| 2267 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2268 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2269 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2272 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2273 | { |
| 2274 | /* check if two start/length pairs overlap */ |
| 2275 | if (s1+l1 <= s2) |
| 2276 | return 0; |
| 2277 | if (s2+l2 <= s1) |
| 2278 | return 0; |
| 2279 | return 1; |
| 2280 | } |
| 2281 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2282 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2283 | { |
| 2284 | unsigned long long blocks; |
| 2285 | sector_t new; |
| 2286 | |
| 2287 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2288 | return -EINVAL; |
| 2289 | |
| 2290 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2291 | return -EINVAL; /* sector conversion overflow */ |
| 2292 | |
| 2293 | new = blocks * 2; |
| 2294 | if (new != blocks * 2) |
| 2295 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2296 | |
| 2297 | *sectors = new; |
| 2298 | return 0; |
| 2299 | } |
| 2300 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2301 | static ssize_t |
| 2302 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2303 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2304 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2305 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2306 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2307 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2308 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2309 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2310 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2311 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2312 | sectors = super_types[my_mddev->major_version]. |
| 2313 | rdev_size_change(rdev, sectors); |
| 2314 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2315 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2316 | } else if (!sectors) |
| 2317 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2318 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2319 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2320 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2321 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2322 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2323 | rdev->sectors = sectors; |
| 2324 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2325 | /* need to check that all other rdevs with the same ->bdev |
| 2326 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2327 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2328 | * we have to change it back, we will have the lock again. |
| 2329 | */ |
| 2330 | mddev_t *mddev; |
| 2331 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2332 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2333 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2334 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2335 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2336 | mdk_rdev_t *rdev2; |
| 2337 | |
| 2338 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2339 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2340 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2341 | (rdev->bdev == rdev2->bdev && |
| 2342 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2343 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2344 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2345 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2346 | overlap = 1; |
| 2347 | break; |
| 2348 | } |
| 2349 | mddev_unlock(mddev); |
| 2350 | if (overlap) { |
| 2351 | mddev_put(mddev); |
| 2352 | break; |
| 2353 | } |
| 2354 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2355 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2356 | if (overlap) { |
| 2357 | /* Someone else could have slipped in a size |
| 2358 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2359 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2360 | * safe, and trust userspace not to race with |
| 2361 | * itself |
| 2362 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2363 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2364 | return -EBUSY; |
| 2365 | } |
| 2366 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2367 | return len; |
| 2368 | } |
| 2369 | |
| 2370 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2371 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2372 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2373 | static struct attribute *rdev_default_attrs[] = { |
| 2374 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2375 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2376 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2377 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2378 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2379 | NULL, |
| 2380 | }; |
| 2381 | static ssize_t |
| 2382 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2383 | { |
| 2384 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2385 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2386 | mddev_t *mddev = rdev->mddev; |
| 2387 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2388 | |
| 2389 | if (!entry->show) |
| 2390 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2391 | |
| 2392 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2393 | if (!rv) { |
| 2394 | if (rdev->mddev == NULL) |
| 2395 | rv = -EBUSY; |
| 2396 | else |
| 2397 | rv = entry->show(rdev, page); |
| 2398 | mddev_unlock(mddev); |
| 2399 | } |
| 2400 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | static ssize_t |
| 2404 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2405 | const char *page, size_t length) |
| 2406 | { |
| 2407 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2408 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2409 | ssize_t rv; |
| 2410 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2411 | |
| 2412 | if (!entry->store) |
| 2413 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2414 | if (!capable(CAP_SYS_ADMIN)) |
| 2415 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2416 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2417 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2418 | if (rdev->mddev == NULL) |
| 2419 | rv = -EBUSY; |
| 2420 | else |
| 2421 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2422 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2423 | } |
| 2424 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | static void rdev_free(struct kobject *ko) |
| 2428 | { |
| 2429 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2430 | kfree(rdev); |
| 2431 | } |
| 2432 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2433 | .show = rdev_attr_show, |
| 2434 | .store = rdev_attr_store, |
| 2435 | }; |
| 2436 | static struct kobj_type rdev_ktype = { |
| 2437 | .release = rdev_free, |
| 2438 | .sysfs_ops = &rdev_sysfs_ops, |
| 2439 | .default_attrs = rdev_default_attrs, |
| 2440 | }; |
| 2441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | /* |
| 2443 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2444 | * |
| 2445 | * mark the device faulty if: |
| 2446 | * |
| 2447 | * - the device is nonexistent (zero size) |
| 2448 | * - the device has no valid superblock |
| 2449 | * |
| 2450 | * a faulty rdev _never_ has rdev->sb set. |
| 2451 | */ |
| 2452 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2453 | { |
| 2454 | char b[BDEVNAME_SIZE]; |
| 2455 | int err; |
| 2456 | mdk_rdev_t *rdev; |
| 2457 | sector_t size; |
| 2458 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2459 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | if (!rdev) { |
| 2461 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2462 | return ERR_PTR(-ENOMEM); |
| 2463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | |
| 2465 | if ((err = alloc_disk_sb(rdev))) |
| 2466 | goto abort_free; |
| 2467 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2468 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | if (err) |
| 2470 | goto abort_free; |
| 2471 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2472 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2475 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2476 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2477 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2479 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2481 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2482 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | |
| 2484 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2485 | if (!size) { |
| 2486 | printk(KERN_WARNING |
| 2487 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2488 | bdevname(rdev->bdev,b)); |
| 2489 | err = -EINVAL; |
| 2490 | goto abort_free; |
| 2491 | } |
| 2492 | |
| 2493 | if (super_format >= 0) { |
| 2494 | err = super_types[super_format]. |
| 2495 | load_super(rdev, NULL, super_minor); |
| 2496 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2497 | printk(KERN_WARNING |
| 2498 | "md: %s does not have a valid v%d.%d " |
| 2499 | "superblock, not importing!\n", |
| 2500 | bdevname(rdev->bdev,b), |
| 2501 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | goto abort_free; |
| 2503 | } |
| 2504 | if (err < 0) { |
| 2505 | printk(KERN_WARNING |
| 2506 | "md: could not read %s's sb, not importing!\n", |
| 2507 | bdevname(rdev->bdev,b)); |
| 2508 | goto abort_free; |
| 2509 | } |
| 2510 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2512 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2513 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | |
| 2515 | return rdev; |
| 2516 | |
| 2517 | abort_free: |
| 2518 | if (rdev->sb_page) { |
| 2519 | if (rdev->bdev) |
| 2520 | unlock_rdev(rdev); |
| 2521 | free_disk_sb(rdev); |
| 2522 | } |
| 2523 | kfree(rdev); |
| 2524 | return ERR_PTR(err); |
| 2525 | } |
| 2526 | |
| 2527 | /* |
| 2528 | * Check a full RAID array for plausibility |
| 2529 | */ |
| 2530 | |
| 2531 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2532 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | { |
| 2534 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2535 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | char b[BDEVNAME_SIZE]; |
| 2537 | |
| 2538 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2539 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | switch (super_types[mddev->major_version]. |
| 2541 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2542 | case 1: |
| 2543 | freshest = rdev; |
| 2544 | break; |
| 2545 | case 0: |
| 2546 | break; |
| 2547 | default: |
| 2548 | printk( KERN_ERR \ |
| 2549 | "md: fatal superblock inconsistency in %s" |
| 2550 | " -- removing from array\n", |
| 2551 | bdevname(rdev->bdev,b)); |
| 2552 | kick_rdev_from_array(rdev); |
| 2553 | } |
| 2554 | |
| 2555 | |
| 2556 | super_types[mddev->major_version]. |
| 2557 | validate_super(mddev, freshest); |
| 2558 | |
| 2559 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2560 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2561 | if (rdev->desc_nr >= mddev->max_disks || |
| 2562 | i > mddev->max_disks) { |
| 2563 | printk(KERN_WARNING |
| 2564 | "md: %s: %s: only %d devices permitted\n", |
| 2565 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2566 | mddev->max_disks); |
| 2567 | kick_rdev_from_array(rdev); |
| 2568 | continue; |
| 2569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | if (rdev != freshest) |
| 2571 | if (super_types[mddev->major_version]. |
| 2572 | validate_super(mddev, rdev)) { |
| 2573 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2574 | " from array!\n", |
| 2575 | bdevname(rdev->bdev,b)); |
| 2576 | kick_rdev_from_array(rdev); |
| 2577 | continue; |
| 2578 | } |
| 2579 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2580 | rdev->desc_nr = i++; |
| 2581 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2582 | set_bit(In_sync, &rdev->flags); |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2583 | } else if (rdev->raid_disk >= mddev->raid_disks) { |
| 2584 | rdev->raid_disk = -1; |
| 2585 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | } |
| 2587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | } |
| 2589 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2590 | static void md_safemode_timeout(unsigned long data); |
| 2591 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2592 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2593 | safe_delay_show(mddev_t *mddev, char *page) |
| 2594 | { |
| 2595 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2596 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2597 | } |
| 2598 | static ssize_t |
| 2599 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2600 | { |
| 2601 | int scale=1; |
| 2602 | int dot=0; |
| 2603 | int i; |
| 2604 | unsigned long msec; |
| 2605 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2606 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2607 | /* remove a period, and count digits after it */ |
| 2608 | if (len >= sizeof(buf)) |
| 2609 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2610 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2611 | for (i=0; i<len; i++) { |
| 2612 | if (dot) { |
| 2613 | if (isdigit(buf[i])) { |
| 2614 | buf[i-1] = buf[i]; |
| 2615 | scale *= 10; |
| 2616 | } |
| 2617 | buf[i] = 0; |
| 2618 | } else if (buf[i] == '.') { |
| 2619 | dot=1; |
| 2620 | buf[i] = 0; |
| 2621 | } |
| 2622 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2623 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2624 | return -EINVAL; |
| 2625 | msec = (msec * 1000) / scale; |
| 2626 | if (msec == 0) |
| 2627 | mddev->safemode_delay = 0; |
| 2628 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2629 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2630 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2631 | if (mddev->safemode_delay == 0) |
| 2632 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2633 | if (mddev->safemode_delay < old_delay) |
| 2634 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2635 | } |
| 2636 | return len; |
| 2637 | } |
| 2638 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2639 | __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] | 2640 | |
| 2641 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2642 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2643 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2644 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2645 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2646 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2647 | else if (mddev->clevel[0]) |
| 2648 | return sprintf(page, "%s\n", mddev->clevel); |
| 2649 | else if (mddev->level != LEVEL_NONE) |
| 2650 | return sprintf(page, "%d\n", mddev->level); |
| 2651 | else |
| 2652 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2655 | static ssize_t |
| 2656 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2657 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2658 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2659 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2660 | struct mdk_personality *pers; |
| 2661 | void *priv; |
| 2662 | |
| 2663 | if (mddev->pers == NULL) { |
| 2664 | if (len == 0) |
| 2665 | return 0; |
| 2666 | if (len >= sizeof(mddev->clevel)) |
| 2667 | return -ENOSPC; |
| 2668 | strncpy(mddev->clevel, buf, len); |
| 2669 | if (mddev->clevel[len-1] == '\n') |
| 2670 | len--; |
| 2671 | mddev->clevel[len] = 0; |
| 2672 | mddev->level = LEVEL_NONE; |
| 2673 | return rv; |
| 2674 | } |
| 2675 | |
| 2676 | /* request to change the personality. Need to ensure: |
| 2677 | * - array is not engaged in resync/recovery/reshape |
| 2678 | * - old personality can be suspended |
| 2679 | * - new personality will access other array. |
| 2680 | */ |
| 2681 | |
| 2682 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2683 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2684 | |
| 2685 | if (!mddev->pers->quiesce) { |
| 2686 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2687 | mdname(mddev), mddev->pers->name); |
| 2688 | return -EINVAL; |
| 2689 | } |
| 2690 | |
| 2691 | /* Now find the new personality */ |
| 2692 | if (len == 0 || len >= sizeof(level)) |
| 2693 | return -EINVAL; |
| 2694 | strncpy(level, buf, len); |
| 2695 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2696 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2697 | level[len] = 0; |
| 2698 | |
| 2699 | request_module("md-%s", level); |
| 2700 | spin_lock(&pers_lock); |
| 2701 | pers = find_pers(LEVEL_NONE, level); |
| 2702 | if (!pers || !try_module_get(pers->owner)) { |
| 2703 | spin_unlock(&pers_lock); |
| 2704 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2705 | return -EINVAL; |
| 2706 | } |
| 2707 | spin_unlock(&pers_lock); |
| 2708 | |
| 2709 | if (pers == mddev->pers) { |
| 2710 | /* Nothing to do! */ |
| 2711 | module_put(pers->owner); |
| 2712 | return rv; |
| 2713 | } |
| 2714 | if (!pers->takeover) { |
| 2715 | module_put(pers->owner); |
| 2716 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2717 | mdname(mddev), level); |
| 2718 | return -EINVAL; |
| 2719 | } |
| 2720 | |
| 2721 | /* ->takeover must set new_* and/or delta_disks |
| 2722 | * if it succeeds, and may set them when it fails. |
| 2723 | */ |
| 2724 | priv = pers->takeover(mddev); |
| 2725 | if (IS_ERR(priv)) { |
| 2726 | mddev->new_level = mddev->level; |
| 2727 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2728 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2729 | mddev->raid_disks -= mddev->delta_disks; |
| 2730 | mddev->delta_disks = 0; |
| 2731 | module_put(pers->owner); |
| 2732 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2733 | mdname(mddev), level); |
| 2734 | return PTR_ERR(priv); |
| 2735 | } |
| 2736 | |
| 2737 | /* Looks like we have a winner */ |
| 2738 | mddev_suspend(mddev); |
| 2739 | mddev->pers->stop(mddev); |
| 2740 | module_put(mddev->pers->owner); |
| 2741 | mddev->pers = pers; |
| 2742 | mddev->private = priv; |
| 2743 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2744 | mddev->level = mddev->new_level; |
| 2745 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2746 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2747 | mddev->delta_disks = 0; |
| 2748 | pers->run(mddev); |
| 2749 | mddev_resume(mddev); |
| 2750 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2751 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2752 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2753 | return rv; |
| 2754 | } |
| 2755 | |
| 2756 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2757 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2758 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2759 | |
| 2760 | static ssize_t |
| 2761 | layout_show(mddev_t *mddev, char *page) |
| 2762 | { |
| 2763 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2764 | if (mddev->reshape_position != MaxSector && |
| 2765 | mddev->layout != mddev->new_layout) |
| 2766 | return sprintf(page, "%d (%d)\n", |
| 2767 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2768 | return sprintf(page, "%d\n", mddev->layout); |
| 2769 | } |
| 2770 | |
| 2771 | static ssize_t |
| 2772 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2773 | { |
| 2774 | char *e; |
| 2775 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2776 | |
| 2777 | if (!*buf || (*e && *e != '\n')) |
| 2778 | return -EINVAL; |
| 2779 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2780 | if (mddev->pers) { |
| 2781 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2782 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2783 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2784 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2785 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2786 | if (err) { |
| 2787 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2788 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2789 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2790 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2791 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2792 | if (mddev->reshape_position == MaxSector) |
| 2793 | mddev->layout = n; |
| 2794 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2795 | return len; |
| 2796 | } |
| 2797 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2798 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2799 | |
| 2800 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2801 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2802 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2803 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2804 | if (mddev->raid_disks == 0) |
| 2805 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2806 | if (mddev->reshape_position != MaxSector && |
| 2807 | mddev->delta_disks != 0) |
| 2808 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2809 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2810 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2811 | } |
| 2812 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2813 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2814 | |
| 2815 | static ssize_t |
| 2816 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2817 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2818 | char *e; |
| 2819 | int rv = 0; |
| 2820 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2821 | |
| 2822 | if (!*buf || (*e && *e != '\n')) |
| 2823 | return -EINVAL; |
| 2824 | |
| 2825 | if (mddev->pers) |
| 2826 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2827 | else if (mddev->reshape_position != MaxSector) { |
| 2828 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2829 | mddev->delta_disks = n - olddisks; |
| 2830 | mddev->raid_disks = n; |
| 2831 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2832 | mddev->raid_disks = n; |
| 2833 | return rv ? rv : len; |
| 2834 | } |
| 2835 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2836 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2837 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2838 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2839 | chunk_size_show(mddev_t *mddev, char *page) |
| 2840 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2841 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2842 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 2843 | return sprintf(page, "%d (%d)\n", |
| 2844 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2845 | mddev->chunk_sectors << 9); |
| 2846 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | static ssize_t |
| 2850 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2851 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2852 | char *e; |
| 2853 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2854 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2855 | if (!*buf || (*e && *e != '\n')) |
| 2856 | return -EINVAL; |
| 2857 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2858 | if (mddev->pers) { |
| 2859 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2860 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2861 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2862 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2863 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2864 | if (err) { |
| 2865 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2866 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2867 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2868 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2869 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2870 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2871 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2872 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2873 | return len; |
| 2874 | } |
| 2875 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2876 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2877 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2878 | static ssize_t |
| 2879 | resync_start_show(mddev_t *mddev, char *page) |
| 2880 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 2881 | if (mddev->recovery_cp == MaxSector) |
| 2882 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2883 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2884 | } |
| 2885 | |
| 2886 | static ssize_t |
| 2887 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2888 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2889 | char *e; |
| 2890 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2891 | |
| 2892 | if (mddev->pers) |
| 2893 | return -EBUSY; |
| 2894 | if (!*buf || (*e && *e != '\n')) |
| 2895 | return -EINVAL; |
| 2896 | |
| 2897 | mddev->recovery_cp = n; |
| 2898 | return len; |
| 2899 | } |
| 2900 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2901 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2902 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2903 | /* |
| 2904 | * The array state can be: |
| 2905 | * |
| 2906 | * clear |
| 2907 | * No devices, no size, no level |
| 2908 | * Equivalent to STOP_ARRAY ioctl |
| 2909 | * inactive |
| 2910 | * May have some settings, but array is not active |
| 2911 | * all IO results in error |
| 2912 | * When written, doesn't tear down array, but just stops it |
| 2913 | * suspended (not supported yet) |
| 2914 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2915 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2916 | * readonly |
| 2917 | * no resync can happen. no superblocks get written. |
| 2918 | * write requests fail |
| 2919 | * read-auto |
| 2920 | * like readonly, but behaves like 'clean' on a write request. |
| 2921 | * |
| 2922 | * clean - no pending writes, but otherwise active. |
| 2923 | * When written to inactive array, starts without resync |
| 2924 | * If a write request arrives then |
| 2925 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2926 | * if not known, block and switch to write-pending |
| 2927 | * If written to an active array that has pending writes, then fails. |
| 2928 | * active |
| 2929 | * fully active: IO and resync can be happening. |
| 2930 | * When written to inactive array, starts with resync |
| 2931 | * |
| 2932 | * write-pending |
| 2933 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2934 | * |
| 2935 | * active-idle |
| 2936 | * like active, but no writes have been seen for a while (100msec). |
| 2937 | * |
| 2938 | */ |
| 2939 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 2940 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2941 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2942 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 2943 | "write-pending", "active-idle", NULL }; |
| 2944 | |
| 2945 | static int match_word(const char *word, char **list) |
| 2946 | { |
| 2947 | int n; |
| 2948 | for (n=0; list[n]; n++) |
| 2949 | if (cmd_match(word, list[n])) |
| 2950 | break; |
| 2951 | return n; |
| 2952 | } |
| 2953 | |
| 2954 | static ssize_t |
| 2955 | array_state_show(mddev_t *mddev, char *page) |
| 2956 | { |
| 2957 | enum array_state st = inactive; |
| 2958 | |
| 2959 | if (mddev->pers) |
| 2960 | switch(mddev->ro) { |
| 2961 | case 1: |
| 2962 | st = readonly; |
| 2963 | break; |
| 2964 | case 2: |
| 2965 | st = read_auto; |
| 2966 | break; |
| 2967 | case 0: |
| 2968 | if (mddev->in_sync) |
| 2969 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2970 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2971 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2972 | else if (mddev->safemode) |
| 2973 | st = active_idle; |
| 2974 | else |
| 2975 | st = active; |
| 2976 | } |
| 2977 | else { |
| 2978 | if (list_empty(&mddev->disks) && |
| 2979 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2980 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2981 | st = clear; |
| 2982 | else |
| 2983 | st = inactive; |
| 2984 | } |
| 2985 | return sprintf(page, "%s\n", array_states[st]); |
| 2986 | } |
| 2987 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 2988 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2989 | static int do_md_run(mddev_t * mddev); |
| 2990 | static int restart_array(mddev_t *mddev); |
| 2991 | |
| 2992 | static ssize_t |
| 2993 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 2994 | { |
| 2995 | int err = -EINVAL; |
| 2996 | enum array_state st = match_word(buf, array_states); |
| 2997 | switch(st) { |
| 2998 | case bad_word: |
| 2999 | break; |
| 3000 | case clear: |
| 3001 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3002 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3003 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3004 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3005 | break; |
| 3006 | case inactive: |
| 3007 | /* stopping an active array */ |
| 3008 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3009 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3010 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3011 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3012 | } else |
| 3013 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3014 | break; |
| 3015 | case suspended: |
| 3016 | break; /* not supported yet */ |
| 3017 | case readonly: |
| 3018 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3019 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3020 | else { |
| 3021 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3022 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3023 | err = do_md_run(mddev); |
| 3024 | } |
| 3025 | break; |
| 3026 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3027 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3028 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3029 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3030 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3031 | err = restart_array(mddev); |
| 3032 | if (err == 0) { |
| 3033 | mddev->ro = 2; |
| 3034 | set_disk_ro(mddev->gendisk, 0); |
| 3035 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3036 | } else { |
| 3037 | mddev->ro = 2; |
| 3038 | err = do_md_run(mddev); |
| 3039 | } |
| 3040 | break; |
| 3041 | case clean: |
| 3042 | if (mddev->pers) { |
| 3043 | restart_array(mddev); |
| 3044 | spin_lock_irq(&mddev->write_lock); |
| 3045 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3046 | if (mddev->in_sync == 0) { |
| 3047 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3048 | if (mddev->safemode == 1) |
| 3049 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3050 | if (mddev->persistent) |
| 3051 | set_bit(MD_CHANGE_CLEAN, |
| 3052 | &mddev->flags); |
| 3053 | } |
| 3054 | err = 0; |
| 3055 | } else |
| 3056 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3057 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3058 | } else |
| 3059 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3060 | break; |
| 3061 | case active: |
| 3062 | if (mddev->pers) { |
| 3063 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3064 | if (mddev->external) |
| 3065 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3066 | wake_up(&mddev->sb_wait); |
| 3067 | err = 0; |
| 3068 | } else { |
| 3069 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3070 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3071 | err = do_md_run(mddev); |
| 3072 | } |
| 3073 | break; |
| 3074 | case write_pending: |
| 3075 | case active_idle: |
| 3076 | /* these cannot be set */ |
| 3077 | break; |
| 3078 | } |
| 3079 | if (err) |
| 3080 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3081 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3082 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3083 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3084 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3085 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3086 | static struct md_sysfs_entry md_array_state = |
| 3087 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3088 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3089 | static ssize_t |
| 3090 | null_show(mddev_t *mddev, char *page) |
| 3091 | { |
| 3092 | return -EINVAL; |
| 3093 | } |
| 3094 | |
| 3095 | static ssize_t |
| 3096 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3097 | { |
| 3098 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3099 | /* The new device is added to the array. |
| 3100 | * If the array has a persistent superblock, we read the |
| 3101 | * superblock to initialise info and check validity. |
| 3102 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3103 | * which mainly checks size. |
| 3104 | */ |
| 3105 | char *e; |
| 3106 | int major = simple_strtoul(buf, &e, 10); |
| 3107 | int minor; |
| 3108 | dev_t dev; |
| 3109 | mdk_rdev_t *rdev; |
| 3110 | int err; |
| 3111 | |
| 3112 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3113 | return -EINVAL; |
| 3114 | minor = simple_strtoul(e+1, &e, 10); |
| 3115 | if (*e && *e != '\n') |
| 3116 | return -EINVAL; |
| 3117 | dev = MKDEV(major, minor); |
| 3118 | if (major != MAJOR(dev) || |
| 3119 | minor != MINOR(dev)) |
| 3120 | return -EOVERFLOW; |
| 3121 | |
| 3122 | |
| 3123 | if (mddev->persistent) { |
| 3124 | rdev = md_import_device(dev, mddev->major_version, |
| 3125 | mddev->minor_version); |
| 3126 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3127 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3128 | mdk_rdev_t, same_set); |
| 3129 | err = super_types[mddev->major_version] |
| 3130 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3131 | if (err < 0) |
| 3132 | goto out; |
| 3133 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3134 | } else if (mddev->external) |
| 3135 | rdev = md_import_device(dev, -2, -1); |
| 3136 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3137 | rdev = md_import_device(dev, -1, -1); |
| 3138 | |
| 3139 | if (IS_ERR(rdev)) |
| 3140 | return PTR_ERR(rdev); |
| 3141 | err = bind_rdev_to_array(rdev, mddev); |
| 3142 | out: |
| 3143 | if (err) |
| 3144 | export_rdev(rdev); |
| 3145 | return err ? err : len; |
| 3146 | } |
| 3147 | |
| 3148 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3149 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3150 | |
| 3151 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3152 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3153 | { |
| 3154 | char *end; |
| 3155 | unsigned long chunk, end_chunk; |
| 3156 | |
| 3157 | if (!mddev->bitmap) |
| 3158 | goto out; |
| 3159 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3160 | while (*buf) { |
| 3161 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3162 | if (buf == end) break; |
| 3163 | if (*end == '-') { /* range */ |
| 3164 | buf = end + 1; |
| 3165 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3166 | if (buf == end) break; |
| 3167 | } |
| 3168 | if (*end && !isspace(*end)) break; |
| 3169 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3170 | buf = end; |
| 3171 | while (isspace(*buf)) buf++; |
| 3172 | } |
| 3173 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3174 | out: |
| 3175 | return len; |
| 3176 | } |
| 3177 | |
| 3178 | static struct md_sysfs_entry md_bitmap = |
| 3179 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3180 | |
| 3181 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3182 | size_show(mddev_t *mddev, char *page) |
| 3183 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3184 | return sprintf(page, "%llu\n", |
| 3185 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3186 | } |
| 3187 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3188 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3189 | |
| 3190 | static ssize_t |
| 3191 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3192 | { |
| 3193 | /* If array is inactive, we can reduce the component size, but |
| 3194 | * not increase it (except from 0). |
| 3195 | * If array is active, we can try an on-line resize |
| 3196 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3197 | sector_t sectors; |
| 3198 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3199 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3200 | if (err < 0) |
| 3201 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3202 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3203 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3204 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3205 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3206 | if (mddev->dev_sectors == 0 || |
| 3207 | mddev->dev_sectors > sectors) |
| 3208 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3209 | else |
| 3210 | err = -ENOSPC; |
| 3211 | } |
| 3212 | return err ? err : len; |
| 3213 | } |
| 3214 | |
| 3215 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3216 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3217 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3218 | |
| 3219 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3220 | * This is one of |
| 3221 | * 'none' for arrays with no metadata (good luck...) |
| 3222 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3223 | * or N.M for internally known formats |
| 3224 | */ |
| 3225 | static ssize_t |
| 3226 | metadata_show(mddev_t *mddev, char *page) |
| 3227 | { |
| 3228 | if (mddev->persistent) |
| 3229 | return sprintf(page, "%d.%d\n", |
| 3230 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3231 | else if (mddev->external) |
| 3232 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3233 | else |
| 3234 | return sprintf(page, "none\n"); |
| 3235 | } |
| 3236 | |
| 3237 | static ssize_t |
| 3238 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3239 | { |
| 3240 | int major, minor; |
| 3241 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3242 | /* Changing the details of 'external' metadata is |
| 3243 | * always permitted. Otherwise there must be |
| 3244 | * no devices attached to the array. |
| 3245 | */ |
| 3246 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3247 | ; |
| 3248 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3249 | return -EBUSY; |
| 3250 | |
| 3251 | if (cmd_match(buf, "none")) { |
| 3252 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3253 | mddev->external = 0; |
| 3254 | mddev->major_version = 0; |
| 3255 | mddev->minor_version = 90; |
| 3256 | return len; |
| 3257 | } |
| 3258 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3259 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3260 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3261 | namelen = sizeof(mddev->metadata_type)-1; |
| 3262 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3263 | mddev->metadata_type[namelen] = 0; |
| 3264 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3265 | mddev->metadata_type[--namelen] = 0; |
| 3266 | mddev->persistent = 0; |
| 3267 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3268 | mddev->major_version = 0; |
| 3269 | mddev->minor_version = 90; |
| 3270 | return len; |
| 3271 | } |
| 3272 | major = simple_strtoul(buf, &e, 10); |
| 3273 | if (e==buf || *e != '.') |
| 3274 | return -EINVAL; |
| 3275 | buf = e+1; |
| 3276 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3277 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3278 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3279 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3280 | return -ENOENT; |
| 3281 | mddev->major_version = major; |
| 3282 | mddev->minor_version = minor; |
| 3283 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3284 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3285 | return len; |
| 3286 | } |
| 3287 | |
| 3288 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3289 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3290 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3291 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3292 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3293 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3294 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3295 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3296 | type = "frozen"; |
| 3297 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3298 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3299 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3300 | type = "reshape"; |
| 3301 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3302 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3303 | type = "resync"; |
| 3304 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3305 | type = "check"; |
| 3306 | else |
| 3307 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3308 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3309 | type = "recover"; |
| 3310 | } |
| 3311 | return sprintf(page, "%s\n", type); |
| 3312 | } |
| 3313 | |
| 3314 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3315 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3316 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3317 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3318 | return -EINVAL; |
| 3319 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3320 | if (cmd_match(page, "frozen")) |
| 3321 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3322 | else |
| 3323 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3324 | |
| 3325 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3326 | if (mddev->sync_thread) { |
| 3327 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3328 | md_unregister_thread(mddev->sync_thread); |
| 3329 | mddev->sync_thread = NULL; |
| 3330 | mddev->recovery = 0; |
| 3331 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3332 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3333 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3334 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3335 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3336 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3337 | else if (cmd_match(page, "recover")) { |
| 3338 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3339 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3340 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3341 | int err; |
| 3342 | if (mddev->pers->start_reshape == NULL) |
| 3343 | return -EINVAL; |
| 3344 | err = mddev->pers->start_reshape(mddev); |
| 3345 | if (err) |
| 3346 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3347 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3348 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3349 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3350 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3351 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3352 | return -EINVAL; |
| 3353 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3354 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3355 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3356 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3357 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3358 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3359 | return len; |
| 3360 | } |
| 3361 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3362 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3363 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3364 | { |
| 3365 | return sprintf(page, "%llu\n", |
| 3366 | (unsigned long long) mddev->resync_mismatches); |
| 3367 | } |
| 3368 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3369 | static struct md_sysfs_entry md_scan_mode = |
| 3370 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3371 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3372 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3373 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3374 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3375 | static ssize_t |
| 3376 | sync_min_show(mddev_t *mddev, char *page) |
| 3377 | { |
| 3378 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3379 | mddev->sync_speed_min ? "local": "system"); |
| 3380 | } |
| 3381 | |
| 3382 | static ssize_t |
| 3383 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3384 | { |
| 3385 | int min; |
| 3386 | char *e; |
| 3387 | if (strncmp(buf, "system", 6)==0) { |
| 3388 | mddev->sync_speed_min = 0; |
| 3389 | return len; |
| 3390 | } |
| 3391 | min = simple_strtoul(buf, &e, 10); |
| 3392 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3393 | return -EINVAL; |
| 3394 | mddev->sync_speed_min = min; |
| 3395 | return len; |
| 3396 | } |
| 3397 | |
| 3398 | static struct md_sysfs_entry md_sync_min = |
| 3399 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3400 | |
| 3401 | static ssize_t |
| 3402 | sync_max_show(mddev_t *mddev, char *page) |
| 3403 | { |
| 3404 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3405 | mddev->sync_speed_max ? "local": "system"); |
| 3406 | } |
| 3407 | |
| 3408 | static ssize_t |
| 3409 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3410 | { |
| 3411 | int max; |
| 3412 | char *e; |
| 3413 | if (strncmp(buf, "system", 6)==0) { |
| 3414 | mddev->sync_speed_max = 0; |
| 3415 | return len; |
| 3416 | } |
| 3417 | max = simple_strtoul(buf, &e, 10); |
| 3418 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3419 | return -EINVAL; |
| 3420 | mddev->sync_speed_max = max; |
| 3421 | return len; |
| 3422 | } |
| 3423 | |
| 3424 | static struct md_sysfs_entry md_sync_max = |
| 3425 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3426 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3427 | static ssize_t |
| 3428 | degraded_show(mddev_t *mddev, char *page) |
| 3429 | { |
| 3430 | return sprintf(page, "%d\n", mddev->degraded); |
| 3431 | } |
| 3432 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3433 | |
| 3434 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3435 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3436 | { |
| 3437 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3438 | } |
| 3439 | |
| 3440 | static ssize_t |
| 3441 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3442 | { |
| 3443 | long n; |
| 3444 | |
| 3445 | if (strict_strtol(buf, 10, &n)) |
| 3446 | return -EINVAL; |
| 3447 | |
| 3448 | if (n != 0 && n != 1) |
| 3449 | return -EINVAL; |
| 3450 | |
| 3451 | mddev->parallel_resync = n; |
| 3452 | |
| 3453 | if (mddev->sync_thread) |
| 3454 | wake_up(&resync_wait); |
| 3455 | |
| 3456 | return len; |
| 3457 | } |
| 3458 | |
| 3459 | /* force parallel resync, even with shared block devices */ |
| 3460 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3461 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3462 | sync_force_parallel_show, sync_force_parallel_store); |
| 3463 | |
| 3464 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3465 | sync_speed_show(mddev_t *mddev, char *page) |
| 3466 | { |
| 3467 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3468 | if (mddev->curr_resync == 0) |
| 3469 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3470 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3471 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3472 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3473 | db = resync - mddev->resync_mark_cnt; |
| 3474 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3475 | } |
| 3476 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3477 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3478 | |
| 3479 | static ssize_t |
| 3480 | sync_completed_show(mddev_t *mddev, char *page) |
| 3481 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3482 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3483 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3484 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3485 | return sprintf(page, "none\n"); |
| 3486 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3487 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3488 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3489 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3490 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3491 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3492 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3493 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3494 | } |
| 3495 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3496 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3497 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3498 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3499 | min_sync_show(mddev_t *mddev, char *page) |
| 3500 | { |
| 3501 | return sprintf(page, "%llu\n", |
| 3502 | (unsigned long long)mddev->resync_min); |
| 3503 | } |
| 3504 | static ssize_t |
| 3505 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3506 | { |
| 3507 | unsigned long long min; |
| 3508 | if (strict_strtoull(buf, 10, &min)) |
| 3509 | return -EINVAL; |
| 3510 | if (min > mddev->resync_max) |
| 3511 | return -EINVAL; |
| 3512 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3513 | return -EBUSY; |
| 3514 | |
| 3515 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3516 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3517 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3518 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3519 | return -EINVAL; |
| 3520 | } |
| 3521 | mddev->resync_min = min; |
| 3522 | |
| 3523 | return len; |
| 3524 | } |
| 3525 | |
| 3526 | static struct md_sysfs_entry md_min_sync = |
| 3527 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3528 | |
| 3529 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3530 | max_sync_show(mddev_t *mddev, char *page) |
| 3531 | { |
| 3532 | if (mddev->resync_max == MaxSector) |
| 3533 | return sprintf(page, "max\n"); |
| 3534 | else |
| 3535 | return sprintf(page, "%llu\n", |
| 3536 | (unsigned long long)mddev->resync_max); |
| 3537 | } |
| 3538 | static ssize_t |
| 3539 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3540 | { |
| 3541 | if (strncmp(buf, "max", 3) == 0) |
| 3542 | mddev->resync_max = MaxSector; |
| 3543 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3544 | unsigned long long max; |
| 3545 | if (strict_strtoull(buf, 10, &max)) |
| 3546 | return -EINVAL; |
| 3547 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3548 | return -EINVAL; |
| 3549 | if (max < mddev->resync_max && |
| 3550 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3551 | return -EBUSY; |
| 3552 | |
| 3553 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3554 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3555 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3556 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3557 | return -EINVAL; |
| 3558 | } |
| 3559 | mddev->resync_max = max; |
| 3560 | } |
| 3561 | wake_up(&mddev->recovery_wait); |
| 3562 | return len; |
| 3563 | } |
| 3564 | |
| 3565 | static struct md_sysfs_entry md_max_sync = |
| 3566 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3567 | |
| 3568 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3569 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3570 | { |
| 3571 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3572 | } |
| 3573 | |
| 3574 | static ssize_t |
| 3575 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3576 | { |
| 3577 | char *e; |
| 3578 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3579 | |
| 3580 | if (mddev->pers->quiesce == NULL) |
| 3581 | return -EINVAL; |
| 3582 | if (buf == e || (*e && *e != '\n')) |
| 3583 | return -EINVAL; |
| 3584 | if (new >= mddev->suspend_hi || |
| 3585 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3586 | mddev->suspend_lo = new; |
| 3587 | mddev->pers->quiesce(mddev, 2); |
| 3588 | return len; |
| 3589 | } else |
| 3590 | return -EINVAL; |
| 3591 | } |
| 3592 | static struct md_sysfs_entry md_suspend_lo = |
| 3593 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3594 | |
| 3595 | |
| 3596 | static ssize_t |
| 3597 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3598 | { |
| 3599 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3600 | } |
| 3601 | |
| 3602 | static ssize_t |
| 3603 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3604 | { |
| 3605 | char *e; |
| 3606 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3607 | |
| 3608 | if (mddev->pers->quiesce == NULL) |
| 3609 | return -EINVAL; |
| 3610 | if (buf == e || (*e && *e != '\n')) |
| 3611 | return -EINVAL; |
| 3612 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3613 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3614 | mddev->suspend_hi = new; |
| 3615 | mddev->pers->quiesce(mddev, 1); |
| 3616 | mddev->pers->quiesce(mddev, 0); |
| 3617 | return len; |
| 3618 | } else |
| 3619 | return -EINVAL; |
| 3620 | } |
| 3621 | static struct md_sysfs_entry md_suspend_hi = |
| 3622 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3623 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3624 | static ssize_t |
| 3625 | reshape_position_show(mddev_t *mddev, char *page) |
| 3626 | { |
| 3627 | if (mddev->reshape_position != MaxSector) |
| 3628 | return sprintf(page, "%llu\n", |
| 3629 | (unsigned long long)mddev->reshape_position); |
| 3630 | strcpy(page, "none\n"); |
| 3631 | return 5; |
| 3632 | } |
| 3633 | |
| 3634 | static ssize_t |
| 3635 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3636 | { |
| 3637 | char *e; |
| 3638 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3639 | if (mddev->pers) |
| 3640 | return -EBUSY; |
| 3641 | if (buf == e || (*e && *e != '\n')) |
| 3642 | return -EINVAL; |
| 3643 | mddev->reshape_position = new; |
| 3644 | mddev->delta_disks = 0; |
| 3645 | mddev->new_level = mddev->level; |
| 3646 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3647 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3648 | return len; |
| 3649 | } |
| 3650 | |
| 3651 | static struct md_sysfs_entry md_reshape_position = |
| 3652 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3653 | reshape_position_store); |
| 3654 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3655 | static ssize_t |
| 3656 | array_size_show(mddev_t *mddev, char *page) |
| 3657 | { |
| 3658 | if (mddev->external_size) |
| 3659 | return sprintf(page, "%llu\n", |
| 3660 | (unsigned long long)mddev->array_sectors/2); |
| 3661 | else |
| 3662 | return sprintf(page, "default\n"); |
| 3663 | } |
| 3664 | |
| 3665 | static ssize_t |
| 3666 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3667 | { |
| 3668 | sector_t sectors; |
| 3669 | |
| 3670 | if (strncmp(buf, "default", 7) == 0) { |
| 3671 | if (mddev->pers) |
| 3672 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3673 | else |
| 3674 | sectors = mddev->array_sectors; |
| 3675 | |
| 3676 | mddev->external_size = 0; |
| 3677 | } else { |
| 3678 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3679 | return -EINVAL; |
| 3680 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3681 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3682 | |
| 3683 | mddev->external_size = 1; |
| 3684 | } |
| 3685 | |
| 3686 | mddev->array_sectors = sectors; |
| 3687 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 3688 | if (mddev->pers) { |
| 3689 | struct block_device *bdev = bdget_disk(mddev->gendisk, 0); |
| 3690 | |
| 3691 | if (bdev) { |
| 3692 | mutex_lock(&bdev->bd_inode->i_mutex); |
| 3693 | i_size_write(bdev->bd_inode, |
| 3694 | (loff_t)mddev->array_sectors << 9); |
| 3695 | mutex_unlock(&bdev->bd_inode->i_mutex); |
| 3696 | bdput(bdev); |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | return len; |
| 3701 | } |
| 3702 | |
| 3703 | static struct md_sysfs_entry md_array_size = |
| 3704 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3705 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3706 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3707 | static struct attribute *md_default_attrs[] = { |
| 3708 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3709 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3710 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3711 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3712 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3713 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3714 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3715 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3716 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3717 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3718 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3719 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3720 | NULL, |
| 3721 | }; |
| 3722 | |
| 3723 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3724 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3725 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3726 | &md_sync_min.attr, |
| 3727 | &md_sync_max.attr, |
| 3728 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3729 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3730 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3731 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3732 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3733 | &md_suspend_lo.attr, |
| 3734 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3735 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3736 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3737 | NULL, |
| 3738 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3739 | static struct attribute_group md_redundancy_group = { |
| 3740 | .name = NULL, |
| 3741 | .attrs = md_redundancy_attrs, |
| 3742 | }; |
| 3743 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3744 | |
| 3745 | static ssize_t |
| 3746 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3747 | { |
| 3748 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3749 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3750 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3751 | |
| 3752 | if (!entry->show) |
| 3753 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3754 | rv = mddev_lock(mddev); |
| 3755 | if (!rv) { |
| 3756 | rv = entry->show(mddev, page); |
| 3757 | mddev_unlock(mddev); |
| 3758 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3759 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | static ssize_t |
| 3763 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3764 | const char *page, size_t length) |
| 3765 | { |
| 3766 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3767 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3768 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3769 | |
| 3770 | if (!entry->store) |
| 3771 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3772 | if (!capable(CAP_SYS_ADMIN)) |
| 3773 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3774 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3775 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3776 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3777 | if (!rv) { |
| 3778 | rv = entry->store(mddev, page, length); |
| 3779 | mddev_unlock(mddev); |
| 3780 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3781 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | static void md_free(struct kobject *ko) |
| 3785 | { |
| 3786 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3787 | |
| 3788 | if (mddev->sysfs_state) |
| 3789 | sysfs_put(mddev->sysfs_state); |
| 3790 | |
| 3791 | if (mddev->gendisk) { |
| 3792 | del_gendisk(mddev->gendisk); |
| 3793 | put_disk(mddev->gendisk); |
| 3794 | } |
| 3795 | if (mddev->queue) |
| 3796 | blk_cleanup_queue(mddev->queue); |
| 3797 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3798 | kfree(mddev); |
| 3799 | } |
| 3800 | |
| 3801 | static struct sysfs_ops md_sysfs_ops = { |
| 3802 | .show = md_attr_show, |
| 3803 | .store = md_attr_store, |
| 3804 | }; |
| 3805 | static struct kobj_type md_ktype = { |
| 3806 | .release = md_free, |
| 3807 | .sysfs_ops = &md_sysfs_ops, |
| 3808 | .default_attrs = md_default_attrs, |
| 3809 | }; |
| 3810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | int mdp_major = 0; |
| 3812 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3813 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3814 | { |
| 3815 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3816 | |
| 3817 | if (mddev->private == &md_redundancy_group) { |
| 3818 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3819 | if (mddev->sysfs_action) |
| 3820 | sysfs_put(mddev->sysfs_action); |
| 3821 | mddev->sysfs_action = NULL; |
| 3822 | mddev->private = NULL; |
| 3823 | } |
| 3824 | kobject_del(&mddev->kobj); |
| 3825 | kobject_put(&mddev->kobj); |
| 3826 | } |
| 3827 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3828 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3830 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | mddev_t *mddev = mddev_find(dev); |
| 3832 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3833 | int partitioned; |
| 3834 | int shift; |
| 3835 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3836 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | |
| 3838 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3839 | return -ENODEV; |
| 3840 | |
| 3841 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3842 | shift = partitioned ? MdpMinorShift : 0; |
| 3843 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3845 | /* wait for any previous instance if this device |
| 3846 | * to be completed removed (mddev_delayed_delete). |
| 3847 | */ |
| 3848 | flush_scheduled_work(); |
| 3849 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3850 | mutex_lock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | if (mddev->gendisk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3852 | mutex_unlock(&disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3854 | return -EEXIST; |
| 3855 | } |
| 3856 | |
| 3857 | if (name) { |
| 3858 | /* Need to ensure that 'name' is not a duplicate. |
| 3859 | */ |
| 3860 | mddev_t *mddev2; |
| 3861 | spin_lock(&all_mddevs_lock); |
| 3862 | |
| 3863 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3864 | if (mddev2->gendisk && |
| 3865 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3866 | spin_unlock(&all_mddevs_lock); |
| 3867 | return -EEXIST; |
| 3868 | } |
| 3869 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3871 | |
| 3872 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
| 3873 | if (!mddev->queue) { |
| 3874 | mutex_unlock(&disks_mutex); |
| 3875 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3876 | return -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3877 | } |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3878 | mddev->queue->queuedata = mddev; |
| 3879 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3880 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3881 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3882 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3883 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | disk = alloc_disk(1 << shift); |
| 3886 | if (!disk) { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3887 | mutex_unlock(&disks_mutex); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3888 | blk_cleanup_queue(mddev->queue); |
| 3889 | mddev->queue = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3891 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3893 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3895 | if (name) |
| 3896 | strcpy(disk->disk_name, name); |
| 3897 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3899 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | disk->fops = &md_fops; |
| 3902 | disk->private_data = mddev; |
| 3903 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3904 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3905 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3906 | * remove it now. |
| 3907 | */ |
| 3908 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3909 | add_disk(disk); |
| 3910 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3911 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3912 | &disk_to_dev(disk)->kobj, "%s", "md"); |
Neil Brown | f48ed53 | 2008-06-28 08:31:26 +1000 | [diff] [blame] | 3913 | mutex_unlock(&disks_mutex); |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3914 | if (error) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3915 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3916 | disk->disk_name); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3917 | else { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3918 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3919 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3920 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3921 | mddev_put(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3922 | return 0; |
| 3923 | } |
| 3924 | |
| 3925 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3926 | { |
| 3927 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | return NULL; |
| 3929 | } |
| 3930 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3931 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 3932 | { |
| 3933 | /* val must be "md_*" where * is not all digits. |
| 3934 | * We allocate an array with a large free minor number, and |
| 3935 | * set the name to val. val must not already be an active name. |
| 3936 | */ |
| 3937 | int len = strlen(val); |
| 3938 | char buf[DISK_NAME_LEN]; |
| 3939 | |
| 3940 | while (len && val[len-1] == '\n') |
| 3941 | len--; |
| 3942 | if (len >= DISK_NAME_LEN) |
| 3943 | return -E2BIG; |
| 3944 | strlcpy(buf, val, len+1); |
| 3945 | if (strncmp(buf, "md_", 3) != 0) |
| 3946 | return -EINVAL; |
| 3947 | return md_alloc(0, buf); |
| 3948 | } |
| 3949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | static void md_safemode_timeout(unsigned long data) |
| 3951 | { |
| 3952 | mddev_t *mddev = (mddev_t *) data; |
| 3953 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3954 | if (!atomic_read(&mddev->writes_pending)) { |
| 3955 | mddev->safemode = 1; |
| 3956 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3957 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3958 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | md_wakeup_thread(mddev->thread); |
| 3960 | } |
| 3961 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 3962 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | |
| 3964 | static int do_md_run(mddev_t * mddev) |
| 3965 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3966 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | mdk_rdev_t *rdev; |
| 3968 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3969 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3971 | if (list_empty(&mddev->disks)) |
| 3972 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | |
| 3975 | if (mddev->pers) |
| 3976 | return -EBUSY; |
| 3977 | |
| 3978 | /* |
| 3979 | * Analyze all RAID superblock(s) |
| 3980 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3981 | if (!mddev->raid_disks) { |
| 3982 | if (!mddev->persistent) |
| 3983 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 3984 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 3985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3987 | if (mddev->level != LEVEL_NONE) |
| 3988 | request_module("md-level-%d", mddev->level); |
| 3989 | else if (mddev->clevel[0]) |
| 3990 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | |
| 3992 | /* |
| 3993 | * Drop all container device buffers, from now on |
| 3994 | * the only valid external interface is through the md |
| 3995 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3997 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3998 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | continue; |
| 4000 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4001 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4002 | |
| 4003 | /* perform some consistency tests on the device. |
| 4004 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4005 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4006 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4007 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4008 | if (mddev->dev_sectors && |
| 4009 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4010 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4011 | printk("md: %s: data overlaps metadata\n", |
| 4012 | mdname(mddev)); |
| 4013 | return -EINVAL; |
| 4014 | } |
| 4015 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4016 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4017 | > rdev->data_offset) { |
| 4018 | printk("md: %s: metadata overlaps data\n", |
| 4019 | mdname(mddev)); |
| 4020 | return -EINVAL; |
| 4021 | } |
| 4022 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4023 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
| 4026 | md_probe(mddev->unit, NULL, NULL); |
| 4027 | disk = mddev->gendisk; |
| 4028 | if (!disk) |
| 4029 | return -ENOMEM; |
| 4030 | |
| 4031 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4032 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4033 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4035 | if (mddev->level != LEVEL_NONE) |
| 4036 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4037 | mddev->level); |
| 4038 | else |
| 4039 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4040 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | return -EINVAL; |
| 4042 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4043 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4045 | if (mddev->level != pers->level) { |
| 4046 | mddev->level = pers->level; |
| 4047 | mddev->new_level = pers->level; |
| 4048 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4049 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4051 | if (pers->level >= 4 && pers->level <= 6) |
| 4052 | /* Cannot support integrity (yet) */ |
| 4053 | blk_integrity_unregister(mddev->gendisk); |
| 4054 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4055 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4056 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4057 | /* This personality cannot handle reshaping... */ |
| 4058 | mddev->pers = NULL; |
| 4059 | module_put(pers->owner); |
| 4060 | return -EINVAL; |
| 4061 | } |
| 4062 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4063 | if (pers->sync_request) { |
| 4064 | /* Warn if this is a potentially silly |
| 4065 | * configuration. |
| 4066 | */ |
| 4067 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4068 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4069 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4070 | |
| 4071 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4072 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4073 | if (rdev < rdev2 && |
| 4074 | rdev->bdev->bd_contains == |
| 4075 | rdev2->bdev->bd_contains) { |
| 4076 | printk(KERN_WARNING |
| 4077 | "%s: WARNING: %s appears to be" |
| 4078 | " on the same physical disk as" |
| 4079 | " %s.\n", |
| 4080 | mdname(mddev), |
| 4081 | bdevname(rdev->bdev,b), |
| 4082 | bdevname(rdev2->bdev,b2)); |
| 4083 | warned = 1; |
| 4084 | } |
| 4085 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4086 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4087 | if (warned) |
| 4088 | printk(KERN_WARNING |
| 4089 | "True protection against single-disk" |
| 4090 | " failure might be compromised.\n"); |
| 4091 | } |
| 4092 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4093 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4094 | /* may be over-ridden by personality */ |
| 4095 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4096 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4097 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4098 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4100 | if (start_readonly) |
| 4101 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4102 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4103 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4104 | if (err) |
| 4105 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4106 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4107 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4108 | " but 'external_size' not in effect?\n", __func__); |
| 4109 | printk(KERN_ERR |
| 4110 | "md: invalid array_size %llu > default size %llu\n", |
| 4111 | (unsigned long long)mddev->array_sectors / 2, |
| 4112 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4113 | err = -EINVAL; |
| 4114 | mddev->pers->stop(mddev); |
| 4115 | } |
| 4116 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4117 | err = bitmap_create(mddev); |
| 4118 | if (err) { |
| 4119 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4120 | mdname(mddev), err); |
| 4121 | mddev->pers->stop(mddev); |
| 4122 | } |
| 4123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | module_put(mddev->pers->owner); |
| 4126 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4127 | bitmap_destroy(mddev); |
| 4128 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4130 | if (mddev->pers->sync_request) { |
| 4131 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4132 | printk(KERN_WARNING |
| 4133 | "md: cannot register extra attributes for %s\n", |
| 4134 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4135 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4136 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4137 | mddev->ro = 0; |
| 4138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | atomic_set(&mddev->writes_pending,0); |
| 4140 | mddev->safemode = 0; |
| 4141 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4142 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4143 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4145 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4146 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4147 | if (rdev->raid_disk >= 0) { |
| 4148 | char nm[20]; |
| 4149 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4150 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4151 | printk("md: cannot register %s for %s\n", |
| 4152 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | |
| 4155 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4156 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4157 | if (mddev->flags) |
| 4158 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4160 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4162 | /* If there is a partially-recovered drive we need to |
| 4163 | * start recovery here. If we leave it to md_check_recovery, |
| 4164 | * it will remove the drives and not do the right thing |
| 4165 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4166 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4167 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4168 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4169 | if (rdev->raid_disk >= 0 && |
| 4170 | !test_bit(In_sync, &rdev->flags) && |
| 4171 | !test_bit(Faulty, &rdev->flags)) |
| 4172 | /* complete an interrupted recovery */ |
| 4173 | spares++; |
| 4174 | if (spares && mddev->pers->sync_request) { |
| 4175 | mddev->recovery = 0; |
| 4176 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4177 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4178 | mddev, |
| 4179 | "%s_resync"); |
| 4180 | if (!mddev->sync_thread) { |
| 4181 | printk(KERN_ERR "%s: could not start resync" |
| 4182 | " thread...\n", |
| 4183 | mdname(mddev)); |
| 4184 | /* leave the spares where they are, it shouldn't hurt */ |
| 4185 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4186 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4187 | } |
| 4188 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4189 | md_wakeup_thread(mddev->thread); |
| 4190 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4191 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4192 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4193 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4194 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4195 | if (mddev->sysfs_action) |
| 4196 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4197 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4198 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | return 0; |
| 4200 | } |
| 4201 | |
| 4202 | static int restart_array(mddev_t *mddev) |
| 4203 | { |
| 4204 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4206 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4208 | return -ENXIO; |
| 4209 | if (!mddev->pers) |
| 4210 | return -EINVAL; |
| 4211 | if (!mddev->ro) |
| 4212 | return -EBUSY; |
| 4213 | mddev->safemode = 0; |
| 4214 | mddev->ro = 0; |
| 4215 | set_disk_ro(disk, 0); |
| 4216 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4217 | mdname(mddev)); |
| 4218 | /* Kick recovery or resync if necessary */ |
| 4219 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4220 | md_wakeup_thread(mddev->thread); |
| 4221 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4222 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4223 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | } |
| 4225 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4226 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4227 | * to the file ourselves */ |
| 4228 | static int deny_bitmap_write_access(struct file * file) |
| 4229 | { |
| 4230 | struct inode *inode = file->f_mapping->host; |
| 4231 | |
| 4232 | spin_lock(&inode->i_lock); |
| 4233 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4234 | spin_unlock(&inode->i_lock); |
| 4235 | return -ETXTBSY; |
| 4236 | } |
| 4237 | atomic_set(&inode->i_writecount, -1); |
| 4238 | spin_unlock(&inode->i_lock); |
| 4239 | |
| 4240 | return 0; |
| 4241 | } |
| 4242 | |
| 4243 | static void restore_bitmap_write_access(struct file *file) |
| 4244 | { |
| 4245 | struct inode *inode = file->f_mapping->host; |
| 4246 | |
| 4247 | spin_lock(&inode->i_lock); |
| 4248 | atomic_set(&inode->i_writecount, 1); |
| 4249 | spin_unlock(&inode->i_lock); |
| 4250 | } |
| 4251 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4252 | /* mode: |
| 4253 | * 0 - completely stop and dis-assemble array |
| 4254 | * 1 - switch to readonly |
| 4255 | * 2 - stop but do not disassemble array |
| 4256 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4257 | 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] | 4258 | { |
| 4259 | int err = 0; |
| 4260 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4261 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4263 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4264 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4265 | return -EBUSY; |
| 4266 | } |
| 4267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | |
| 4270 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4271 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4273 | md_unregister_thread(mddev->sync_thread); |
| 4274 | mddev->sync_thread = NULL; |
| 4275 | } |
| 4276 | |
| 4277 | del_timer_sync(&mddev->safemode_timer); |
| 4278 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4279 | switch(mode) { |
| 4280 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4282 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | goto out; |
| 4284 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4285 | break; |
| 4286 | case 0: /* disassemble */ |
| 4287 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4288 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4289 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | if (mddev->ro) |
| 4291 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4293 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4294 | mddev->queue->merge_bvec_fn = NULL; |
| 4295 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4296 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4298 | if (mddev->pers->sync_request) |
| 4299 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4301 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4302 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4303 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4304 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4305 | if (rdev->raid_disk >= 0) { |
| 4306 | char nm[20]; |
| 4307 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4308 | sysfs_remove_link(&mddev->kobj, nm); |
| 4309 | } |
| 4310 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4311 | set_capacity(disk, 0); |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4312 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | if (mddev->ro) |
| 4315 | mddev->ro = 0; |
| 4316 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4317 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4318 | /* mark array as shutdown cleanly */ |
| 4319 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4320 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4322 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4324 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | /* |
| 4328 | * Free resources if final stop |
| 4329 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4330 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4333 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4334 | bitmap_destroy(mddev); |
| 4335 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4336 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4337 | fput(mddev->bitmap_file); |
| 4338 | mddev->bitmap_file = NULL; |
| 4339 | } |
| 4340 | mddev->bitmap_offset = 0; |
| 4341 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4342 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4343 | flush_scheduled_work(); |
| 4344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | export_array(mddev); |
| 4346 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4347 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4348 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4349 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4350 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4351 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4352 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4353 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4354 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4355 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4356 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4357 | mddev->level = LEVEL_NONE; |
| 4358 | mddev->clevel[0] = 0; |
| 4359 | mddev->flags = 0; |
| 4360 | mddev->ro = 0; |
| 4361 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4362 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4363 | mddev->ctime = mddev->utime = 0; |
| 4364 | mddev->layout = 0; |
| 4365 | mddev->max_disks = 0; |
| 4366 | mddev->events = 0; |
| 4367 | mddev->delta_disks = 0; |
| 4368 | mddev->new_level = LEVEL_NONE; |
| 4369 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4370 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4371 | mddev->curr_resync = 0; |
| 4372 | mddev->resync_mismatches = 0; |
| 4373 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4374 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4375 | mddev->recovery = 0; |
| 4376 | mddev->in_sync = 0; |
| 4377 | mddev->changed = 0; |
| 4378 | mddev->degraded = 0; |
| 4379 | mddev->barriers_work = 0; |
| 4380 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4381 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4382 | if (mddev->hold_active == UNTIL_STOP) |
| 4383 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4384 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4385 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4387 | mdname(mddev)); |
| 4388 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4389 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4390 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4391 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | out: |
| 4393 | return err; |
| 4394 | } |
| 4395 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4396 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | static void autorun_array(mddev_t *mddev) |
| 4398 | { |
| 4399 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | int err; |
| 4401 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4402 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | |
| 4405 | printk(KERN_INFO "md: running: "); |
| 4406 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4407 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | char b[BDEVNAME_SIZE]; |
| 4409 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4410 | } |
| 4411 | printk("\n"); |
| 4412 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4413 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | if (err) { |
| 4415 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4416 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | /* |
| 4421 | * lets try to run arrays based on all disks that have arrived |
| 4422 | * until now. (those are in pending_raid_disks) |
| 4423 | * |
| 4424 | * the method: pick the first pending disk, collect all disks with |
| 4425 | * the same UUID, remove all from the pending list and put them into |
| 4426 | * the 'same_array' list. Then order this list based on superblock |
| 4427 | * update time (freshest comes first), kick out 'old' disks and |
| 4428 | * compare superblocks. If everything's fine then run it. |
| 4429 | * |
| 4430 | * If "unit" is allocated, then bump its reference count |
| 4431 | */ |
| 4432 | static void autorun_devices(int part) |
| 4433 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4434 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | mddev_t *mddev; |
| 4436 | char b[BDEVNAME_SIZE]; |
| 4437 | |
| 4438 | printk(KERN_INFO "md: autorun ...\n"); |
| 4439 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4440 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4441 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4442 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | rdev0 = list_entry(pending_raid_disks.next, |
| 4444 | mdk_rdev_t, same_set); |
| 4445 | |
| 4446 | printk(KERN_INFO "md: considering %s ...\n", |
| 4447 | bdevname(rdev0->bdev,b)); |
| 4448 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4449 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4451 | printk(KERN_INFO "md: adding %s ...\n", |
| 4452 | bdevname(rdev->bdev,b)); |
| 4453 | list_move(&rdev->same_set, &candidates); |
| 4454 | } |
| 4455 | /* |
| 4456 | * now we have a set of devices, with all of them having |
| 4457 | * mostly sane superblocks. It's time to allocate the |
| 4458 | * mddev. |
| 4459 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4460 | if (part) { |
| 4461 | dev = MKDEV(mdp_major, |
| 4462 | rdev0->preferred_minor << MdpMinorShift); |
| 4463 | unit = MINOR(dev) >> MdpMinorShift; |
| 4464 | } else { |
| 4465 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4466 | unit = MINOR(dev); |
| 4467 | } |
| 4468 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4470 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4471 | break; |
| 4472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | |
| 4474 | md_probe(dev, NULL, NULL); |
| 4475 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4476 | if (!mddev || !mddev->gendisk) { |
| 4477 | if (mddev) |
| 4478 | mddev_put(mddev); |
| 4479 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | "md: cannot allocate memory for md drive.\n"); |
| 4481 | break; |
| 4482 | } |
| 4483 | if (mddev_lock(mddev)) |
| 4484 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4485 | mdname(mddev)); |
| 4486 | else if (mddev->raid_disks || mddev->major_version |
| 4487 | || !list_empty(&mddev->disks)) { |
| 4488 | printk(KERN_WARNING |
| 4489 | "md: %s already running, cannot run %s\n", |
| 4490 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4491 | mddev_unlock(mddev); |
| 4492 | } else { |
| 4493 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4494 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4495 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | list_del_init(&rdev->same_set); |
| 4497 | if (bind_rdev_to_array(rdev, mddev)) |
| 4498 | export_rdev(rdev); |
| 4499 | } |
| 4500 | autorun_array(mddev); |
| 4501 | mddev_unlock(mddev); |
| 4502 | } |
| 4503 | /* on success, candidates will be empty, on error |
| 4504 | * it won't... |
| 4505 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4506 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4507 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4510 | mddev_put(mddev); |
| 4511 | } |
| 4512 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4513 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4514 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | static int get_version(void __user * arg) |
| 4517 | { |
| 4518 | mdu_version_t ver; |
| 4519 | |
| 4520 | ver.major = MD_MAJOR_VERSION; |
| 4521 | ver.minor = MD_MINOR_VERSION; |
| 4522 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4523 | |
| 4524 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4525 | return -EFAULT; |
| 4526 | |
| 4527 | return 0; |
| 4528 | } |
| 4529 | |
| 4530 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4531 | { |
| 4532 | mdu_array_info_t info; |
| 4533 | int nr,working,active,failed,spare; |
| 4534 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4535 | |
| 4536 | nr=working=active=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4537 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4539 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | failed++; |
| 4541 | else { |
| 4542 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4543 | if (test_bit(In_sync, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | active++; |
| 4545 | else |
| 4546 | spare++; |
| 4547 | } |
| 4548 | } |
| 4549 | |
| 4550 | info.major_version = mddev->major_version; |
| 4551 | info.minor_version = mddev->minor_version; |
| 4552 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4553 | info.ctime = mddev->ctime; |
| 4554 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4555 | info.size = mddev->dev_sectors / 2; |
| 4556 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4557 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | info.nr_disks = nr; |
| 4559 | info.raid_disks = mddev->raid_disks; |
| 4560 | info.md_minor = mddev->md_minor; |
| 4561 | info.not_persistent= !mddev->persistent; |
| 4562 | |
| 4563 | info.utime = mddev->utime; |
| 4564 | info.state = 0; |
| 4565 | if (mddev->in_sync) |
| 4566 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4567 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4568 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | info.active_disks = active; |
| 4570 | info.working_disks = working; |
| 4571 | info.failed_disks = failed; |
| 4572 | info.spare_disks = spare; |
| 4573 | |
| 4574 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4575 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4576 | |
| 4577 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4578 | return -EFAULT; |
| 4579 | |
| 4580 | return 0; |
| 4581 | } |
| 4582 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4583 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4584 | { |
| 4585 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4586 | char *ptr, *buf = NULL; |
| 4587 | int err = -ENOMEM; |
| 4588 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4589 | if (md_allow_write(mddev)) |
| 4590 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4591 | else |
| 4592 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4593 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4594 | if (!file) |
| 4595 | goto out; |
| 4596 | |
| 4597 | /* bitmap disabled, zero the first byte and copy out */ |
| 4598 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4599 | file->pathname[0] = '\0'; |
| 4600 | goto copy_out; |
| 4601 | } |
| 4602 | |
| 4603 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4604 | if (!buf) |
| 4605 | goto out; |
| 4606 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4607 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4608 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4609 | goto out; |
| 4610 | |
| 4611 | strcpy(file->pathname, ptr); |
| 4612 | |
| 4613 | copy_out: |
| 4614 | err = 0; |
| 4615 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4616 | err = -EFAULT; |
| 4617 | out: |
| 4618 | kfree(buf); |
| 4619 | kfree(file); |
| 4620 | return err; |
| 4621 | } |
| 4622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4623 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4624 | { |
| 4625 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | mdk_rdev_t *rdev; |
| 4627 | |
| 4628 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4629 | return -EFAULT; |
| 4630 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4631 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | if (rdev) { |
| 4633 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4634 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4635 | info.raid_disk = rdev->raid_disk; |
| 4636 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4637 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4639 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4640 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4641 | info.state |= (1<<MD_DISK_SYNC); |
| 4642 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4643 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4644 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | } else { |
| 4646 | info.major = info.minor = 0; |
| 4647 | info.raid_disk = -1; |
| 4648 | info.state = (1<<MD_DISK_REMOVED); |
| 4649 | } |
| 4650 | |
| 4651 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4652 | return -EFAULT; |
| 4653 | |
| 4654 | return 0; |
| 4655 | } |
| 4656 | |
| 4657 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4658 | { |
| 4659 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4660 | mdk_rdev_t *rdev; |
| 4661 | dev_t dev = MKDEV(info->major,info->minor); |
| 4662 | |
| 4663 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4664 | return -EOVERFLOW; |
| 4665 | |
| 4666 | if (!mddev->raid_disks) { |
| 4667 | int err; |
| 4668 | /* expecting a device which has a superblock */ |
| 4669 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4670 | if (IS_ERR(rdev)) { |
| 4671 | printk(KERN_WARNING |
| 4672 | "md: md_import_device returned %ld\n", |
| 4673 | PTR_ERR(rdev)); |
| 4674 | return PTR_ERR(rdev); |
| 4675 | } |
| 4676 | if (!list_empty(&mddev->disks)) { |
| 4677 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4678 | mdk_rdev_t, same_set); |
| 4679 | int err = super_types[mddev->major_version] |
| 4680 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4681 | if (err < 0) { |
| 4682 | printk(KERN_WARNING |
| 4683 | "md: %s has different UUID to %s\n", |
| 4684 | bdevname(rdev->bdev,b), |
| 4685 | bdevname(rdev0->bdev,b2)); |
| 4686 | export_rdev(rdev); |
| 4687 | return -EINVAL; |
| 4688 | } |
| 4689 | } |
| 4690 | err = bind_rdev_to_array(rdev, mddev); |
| 4691 | if (err) |
| 4692 | export_rdev(rdev); |
| 4693 | return err; |
| 4694 | } |
| 4695 | |
| 4696 | /* |
| 4697 | * add_new_disk can be used once the array is assembled |
| 4698 | * to add "hot spares". They must already have a superblock |
| 4699 | * written |
| 4700 | */ |
| 4701 | if (mddev->pers) { |
| 4702 | int err; |
| 4703 | if (!mddev->pers->hot_add_disk) { |
| 4704 | printk(KERN_WARNING |
| 4705 | "%s: personality does not support diskops!\n", |
| 4706 | mdname(mddev)); |
| 4707 | return -EINVAL; |
| 4708 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4709 | if (mddev->persistent) |
| 4710 | rdev = md_import_device(dev, mddev->major_version, |
| 4711 | mddev->minor_version); |
| 4712 | else |
| 4713 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | if (IS_ERR(rdev)) { |
| 4715 | printk(KERN_WARNING |
| 4716 | "md: md_import_device returned %ld\n", |
| 4717 | PTR_ERR(rdev)); |
| 4718 | return PTR_ERR(rdev); |
| 4719 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4720 | /* set save_raid_disk if appropriate */ |
| 4721 | if (!mddev->persistent) { |
| 4722 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4723 | info->raid_disk < mddev->raid_disks) |
| 4724 | rdev->raid_disk = info->raid_disk; |
| 4725 | else |
| 4726 | rdev->raid_disk = -1; |
| 4727 | } else |
| 4728 | super_types[mddev->major_version]. |
| 4729 | validate_super(mddev, rdev); |
| 4730 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4731 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4732 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4733 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4734 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4735 | else |
| 4736 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | rdev->raid_disk = -1; |
| 4739 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4740 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4741 | /* If there is hot_add_disk but no hot_remove_disk |
| 4742 | * then added disks for geometry changes, |
| 4743 | * and should be added immediately. |
| 4744 | */ |
| 4745 | super_types[mddev->major_version]. |
| 4746 | validate_super(mddev, rdev); |
| 4747 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4748 | if (err) |
| 4749 | unbind_rdev_from_array(rdev); |
| 4750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | if (err) |
| 4752 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4753 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4754 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4755 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4756 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4757 | if (mddev->degraded) |
| 4758 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4759 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4760 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | return err; |
| 4762 | } |
| 4763 | |
| 4764 | /* otherwise, add_new_disk is only allowed |
| 4765 | * for major_version==0 superblocks |
| 4766 | */ |
| 4767 | if (mddev->major_version != 0) { |
| 4768 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4769 | mdname(mddev)); |
| 4770 | return -EINVAL; |
| 4771 | } |
| 4772 | |
| 4773 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4774 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4775 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | if (IS_ERR(rdev)) { |
| 4777 | printk(KERN_WARNING |
| 4778 | "md: error, md_import_device() returned %ld\n", |
| 4779 | PTR_ERR(rdev)); |
| 4780 | return PTR_ERR(rdev); |
| 4781 | } |
| 4782 | rdev->desc_nr = info->number; |
| 4783 | if (info->raid_disk < mddev->raid_disks) |
| 4784 | rdev->raid_disk = info->raid_disk; |
| 4785 | else |
| 4786 | rdev->raid_disk = -1; |
| 4787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4789 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4790 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4792 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4793 | set_bit(WriteMostly, &rdev->flags); |
| 4794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4795 | if (!mddev->persistent) { |
| 4796 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4797 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4799 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame^] | 4800 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4802 | err = bind_rdev_to_array(rdev, mddev); |
| 4803 | if (err) { |
| 4804 | export_rdev(rdev); |
| 4805 | return err; |
| 4806 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
| 4812 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4813 | { |
| 4814 | char b[BDEVNAME_SIZE]; |
| 4815 | mdk_rdev_t *rdev; |
| 4816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | rdev = find_rdev(mddev, dev); |
| 4818 | if (!rdev) |
| 4819 | return -ENXIO; |
| 4820 | |
| 4821 | if (rdev->raid_disk >= 0) |
| 4822 | goto busy; |
| 4823 | |
| 4824 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4825 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4826 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | |
| 4828 | return 0; |
| 4829 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4830 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4832 | return -EBUSY; |
| 4833 | } |
| 4834 | |
| 4835 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4836 | { |
| 4837 | char b[BDEVNAME_SIZE]; |
| 4838 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | mdk_rdev_t *rdev; |
| 4840 | |
| 4841 | if (!mddev->pers) |
| 4842 | return -ENODEV; |
| 4843 | |
| 4844 | if (mddev->major_version != 0) { |
| 4845 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4846 | " version-0 superblocks.\n", |
| 4847 | mdname(mddev)); |
| 4848 | return -EINVAL; |
| 4849 | } |
| 4850 | if (!mddev->pers->hot_add_disk) { |
| 4851 | printk(KERN_WARNING |
| 4852 | "%s: personality does not support diskops!\n", |
| 4853 | mdname(mddev)); |
| 4854 | return -EINVAL; |
| 4855 | } |
| 4856 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4857 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | if (IS_ERR(rdev)) { |
| 4859 | printk(KERN_WARNING |
| 4860 | "md: error, md_import_device() returned %ld\n", |
| 4861 | PTR_ERR(rdev)); |
| 4862 | return -EINVAL; |
| 4863 | } |
| 4864 | |
| 4865 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4866 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4868 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame^] | 4870 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4872 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | printk(KERN_WARNING |
| 4874 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4875 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4876 | err = -EINVAL; |
| 4877 | goto abort_export; |
| 4878 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4879 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4880 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4881 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4882 | err = bind_rdev_to_array(rdev, mddev); |
| 4883 | if (err) |
| 4884 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | |
| 4886 | /* |
| 4887 | * The rest should better be atomic, we can have disk failures |
| 4888 | * noticed in interrupt contexts ... |
| 4889 | */ |
| 4890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | rdev->raid_disk = -1; |
| 4892 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4893 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
| 4895 | /* |
| 4896 | * Kick recovery, maybe this spare has to be added to the |
| 4897 | * array immediately. |
| 4898 | */ |
| 4899 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4900 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4901 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | return 0; |
| 4903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | abort_export: |
| 4905 | export_rdev(rdev); |
| 4906 | return err; |
| 4907 | } |
| 4908 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4909 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4910 | { |
| 4911 | int err; |
| 4912 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4913 | if (mddev->pers) { |
| 4914 | if (!mddev->pers->quiesce) |
| 4915 | return -EBUSY; |
| 4916 | if (mddev->recovery || mddev->sync_thread) |
| 4917 | return -EBUSY; |
| 4918 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4919 | } |
| 4920 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4921 | |
| 4922 | if (fd >= 0) { |
| 4923 | if (mddev->bitmap) |
| 4924 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4925 | mddev->bitmap_file = fget(fd); |
| 4926 | |
| 4927 | if (mddev->bitmap_file == NULL) { |
| 4928 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4929 | mdname(mddev)); |
| 4930 | return -EBADF; |
| 4931 | } |
| 4932 | |
| 4933 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4934 | if (err) { |
| 4935 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4936 | mdname(mddev)); |
| 4937 | fput(mddev->bitmap_file); |
| 4938 | mddev->bitmap_file = NULL; |
| 4939 | return err; |
| 4940 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4941 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4942 | } else if (mddev->bitmap == NULL) |
| 4943 | return -ENOENT; /* cannot remove what isn't there */ |
| 4944 | err = 0; |
| 4945 | if (mddev->pers) { |
| 4946 | mddev->pers->quiesce(mddev, 1); |
| 4947 | if (fd >= 0) |
| 4948 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4949 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4950 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4951 | fd = -1; /* make sure to put the file */ |
| 4952 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4953 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4954 | } |
| 4955 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4956 | if (mddev->bitmap_file) { |
| 4957 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4958 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4959 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4960 | mddev->bitmap_file = NULL; |
| 4961 | } |
| 4962 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4963 | return err; |
| 4964 | } |
| 4965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | /* |
| 4967 | * set_array_info is used two different ways |
| 4968 | * The original usage is when creating a new array. |
| 4969 | * In this usage, raid_disks is > 0 and it together with |
| 4970 | * level, size, not_persistent,layout,chunksize determine the |
| 4971 | * shape of the array. |
| 4972 | * This will always create an array with a type-0.90.0 superblock. |
| 4973 | * The newer usage is when assembling an array. |
| 4974 | * In this case raid_disks will be 0, and the major_version field is |
| 4975 | * use to determine which style super-blocks are to be found on the devices. |
| 4976 | * The minor and patch _version numbers are also kept incase the |
| 4977 | * super_block handler wishes to interpret them. |
| 4978 | */ |
| 4979 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 4980 | { |
| 4981 | |
| 4982 | if (info->raid_disks == 0) { |
| 4983 | /* just setting version number for superblock loading */ |
| 4984 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4985 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4986 | super_types[info->major_version].name == NULL) { |
| 4987 | /* maybe try to auto-load a module? */ |
| 4988 | printk(KERN_INFO |
| 4989 | "md: superblock version %d not known\n", |
| 4990 | info->major_version); |
| 4991 | return -EINVAL; |
| 4992 | } |
| 4993 | mddev->major_version = info->major_version; |
| 4994 | mddev->minor_version = info->minor_version; |
| 4995 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4996 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | return 0; |
| 4998 | } |
| 4999 | mddev->major_version = MD_MAJOR_VERSION; |
| 5000 | mddev->minor_version = MD_MINOR_VERSION; |
| 5001 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5002 | mddev->ctime = get_seconds(); |
| 5003 | |
| 5004 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5005 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5006 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | mddev->raid_disks = info->raid_disks; |
| 5008 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5009 | * openned |
| 5010 | */ |
| 5011 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5012 | mddev->recovery_cp = MaxSector; |
| 5013 | else |
| 5014 | mddev->recovery_cp = 0; |
| 5015 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5016 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5017 | |
| 5018 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5019 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5020 | |
| 5021 | mddev->max_disks = MD_SB_DISKS; |
| 5022 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5023 | if (mddev->persistent) |
| 5024 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5025 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5027 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 5028 | mddev->bitmap_offset = 0; |
| 5029 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5030 | mddev->reshape_position = MaxSector; |
| 5031 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | /* |
| 5033 | * Generate a 128 bit UUID |
| 5034 | */ |
| 5035 | get_random_bytes(mddev->uuid, 16); |
| 5036 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5037 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5038 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5039 | mddev->new_layout = mddev->layout; |
| 5040 | mddev->delta_disks = 0; |
| 5041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | return 0; |
| 5043 | } |
| 5044 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5045 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5046 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5047 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5048 | |
| 5049 | if (mddev->external_size) |
| 5050 | return; |
| 5051 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5052 | mddev->array_sectors = array_sectors; |
| 5053 | } |
| 5054 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5055 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5056 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5057 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5058 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5059 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5060 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5061 | |
| 5062 | if (mddev->pers->resize == NULL) |
| 5063 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5064 | /* The "num_sectors" is the number of sectors of each device that |
| 5065 | * is used. This can only make sense for arrays with redundancy. |
| 5066 | * linear and raid0 always use whatever space is available. We can only |
| 5067 | * consider changing this number if no resync or reconstruction is |
| 5068 | * 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] | 5069 | * 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] | 5070 | * of each device. If num_sectors is zero, we find the largest size |
| 5071 | * that fits. |
| 5072 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5073 | */ |
| 5074 | if (mddev->sync_thread) |
| 5075 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5076 | if (mddev->bitmap) |
| 5077 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5078 | * grow, and re-add. |
| 5079 | */ |
| 5080 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5081 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5082 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5083 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5084 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5085 | num_sectors = avail; |
| 5086 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5087 | return -ENOSPC; |
| 5088 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5089 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5090 | if (!rv) { |
| 5091 | struct block_device *bdev; |
| 5092 | |
| 5093 | bdev = bdget_disk(mddev->gendisk, 0); |
| 5094 | if (bdev) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5095 | mutex_lock(&bdev->bd_inode->i_mutex); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5096 | i_size_write(bdev->bd_inode, |
| 5097 | (loff_t)mddev->array_sectors << 9); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5098 | mutex_unlock(&bdev->bd_inode->i_mutex); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5099 | bdput(bdev); |
| 5100 | } |
| 5101 | } |
| 5102 | return rv; |
| 5103 | } |
| 5104 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5105 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5106 | { |
| 5107 | int rv; |
| 5108 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5109 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5110 | return -EINVAL; |
| 5111 | if (raid_disks <= 0 || |
| 5112 | raid_disks >= mddev->max_disks) |
| 5113 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5114 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5115 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5116 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5117 | |
| 5118 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5119 | return rv; |
| 5120 | } |
| 5121 | |
| 5122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | /* |
| 5124 | * update_array_info is used to change the configuration of an |
| 5125 | * on-line array. |
| 5126 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5127 | * fields in the info are checked against the array. |
| 5128 | * Any differences that cannot be handled will cause an error. |
| 5129 | * Normally, only one change can be managed at a time. |
| 5130 | */ |
| 5131 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5132 | { |
| 5133 | int rv = 0; |
| 5134 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5135 | int state = 0; |
| 5136 | |
| 5137 | /* calculate expected state,ignoring low bits */ |
| 5138 | if (mddev->bitmap && mddev->bitmap_offset) |
| 5139 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | |
| 5141 | if (mddev->major_version != info->major_version || |
| 5142 | mddev->minor_version != info->minor_version || |
| 5143 | /* mddev->patch_version != info->patch_version || */ |
| 5144 | mddev->ctime != info->ctime || |
| 5145 | mddev->level != info->level || |
| 5146 | /* mddev->layout != info->layout || */ |
| 5147 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5148 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5149 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5150 | ((state^info->state) & 0xfffffe00) |
| 5151 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | return -EINVAL; |
| 5153 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5154 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5155 | cnt++; |
| 5156 | if (mddev->raid_disks != info->raid_disks) |
| 5157 | cnt++; |
| 5158 | if (mddev->layout != info->layout) |
| 5159 | cnt++; |
| 5160 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5161 | cnt++; |
| 5162 | if (cnt == 0) |
| 5163 | return 0; |
| 5164 | if (cnt > 1) |
| 5165 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | |
| 5167 | if (mddev->layout != info->layout) { |
| 5168 | /* Change layout |
| 5169 | * we don't need to do anything at the md level, the |
| 5170 | * personality will take care of it all. |
| 5171 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5172 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5174 | else { |
| 5175 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5176 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5177 | if (rv) |
| 5178 | mddev->new_layout = mddev->layout; |
| 5179 | return rv; |
| 5180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5182 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5183 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5185 | if (mddev->raid_disks != info->raid_disks) |
| 5186 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5188 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5189 | if (mddev->pers->quiesce == NULL) |
| 5190 | return -EINVAL; |
| 5191 | if (mddev->recovery || mddev->sync_thread) |
| 5192 | return -EBUSY; |
| 5193 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5194 | /* add the bitmap */ |
| 5195 | if (mddev->bitmap) |
| 5196 | return -EEXIST; |
| 5197 | if (mddev->default_bitmap_offset == 0) |
| 5198 | return -EINVAL; |
| 5199 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 5200 | mddev->pers->quiesce(mddev, 1); |
| 5201 | rv = bitmap_create(mddev); |
| 5202 | if (rv) |
| 5203 | bitmap_destroy(mddev); |
| 5204 | mddev->pers->quiesce(mddev, 0); |
| 5205 | } else { |
| 5206 | /* remove the bitmap */ |
| 5207 | if (!mddev->bitmap) |
| 5208 | return -ENOENT; |
| 5209 | if (mddev->bitmap->file) |
| 5210 | return -EINVAL; |
| 5211 | mddev->pers->quiesce(mddev, 1); |
| 5212 | bitmap_destroy(mddev); |
| 5213 | mddev->pers->quiesce(mddev, 0); |
| 5214 | mddev->bitmap_offset = 0; |
| 5215 | } |
| 5216 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5217 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | return rv; |
| 5219 | } |
| 5220 | |
| 5221 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5222 | { |
| 5223 | mdk_rdev_t *rdev; |
| 5224 | |
| 5225 | if (mddev->pers == NULL) |
| 5226 | return -ENODEV; |
| 5227 | |
| 5228 | rdev = find_rdev(mddev, dev); |
| 5229 | if (!rdev) |
| 5230 | return -ENODEV; |
| 5231 | |
| 5232 | md_error(mddev, rdev); |
| 5233 | return 0; |
| 5234 | } |
| 5235 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5236 | /* |
| 5237 | * We have a problem here : there is no easy way to give a CHS |
| 5238 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5239 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5240 | * dosfs just mad... ;-) |
| 5241 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5242 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5243 | { |
| 5244 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5245 | |
| 5246 | geo->heads = 2; |
| 5247 | geo->sectors = 4; |
| 5248 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5249 | return 0; |
| 5250 | } |
| 5251 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5252 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | unsigned int cmd, unsigned long arg) |
| 5254 | { |
| 5255 | int err = 0; |
| 5256 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | mddev_t *mddev = NULL; |
| 5258 | |
| 5259 | if (!capable(CAP_SYS_ADMIN)) |
| 5260 | return -EACCES; |
| 5261 | |
| 5262 | /* |
| 5263 | * Commands dealing with the RAID driver but not any |
| 5264 | * particular array: |
| 5265 | */ |
| 5266 | switch (cmd) |
| 5267 | { |
| 5268 | case RAID_VERSION: |
| 5269 | err = get_version(argp); |
| 5270 | goto done; |
| 5271 | |
| 5272 | case PRINT_RAID_DEBUG: |
| 5273 | err = 0; |
| 5274 | md_print_devices(); |
| 5275 | goto done; |
| 5276 | |
| 5277 | #ifndef MODULE |
| 5278 | case RAID_AUTORUN: |
| 5279 | err = 0; |
| 5280 | autostart_arrays(arg); |
| 5281 | goto done; |
| 5282 | #endif |
| 5283 | default:; |
| 5284 | } |
| 5285 | |
| 5286 | /* |
| 5287 | * Commands creating/starting a new array: |
| 5288 | */ |
| 5289 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5290 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5291 | |
| 5292 | if (!mddev) { |
| 5293 | BUG(); |
| 5294 | goto abort; |
| 5295 | } |
| 5296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | err = mddev_lock(mddev); |
| 5298 | if (err) { |
| 5299 | printk(KERN_INFO |
| 5300 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5301 | err, cmd); |
| 5302 | goto abort; |
| 5303 | } |
| 5304 | |
| 5305 | switch (cmd) |
| 5306 | { |
| 5307 | case SET_ARRAY_INFO: |
| 5308 | { |
| 5309 | mdu_array_info_t info; |
| 5310 | if (!arg) |
| 5311 | memset(&info, 0, sizeof(info)); |
| 5312 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5313 | err = -EFAULT; |
| 5314 | goto abort_unlock; |
| 5315 | } |
| 5316 | if (mddev->pers) { |
| 5317 | err = update_array_info(mddev, &info); |
| 5318 | if (err) { |
| 5319 | printk(KERN_WARNING "md: couldn't update" |
| 5320 | " array info. %d\n", err); |
| 5321 | goto abort_unlock; |
| 5322 | } |
| 5323 | goto done_unlock; |
| 5324 | } |
| 5325 | if (!list_empty(&mddev->disks)) { |
| 5326 | printk(KERN_WARNING |
| 5327 | "md: array %s already has disks!\n", |
| 5328 | mdname(mddev)); |
| 5329 | err = -EBUSY; |
| 5330 | goto abort_unlock; |
| 5331 | } |
| 5332 | if (mddev->raid_disks) { |
| 5333 | printk(KERN_WARNING |
| 5334 | "md: array %s already initialised!\n", |
| 5335 | mdname(mddev)); |
| 5336 | err = -EBUSY; |
| 5337 | goto abort_unlock; |
| 5338 | } |
| 5339 | err = set_array_info(mddev, &info); |
| 5340 | if (err) { |
| 5341 | printk(KERN_WARNING "md: couldn't set" |
| 5342 | " array info. %d\n", err); |
| 5343 | goto abort_unlock; |
| 5344 | } |
| 5345 | } |
| 5346 | goto done_unlock; |
| 5347 | |
| 5348 | default:; |
| 5349 | } |
| 5350 | |
| 5351 | /* |
| 5352 | * Commands querying/configuring an existing array: |
| 5353 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5354 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5355 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5356 | if ((!mddev->raid_disks && !mddev->external) |
| 5357 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5358 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5359 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | err = -ENODEV; |
| 5361 | goto abort_unlock; |
| 5362 | } |
| 5363 | |
| 5364 | /* |
| 5365 | * Commands even a read-only array can execute: |
| 5366 | */ |
| 5367 | switch (cmd) |
| 5368 | { |
| 5369 | case GET_ARRAY_INFO: |
| 5370 | err = get_array_info(mddev, argp); |
| 5371 | goto done_unlock; |
| 5372 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5373 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5374 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5375 | goto done_unlock; |
| 5376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | case GET_DISK_INFO: |
| 5378 | err = get_disk_info(mddev, argp); |
| 5379 | goto done_unlock; |
| 5380 | |
| 5381 | case RESTART_ARRAY_RW: |
| 5382 | err = restart_array(mddev); |
| 5383 | goto done_unlock; |
| 5384 | |
| 5385 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5386 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | goto done_unlock; |
| 5388 | |
| 5389 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5390 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | goto done_unlock; |
| 5392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | } |
| 5394 | |
| 5395 | /* |
| 5396 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5397 | * superblock, so we do not allow them on read-only arrays. |
| 5398 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5399 | * here and hit the 'default' below, so only disallow |
| 5400 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5402 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5403 | if (mddev->ro == 2) { |
| 5404 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5405 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5406 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5407 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5408 | } else { |
| 5409 | err = -EROFS; |
| 5410 | goto abort_unlock; |
| 5411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | switch (cmd) |
| 5415 | { |
| 5416 | case ADD_NEW_DISK: |
| 5417 | { |
| 5418 | mdu_disk_info_t info; |
| 5419 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5420 | err = -EFAULT; |
| 5421 | else |
| 5422 | err = add_new_disk(mddev, &info); |
| 5423 | goto done_unlock; |
| 5424 | } |
| 5425 | |
| 5426 | case HOT_REMOVE_DISK: |
| 5427 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5428 | goto done_unlock; |
| 5429 | |
| 5430 | case HOT_ADD_DISK: |
| 5431 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5432 | goto done_unlock; |
| 5433 | |
| 5434 | case SET_DISK_FAULTY: |
| 5435 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5436 | goto done_unlock; |
| 5437 | |
| 5438 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5439 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | goto done_unlock; |
| 5441 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5442 | case SET_BITMAP_FILE: |
| 5443 | err = set_bitmap_file(mddev, (int)arg); |
| 5444 | goto done_unlock; |
| 5445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | err = -EINVAL; |
| 5448 | goto abort_unlock; |
| 5449 | } |
| 5450 | |
| 5451 | done_unlock: |
| 5452 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5453 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5454 | err != -EINVAL) |
| 5455 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | mddev_unlock(mddev); |
| 5457 | |
| 5458 | return err; |
| 5459 | done: |
| 5460 | if (err) |
| 5461 | MD_BUG(); |
| 5462 | abort: |
| 5463 | return err; |
| 5464 | } |
| 5465 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5466 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | { |
| 5468 | /* |
| 5469 | * Succeed if we can lock the mddev, which confirms that |
| 5470 | * it isn't being stopped right now. |
| 5471 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5472 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | int err; |
| 5474 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5475 | if (mddev->gendisk != bdev->bd_disk) { |
| 5476 | /* we are racing with mddev_put which is discarding this |
| 5477 | * bd_disk. |
| 5478 | */ |
| 5479 | mddev_put(mddev); |
| 5480 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5481 | flush_scheduled_work(); |
| 5482 | /* Then retry the open from the top */ |
| 5483 | return -ERESTARTSYS; |
| 5484 | } |
| 5485 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5486 | |
NeilBrown | d63a5a7 | 2006-12-08 02:36:17 -0800 | [diff] [blame] | 5487 | if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | goto out; |
| 5489 | |
| 5490 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5491 | atomic_inc(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | mddev_unlock(mddev); |
| 5493 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5494 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | out: |
| 5496 | return err; |
| 5497 | } |
| 5498 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5499 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5500 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5501 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5503 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5504 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5505 | mddev_put(mddev); |
| 5506 | |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5510 | static int md_media_changed(struct gendisk *disk) |
| 5511 | { |
| 5512 | mddev_t *mddev = disk->private_data; |
| 5513 | |
| 5514 | return mddev->changed; |
| 5515 | } |
| 5516 | |
| 5517 | static int md_revalidate(struct gendisk *disk) |
| 5518 | { |
| 5519 | mddev_t *mddev = disk->private_data; |
| 5520 | |
| 5521 | mddev->changed = 0; |
| 5522 | return 0; |
| 5523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | static struct block_device_operations md_fops = |
| 5525 | { |
| 5526 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5527 | .open = md_open, |
| 5528 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5529 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5530 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce6294 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5531 | .media_changed = md_media_changed, |
| 5532 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | }; |
| 5534 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5535 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | { |
| 5537 | mdk_thread_t *thread = arg; |
| 5538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | /* |
| 5540 | * md_thread is a 'system-thread', it's priority should be very |
| 5541 | * high. We avoid resource deadlocks individually in each |
| 5542 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5543 | * the very same RT priority as kswapd, thus we will never get |
| 5544 | * into a priority inversion deadlock. |
| 5545 | * |
| 5546 | * we definitely have to have equal or higher priority than |
| 5547 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5548 | * many dirty RAID5 blocks. |
| 5549 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5551 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5552 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5554 | /* We need to wait INTERRUPTIBLE so that |
| 5555 | * we don't add to the load-average. |
| 5556 | * That means we need to be sure no signals are |
| 5557 | * pending |
| 5558 | */ |
| 5559 | if (signal_pending(current)) |
| 5560 | flush_signals(current); |
| 5561 | |
| 5562 | wait_event_interruptible_timeout |
| 5563 | (thread->wqueue, |
| 5564 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5565 | || kthread_should_stop(), |
| 5566 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | |
| 5568 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5569 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5570 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5573 | return 0; |
| 5574 | } |
| 5575 | |
| 5576 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5577 | { |
| 5578 | if (thread) { |
| 5579 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5580 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5581 | wake_up(&thread->wqueue); |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5586 | const char *name) |
| 5587 | { |
| 5588 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5590 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | if (!thread) |
| 5592 | return NULL; |
| 5593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | init_waitqueue_head(&thread->wqueue); |
| 5595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | thread->run = run; |
| 5597 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5598 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5599 | thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5600 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | kfree(thread); |
| 5602 | return NULL; |
| 5603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | return thread; |
| 5605 | } |
| 5606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | void md_unregister_thread(mdk_thread_t *thread) |
| 5608 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5609 | if (!thread) |
| 5610 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5611 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5612 | |
| 5613 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5614 | kfree(thread); |
| 5615 | } |
| 5616 | |
| 5617 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5618 | { |
| 5619 | if (!mddev) { |
| 5620 | MD_BUG(); |
| 5621 | return; |
| 5622 | } |
| 5623 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5624 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5626 | |
| 5627 | if (mddev->external) |
| 5628 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5629 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5630 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5631 | mdname(mddev), |
| 5632 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5633 | __builtin_return_address(0),__builtin_return_address(1), |
| 5634 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5635 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5636 | if (!mddev->pers) |
| 5637 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | if (!mddev->pers->error_handler) |
| 5639 | return; |
| 5640 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5641 | if (mddev->degraded) |
| 5642 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5643 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5645 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5646 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5647 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | /* seq_file implementation /proc/mdstat */ |
| 5651 | |
| 5652 | static void status_unused(struct seq_file *seq) |
| 5653 | { |
| 5654 | int i = 0; |
| 5655 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | |
| 5657 | seq_printf(seq, "unused devices: "); |
| 5658 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5659 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | char b[BDEVNAME_SIZE]; |
| 5661 | i++; |
| 5662 | seq_printf(seq, "%s ", |
| 5663 | bdevname(rdev->bdev,b)); |
| 5664 | } |
| 5665 | if (!i) |
| 5666 | seq_printf(seq, "<none>"); |
| 5667 | |
| 5668 | seq_printf(seq, "\n"); |
| 5669 | } |
| 5670 | |
| 5671 | |
| 5672 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5673 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5674 | sector_t max_sectors, resync, res; |
| 5675 | unsigned long dt, db; |
| 5676 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5677 | int scale; |
| 5678 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5680 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5681 | |
| 5682 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5683 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5685 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | |
| 5687 | /* |
| 5688 | * Should not happen. |
| 5689 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5690 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5691 | MD_BUG(); |
| 5692 | return; |
| 5693 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5694 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5695 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5696 | * u32, as those are the requirements for sector_div. |
| 5697 | * Thus 'scale' must be at least 10 |
| 5698 | */ |
| 5699 | scale = 10; |
| 5700 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5701 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5702 | scale++; |
| 5703 | } |
| 5704 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5705 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5706 | |
| 5707 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5709 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | seq_printf(seq, "["); |
| 5711 | for (i = 0; i < x; i++) |
| 5712 | seq_printf(seq, "="); |
| 5713 | seq_printf(seq, ">"); |
| 5714 | for (i = 0; i < y; i++) |
| 5715 | seq_printf(seq, "."); |
| 5716 | seq_printf(seq, "] "); |
| 5717 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5718 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5719 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5720 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5721 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5722 | "check" : |
| 5723 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5724 | "resync" : "recovery"))), |
| 5725 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5726 | (unsigned long long) resync/2, |
| 5727 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | |
| 5729 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | * dt: time from mark until now |
| 5731 | * db: blocks written from mark until now |
| 5732 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5733 | * |
| 5734 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5735 | * So we divide before multiply in case it is 32bit and close |
| 5736 | * to the limit. |
| 5737 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5738 | * near the end of resync when the number of remaining sectors |
| 5739 | * is close to 'db'. |
| 5740 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5741 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | */ |
| 5743 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5744 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5745 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5746 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5748 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5749 | sector_div(rt, db/32+1); |
| 5750 | rt *= dt; |
| 5751 | rt >>= 5; |
| 5752 | |
| 5753 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5754 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5756 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5760 | { |
| 5761 | struct list_head *tmp; |
| 5762 | loff_t l = *pos; |
| 5763 | mddev_t *mddev; |
| 5764 | |
| 5765 | if (l >= 0x10000) |
| 5766 | return NULL; |
| 5767 | if (!l--) |
| 5768 | /* header */ |
| 5769 | return (void*)1; |
| 5770 | |
| 5771 | spin_lock(&all_mddevs_lock); |
| 5772 | list_for_each(tmp,&all_mddevs) |
| 5773 | if (!l--) { |
| 5774 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5775 | mddev_get(mddev); |
| 5776 | spin_unlock(&all_mddevs_lock); |
| 5777 | return mddev; |
| 5778 | } |
| 5779 | spin_unlock(&all_mddevs_lock); |
| 5780 | if (!l--) |
| 5781 | return (void*)2;/* tail */ |
| 5782 | return NULL; |
| 5783 | } |
| 5784 | |
| 5785 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5786 | { |
| 5787 | struct list_head *tmp; |
| 5788 | mddev_t *next_mddev, *mddev = v; |
| 5789 | |
| 5790 | ++*pos; |
| 5791 | if (v == (void*)2) |
| 5792 | return NULL; |
| 5793 | |
| 5794 | spin_lock(&all_mddevs_lock); |
| 5795 | if (v == (void*)1) |
| 5796 | tmp = all_mddevs.next; |
| 5797 | else |
| 5798 | tmp = mddev->all_mddevs.next; |
| 5799 | if (tmp != &all_mddevs) |
| 5800 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5801 | else { |
| 5802 | next_mddev = (void*)2; |
| 5803 | *pos = 0x10000; |
| 5804 | } |
| 5805 | spin_unlock(&all_mddevs_lock); |
| 5806 | |
| 5807 | if (v != (void*)1) |
| 5808 | mddev_put(mddev); |
| 5809 | return next_mddev; |
| 5810 | |
| 5811 | } |
| 5812 | |
| 5813 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5814 | { |
| 5815 | mddev_t *mddev = v; |
| 5816 | |
| 5817 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5818 | mddev_put(mddev); |
| 5819 | } |
| 5820 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5821 | struct mdstat_info { |
| 5822 | int event; |
| 5823 | }; |
| 5824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5826 | { |
| 5827 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5828 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5829 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5830 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5831 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5832 | |
| 5833 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5834 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | seq_printf(seq, "Personalities : "); |
| 5836 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5837 | list_for_each_entry(pers, &pers_list, list) |
| 5838 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | |
| 5840 | spin_unlock(&pers_lock); |
| 5841 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5842 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | return 0; |
| 5844 | } |
| 5845 | if (v == (void*)2) { |
| 5846 | status_unused(seq); |
| 5847 | return 0; |
| 5848 | } |
| 5849 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5850 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5853 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5854 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5855 | mddev->pers ? "" : "in"); |
| 5856 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5857 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5858 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5859 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5860 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | seq_printf(seq, " %s", mddev->pers->name); |
| 5862 | } |
| 5863 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5864 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5865 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | char b[BDEVNAME_SIZE]; |
| 5867 | seq_printf(seq, " %s[%d]", |
| 5868 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5869 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5870 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5871 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | seq_printf(seq, "(F)"); |
| 5873 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5874 | } else if (rdev->raid_disk < 0) |
| 5875 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5876 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5877 | } |
| 5878 | |
| 5879 | if (!list_empty(&mddev->disks)) { |
| 5880 | if (mddev->pers) |
| 5881 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5882 | (unsigned long long) |
| 5883 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | else |
| 5885 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5886 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5888 | if (mddev->persistent) { |
| 5889 | if (mddev->major_version != 0 || |
| 5890 | mddev->minor_version != 90) { |
| 5891 | seq_printf(seq," super %d.%d", |
| 5892 | mddev->major_version, |
| 5893 | mddev->minor_version); |
| 5894 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5895 | } else if (mddev->external) |
| 5896 | seq_printf(seq, " super external:%s", |
| 5897 | mddev->metadata_type); |
| 5898 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5899 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | |
| 5901 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5902 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5904 | if (mddev->pers->sync_request) { |
| 5905 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5906 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5907 | seq_printf(seq, "\n "); |
| 5908 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5909 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5910 | else if (mddev->recovery_cp < MaxSector) |
| 5911 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5912 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5913 | } else |
| 5914 | seq_printf(seq, "\n "); |
| 5915 | |
| 5916 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5917 | unsigned long chunk_kb; |
| 5918 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5919 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5920 | chunk_kb = bitmap->chunksize >> 10; |
| 5921 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5922 | "%lu%s chunk", |
| 5923 | bitmap->pages - bitmap->missing_pages, |
| 5924 | bitmap->pages, |
| 5925 | (bitmap->pages - bitmap->missing_pages) |
| 5926 | << (PAGE_SHIFT - 10), |
| 5927 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5928 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5929 | if (bitmap->file) { |
| 5930 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5931 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5932 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5933 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5934 | seq_printf(seq, "\n"); |
| 5935 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | } |
| 5937 | |
| 5938 | seq_printf(seq, "\n"); |
| 5939 | } |
| 5940 | mddev_unlock(mddev); |
| 5941 | |
| 5942 | return 0; |
| 5943 | } |
| 5944 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 5945 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | .start = md_seq_start, |
| 5947 | .next = md_seq_next, |
| 5948 | .stop = md_seq_stop, |
| 5949 | .show = md_seq_show, |
| 5950 | }; |
| 5951 | |
| 5952 | static int md_seq_open(struct inode *inode, struct file *file) |
| 5953 | { |
| 5954 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5955 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 5956 | if (mi == NULL) |
| 5957 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | |
| 5959 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5960 | if (error) |
| 5961 | kfree(mi); |
| 5962 | else { |
| 5963 | struct seq_file *p = file->private_data; |
| 5964 | p->private = mi; |
| 5965 | mi->event = atomic_read(&md_event_count); |
| 5966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | return error; |
| 5968 | } |
| 5969 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5970 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 5971 | { |
| 5972 | struct seq_file *m = filp->private_data; |
| 5973 | struct mdstat_info *mi = m->private; |
| 5974 | int mask; |
| 5975 | |
| 5976 | poll_wait(filp, &md_event_waiters, wait); |
| 5977 | |
| 5978 | /* always allow read */ |
| 5979 | mask = POLLIN | POLLRDNORM; |
| 5980 | |
| 5981 | if (mi->event != atomic_read(&md_event_count)) |
| 5982 | mask |= POLLERR | POLLPRI; |
| 5983 | return mask; |
| 5984 | } |
| 5985 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 5986 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 5987 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | .open = md_seq_open, |
| 5989 | .read = seq_read, |
| 5990 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 5991 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5992 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | }; |
| 5994 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5995 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5998 | list_add_tail(&p->list, &pers_list); |
| 5999 | 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] | 6000 | spin_unlock(&pers_lock); |
| 6001 | return 0; |
| 6002 | } |
| 6003 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6004 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6006 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6008 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | spin_unlock(&pers_lock); |
| 6010 | return 0; |
| 6011 | } |
| 6012 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6013 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | { |
| 6015 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6016 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6017 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | |
| 6019 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6020 | rcu_read_lock(); |
| 6021 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6023 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6024 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6025 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6026 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6027 | * as sync_io is counted when a request starts, and |
| 6028 | * disk_stats is counted when it completes. |
| 6029 | * So resync activity will cause curr_events to be smaller than |
| 6030 | * when there was no such activity. |
| 6031 | * non-sync IO will cause disk_stat to increase without |
| 6032 | * increasing sync_io so curr_events will (eventually) |
| 6033 | * be larger than it was before. Once it becomes |
| 6034 | * substantially larger, the test below will cause |
| 6035 | * the array to appear non-idle, and resync will slow |
| 6036 | * down. |
| 6037 | * If there is a lot of outstanding resync activity when |
| 6038 | * we set last_event to curr_events, then all that activity |
| 6039 | * completing might cause the array to appear non-idle |
| 6040 | * and resync will be slowed down even though there might |
| 6041 | * not have been non-resync activity. This will only |
| 6042 | * happen once though. 'last_events' will soon reflect |
| 6043 | * the state where there is little or no outstanding |
| 6044 | * resync requests, and further resync activity will |
| 6045 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6046 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6048 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | rdev->last_events = curr_events; |
| 6050 | idle = 0; |
| 6051 | } |
| 6052 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6053 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6054 | return idle; |
| 6055 | } |
| 6056 | |
| 6057 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6058 | { |
| 6059 | /* another "blocks" (512byte) blocks have been synced */ |
| 6060 | atomic_sub(blocks, &mddev->recovery_active); |
| 6061 | wake_up(&mddev->recovery_wait); |
| 6062 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6063 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | md_wakeup_thread(mddev->thread); |
| 6065 | // stop recovery, signal do_sync .... |
| 6066 | } |
| 6067 | } |
| 6068 | |
| 6069 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6070 | /* md_write_start(mddev, bi) |
| 6071 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6072 | * in superblock) before writing, schedule a superblock update |
| 6073 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6074 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6075 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6077 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6078 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6079 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6080 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6081 | BUG_ON(mddev->ro == 1); |
| 6082 | if (mddev->ro == 2) { |
| 6083 | /* need to switch to read/write */ |
| 6084 | mddev->ro = 0; |
| 6085 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6086 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6087 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6088 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6089 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6090 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6091 | if (mddev->safemode == 1) |
| 6092 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6093 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6094 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6095 | if (mddev->in_sync) { |
| 6096 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6097 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6098 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6099 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6100 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6101 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6102 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6103 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6104 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6105 | wait_event(mddev->sb_wait, |
| 6106 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6107 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | } |
| 6109 | |
| 6110 | void md_write_end(mddev_t *mddev) |
| 6111 | { |
| 6112 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6113 | if (mddev->safemode == 2) |
| 6114 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6115 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6117 | } |
| 6118 | } |
| 6119 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6120 | /* md_allow_write(mddev) |
| 6121 | * Calling this ensures that the array is marked 'active' so that writes |
| 6122 | * may proceed without blocking. It is important to call this before |
| 6123 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6124 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6125 | * |
| 6126 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6127 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6128 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6129 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6130 | { |
| 6131 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6132 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6133 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6134 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6135 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6136 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6137 | |
| 6138 | spin_lock_irq(&mddev->write_lock); |
| 6139 | if (mddev->in_sync) { |
| 6140 | mddev->in_sync = 0; |
| 6141 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6142 | if (mddev->safemode_delay && |
| 6143 | mddev->safemode == 0) |
| 6144 | mddev->safemode = 1; |
| 6145 | spin_unlock_irq(&mddev->write_lock); |
| 6146 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6147 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6148 | } else |
| 6149 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6150 | |
| 6151 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6152 | return -EAGAIN; |
| 6153 | else |
| 6154 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6155 | } |
| 6156 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | #define SYNC_MARKS 10 |
| 6159 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6160 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | { |
| 6162 | mddev_t *mddev2; |
| 6163 | unsigned int currspeed = 0, |
| 6164 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6165 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | unsigned long mark[SYNC_MARKS]; |
| 6167 | sector_t mark_cnt[SYNC_MARKS]; |
| 6168 | int last_mark,m; |
| 6169 | struct list_head *tmp; |
| 6170 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6171 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6172 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6173 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | |
| 6175 | /* just incase thread restarts... */ |
| 6176 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6177 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6178 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6179 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6181 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6182 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6183 | desc = "data-check"; |
| 6184 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6185 | desc = "requested-resync"; |
| 6186 | else |
| 6187 | desc = "resync"; |
| 6188 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6189 | desc = "reshape"; |
| 6190 | else |
| 6191 | desc = "recovery"; |
| 6192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | /* we overload curr_resync somewhat here. |
| 6194 | * 0 == not engaged in resync at all |
| 6195 | * 2 == checking that there is no conflict with another sync |
| 6196 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6197 | * commense |
| 6198 | * other == active in resync - this many blocks |
| 6199 | * |
| 6200 | * Before starting a resync we must have set curr_resync to |
| 6201 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6202 | * less than ours. When we find one that is the same or higher |
| 6203 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6204 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6205 | * This will mean we have to start checking from the beginning again. |
| 6206 | * |
| 6207 | */ |
| 6208 | |
| 6209 | do { |
| 6210 | mddev->curr_resync = 2; |
| 6211 | |
| 6212 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6213 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6214 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6215 | goto skip; |
| 6216 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6217 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6218 | if (mddev2 == mddev) |
| 6219 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6220 | if (!mddev->parallel_resync |
| 6221 | && mddev2->curr_resync |
| 6222 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | DEFINE_WAIT(wq); |
| 6224 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6225 | /* arbitrarily yield */ |
| 6226 | mddev->curr_resync = 1; |
| 6227 | wake_up(&resync_wait); |
| 6228 | } |
| 6229 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6230 | /* no need to wait here, we can wait the next |
| 6231 | * time 'round when curr_resync == 2 |
| 6232 | */ |
| 6233 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6234 | /* We need to wait 'interruptible' so as not to |
| 6235 | * contribute to the load average, and not to |
| 6236 | * be caught by 'softlockup' |
| 6237 | */ |
| 6238 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6239 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6240 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6241 | printk(KERN_INFO "md: delaying %s of %s" |
| 6242 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6244 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6246 | if (signal_pending(current)) |
| 6247 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | schedule(); |
| 6249 | finish_wait(&resync_wait, &wq); |
| 6250 | goto try_again; |
| 6251 | } |
| 6252 | finish_wait(&resync_wait, &wq); |
| 6253 | } |
| 6254 | } |
| 6255 | } while (mddev->curr_resync < 2); |
| 6256 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6257 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6258 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6260 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6261 | */ |
| 6262 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6263 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6264 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6265 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6266 | j = mddev->resync_min; |
| 6267 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6268 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6269 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6270 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6271 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6272 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6274 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6275 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6276 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6277 | if (rdev->raid_disk >= 0 && |
| 6278 | !test_bit(Faulty, &rdev->flags) && |
| 6279 | !test_bit(In_sync, &rdev->flags) && |
| 6280 | rdev->recovery_offset < j) |
| 6281 | j = rdev->recovery_offset; |
| 6282 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6283 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6284 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6285 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6286 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6287 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6288 | "(but not more than %d KB/sec) for %s.\n", |
| 6289 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6291 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6292 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6293 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6295 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6296 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | } |
| 6298 | last_mark = 0; |
| 6299 | mddev->resync_mark = mark[last_mark]; |
| 6300 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6301 | |
| 6302 | /* |
| 6303 | * Tune reconstruction: |
| 6304 | */ |
| 6305 | window = 32*(PAGE_SIZE/512); |
| 6306 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6307 | window/2,(unsigned long long) max_sectors/2); |
| 6308 | |
| 6309 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | last_check = 0; |
| 6311 | |
| 6312 | if (j>2) { |
| 6313 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6314 | "md: resuming %s of %s from checkpoint.\n", |
| 6315 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | mddev->curr_resync = j; |
| 6317 | } |
| 6318 | |
| 6319 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6320 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6322 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6323 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6324 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6325 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6326 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6327 | > (max_sectors >> 4)) || |
| 6328 | (j - mddev->curr_resync_completed)*2 |
| 6329 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6330 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6331 | /* time to update curr_resync_completed */ |
| 6332 | blk_unplug(mddev->queue); |
| 6333 | wait_event(mddev->recovery_wait, |
| 6334 | atomic_read(&mddev->recovery_active) == 0); |
| 6335 | mddev->curr_resync_completed = |
| 6336 | mddev->curr_resync; |
| 6337 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6338 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6339 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6340 | |
| 6341 | if (j >= mddev->resync_max) |
| 6342 | wait_event(mddev->recovery_wait, |
| 6343 | mddev->resync_max > j |
| 6344 | || kthread_should_stop()); |
| 6345 | |
| 6346 | if (kthread_should_stop()) |
| 6347 | goto interrupted; |
| 6348 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6349 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6350 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6351 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6352 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | goto out; |
| 6354 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6355 | |
| 6356 | if (!skipped) { /* actual IO requested */ |
| 6357 | io_sectors += sectors; |
| 6358 | atomic_add(sectors, &mddev->recovery_active); |
| 6359 | } |
| 6360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | j += sectors; |
| 6362 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6363 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6364 | if (last_check == 0) |
| 6365 | /* this is the earliers that rebuilt will be |
| 6366 | * visible in /proc/mdstat |
| 6367 | */ |
| 6368 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6369 | |
| 6370 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | continue; |
| 6372 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6373 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6374 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6375 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | break; |
| 6377 | |
| 6378 | repeat: |
| 6379 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6380 | /* step marks */ |
| 6381 | int next = (last_mark+1) % SYNC_MARKS; |
| 6382 | |
| 6383 | mddev->resync_mark = mark[next]; |
| 6384 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6385 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6386 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6387 | last_mark = next; |
| 6388 | } |
| 6389 | |
| 6390 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6391 | if (kthread_should_stop()) |
| 6392 | goto interrupted; |
| 6393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | |
| 6395 | /* |
| 6396 | * this loop exits only if either when we are slower than |
| 6397 | * the 'hard' speed limit, or the system was IO-idle for |
| 6398 | * a jiffy. |
| 6399 | * the system might be non-idle CPU-wise, but we only care |
| 6400 | * about not overloading the IO subsystem. (things like an |
| 6401 | * e2fsck being done on the RAID array should execute fast) |
| 6402 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6403 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | cond_resched(); |
| 6405 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6406 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6407 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6409 | if (currspeed > speed_min(mddev)) { |
| 6410 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6411 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6412 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6413 | goto repeat; |
| 6414 | } |
| 6415 | } |
| 6416 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6417 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | /* |
| 6419 | * this also signals 'finished resyncing' to md_stop |
| 6420 | */ |
| 6421 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6422 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | |
| 6424 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6425 | |
| 6426 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6427 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6429 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6430 | mddev->curr_resync > 2) { |
| 6431 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6432 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6433 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6434 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6435 | "md: checkpointing %s of %s.\n", |
| 6436 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6437 | mddev->recovery_cp = mddev->curr_resync; |
| 6438 | } |
| 6439 | } else |
| 6440 | mddev->recovery_cp = MaxSector; |
| 6441 | } else { |
| 6442 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6443 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6444 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6445 | if (rdev->raid_disk >= 0 && |
| 6446 | !test_bit(Faulty, &rdev->flags) && |
| 6447 | !test_bit(In_sync, &rdev->flags) && |
| 6448 | rdev->recovery_offset < mddev->curr_resync) |
| 6449 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6450 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6452 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | skip: |
| 6455 | mddev->curr_resync = 0; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6456 | mddev->curr_resync_completed = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6457 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6458 | mddev->resync_max = MaxSector; |
| 6459 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | wake_up(&resync_wait); |
| 6461 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6462 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6463 | return; |
| 6464 | |
| 6465 | interrupted: |
| 6466 | /* |
| 6467 | * got a signal, exit. |
| 6468 | */ |
| 6469 | printk(KERN_INFO |
| 6470 | "md: md_do_sync() got signal ... exiting\n"); |
| 6471 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6472 | goto out; |
| 6473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6474 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6475 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6476 | |
| 6477 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6478 | static int remove_and_add_spares(mddev_t *mddev) |
| 6479 | { |
| 6480 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6481 | int spares = 0; |
| 6482 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6483 | mddev->curr_resync_completed = 0; |
| 6484 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6485 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6486 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6487 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6488 | (test_bit(Faulty, &rdev->flags) || |
| 6489 | ! test_bit(In_sync, &rdev->flags)) && |
| 6490 | atomic_read(&rdev->nr_pending)==0) { |
| 6491 | if (mddev->pers->hot_remove_disk( |
| 6492 | mddev, rdev->raid_disk)==0) { |
| 6493 | char nm[20]; |
| 6494 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6495 | sysfs_remove_link(&mddev->kobj, nm); |
| 6496 | rdev->raid_disk = -1; |
| 6497 | } |
| 6498 | } |
| 6499 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6500 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6501 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6502 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6503 | !test_bit(In_sync, &rdev->flags) && |
| 6504 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6505 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6506 | if (rdev->raid_disk < 0 |
| 6507 | && !test_bit(Faulty, &rdev->flags)) { |
| 6508 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6509 | if (mddev->pers-> |
| 6510 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6511 | char nm[20]; |
| 6512 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6513 | if (sysfs_create_link(&mddev->kobj, |
| 6514 | &rdev->kobj, nm)) |
| 6515 | printk(KERN_WARNING |
| 6516 | "md: cannot register " |
| 6517 | "%s for %s\n", |
| 6518 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6519 | spares++; |
| 6520 | md_new_event(mddev); |
| 6521 | } else |
| 6522 | break; |
| 6523 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6524 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6525 | } |
| 6526 | return spares; |
| 6527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | /* |
| 6529 | * This routine is regularly called by all per-raid-array threads to |
| 6530 | * deal with generic issues like resync and super-block update. |
| 6531 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6532 | * need this as they never do any recovery or update the superblock. |
| 6533 | * |
| 6534 | * It does not do any resync itself, but rather "forks" off other threads |
| 6535 | * to do that as needed. |
| 6536 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6537 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6538 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | * and wakeups up this thread which will reap the thread and finish up. |
| 6540 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6541 | * |
| 6542 | * The overall approach is: |
| 6543 | * 1/ if the superblock needs updating, update it. |
| 6544 | * 2/ If a recovery thread is running, don't do anything else. |
| 6545 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6546 | * 4/ If there are any faulty devices, remove them. |
| 6547 | * 5/ If array is degraded, try to add spares devices |
| 6548 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6549 | */ |
| 6550 | void md_check_recovery(mddev_t *mddev) |
| 6551 | { |
| 6552 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | |
| 6554 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6555 | if (mddev->bitmap) |
| 6556 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6557 | |
| 6558 | if (mddev->ro) |
| 6559 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6560 | |
| 6561 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6562 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6563 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6564 | mdname(mddev)); |
| 6565 | mddev->safemode = 2; |
| 6566 | } |
| 6567 | flush_signals(current); |
| 6568 | } |
| 6569 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6570 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6571 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6572 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6573 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6575 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6576 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6577 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6578 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | )) |
| 6580 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6581 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6582 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6583 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6584 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6585 | if (mddev->ro) { |
| 6586 | /* Only thing we do on a ro array is remove |
| 6587 | * failed devices. |
| 6588 | */ |
| 6589 | remove_and_add_spares(mddev); |
| 6590 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6591 | goto unlock; |
| 6592 | } |
| 6593 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6594 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6595 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6596 | spin_lock_irq(&mddev->write_lock); |
| 6597 | if (mddev->safemode && |
| 6598 | !atomic_read(&mddev->writes_pending) && |
| 6599 | !mddev->in_sync && |
| 6600 | mddev->recovery_cp == MaxSector) { |
| 6601 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6602 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6603 | if (mddev->persistent) |
| 6604 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6605 | } |
| 6606 | if (mddev->safemode == 1) |
| 6607 | mddev->safemode = 0; |
| 6608 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6609 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6610 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6611 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6612 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6613 | if (mddev->flags) |
| 6614 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6615 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6616 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6617 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6618 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6619 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6621 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6622 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6623 | /* resync/recovery still happening */ |
| 6624 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6625 | goto unlock; |
| 6626 | } |
| 6627 | if (mddev->sync_thread) { |
| 6628 | /* resync has finished, collect result */ |
| 6629 | md_unregister_thread(mddev->sync_thread); |
| 6630 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6631 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6632 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | /* success...*/ |
| 6634 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6635 | if (mddev->pers->spare_active(mddev)) |
| 6636 | sysfs_notify(&mddev->kobj, NULL, |
| 6637 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6638 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6639 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6640 | mddev->pers->finish_reshape) |
| 6641 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6642 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6643 | |
| 6644 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6645 | * information must be scrapped |
| 6646 | */ |
| 6647 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6648 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6649 | rdev->saved_raid_disk = -1; |
| 6650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | mddev->recovery = 0; |
| 6652 | /* flag recovery needed just to double check */ |
| 6653 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6654 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6655 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | goto unlock; |
| 6657 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6658 | /* Set RUNNING before clearing NEEDED to avoid |
| 6659 | * any transients in the value of "sync_action". |
| 6660 | */ |
| 6661 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6662 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6663 | /* Clear some bits that don't mean anything, but |
| 6664 | * might be left set |
| 6665 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6666 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6667 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6669 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6670 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6671 | /* no recovery is running. |
| 6672 | * remove any failed drives, then |
| 6673 | * add spares if possible. |
| 6674 | * Spare are also removed and re-added, to allow |
| 6675 | * the personality to fail the re-add. |
| 6676 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6678 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6679 | if (mddev->pers->check_reshape == NULL || |
| 6680 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6681 | /* Cannot proceed */ |
| 6682 | goto unlock; |
| 6683 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6684 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6685 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6686 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6687 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6688 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6689 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6690 | } else if (mddev->recovery_cp < MaxSector) { |
| 6691 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6692 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6693 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6694 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6695 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6698 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6699 | /* We are adding a device or devices to an array |
| 6700 | * which has the bitmap stored on all devices. |
| 6701 | * So make sure all bitmap pages get written |
| 6702 | */ |
| 6703 | bitmap_write_all(mddev->bitmap); |
| 6704 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6706 | mddev, |
| 6707 | "%s_resync"); |
| 6708 | if (!mddev->sync_thread) { |
| 6709 | printk(KERN_ERR "%s: could not start resync" |
| 6710 | " thread...\n", |
| 6711 | mdname(mddev)); |
| 6712 | /* leave the spares where they are, it shouldn't hurt */ |
| 6713 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6714 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6715 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6716 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6717 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | } |
| 6719 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6720 | if (!mddev->sync_thread) { |
| 6721 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6722 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6723 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6724 | if (mddev->sysfs_action) |
| 6725 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | mddev_unlock(mddev); |
| 6728 | } |
| 6729 | } |
| 6730 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6731 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6732 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6733 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6734 | wait_event_timeout(rdev->blocked_wait, |
| 6735 | !test_bit(Blocked, &rdev->flags), |
| 6736 | msecs_to_jiffies(5000)); |
| 6737 | rdev_dec_pending(rdev, mddev); |
| 6738 | } |
| 6739 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6740 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6741 | static int md_notify_reboot(struct notifier_block *this, |
| 6742 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | { |
| 6744 | struct list_head *tmp; |
| 6745 | mddev_t *mddev; |
| 6746 | |
| 6747 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6748 | |
| 6749 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6750 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6751 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6752 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6753 | /* Force a switch to readonly even array |
| 6754 | * appears to still be in use. Hence |
| 6755 | * the '100'. |
| 6756 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6757 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6758 | mddev_unlock(mddev); |
| 6759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | /* |
| 6761 | * certain more exotic SCSI devices are known to be |
| 6762 | * volatile wrt too early system reboots. While the |
| 6763 | * right place to handle this issue is the given |
| 6764 | * driver, we do want to have a safe RAID driver ... |
| 6765 | */ |
| 6766 | mdelay(1000*1); |
| 6767 | } |
| 6768 | return NOTIFY_DONE; |
| 6769 | } |
| 6770 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6771 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | .notifier_call = md_notify_reboot, |
| 6773 | .next = NULL, |
| 6774 | .priority = INT_MAX, /* before any real devices */ |
| 6775 | }; |
| 6776 | |
| 6777 | static void md_geninit(void) |
| 6778 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6779 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6780 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6781 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6782 | } |
| 6783 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6784 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6786 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | return -1; |
| 6788 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6789 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6790 | return -1; |
| 6791 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6792 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6793 | md_probe, NULL, NULL); |
| 6794 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | md_probe, NULL, NULL); |
| 6796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6797 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6798 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | |
| 6800 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6801 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | |
| 6805 | #ifndef MODULE |
| 6806 | |
| 6807 | /* |
| 6808 | * Searches all registered partitions for autorun RAID arrays |
| 6809 | * at boot time. |
| 6810 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6811 | |
| 6812 | static LIST_HEAD(all_detected_devices); |
| 6813 | struct detected_devices_node { |
| 6814 | struct list_head list; |
| 6815 | dev_t dev; |
| 6816 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | |
| 6818 | void md_autodetect_dev(dev_t dev) |
| 6819 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6820 | struct detected_devices_node *node_detected_dev; |
| 6821 | |
| 6822 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6823 | if (node_detected_dev) { |
| 6824 | node_detected_dev->dev = dev; |
| 6825 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6826 | } else { |
| 6827 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6828 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6830 | } |
| 6831 | |
| 6832 | |
| 6833 | static void autostart_arrays(int part) |
| 6834 | { |
| 6835 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6836 | struct detected_devices_node *node_detected_dev; |
| 6837 | dev_t dev; |
| 6838 | int i_scanned, i_passed; |
| 6839 | |
| 6840 | i_scanned = 0; |
| 6841 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | |
| 6843 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6844 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6845 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6846 | i_scanned++; |
| 6847 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6848 | struct detected_devices_node, list); |
| 6849 | list_del(&node_detected_dev->list); |
| 6850 | dev = node_detected_dev->dev; |
| 6851 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6852 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | if (IS_ERR(rdev)) |
| 6854 | continue; |
| 6855 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6856 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | MD_BUG(); |
| 6858 | continue; |
| 6859 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6860 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6862 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6864 | |
| 6865 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6866 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6867 | |
| 6868 | autorun_devices(part); |
| 6869 | } |
| 6870 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6871 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6872 | |
| 6873 | static __exit void md_exit(void) |
| 6874 | { |
| 6875 | mddev_t *mddev; |
| 6876 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6877 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6878 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6879 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6881 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | unregister_blkdev(mdp_major, "mdp"); |
| 6883 | unregister_reboot_notifier(&md_notifier); |
| 6884 | unregister_sysctl_table(raid_table_header); |
| 6885 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6886 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6887 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6888 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | } |
| 6890 | } |
| 6891 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6892 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | module_exit(md_exit) |
| 6894 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6895 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6896 | { |
| 6897 | return sprintf(buffer, "%d", start_readonly); |
| 6898 | } |
| 6899 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6900 | { |
| 6901 | char *e; |
| 6902 | int num = simple_strtoul(val, &e, 10); |
| 6903 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6904 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6905 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6906 | } |
| 6907 | return -EINVAL; |
| 6908 | } |
| 6909 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6910 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6911 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6912 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6913 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6915 | EXPORT_SYMBOL(register_md_personality); |
| 6916 | EXPORT_SYMBOL(unregister_md_personality); |
| 6917 | EXPORT_SYMBOL(md_error); |
| 6918 | EXPORT_SYMBOL(md_done_sync); |
| 6919 | EXPORT_SYMBOL(md_write_start); |
| 6920 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | EXPORT_SYMBOL(md_register_thread); |
| 6922 | EXPORT_SYMBOL(md_unregister_thread); |
| 6923 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | EXPORT_SYMBOL(md_check_recovery); |
| 6925 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6926 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6927 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |