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 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 141 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 265 | int mddev_congested(mddev_t *mddev, int bits) |
| 266 | { |
| 267 | return mddev->suspended; |
| 268 | } |
| 269 | EXPORT_SYMBOL(mddev_congested); |
| 270 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 273 | { |
| 274 | atomic_inc(&mddev->active); |
| 275 | return mddev; |
| 276 | } |
| 277 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 278 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | static void mddev_put(mddev_t *mddev) |
| 281 | { |
| 282 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 283 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 284 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
| 285 | !mddev->hold_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 287 | if (mddev->gendisk) { |
| 288 | /* we did a probe so need to clean up. |
| 289 | * Call schedule_work inside the spinlock |
| 290 | * so that flush_scheduled_work() after |
| 291 | * mddev_find will succeed in waiting for the |
| 292 | * work to be done. |
| 293 | */ |
| 294 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 295 | schedule_work(&mddev->del_work); |
| 296 | } else |
| 297 | kfree(mddev); |
| 298 | } |
| 299 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static mddev_t * mddev_find(dev_t unit) |
| 303 | { |
| 304 | mddev_t *mddev, *new = NULL; |
| 305 | |
| 306 | retry: |
| 307 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 309 | if (unit) { |
| 310 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 311 | if (mddev->unit == unit) { |
| 312 | mddev_get(mddev); |
| 313 | spin_unlock(&all_mddevs_lock); |
| 314 | kfree(new); |
| 315 | return mddev; |
| 316 | } |
| 317 | |
| 318 | if (new) { |
| 319 | list_add(&new->all_mddevs, &all_mddevs); |
| 320 | spin_unlock(&all_mddevs_lock); |
| 321 | new->hold_active = UNTIL_IOCTL; |
| 322 | return new; |
| 323 | } |
| 324 | } else if (new) { |
| 325 | /* find an unused unit number */ |
| 326 | static int next_minor = 512; |
| 327 | int start = next_minor; |
| 328 | int is_free = 0; |
| 329 | int dev = 0; |
| 330 | while (!is_free) { |
| 331 | dev = MKDEV(MD_MAJOR, next_minor); |
| 332 | next_minor++; |
| 333 | if (next_minor > MINORMASK) |
| 334 | next_minor = 0; |
| 335 | if (next_minor == start) { |
| 336 | /* Oh dear, all in use. */ |
| 337 | spin_unlock(&all_mddevs_lock); |
| 338 | kfree(new); |
| 339 | return NULL; |
| 340 | } |
| 341 | |
| 342 | is_free = 1; |
| 343 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 344 | if (mddev->unit == dev) { |
| 345 | is_free = 0; |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | new->unit = dev; |
| 350 | new->md_minor = MINOR(dev); |
| 351 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | list_add(&new->all_mddevs, &all_mddevs); |
| 353 | spin_unlock(&all_mddevs_lock); |
| 354 | return new; |
| 355 | } |
| 356 | spin_unlock(&all_mddevs_lock); |
| 357 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 358 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (!new) |
| 360 | return NULL; |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | new->unit = unit; |
| 363 | if (MAJOR(unit) == MD_MAJOR) |
| 364 | new->md_minor = MINOR(unit); |
| 365 | else |
| 366 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 367 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 368 | mutex_init(&new->open_mutex); |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 369 | mutex_init(&new->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | INIT_LIST_HEAD(&new->disks); |
| 371 | INIT_LIST_HEAD(&new->all_mddevs); |
| 372 | init_timer(&new->safemode_timer); |
| 373 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 374 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 375 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 376 | spin_lock_init(&new->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 377 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 378 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 379 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 380 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 381 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 382 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | goto retry; |
| 385 | } |
| 386 | |
| 387 | static inline int mddev_lock(mddev_t * mddev) |
| 388 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 389 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 392 | static inline int mddev_is_locked(mddev_t *mddev) |
| 393 | { |
| 394 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 395 | } |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | static inline int mddev_trylock(mddev_t * mddev) |
| 398 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 399 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static inline void mddev_unlock(mddev_t * mddev) |
| 403 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 404 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 406 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 409 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 411 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 413 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (rdev->desc_nr == nr) |
| 415 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 421 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | mdk_rdev_t *rdev; |
| 423 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 424 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (rdev->bdev->bd_dev == dev) |
| 426 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return NULL; |
| 429 | } |
| 430 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 431 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 432 | { |
| 433 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 434 | list_for_each_entry(pers, &pers_list, list) { |
| 435 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 436 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 437 | if (strcmp(pers->name, clevel)==0) |
| 438 | return pers; |
| 439 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 440 | return NULL; |
| 441 | } |
| 442 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 443 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 444 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 446 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 447 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 451 | { |
| 452 | if (rdev->sb_page) |
| 453 | MD_BUG(); |
| 454 | |
| 455 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 456 | if (!rdev->sb_page) { |
| 457 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 458 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 465 | { |
| 466 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 467 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | rdev->sb_loaded = 0; |
| 469 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 470 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 471 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
| 475 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 476 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 477 | { |
| 478 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 479 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 480 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 481 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 482 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 483 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 484 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 485 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 486 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 487 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 488 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 489 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 490 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 491 | } |
| 492 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 493 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 494 | { |
| 495 | struct bio *bio2 = bio->bi_private; |
| 496 | mdk_rdev_t *rdev = bio2->bi_private; |
| 497 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 498 | |
| 499 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 500 | error == -EOPNOTSUPP) { |
| 501 | unsigned long flags; |
| 502 | /* barriers don't appear to be supported :-( */ |
| 503 | set_bit(BarriersNotsupp, &rdev->flags); |
| 504 | mddev->barriers_work = 0; |
| 505 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 506 | bio2->bi_next = mddev->biolist; |
| 507 | mddev->biolist = bio2; |
| 508 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 509 | wake_up(&mddev->sb_wait); |
| 510 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 511 | } else { |
| 512 | bio_put(bio2); |
| 513 | bio->bi_private = rdev; |
| 514 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 515 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 516 | } |
| 517 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 518 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 519 | sector_t sector, int size, struct page *page) |
| 520 | { |
| 521 | /* write first size bytes of page to sector of rdev |
| 522 | * Increment mddev->pending_writes before returning |
| 523 | * and decrement it on completion, waking up sb_wait |
| 524 | * if zero is reached. |
| 525 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 526 | * |
| 527 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 528 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 529 | */ |
| 530 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 531 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 532 | |
| 533 | bio->bi_bdev = rdev->bdev; |
| 534 | bio->bi_sector = sector; |
| 535 | bio_add_page(bio, page, size, 0); |
| 536 | bio->bi_private = rdev; |
| 537 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 538 | bio->bi_rw = rw; |
| 539 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 540 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 541 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 542 | struct bio *rbio; |
| 543 | rw |= (1<<BIO_RW_BARRIER); |
| 544 | rbio = bio_clone(bio, GFP_NOIO); |
| 545 | rbio->bi_private = bio; |
| 546 | rbio->bi_end_io = super_written_barrier; |
| 547 | submit_bio(rw, rbio); |
| 548 | } else |
| 549 | submit_bio(rw, bio); |
| 550 | } |
| 551 | |
| 552 | void md_super_wait(mddev_t *mddev) |
| 553 | { |
| 554 | /* wait for all superblock writes that were scheduled to complete. |
| 555 | * if any had to be retried (due to BARRIER problems), retry them |
| 556 | */ |
| 557 | DEFINE_WAIT(wq); |
| 558 | for(;;) { |
| 559 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 560 | if (atomic_read(&mddev->pending_writes)==0) |
| 561 | break; |
| 562 | while (mddev->biolist) { |
| 563 | struct bio *bio; |
| 564 | spin_lock_irq(&mddev->write_lock); |
| 565 | bio = mddev->biolist; |
| 566 | mddev->biolist = bio->bi_next ; |
| 567 | bio->bi_next = NULL; |
| 568 | spin_unlock_irq(&mddev->write_lock); |
| 569 | submit_bio(bio->bi_rw, bio); |
| 570 | } |
| 571 | schedule(); |
| 572 | } |
| 573 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 574 | } |
| 575 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 576 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 581 | 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] | 582 | struct page *page, int rw) |
| 583 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 584 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | struct completion event; |
| 586 | int ret; |
| 587 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 588 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | bio->bi_bdev = bdev; |
| 591 | bio->bi_sector = sector; |
| 592 | bio_add_page(bio, page, size, 0); |
| 593 | init_completion(&event); |
| 594 | bio->bi_private = &event; |
| 595 | bio->bi_end_io = bi_complete; |
| 596 | submit_bio(rw, bio); |
| 597 | wait_for_completion(&event); |
| 598 | |
| 599 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 600 | bio_put(bio); |
| 601 | return ret; |
| 602 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 603 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 605 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | char b[BDEVNAME_SIZE]; |
| 608 | if (!rdev->sb_page) { |
| 609 | MD_BUG(); |
| 610 | return -EINVAL; |
| 611 | } |
| 612 | if (rdev->sb_loaded) |
| 613 | return 0; |
| 614 | |
| 615 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 616 | 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] | 617 | goto fail; |
| 618 | rdev->sb_loaded = 1; |
| 619 | return 0; |
| 620 | |
| 621 | fail: |
| 622 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 623 | bdevname(rdev->bdev,b)); |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | |
| 627 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 628 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 629 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 630 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 631 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 632 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 636 | { |
| 637 | int ret; |
| 638 | mdp_super_t *tmp1, *tmp2; |
| 639 | |
| 640 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 641 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 642 | |
| 643 | if (!tmp1 || !tmp2) { |
| 644 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 645 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | goto abort; |
| 647 | } |
| 648 | |
| 649 | *tmp1 = *sb1; |
| 650 | *tmp2 = *sb2; |
| 651 | |
| 652 | /* |
| 653 | * nr_disks is not constant |
| 654 | */ |
| 655 | tmp1->nr_disks = 0; |
| 656 | tmp2->nr_disks = 0; |
| 657 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 658 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 660 | kfree(tmp1); |
| 661 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return ret; |
| 663 | } |
| 664 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 665 | |
| 666 | static u32 md_csum_fold(u32 csum) |
| 667 | { |
| 668 | csum = (csum & 0xffff) + (csum >> 16); |
| 669 | return (csum & 0xffff) + (csum >> 16); |
| 670 | } |
| 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 673 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 674 | u64 newcsum = 0; |
| 675 | u32 *sb32 = (u32*)sb; |
| 676 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | unsigned int disk_csum, csum; |
| 678 | |
| 679 | disk_csum = sb->sb_csum; |
| 680 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 681 | |
| 682 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 683 | newcsum += sb32[i]; |
| 684 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 685 | |
| 686 | |
| 687 | #ifdef CONFIG_ALPHA |
| 688 | /* This used to use csum_partial, which was wrong for several |
| 689 | * reasons including that different results are returned on |
| 690 | * different architectures. It isn't critical that we get exactly |
| 691 | * the same return value as before (we always csum_fold before |
| 692 | * testing, and that removes any differences). However as we |
| 693 | * know that csum_partial always returned a 16bit value on |
| 694 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 695 | */ |
| 696 | sb->sb_csum = md_csum_fold(disk_csum); |
| 697 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 699 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | return csum; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | /* |
| 705 | * Handle superblock details. |
| 706 | * We want to be able to handle multiple superblock formats |
| 707 | * so we have a common interface to them all, and an array of |
| 708 | * different handlers. |
| 709 | * We rely on user-space to write the initial superblock, and support |
| 710 | * reading and updating of superblocks. |
| 711 | * Interface methods are: |
| 712 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 713 | * loads and validates a superblock on dev. |
| 714 | * if refdev != NULL, compare superblocks on both devices |
| 715 | * Return: |
| 716 | * 0 - dev has a superblock that is compatible with refdev |
| 717 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 718 | * so dev should be used as the refdev in future |
| 719 | * -EINVAL superblock incompatible or invalid |
| 720 | * -othererror e.g. -EIO |
| 721 | * |
| 722 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 723 | * Verify that dev is acceptable into mddev. |
| 724 | * The first time, mddev->raid_disks will be 0, and data from |
| 725 | * dev should be merged in. Subsequent calls check that dev |
| 726 | * is new enough. Return 0 or -EINVAL |
| 727 | * |
| 728 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 729 | * Update the superblock for rdev with data in mddev |
| 730 | * This does not write to disc. |
| 731 | * |
| 732 | */ |
| 733 | |
| 734 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 735 | char *name; |
| 736 | struct module *owner; |
| 737 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 738 | int minor_version); |
| 739 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 740 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 741 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 742 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | }; |
| 744 | |
| 745 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 746 | * Check that the given mddev has no bitmap. |
| 747 | * |
| 748 | * This function is called from the run method of all personalities that do not |
| 749 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 750 | * has a bitmap. Otherwise, it returns 0. |
| 751 | * |
| 752 | */ |
| 753 | int md_check_no_bitmap(mddev_t *mddev) |
| 754 | { |
| 755 | if (!mddev->bitmap_file && !mddev->bitmap_offset) |
| 756 | return 0; |
| 757 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 758 | mdname(mddev), mddev->pers->name); |
| 759 | return 1; |
| 760 | } |
| 761 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 762 | |
| 763 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | * load_super for 0.90.0 |
| 765 | */ |
| 766 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 767 | { |
| 768 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 769 | mdp_super_t *sb; |
| 770 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
| 772 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 773 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | * it's at the end of the disk. |
| 775 | * |
| 776 | * It also happens to be a multiple of 4Kb. |
| 777 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 778 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 780 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (ret) return ret; |
| 782 | |
| 783 | ret = -EINVAL; |
| 784 | |
| 785 | bdevname(rdev->bdev, b); |
| 786 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 787 | |
| 788 | if (sb->md_magic != MD_SB_MAGIC) { |
| 789 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 790 | b); |
| 791 | goto abort; |
| 792 | } |
| 793 | |
| 794 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 795 | sb->minor_version < 90 || |
| 796 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 798 | sb->major_version, sb->minor_version, |
| 799 | b); |
| 800 | goto abort; |
| 801 | } |
| 802 | |
| 803 | if (sb->raid_disks <= 0) |
| 804 | goto abort; |
| 805 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 806 | 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] | 807 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 808 | b); |
| 809 | goto abort; |
| 810 | } |
| 811 | |
| 812 | rdev->preferred_minor = sb->md_minor; |
| 813 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 814 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
| 816 | if (sb->level == LEVEL_MULTIPATH) |
| 817 | rdev->desc_nr = -1; |
| 818 | else |
| 819 | rdev->desc_nr = sb->this_disk.number; |
| 820 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 821 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 823 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | __u64 ev1, ev2; |
| 825 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 826 | if (!uuid_equal(refsb, sb)) { |
| 827 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 828 | b, bdevname(refdev->bdev,b2)); |
| 829 | goto abort; |
| 830 | } |
| 831 | if (!sb_equal(refsb, sb)) { |
| 832 | printk(KERN_WARNING "md: %s has same UUID" |
| 833 | " but different superblock to %s\n", |
| 834 | b, bdevname(refdev->bdev, b2)); |
| 835 | goto abort; |
| 836 | } |
| 837 | ev1 = md_event(sb); |
| 838 | ev2 = md_event(refsb); |
| 839 | if (ev1 > ev2) |
| 840 | ret = 1; |
| 841 | else |
| 842 | ret = 0; |
| 843 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 844 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 846 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 847 | /* "this cannot possibly happen" ... */ |
| 848 | ret = -EINVAL; |
| 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | abort: |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * validate_super for 0.90.0 |
| 856 | */ |
| 857 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 858 | { |
| 859 | mdp_disk_t *desc; |
| 860 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 861 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 863 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 864 | clear_bit(Faulty, &rdev->flags); |
| 865 | clear_bit(In_sync, &rdev->flags); |
| 866 | clear_bit(WriteMostly, &rdev->flags); |
| 867 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | if (mddev->raid_disks == 0) { |
| 870 | mddev->major_version = 0; |
| 871 | mddev->minor_version = sb->minor_version; |
| 872 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 873 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 874 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | mddev->ctime = sb->ctime; |
| 876 | mddev->utime = sb->utime; |
| 877 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 878 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | mddev->layout = sb->layout; |
| 880 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 881 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 882 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 883 | mddev->bitmap_offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 884 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 886 | if (mddev->minor_version >= 91) { |
| 887 | mddev->reshape_position = sb->reshape_position; |
| 888 | mddev->delta_disks = sb->delta_disks; |
| 889 | mddev->new_level = sb->new_level; |
| 890 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 891 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 892 | } else { |
| 893 | mddev->reshape_position = MaxSector; |
| 894 | mddev->delta_disks = 0; |
| 895 | mddev->new_level = mddev->level; |
| 896 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 897 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 901 | mddev->recovery_cp = MaxSector; |
| 902 | else { |
| 903 | if (sb->events_hi == sb->cp_events_hi && |
| 904 | sb->events_lo == sb->cp_events_lo) { |
| 905 | mddev->recovery_cp = sb->recovery_cp; |
| 906 | } else |
| 907 | mddev->recovery_cp = 0; |
| 908 | } |
| 909 | |
| 910 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 911 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 912 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 913 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 914 | |
| 915 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 916 | |
| 917 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 918 | mddev->bitmap_file == NULL) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 919 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 920 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 921 | } else if (mddev->pers == NULL) { |
| 922 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | ++ev1; |
| 924 | if (ev1 < mddev->events) |
| 925 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 926 | } else if (mddev->bitmap) { |
| 927 | /* if adding to array with a bitmap, then we can accept an |
| 928 | * older device ... but not too old. |
| 929 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 930 | if (ev1 < mddev->bitmap->events_cleared) |
| 931 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 932 | } else { |
| 933 | if (ev1 < mddev->events) |
| 934 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 935 | return 0; |
| 936 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | desc = sb->disks + rdev->desc_nr; |
| 940 | |
| 941 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 942 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 943 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 944 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 945 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame^] | 947 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 948 | /* active but not in sync implies recovery up to |
| 949 | * reshape position. We don't know exactly where |
| 950 | * that is, so set to zero for now */ |
| 951 | if (mddev->minor_version >= 91) { |
| 952 | rdev->recovery_offset = 0; |
| 953 | rdev->raid_disk = desc->raid_disk; |
| 954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 956 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 957 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 958 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 959 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * sync_super for 0.90.0 |
| 965 | */ |
| 966 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 967 | { |
| 968 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | mdk_rdev_t *rdev2; |
| 970 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
| 973 | /* make rdev->sb match mddev data.. |
| 974 | * |
| 975 | * 1/ zero out disks |
| 976 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 977 | * 3/ any empty disks < next_spare become removed |
| 978 | * |
| 979 | * disks[0] gets initialised to REMOVED because |
| 980 | * we cannot be sure from other fields if it has |
| 981 | * been initialised or not. |
| 982 | */ |
| 983 | int i; |
| 984 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 985 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 986 | rdev->sb_size = MD_SB_BYTES; |
| 987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 989 | |
| 990 | memset(sb, 0, sizeof(*sb)); |
| 991 | |
| 992 | sb->md_magic = MD_SB_MAGIC; |
| 993 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | sb->patch_version = mddev->patch_version; |
| 995 | sb->gvalid_words = 0; /* ignored */ |
| 996 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 997 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 998 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 999 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1000 | |
| 1001 | sb->ctime = mddev->ctime; |
| 1002 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1003 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | sb->raid_disks = mddev->raid_disks; |
| 1005 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1006 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | sb->utime = mddev->utime; |
| 1008 | sb->state = 0; |
| 1009 | sb->events_hi = (mddev->events>>32); |
| 1010 | sb->events_lo = (u32)mddev->events; |
| 1011 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1012 | if (mddev->reshape_position == MaxSector) |
| 1013 | sb->minor_version = 90; |
| 1014 | else { |
| 1015 | sb->minor_version = 91; |
| 1016 | sb->reshape_position = mddev->reshape_position; |
| 1017 | sb->new_level = mddev->new_level; |
| 1018 | sb->delta_disks = mddev->delta_disks; |
| 1019 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1020 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1021 | } |
| 1022 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (mddev->in_sync) |
| 1024 | { |
| 1025 | sb->recovery_cp = mddev->recovery_cp; |
| 1026 | sb->cp_events_hi = (mddev->events>>32); |
| 1027 | sb->cp_events_lo = (u32)mddev->events; |
| 1028 | if (mddev->recovery_cp == MaxSector) |
| 1029 | sb->state = (1<< MD_SB_CLEAN); |
| 1030 | } else |
| 1031 | sb->recovery_cp = 0; |
| 1032 | |
| 1033 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1034 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1036 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 1037 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1040 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1042 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame^] | 1043 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1044 | |
| 1045 | if (rdev2->raid_disk >= 0 && |
| 1046 | sb->minor_version >= 91) |
| 1047 | /* we have nowhere to store the recovery_offset, |
| 1048 | * but if it is not below the reshape_position, |
| 1049 | * we can piggy-back on that. |
| 1050 | */ |
| 1051 | is_active = 1; |
| 1052 | if (rdev2->raid_disk < 0 || |
| 1053 | test_bit(Faulty, &rdev2->flags)) |
| 1054 | is_active = 0; |
| 1055 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1056 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1058 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1059 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | d = &sb->disks[rdev2->desc_nr]; |
| 1061 | nr_disks++; |
| 1062 | d->number = rdev2->desc_nr; |
| 1063 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1064 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame^] | 1065 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | d->raid_disk = rdev2->raid_disk; |
| 1067 | else |
| 1068 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1069 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame^] | 1071 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame^] | 1073 | if (test_bit(In_sync, &rdev2->flags)) |
| 1074 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | active++; |
| 1076 | working++; |
| 1077 | } else { |
| 1078 | d->state = 0; |
| 1079 | spare++; |
| 1080 | working++; |
| 1081 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1082 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1083 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1086 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1087 | mdp_disk_t *d = &sb->disks[i]; |
| 1088 | if (d->state == 0 && d->number == 0) { |
| 1089 | d->number = i; |
| 1090 | d->raid_disk = i; |
| 1091 | d->state = (1<<MD_DISK_REMOVED); |
| 1092 | d->state |= (1<<MD_DISK_FAULTY); |
| 1093 | failed++; |
| 1094 | } |
| 1095 | } |
| 1096 | sb->nr_disks = nr_disks; |
| 1097 | sb->active_disks = active; |
| 1098 | sb->working_disks = working; |
| 1099 | sb->failed_disks = failed; |
| 1100 | sb->spare_disks = spare; |
| 1101 | |
| 1102 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1103 | sb->sb_csum = calc_sb_csum(sb); |
| 1104 | } |
| 1105 | |
| 1106 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1107 | * rdev_size_change for 0.90.0 |
| 1108 | */ |
| 1109 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1110 | 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] | 1111 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1112 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1113 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1114 | if (rdev->mddev->bitmap_offset) |
| 1115 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1116 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1117 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1118 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1119 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1120 | rdev->sb_page); |
| 1121 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1122 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | * version 1 superblock |
| 1128 | */ |
| 1129 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1130 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1132 | __le32 disk_csum; |
| 1133 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | unsigned long long newcsum; |
| 1135 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1136 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | int i; |
| 1138 | |
| 1139 | disk_csum = sb->sb_csum; |
| 1140 | sb->sb_csum = 0; |
| 1141 | newcsum = 0; |
| 1142 | for (i=0; size>=4; size -= 4 ) |
| 1143 | newcsum += le32_to_cpu(*isuper++); |
| 1144 | |
| 1145 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1146 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
| 1148 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1149 | sb->sb_csum = disk_csum; |
| 1150 | return cpu_to_le32(csum); |
| 1151 | } |
| 1152 | |
| 1153 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1154 | { |
| 1155 | struct mdp_superblock_1 *sb; |
| 1156 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1157 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1159 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
| 1161 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1162 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | * It is always aligned to a 4K boundary and |
| 1164 | * depeding on minor_version, it can be: |
| 1165 | * 0: At least 8K, but less than 12K, from end of device |
| 1166 | * 1: At start of device |
| 1167 | * 2: 4K from start of device. |
| 1168 | */ |
| 1169 | switch(minor_version) { |
| 1170 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1171 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1172 | sb_start -= 8*2; |
| 1173 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | break; |
| 1175 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1176 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | break; |
| 1178 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1179 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | break; |
| 1181 | default: |
| 1182 | return -EINVAL; |
| 1183 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1184 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1186 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1187 | * and it is safe to read 4k, so we do that |
| 1188 | */ |
| 1189 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | if (ret) return ret; |
| 1191 | |
| 1192 | |
| 1193 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1194 | |
| 1195 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1196 | sb->major_version != cpu_to_le32(1) || |
| 1197 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1198 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1199 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | return -EINVAL; |
| 1201 | |
| 1202 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1203 | printk("md: invalid superblock checksum on %s\n", |
| 1204 | bdevname(rdev->bdev,b)); |
| 1205 | return -EINVAL; |
| 1206 | } |
| 1207 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1208 | printk("md: data_size too small on %s\n", |
| 1209 | bdevname(rdev->bdev,b)); |
| 1210 | return -EINVAL; |
| 1211 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | rdev->preferred_minor = 0xffff; |
| 1214 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1215 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1217 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1218 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1219 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1220 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1221 | |
| 1222 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1223 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1224 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1225 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1226 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1227 | rdev->desc_nr = -1; |
| 1228 | else |
| 1229 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1230 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1231 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1232 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1233 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | __u64 ev1, ev2; |
| 1235 | struct mdp_superblock_1 *refsb = |
| 1236 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1237 | |
| 1238 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1239 | sb->level != refsb->level || |
| 1240 | sb->layout != refsb->layout || |
| 1241 | sb->chunksize != refsb->chunksize) { |
| 1242 | printk(KERN_WARNING "md: %s has strangely different" |
| 1243 | " superblock to %s\n", |
| 1244 | bdevname(rdev->bdev,b), |
| 1245 | bdevname(refdev->bdev,b2)); |
| 1246 | return -EINVAL; |
| 1247 | } |
| 1248 | ev1 = le64_to_cpu(sb->events); |
| 1249 | ev2 = le64_to_cpu(refsb->events); |
| 1250 | |
| 1251 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1252 | ret = 1; |
| 1253 | else |
| 1254 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1256 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1257 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1258 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1260 | rdev->sectors = rdev->sb_start; |
| 1261 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1263 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1264 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1265 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1266 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1270 | { |
| 1271 | 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] | 1272 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1274 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1275 | clear_bit(Faulty, &rdev->flags); |
| 1276 | clear_bit(In_sync, &rdev->flags); |
| 1277 | clear_bit(WriteMostly, &rdev->flags); |
| 1278 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | if (mddev->raid_disks == 0) { |
| 1281 | mddev->major_version = 1; |
| 1282 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1283 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1284 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1286 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1287 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1288 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | mddev->layout = le32_to_cpu(sb->layout); |
| 1290 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1291 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1292 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1293 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1294 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
| 1296 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1297 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1298 | |
| 1299 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1300 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1301 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1302 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1303 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1304 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1305 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1306 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1307 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1308 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1309 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1310 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1311 | } else { |
| 1312 | mddev->reshape_position = MaxSector; |
| 1313 | mddev->delta_disks = 0; |
| 1314 | mddev->new_level = mddev->level; |
| 1315 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1316 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1319 | } else if (mddev->pers == NULL) { |
| 1320 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | ++ev1; |
| 1322 | if (ev1 < mddev->events) |
| 1323 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1324 | } else if (mddev->bitmap) { |
| 1325 | /* If adding to array with a bitmap, then we can accept an |
| 1326 | * older device, but not too old. |
| 1327 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1328 | if (ev1 < mddev->bitmap->events_cleared) |
| 1329 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1330 | } else { |
| 1331 | if (ev1 < mddev->events) |
| 1332 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1333 | return 0; |
| 1334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1336 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1337 | if (rdev->desc_nr < 0 || |
| 1338 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1339 | role = 0xffff; |
| 1340 | rdev->desc_nr = -1; |
| 1341 | } else |
| 1342 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | switch(role) { |
| 1344 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | break; |
| 1346 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1347 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | break; |
| 1349 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1350 | if ((le32_to_cpu(sb->feature_map) & |
| 1351 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1352 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1353 | else |
| 1354 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | rdev->raid_disk = role; |
| 1356 | break; |
| 1357 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1358 | if (sb->devflags & WriteMostly1) |
| 1359 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1360 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1361 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1367 | { |
| 1368 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | mdk_rdev_t *rdev2; |
| 1370 | int max_dev, i; |
| 1371 | /* make rdev->sb match mddev and rdev data. */ |
| 1372 | |
| 1373 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1374 | |
| 1375 | sb->feature_map = 0; |
| 1376 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1377 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1379 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1380 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1381 | |
| 1382 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1383 | sb->events = cpu_to_le64(mddev->events); |
| 1384 | if (mddev->in_sync) |
| 1385 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1386 | else |
| 1387 | sb->resync_offset = cpu_to_le64(0); |
| 1388 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1389 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1390 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1391 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1392 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1393 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1394 | sb->level = cpu_to_le32(mddev->level); |
| 1395 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1396 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1397 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1398 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1399 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1400 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1401 | |
| 1402 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1403 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1404 | if (rdev->recovery_offset > 0) { |
| 1405 | sb->feature_map |= |
| 1406 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1407 | sb->recovery_offset = |
| 1408 | cpu_to_le64(rdev->recovery_offset); |
| 1409 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1412 | if (mddev->reshape_position != MaxSector) { |
| 1413 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1414 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1415 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1416 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1417 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1418 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1419 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1422 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | if (rdev2->desc_nr+1 > max_dev) |
| 1424 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1425 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1426 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1427 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1428 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1429 | rdev->sb_size = max_dev * 2 + 256; |
| 1430 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1431 | if (rdev->sb_size & bmask) |
| 1432 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | for (i=0; i<max_dev;i++) |
| 1435 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1436 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1437 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1439 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1441 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1443 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1444 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | else |
| 1446 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1447 | } |
| 1448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1450 | } |
| 1451 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1452 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1453 | 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] | 1454 | { |
| 1455 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1456 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1457 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1458 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1459 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1460 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1461 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1462 | max_sectors -= rdev->data_offset; |
| 1463 | if (!num_sectors || num_sectors > max_sectors) |
| 1464 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1465 | } else if (rdev->mddev->bitmap_offset) { |
| 1466 | /* minor version 0 with bitmap we can't move */ |
| 1467 | return 0; |
| 1468 | } else { |
| 1469 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1470 | sector_t sb_start; |
| 1471 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1472 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1473 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1474 | if (!num_sectors || num_sectors > max_sectors) |
| 1475 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1476 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1477 | } |
| 1478 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1479 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1480 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1481 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1482 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1483 | rdev->sb_page); |
| 1484 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1485 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1488 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | [0] = { |
| 1490 | .name = "0.90.0", |
| 1491 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1492 | .load_super = super_90_load, |
| 1493 | .validate_super = super_90_validate, |
| 1494 | .sync_super = super_90_sync, |
| 1495 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | }, |
| 1497 | [1] = { |
| 1498 | .name = "md-1", |
| 1499 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1500 | .load_super = super_1_load, |
| 1501 | .validate_super = super_1_validate, |
| 1502 | .sync_super = super_1_sync, |
| 1503 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | }, |
| 1505 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
| 1507 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1508 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1509 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1511 | rcu_read_lock(); |
| 1512 | rdev_for_each_rcu(rdev, mddev1) |
| 1513 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1514 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1515 | rdev2->bdev->bd_contains) { |
| 1516 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1517 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1518 | } |
| 1519 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | static LIST_HEAD(pending_raid_disks); |
| 1524 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1525 | /* |
| 1526 | * Try to register data integrity profile for an mddev |
| 1527 | * |
| 1528 | * This is called when an array is started and after a disk has been kicked |
| 1529 | * from the array. It only succeeds if all working and active component devices |
| 1530 | * are integrity capable with matching profiles. |
| 1531 | */ |
| 1532 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1533 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1534 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1535 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1536 | if (list_empty(&mddev->disks)) |
| 1537 | return 0; /* nothing to do */ |
| 1538 | if (blk_get_integrity(mddev->gendisk)) |
| 1539 | return 0; /* already registered */ |
| 1540 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1541 | /* skip spares and non-functional disks */ |
| 1542 | if (test_bit(Faulty, &rdev->flags)) |
| 1543 | continue; |
| 1544 | if (rdev->raid_disk < 0) |
| 1545 | continue; |
| 1546 | /* |
| 1547 | * If at least one rdev is not integrity capable, we can not |
| 1548 | * enable data integrity for the md device. |
| 1549 | */ |
| 1550 | if (!bdev_get_integrity(rdev->bdev)) |
| 1551 | return -EINVAL; |
| 1552 | if (!reference) { |
| 1553 | /* Use the first rdev as the reference */ |
| 1554 | reference = rdev; |
| 1555 | continue; |
| 1556 | } |
| 1557 | /* does this rdev's profile match the reference profile? */ |
| 1558 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1559 | rdev->bdev->bd_disk) < 0) |
| 1560 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1561 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1562 | /* |
| 1563 | * All component devices are integrity capable and have matching |
| 1564 | * profiles, register the common profile for the md device. |
| 1565 | */ |
| 1566 | if (blk_integrity_register(mddev->gendisk, |
| 1567 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1568 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1569 | mdname(mddev)); |
| 1570 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1571 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1572 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1573 | mdname(mddev)); |
| 1574 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1575 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1576 | EXPORT_SYMBOL(md_integrity_register); |
| 1577 | |
| 1578 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1579 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1580 | { |
| 1581 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1582 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1583 | |
| 1584 | if (!bi_mddev) /* nothing to do */ |
| 1585 | return; |
| 1586 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1587 | return; |
| 1588 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1589 | rdev->bdev->bd_disk) >= 0) |
| 1590 | return; |
| 1591 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1592 | blk_integrity_unregister(mddev->gendisk); |
| 1593 | } |
| 1594 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1597 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1598 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1599 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1600 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1601 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
| 1603 | if (rdev->mddev) { |
| 1604 | MD_BUG(); |
| 1605 | return -EINVAL; |
| 1606 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1607 | |
| 1608 | /* prevent duplicates */ |
| 1609 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1610 | return -EEXIST; |
| 1611 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1612 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1613 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1614 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1615 | if (mddev->pers) { |
| 1616 | /* Cannot change size, so fail |
| 1617 | * If mddev->level <= 0, then we don't care |
| 1618 | * about aligning sizes (e.g. linear) |
| 1619 | */ |
| 1620 | if (mddev->level > 0) |
| 1621 | return -ENOSPC; |
| 1622 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1623 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
| 1626 | /* Verify rdev->desc_nr is unique. |
| 1627 | * If it is -1, assign a free number, else |
| 1628 | * check number is not in use |
| 1629 | */ |
| 1630 | if (rdev->desc_nr < 0) { |
| 1631 | int choice = 0; |
| 1632 | if (mddev->pers) choice = mddev->raid_disks; |
| 1633 | while (find_rdev_nr(mddev, choice)) |
| 1634 | choice++; |
| 1635 | rdev->desc_nr = choice; |
| 1636 | } else { |
| 1637 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1638 | return -EBUSY; |
| 1639 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1640 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1641 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1642 | mdname(mddev), mddev->max_disks); |
| 1643 | return -EBUSY; |
| 1644 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1645 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1646 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1647 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1650 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1651 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1652 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1653 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1654 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1655 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1656 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1657 | kobject_del(&rdev->kobj); |
| 1658 | goto fail; |
| 1659 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1660 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1661 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1662 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1663 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1664 | |
| 1665 | /* May as well allow recovery to be retried once */ |
| 1666 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1669 | |
| 1670 | fail: |
| 1671 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1672 | b, mdname(mddev)); |
| 1673 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1676 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1677 | { |
| 1678 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1679 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1680 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1684 | { |
| 1685 | char b[BDEVNAME_SIZE]; |
| 1686 | if (!rdev->mddev) { |
| 1687 | MD_BUG(); |
| 1688 | return; |
| 1689 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1690 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1691 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1693 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1694 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1695 | sysfs_put(rdev->sysfs_state); |
| 1696 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1697 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1698 | * writing to 'remove' to "dev/state". We also need |
| 1699 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1700 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1701 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1702 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1703 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1704 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /* |
| 1708 | * prevent the device from being mounted, repartitioned or |
| 1709 | * otherwise reused by a RAID array (or any other kernel |
| 1710 | * subsystem), by bd_claiming the device. |
| 1711 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1712 | 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] | 1713 | { |
| 1714 | int err = 0; |
| 1715 | struct block_device *bdev; |
| 1716 | char b[BDEVNAME_SIZE]; |
| 1717 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1718 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | if (IS_ERR(bdev)) { |
| 1720 | printk(KERN_ERR "md: could not open %s.\n", |
| 1721 | __bdevname(dev, b)); |
| 1722 | return PTR_ERR(bdev); |
| 1723 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1724 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | if (err) { |
| 1726 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1727 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1728 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | return err; |
| 1730 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1731 | if (!shared) |
| 1732 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | rdev->bdev = bdev; |
| 1734 | return err; |
| 1735 | } |
| 1736 | |
| 1737 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1738 | { |
| 1739 | struct block_device *bdev = rdev->bdev; |
| 1740 | rdev->bdev = NULL; |
| 1741 | if (!bdev) |
| 1742 | MD_BUG(); |
| 1743 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1744 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | void md_autodetect_dev(dev_t dev); |
| 1748 | |
| 1749 | static void export_rdev(mdk_rdev_t * rdev) |
| 1750 | { |
| 1751 | char b[BDEVNAME_SIZE]; |
| 1752 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1753 | bdevname(rdev->bdev,b)); |
| 1754 | if (rdev->mddev) |
| 1755 | MD_BUG(); |
| 1756 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1758 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1759 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | #endif |
| 1761 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1762 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1766 | { |
| 1767 | unbind_rdev_from_array(rdev); |
| 1768 | export_rdev(rdev); |
| 1769 | } |
| 1770 | |
| 1771 | static void export_array(mddev_t *mddev) |
| 1772 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1773 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1775 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | if (!rdev->mddev) { |
| 1777 | MD_BUG(); |
| 1778 | continue; |
| 1779 | } |
| 1780 | kick_rdev_from_array(rdev); |
| 1781 | } |
| 1782 | if (!list_empty(&mddev->disks)) |
| 1783 | MD_BUG(); |
| 1784 | mddev->raid_disks = 0; |
| 1785 | mddev->major_version = 0; |
| 1786 | } |
| 1787 | |
| 1788 | static void print_desc(mdp_disk_t *desc) |
| 1789 | { |
| 1790 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1791 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1792 | } |
| 1793 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1794 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | { |
| 1796 | int i; |
| 1797 | |
| 1798 | printk(KERN_INFO |
| 1799 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1800 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1801 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1802 | sb->ctime); |
| 1803 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1804 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1805 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1806 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1807 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1808 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1809 | sb->failed_disks, sb->spare_disks, |
| 1810 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1811 | |
| 1812 | printk(KERN_INFO); |
| 1813 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1814 | mdp_disk_t *desc; |
| 1815 | |
| 1816 | desc = sb->disks + i; |
| 1817 | if (desc->number || desc->major || desc->minor || |
| 1818 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1819 | printk(" D %2d: ", i); |
| 1820 | print_desc(desc); |
| 1821 | } |
| 1822 | } |
| 1823 | printk(KERN_INFO "md: THIS: "); |
| 1824 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1827 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1828 | { |
| 1829 | __u8 *uuid; |
| 1830 | |
| 1831 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1832 | printk(KERN_INFO |
| 1833 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1834 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1835 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1836 | le32_to_cpu(sb->major_version), |
| 1837 | le32_to_cpu(sb->feature_map), |
| 1838 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1839 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1840 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1841 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1842 | sb->set_name, |
| 1843 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1844 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1845 | |
| 1846 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1847 | printk(KERN_INFO |
| 1848 | "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu" |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1849 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1850 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1851 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1852 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1853 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1854 | le32_to_cpu(sb->level), |
| 1855 | (unsigned long long)le64_to_cpu(sb->size), |
| 1856 | le32_to_cpu(sb->raid_disks), |
| 1857 | le32_to_cpu(sb->layout), |
| 1858 | le32_to_cpu(sb->chunksize), |
| 1859 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1860 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1861 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1862 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1863 | le32_to_cpu(sb->dev_number), |
| 1864 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1865 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1866 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1867 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1868 | sb->devflags, |
| 1869 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1870 | (unsigned long long)le64_to_cpu(sb->events), |
| 1871 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1872 | le32_to_cpu(sb->sb_csum), |
| 1873 | le32_to_cpu(sb->max_dev) |
| 1874 | ); |
| 1875 | } |
| 1876 | |
| 1877 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | { |
| 1879 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1880 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1881 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1882 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1883 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1885 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1886 | switch (major_version) { |
| 1887 | case 0: |
| 1888 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1889 | break; |
| 1890 | case 1: |
| 1891 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1892 | break; |
| 1893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | } else |
| 1895 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1896 | } |
| 1897 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1898 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1900 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | mdk_rdev_t *rdev; |
| 1902 | mddev_t *mddev; |
| 1903 | char b[BDEVNAME_SIZE]; |
| 1904 | |
| 1905 | printk("\n"); |
| 1906 | printk("md: **********************************\n"); |
| 1907 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1908 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1909 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1911 | if (mddev->bitmap) |
| 1912 | bitmap_print_sb(mddev->bitmap); |
| 1913 | else |
| 1914 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1915 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1917 | printk("\n"); |
| 1918 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1919 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1920 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | } |
| 1922 | printk("md: **********************************\n"); |
| 1923 | printk("\n"); |
| 1924 | } |
| 1925 | |
| 1926 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1927 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1929 | /* Update each superblock (in-memory image), but |
| 1930 | * if we are allowed to, skip spares which already |
| 1931 | * have the right event counter, or have one earlier |
| 1932 | * (which would mean they aren't being marked as dirty |
| 1933 | * with the rest of the array) |
| 1934 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 1937 | /* First make sure individual recovery_offsets are correct */ |
| 1938 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1939 | if (rdev->raid_disk >= 0 && |
| 1940 | !test_bit(In_sync, &rdev->flags) && |
| 1941 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 1942 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 1943 | |
| 1944 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1945 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1946 | if (rdev->sb_events == mddev->events || |
| 1947 | (nospares && |
| 1948 | rdev->raid_disk < 0 && |
| 1949 | (rdev->sb_events&1)==0 && |
| 1950 | rdev->sb_events+1 == mddev->events)) { |
| 1951 | /* Don't update this superblock */ |
| 1952 | rdev->sb_loaded = 2; |
| 1953 | } else { |
| 1954 | super_types[mddev->major_version]. |
| 1955 | sync_super(mddev, rdev); |
| 1956 | rdev->sb_loaded = 1; |
| 1957 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1961 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1964 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1965 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 1967 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1968 | if (mddev->external) |
| 1969 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1971 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1972 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1973 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1974 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1975 | force_change = 1; |
| 1976 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1977 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1978 | * though if events isn't the right even/odd, we will have to do |
| 1979 | * spares after all |
| 1980 | */ |
| 1981 | nospares = 1; |
| 1982 | if (force_change) |
| 1983 | nospares = 0; |
| 1984 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1985 | /* If the array is degraded, then skipping spares is both |
| 1986 | * dangerous and fairly pointless. |
| 1987 | * Dangerous because a device that was removed from the array |
| 1988 | * might have a event_count that still looks up-to-date, |
| 1989 | * so it can be re-added without a resync. |
| 1990 | * Pointless because if there are any spares to skip, |
| 1991 | * then a recovery will happen and soon that array won't |
| 1992 | * be degraded any more and the spare can go back to sleep then. |
| 1993 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1994 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1995 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1996 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1997 | |
| 1998 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1999 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2000 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2001 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2002 | && (mddev->events & 1) |
| 2003 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2004 | mddev->events--; |
| 2005 | else { |
| 2006 | /* otherwise we have to go forward and ... */ |
| 2007 | mddev->events ++; |
| 2008 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2009 | /* .. if the array isn't clean, an 'even' event must also go |
| 2010 | * to spares. */ |
| 2011 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2012 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2013 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2014 | /* otherwise an 'odd' event must go to spares */ |
| 2015 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2016 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2017 | } |
| 2018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | |
| 2020 | if (!mddev->events) { |
| 2021 | /* |
| 2022 | * oops, this 64-bit counter should never wrap. |
| 2023 | * Either we are in around ~1 trillion A.C., assuming |
| 2024 | * 1 reboot per second, or we have a bug: |
| 2025 | */ |
| 2026 | MD_BUG(); |
| 2027 | mddev->events --; |
| 2028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
| 2030 | /* |
| 2031 | * do not write anything to disk if using |
| 2032 | * nonpersistent superblocks |
| 2033 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2034 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2035 | if (!mddev->external) |
| 2036 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2037 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2038 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2039 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2041 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2042 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2043 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | |
| 2045 | dprintk(KERN_INFO |
| 2046 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2047 | mdname(mddev),mddev->in_sync); |
| 2048 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2049 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2050 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | char b[BDEVNAME_SIZE]; |
| 2052 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2053 | if (rdev->sb_loaded != 1) |
| 2054 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2055 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | dprintk("(skipping faulty "); |
| 2057 | |
| 2058 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2059 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2060 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2061 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2062 | rdev->sb_page); |
| 2063 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2064 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2065 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2066 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | } else |
| 2069 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2070 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | /* only need to write one superblock... */ |
| 2072 | break; |
| 2073 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2074 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2075 | /* 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] | 2076 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2077 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2078 | if (mddev->in_sync != sync_req || |
| 2079 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2080 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2081 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2082 | goto repeat; |
| 2083 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2084 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2085 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2086 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2087 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2088 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2092 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2093 | * We want to accept with case. For this we use cmd_match. |
| 2094 | */ |
| 2095 | static int cmd_match(const char *cmd, const char *str) |
| 2096 | { |
| 2097 | /* See if cmd, written into a sysfs file, matches |
| 2098 | * str. They must either be the same, or cmd can |
| 2099 | * have a trailing newline |
| 2100 | */ |
| 2101 | while (*cmd && *str && *cmd == *str) { |
| 2102 | cmd++; |
| 2103 | str++; |
| 2104 | } |
| 2105 | if (*cmd == '\n') |
| 2106 | cmd++; |
| 2107 | if (*str || *cmd) |
| 2108 | return 0; |
| 2109 | return 1; |
| 2110 | } |
| 2111 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2112 | struct rdev_sysfs_entry { |
| 2113 | struct attribute attr; |
| 2114 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2115 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2116 | }; |
| 2117 | |
| 2118 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2119 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2120 | { |
| 2121 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2122 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2123 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2124 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2125 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2126 | sep = ","; |
| 2127 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2128 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2129 | len += sprintf(page+len, "%sin_sync",sep); |
| 2130 | sep = ","; |
| 2131 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2132 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2133 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2134 | sep = ","; |
| 2135 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2136 | if (test_bit(Blocked, &rdev->flags)) { |
| 2137 | len += sprintf(page+len, "%sblocked", sep); |
| 2138 | sep = ","; |
| 2139 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2140 | if (!test_bit(Faulty, &rdev->flags) && |
| 2141 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2142 | len += sprintf(page+len, "%sspare", sep); |
| 2143 | sep = ","; |
| 2144 | } |
| 2145 | return len+sprintf(page+len, "\n"); |
| 2146 | } |
| 2147 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2148 | static ssize_t |
| 2149 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2150 | { |
| 2151 | /* can write |
| 2152 | * faulty - simulates and error |
| 2153 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2154 | * writemostly - sets write_mostly |
| 2155 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2156 | * blocked - sets the Blocked flag |
| 2157 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2158 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2159 | */ |
| 2160 | int err = -EINVAL; |
| 2161 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2162 | md_error(rdev->mddev, rdev); |
| 2163 | err = 0; |
| 2164 | } else if (cmd_match(buf, "remove")) { |
| 2165 | if (rdev->raid_disk >= 0) |
| 2166 | err = -EBUSY; |
| 2167 | else { |
| 2168 | mddev_t *mddev = rdev->mddev; |
| 2169 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2170 | if (mddev->pers) |
| 2171 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2172 | md_new_event(mddev); |
| 2173 | err = 0; |
| 2174 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2175 | } else if (cmd_match(buf, "writemostly")) { |
| 2176 | set_bit(WriteMostly, &rdev->flags); |
| 2177 | err = 0; |
| 2178 | } else if (cmd_match(buf, "-writemostly")) { |
| 2179 | clear_bit(WriteMostly, &rdev->flags); |
| 2180 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2181 | } else if (cmd_match(buf, "blocked")) { |
| 2182 | set_bit(Blocked, &rdev->flags); |
| 2183 | err = 0; |
| 2184 | } else if (cmd_match(buf, "-blocked")) { |
| 2185 | clear_bit(Blocked, &rdev->flags); |
| 2186 | wake_up(&rdev->blocked_wait); |
| 2187 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2188 | md_wakeup_thread(rdev->mddev->thread); |
| 2189 | |
| 2190 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2191 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2192 | set_bit(In_sync, &rdev->flags); |
| 2193 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2194 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2195 | if (!err && rdev->sysfs_state) |
| 2196 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2197 | return err ? err : len; |
| 2198 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2199 | static struct rdev_sysfs_entry rdev_state = |
| 2200 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2201 | |
| 2202 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2203 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2204 | { |
| 2205 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2206 | } |
| 2207 | |
| 2208 | static ssize_t |
| 2209 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2210 | { |
| 2211 | char *e; |
| 2212 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2213 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2214 | atomic_set(&rdev->corrected_errors, n); |
| 2215 | return len; |
| 2216 | } |
| 2217 | return -EINVAL; |
| 2218 | } |
| 2219 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2220 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2221 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2222 | static ssize_t |
| 2223 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2224 | { |
| 2225 | if (rdev->raid_disk < 0) |
| 2226 | return sprintf(page, "none\n"); |
| 2227 | else |
| 2228 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2229 | } |
| 2230 | |
| 2231 | static ssize_t |
| 2232 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2233 | { |
| 2234 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2235 | int err; |
| 2236 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2237 | int slot = simple_strtoul(buf, &e, 10); |
| 2238 | if (strncmp(buf, "none", 4)==0) |
| 2239 | slot = -1; |
| 2240 | else if (e==buf || (*e && *e!= '\n')) |
| 2241 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2242 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2243 | /* Setting 'slot' on an active array requires also |
| 2244 | * updating the 'rd%d' link, and communicating |
| 2245 | * with the personality with ->hot_*_disk. |
| 2246 | * For now we only support removing |
| 2247 | * failed/spare devices. This normally happens automatically, |
| 2248 | * but not when the metadata is externally managed. |
| 2249 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2250 | if (rdev->raid_disk == -1) |
| 2251 | return -EEXIST; |
| 2252 | /* personality does all needed checks */ |
| 2253 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2254 | return -EINVAL; |
| 2255 | err = rdev->mddev->pers-> |
| 2256 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2257 | if (err) |
| 2258 | return err; |
| 2259 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2260 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2261 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2262 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2263 | } else if (rdev->mddev->pers) { |
| 2264 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2265 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2266 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2267 | */ |
| 2268 | |
| 2269 | if (rdev->raid_disk != -1) |
| 2270 | return -EBUSY; |
| 2271 | |
| 2272 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2273 | return -EINVAL; |
| 2274 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2275 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2276 | if (rdev2->raid_disk == slot) |
| 2277 | return -EEXIST; |
| 2278 | |
| 2279 | rdev->raid_disk = slot; |
| 2280 | if (test_bit(In_sync, &rdev->flags)) |
| 2281 | rdev->saved_raid_disk = slot; |
| 2282 | else |
| 2283 | rdev->saved_raid_disk = -1; |
| 2284 | err = rdev->mddev->pers-> |
| 2285 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2286 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2287 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2288 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2289 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2290 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2291 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2292 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2293 | printk(KERN_WARNING |
| 2294 | "md: cannot register " |
| 2295 | "%s for %s\n", |
| 2296 | nm, mdname(rdev->mddev)); |
| 2297 | |
| 2298 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2299 | } else { |
| 2300 | if (slot >= rdev->mddev->raid_disks) |
| 2301 | return -ENOSPC; |
| 2302 | rdev->raid_disk = slot; |
| 2303 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2304 | clear_bit(Faulty, &rdev->flags); |
| 2305 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2306 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2307 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2308 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2309 | return len; |
| 2310 | } |
| 2311 | |
| 2312 | |
| 2313 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2314 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2315 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2316 | static ssize_t |
| 2317 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2318 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2319 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | static ssize_t |
| 2323 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2324 | { |
| 2325 | char *e; |
| 2326 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2327 | if (e==buf || (*e && *e != '\n')) |
| 2328 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2329 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2330 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2331 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2332 | /* Must set offset before size, so overlap checks |
| 2333 | * can be sane */ |
| 2334 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2335 | rdev->data_offset = offset; |
| 2336 | return len; |
| 2337 | } |
| 2338 | |
| 2339 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2340 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2341 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2342 | static ssize_t |
| 2343 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2344 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2345 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2346 | } |
| 2347 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2348 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2349 | { |
| 2350 | /* check if two start/length pairs overlap */ |
| 2351 | if (s1+l1 <= s2) |
| 2352 | return 0; |
| 2353 | if (s2+l2 <= s1) |
| 2354 | return 0; |
| 2355 | return 1; |
| 2356 | } |
| 2357 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2358 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2359 | { |
| 2360 | unsigned long long blocks; |
| 2361 | sector_t new; |
| 2362 | |
| 2363 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2364 | return -EINVAL; |
| 2365 | |
| 2366 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2367 | return -EINVAL; /* sector conversion overflow */ |
| 2368 | |
| 2369 | new = blocks * 2; |
| 2370 | if (new != blocks * 2) |
| 2371 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2372 | |
| 2373 | *sectors = new; |
| 2374 | return 0; |
| 2375 | } |
| 2376 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2377 | static ssize_t |
| 2378 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2379 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2380 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2381 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2382 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2383 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2384 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2385 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2386 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2387 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2388 | sectors = super_types[my_mddev->major_version]. |
| 2389 | rdev_size_change(rdev, sectors); |
| 2390 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2391 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2392 | } else if (!sectors) |
| 2393 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2394 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2395 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2396 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2397 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2398 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2399 | rdev->sectors = sectors; |
| 2400 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2401 | /* need to check that all other rdevs with the same ->bdev |
| 2402 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2403 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2404 | * we have to change it back, we will have the lock again. |
| 2405 | */ |
| 2406 | mddev_t *mddev; |
| 2407 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2408 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2409 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2410 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2411 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2412 | mdk_rdev_t *rdev2; |
| 2413 | |
| 2414 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2415 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2416 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2417 | (rdev->bdev == rdev2->bdev && |
| 2418 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2419 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2420 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2421 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2422 | overlap = 1; |
| 2423 | break; |
| 2424 | } |
| 2425 | mddev_unlock(mddev); |
| 2426 | if (overlap) { |
| 2427 | mddev_put(mddev); |
| 2428 | break; |
| 2429 | } |
| 2430 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2431 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2432 | if (overlap) { |
| 2433 | /* Someone else could have slipped in a size |
| 2434 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2435 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2436 | * safe, and trust userspace not to race with |
| 2437 | * itself |
| 2438 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2439 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2440 | return -EBUSY; |
| 2441 | } |
| 2442 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2443 | return len; |
| 2444 | } |
| 2445 | |
| 2446 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2447 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2448 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2449 | static struct attribute *rdev_default_attrs[] = { |
| 2450 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2451 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2452 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2453 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2454 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2455 | NULL, |
| 2456 | }; |
| 2457 | static ssize_t |
| 2458 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2459 | { |
| 2460 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2461 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2462 | mddev_t *mddev = rdev->mddev; |
| 2463 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2464 | |
| 2465 | if (!entry->show) |
| 2466 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2467 | |
| 2468 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2469 | if (!rv) { |
| 2470 | if (rdev->mddev == NULL) |
| 2471 | rv = -EBUSY; |
| 2472 | else |
| 2473 | rv = entry->show(rdev, page); |
| 2474 | mddev_unlock(mddev); |
| 2475 | } |
| 2476 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | static ssize_t |
| 2480 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2481 | const char *page, size_t length) |
| 2482 | { |
| 2483 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2484 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2485 | ssize_t rv; |
| 2486 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2487 | |
| 2488 | if (!entry->store) |
| 2489 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2490 | if (!capable(CAP_SYS_ADMIN)) |
| 2491 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2492 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2493 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2494 | if (rdev->mddev == NULL) |
| 2495 | rv = -EBUSY; |
| 2496 | else |
| 2497 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2498 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2499 | } |
| 2500 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | static void rdev_free(struct kobject *ko) |
| 2504 | { |
| 2505 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2506 | kfree(rdev); |
| 2507 | } |
| 2508 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2509 | .show = rdev_attr_show, |
| 2510 | .store = rdev_attr_store, |
| 2511 | }; |
| 2512 | static struct kobj_type rdev_ktype = { |
| 2513 | .release = rdev_free, |
| 2514 | .sysfs_ops = &rdev_sysfs_ops, |
| 2515 | .default_attrs = rdev_default_attrs, |
| 2516 | }; |
| 2517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | /* |
| 2519 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2520 | * |
| 2521 | * mark the device faulty if: |
| 2522 | * |
| 2523 | * - the device is nonexistent (zero size) |
| 2524 | * - the device has no valid superblock |
| 2525 | * |
| 2526 | * a faulty rdev _never_ has rdev->sb set. |
| 2527 | */ |
| 2528 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2529 | { |
| 2530 | char b[BDEVNAME_SIZE]; |
| 2531 | int err; |
| 2532 | mdk_rdev_t *rdev; |
| 2533 | sector_t size; |
| 2534 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2535 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | if (!rdev) { |
| 2537 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2538 | return ERR_PTR(-ENOMEM); |
| 2539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | |
| 2541 | if ((err = alloc_disk_sb(rdev))) |
| 2542 | goto abort_free; |
| 2543 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2544 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | if (err) |
| 2546 | goto abort_free; |
| 2547 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2548 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2551 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2552 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2553 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2555 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2557 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2558 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | |
| 2560 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2561 | if (!size) { |
| 2562 | printk(KERN_WARNING |
| 2563 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2564 | bdevname(rdev->bdev,b)); |
| 2565 | err = -EINVAL; |
| 2566 | goto abort_free; |
| 2567 | } |
| 2568 | |
| 2569 | if (super_format >= 0) { |
| 2570 | err = super_types[super_format]. |
| 2571 | load_super(rdev, NULL, super_minor); |
| 2572 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2573 | printk(KERN_WARNING |
| 2574 | "md: %s does not have a valid v%d.%d " |
| 2575 | "superblock, not importing!\n", |
| 2576 | bdevname(rdev->bdev,b), |
| 2577 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | goto abort_free; |
| 2579 | } |
| 2580 | if (err < 0) { |
| 2581 | printk(KERN_WARNING |
| 2582 | "md: could not read %s's sb, not importing!\n", |
| 2583 | bdevname(rdev->bdev,b)); |
| 2584 | goto abort_free; |
| 2585 | } |
| 2586 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2589 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | |
| 2591 | return rdev; |
| 2592 | |
| 2593 | abort_free: |
| 2594 | if (rdev->sb_page) { |
| 2595 | if (rdev->bdev) |
| 2596 | unlock_rdev(rdev); |
| 2597 | free_disk_sb(rdev); |
| 2598 | } |
| 2599 | kfree(rdev); |
| 2600 | return ERR_PTR(err); |
| 2601 | } |
| 2602 | |
| 2603 | /* |
| 2604 | * Check a full RAID array for plausibility |
| 2605 | */ |
| 2606 | |
| 2607 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2608 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | { |
| 2610 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2611 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | char b[BDEVNAME_SIZE]; |
| 2613 | |
| 2614 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2615 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | switch (super_types[mddev->major_version]. |
| 2617 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2618 | case 1: |
| 2619 | freshest = rdev; |
| 2620 | break; |
| 2621 | case 0: |
| 2622 | break; |
| 2623 | default: |
| 2624 | printk( KERN_ERR \ |
| 2625 | "md: fatal superblock inconsistency in %s" |
| 2626 | " -- removing from array\n", |
| 2627 | bdevname(rdev->bdev,b)); |
| 2628 | kick_rdev_from_array(rdev); |
| 2629 | } |
| 2630 | |
| 2631 | |
| 2632 | super_types[mddev->major_version]. |
| 2633 | validate_super(mddev, freshest); |
| 2634 | |
| 2635 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2636 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2637 | if (rdev->desc_nr >= mddev->max_disks || |
| 2638 | i > mddev->max_disks) { |
| 2639 | printk(KERN_WARNING |
| 2640 | "md: %s: %s: only %d devices permitted\n", |
| 2641 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2642 | mddev->max_disks); |
| 2643 | kick_rdev_from_array(rdev); |
| 2644 | continue; |
| 2645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | if (rdev != freshest) |
| 2647 | if (super_types[mddev->major_version]. |
| 2648 | validate_super(mddev, rdev)) { |
| 2649 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2650 | " from array!\n", |
| 2651 | bdevname(rdev->bdev,b)); |
| 2652 | kick_rdev_from_array(rdev); |
| 2653 | continue; |
| 2654 | } |
| 2655 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2656 | rdev->desc_nr = i++; |
| 2657 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2658 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2659 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2660 | rdev->raid_disk = -1; |
| 2661 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | } |
| 2663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2666 | static void md_safemode_timeout(unsigned long data); |
| 2667 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2668 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2669 | safe_delay_show(mddev_t *mddev, char *page) |
| 2670 | { |
| 2671 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2672 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2673 | } |
| 2674 | static ssize_t |
| 2675 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2676 | { |
| 2677 | int scale=1; |
| 2678 | int dot=0; |
| 2679 | int i; |
| 2680 | unsigned long msec; |
| 2681 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2682 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2683 | /* remove a period, and count digits after it */ |
| 2684 | if (len >= sizeof(buf)) |
| 2685 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2686 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2687 | for (i=0; i<len; i++) { |
| 2688 | if (dot) { |
| 2689 | if (isdigit(buf[i])) { |
| 2690 | buf[i-1] = buf[i]; |
| 2691 | scale *= 10; |
| 2692 | } |
| 2693 | buf[i] = 0; |
| 2694 | } else if (buf[i] == '.') { |
| 2695 | dot=1; |
| 2696 | buf[i] = 0; |
| 2697 | } |
| 2698 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2699 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2700 | return -EINVAL; |
| 2701 | msec = (msec * 1000) / scale; |
| 2702 | if (msec == 0) |
| 2703 | mddev->safemode_delay = 0; |
| 2704 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2705 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2706 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2707 | if (mddev->safemode_delay == 0) |
| 2708 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2709 | if (mddev->safemode_delay < old_delay) |
| 2710 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2711 | } |
| 2712 | return len; |
| 2713 | } |
| 2714 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2715 | __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] | 2716 | |
| 2717 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2718 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2719 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2720 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2721 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2722 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2723 | else if (mddev->clevel[0]) |
| 2724 | return sprintf(page, "%s\n", mddev->clevel); |
| 2725 | else if (mddev->level != LEVEL_NONE) |
| 2726 | return sprintf(page, "%d\n", mddev->level); |
| 2727 | else |
| 2728 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2729 | } |
| 2730 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2731 | static ssize_t |
| 2732 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2733 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2734 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2735 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2736 | struct mdk_personality *pers; |
| 2737 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2738 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2739 | |
| 2740 | if (mddev->pers == NULL) { |
| 2741 | if (len == 0) |
| 2742 | return 0; |
| 2743 | if (len >= sizeof(mddev->clevel)) |
| 2744 | return -ENOSPC; |
| 2745 | strncpy(mddev->clevel, buf, len); |
| 2746 | if (mddev->clevel[len-1] == '\n') |
| 2747 | len--; |
| 2748 | mddev->clevel[len] = 0; |
| 2749 | mddev->level = LEVEL_NONE; |
| 2750 | return rv; |
| 2751 | } |
| 2752 | |
| 2753 | /* request to change the personality. Need to ensure: |
| 2754 | * - array is not engaged in resync/recovery/reshape |
| 2755 | * - old personality can be suspended |
| 2756 | * - new personality will access other array. |
| 2757 | */ |
| 2758 | |
| 2759 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2760 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2761 | |
| 2762 | if (!mddev->pers->quiesce) { |
| 2763 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2764 | mdname(mddev), mddev->pers->name); |
| 2765 | return -EINVAL; |
| 2766 | } |
| 2767 | |
| 2768 | /* Now find the new personality */ |
| 2769 | if (len == 0 || len >= sizeof(level)) |
| 2770 | return -EINVAL; |
| 2771 | strncpy(level, buf, len); |
| 2772 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2773 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2774 | level[len] = 0; |
| 2775 | |
| 2776 | request_module("md-%s", level); |
| 2777 | spin_lock(&pers_lock); |
| 2778 | pers = find_pers(LEVEL_NONE, level); |
| 2779 | if (!pers || !try_module_get(pers->owner)) { |
| 2780 | spin_unlock(&pers_lock); |
| 2781 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2782 | return -EINVAL; |
| 2783 | } |
| 2784 | spin_unlock(&pers_lock); |
| 2785 | |
| 2786 | if (pers == mddev->pers) { |
| 2787 | /* Nothing to do! */ |
| 2788 | module_put(pers->owner); |
| 2789 | return rv; |
| 2790 | } |
| 2791 | if (!pers->takeover) { |
| 2792 | module_put(pers->owner); |
| 2793 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2794 | mdname(mddev), level); |
| 2795 | return -EINVAL; |
| 2796 | } |
| 2797 | |
| 2798 | /* ->takeover must set new_* and/or delta_disks |
| 2799 | * if it succeeds, and may set them when it fails. |
| 2800 | */ |
| 2801 | priv = pers->takeover(mddev); |
| 2802 | if (IS_ERR(priv)) { |
| 2803 | mddev->new_level = mddev->level; |
| 2804 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2805 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2806 | mddev->raid_disks -= mddev->delta_disks; |
| 2807 | mddev->delta_disks = 0; |
| 2808 | module_put(pers->owner); |
| 2809 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2810 | mdname(mddev), level); |
| 2811 | return PTR_ERR(priv); |
| 2812 | } |
| 2813 | |
| 2814 | /* Looks like we have a winner */ |
| 2815 | mddev_suspend(mddev); |
| 2816 | mddev->pers->stop(mddev); |
| 2817 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2818 | /* Invalidate devices that are now superfluous */ |
| 2819 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2820 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2821 | rdev->raid_disk = -1; |
| 2822 | clear_bit(In_sync, &rdev->flags); |
| 2823 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2824 | mddev->pers = pers; |
| 2825 | mddev->private = priv; |
| 2826 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2827 | mddev->level = mddev->new_level; |
| 2828 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2829 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2830 | mddev->delta_disks = 0; |
| 2831 | pers->run(mddev); |
| 2832 | mddev_resume(mddev); |
| 2833 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2834 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2835 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2836 | return rv; |
| 2837 | } |
| 2838 | |
| 2839 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2840 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2841 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2842 | |
| 2843 | static ssize_t |
| 2844 | layout_show(mddev_t *mddev, char *page) |
| 2845 | { |
| 2846 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2847 | if (mddev->reshape_position != MaxSector && |
| 2848 | mddev->layout != mddev->new_layout) |
| 2849 | return sprintf(page, "%d (%d)\n", |
| 2850 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2851 | return sprintf(page, "%d\n", mddev->layout); |
| 2852 | } |
| 2853 | |
| 2854 | static ssize_t |
| 2855 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2856 | { |
| 2857 | char *e; |
| 2858 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2859 | |
| 2860 | if (!*buf || (*e && *e != '\n')) |
| 2861 | return -EINVAL; |
| 2862 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2863 | if (mddev->pers) { |
| 2864 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2865 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2866 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2867 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2868 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2869 | if (err) { |
| 2870 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2871 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2872 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2873 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2874 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2875 | if (mddev->reshape_position == MaxSector) |
| 2876 | mddev->layout = n; |
| 2877 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2878 | return len; |
| 2879 | } |
| 2880 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2881 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2882 | |
| 2883 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2884 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2885 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2886 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2887 | if (mddev->raid_disks == 0) |
| 2888 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2889 | if (mddev->reshape_position != MaxSector && |
| 2890 | mddev->delta_disks != 0) |
| 2891 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2892 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2893 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2894 | } |
| 2895 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2896 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2897 | |
| 2898 | static ssize_t |
| 2899 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2900 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2901 | char *e; |
| 2902 | int rv = 0; |
| 2903 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2904 | |
| 2905 | if (!*buf || (*e && *e != '\n')) |
| 2906 | return -EINVAL; |
| 2907 | |
| 2908 | if (mddev->pers) |
| 2909 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2910 | else if (mddev->reshape_position != MaxSector) { |
| 2911 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2912 | mddev->delta_disks = n - olddisks; |
| 2913 | mddev->raid_disks = n; |
| 2914 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2915 | mddev->raid_disks = n; |
| 2916 | return rv ? rv : len; |
| 2917 | } |
| 2918 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2919 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2920 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2921 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2922 | chunk_size_show(mddev_t *mddev, char *page) |
| 2923 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2924 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2925 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 2926 | return sprintf(page, "%d (%d)\n", |
| 2927 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2928 | mddev->chunk_sectors << 9); |
| 2929 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | static ssize_t |
| 2933 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2934 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2935 | char *e; |
| 2936 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2937 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2938 | if (!*buf || (*e && *e != '\n')) |
| 2939 | return -EINVAL; |
| 2940 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2941 | if (mddev->pers) { |
| 2942 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2943 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2944 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2945 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2946 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2947 | if (err) { |
| 2948 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2949 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2950 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2951 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2952 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2953 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2954 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2955 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2956 | return len; |
| 2957 | } |
| 2958 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2959 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2960 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2961 | static ssize_t |
| 2962 | resync_start_show(mddev_t *mddev, char *page) |
| 2963 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 2964 | if (mddev->recovery_cp == MaxSector) |
| 2965 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2966 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2967 | } |
| 2968 | |
| 2969 | static ssize_t |
| 2970 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2971 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2972 | char *e; |
| 2973 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2974 | |
| 2975 | if (mddev->pers) |
| 2976 | return -EBUSY; |
| 2977 | if (!*buf || (*e && *e != '\n')) |
| 2978 | return -EINVAL; |
| 2979 | |
| 2980 | mddev->recovery_cp = n; |
| 2981 | return len; |
| 2982 | } |
| 2983 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2984 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2985 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2986 | /* |
| 2987 | * The array state can be: |
| 2988 | * |
| 2989 | * clear |
| 2990 | * No devices, no size, no level |
| 2991 | * Equivalent to STOP_ARRAY ioctl |
| 2992 | * inactive |
| 2993 | * May have some settings, but array is not active |
| 2994 | * all IO results in error |
| 2995 | * When written, doesn't tear down array, but just stops it |
| 2996 | * suspended (not supported yet) |
| 2997 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2998 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2999 | * readonly |
| 3000 | * no resync can happen. no superblocks get written. |
| 3001 | * write requests fail |
| 3002 | * read-auto |
| 3003 | * like readonly, but behaves like 'clean' on a write request. |
| 3004 | * |
| 3005 | * clean - no pending writes, but otherwise active. |
| 3006 | * When written to inactive array, starts without resync |
| 3007 | * If a write request arrives then |
| 3008 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3009 | * if not known, block and switch to write-pending |
| 3010 | * If written to an active array that has pending writes, then fails. |
| 3011 | * active |
| 3012 | * fully active: IO and resync can be happening. |
| 3013 | * When written to inactive array, starts with resync |
| 3014 | * |
| 3015 | * write-pending |
| 3016 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3017 | * |
| 3018 | * active-idle |
| 3019 | * like active, but no writes have been seen for a while (100msec). |
| 3020 | * |
| 3021 | */ |
| 3022 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3023 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3024 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3025 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3026 | "write-pending", "active-idle", NULL }; |
| 3027 | |
| 3028 | static int match_word(const char *word, char **list) |
| 3029 | { |
| 3030 | int n; |
| 3031 | for (n=0; list[n]; n++) |
| 3032 | if (cmd_match(word, list[n])) |
| 3033 | break; |
| 3034 | return n; |
| 3035 | } |
| 3036 | |
| 3037 | static ssize_t |
| 3038 | array_state_show(mddev_t *mddev, char *page) |
| 3039 | { |
| 3040 | enum array_state st = inactive; |
| 3041 | |
| 3042 | if (mddev->pers) |
| 3043 | switch(mddev->ro) { |
| 3044 | case 1: |
| 3045 | st = readonly; |
| 3046 | break; |
| 3047 | case 2: |
| 3048 | st = read_auto; |
| 3049 | break; |
| 3050 | case 0: |
| 3051 | if (mddev->in_sync) |
| 3052 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3053 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3054 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3055 | else if (mddev->safemode) |
| 3056 | st = active_idle; |
| 3057 | else |
| 3058 | st = active; |
| 3059 | } |
| 3060 | else { |
| 3061 | if (list_empty(&mddev->disks) && |
| 3062 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3063 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3064 | st = clear; |
| 3065 | else |
| 3066 | st = inactive; |
| 3067 | } |
| 3068 | return sprintf(page, "%s\n", array_states[st]); |
| 3069 | } |
| 3070 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3071 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3072 | static int do_md_run(mddev_t * mddev); |
| 3073 | static int restart_array(mddev_t *mddev); |
| 3074 | |
| 3075 | static ssize_t |
| 3076 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3077 | { |
| 3078 | int err = -EINVAL; |
| 3079 | enum array_state st = match_word(buf, array_states); |
| 3080 | switch(st) { |
| 3081 | case bad_word: |
| 3082 | break; |
| 3083 | case clear: |
| 3084 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3085 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3086 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3087 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3088 | break; |
| 3089 | case inactive: |
| 3090 | /* stopping an active array */ |
| 3091 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3092 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3093 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3094 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3095 | } else |
| 3096 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3097 | break; |
| 3098 | case suspended: |
| 3099 | break; /* not supported yet */ |
| 3100 | case readonly: |
| 3101 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3102 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3103 | else { |
| 3104 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3105 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3106 | err = do_md_run(mddev); |
| 3107 | } |
| 3108 | break; |
| 3109 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3110 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3111 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3112 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3113 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3114 | err = restart_array(mddev); |
| 3115 | if (err == 0) { |
| 3116 | mddev->ro = 2; |
| 3117 | set_disk_ro(mddev->gendisk, 0); |
| 3118 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3119 | } else { |
| 3120 | mddev->ro = 2; |
| 3121 | err = do_md_run(mddev); |
| 3122 | } |
| 3123 | break; |
| 3124 | case clean: |
| 3125 | if (mddev->pers) { |
| 3126 | restart_array(mddev); |
| 3127 | spin_lock_irq(&mddev->write_lock); |
| 3128 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3129 | if (mddev->in_sync == 0) { |
| 3130 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3131 | if (mddev->safemode == 1) |
| 3132 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3133 | if (mddev->persistent) |
| 3134 | set_bit(MD_CHANGE_CLEAN, |
| 3135 | &mddev->flags); |
| 3136 | } |
| 3137 | err = 0; |
| 3138 | } else |
| 3139 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3140 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3141 | } else |
| 3142 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3143 | break; |
| 3144 | case active: |
| 3145 | if (mddev->pers) { |
| 3146 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3147 | if (mddev->external) |
| 3148 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3149 | wake_up(&mddev->sb_wait); |
| 3150 | err = 0; |
| 3151 | } else { |
| 3152 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3153 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3154 | err = do_md_run(mddev); |
| 3155 | } |
| 3156 | break; |
| 3157 | case write_pending: |
| 3158 | case active_idle: |
| 3159 | /* these cannot be set */ |
| 3160 | break; |
| 3161 | } |
| 3162 | if (err) |
| 3163 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3164 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3165 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3166 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3167 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3168 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3169 | static struct md_sysfs_entry md_array_state = |
| 3170 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3171 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3172 | static ssize_t |
| 3173 | null_show(mddev_t *mddev, char *page) |
| 3174 | { |
| 3175 | return -EINVAL; |
| 3176 | } |
| 3177 | |
| 3178 | static ssize_t |
| 3179 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3180 | { |
| 3181 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3182 | /* The new device is added to the array. |
| 3183 | * If the array has a persistent superblock, we read the |
| 3184 | * superblock to initialise info and check validity. |
| 3185 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3186 | * which mainly checks size. |
| 3187 | */ |
| 3188 | char *e; |
| 3189 | int major = simple_strtoul(buf, &e, 10); |
| 3190 | int minor; |
| 3191 | dev_t dev; |
| 3192 | mdk_rdev_t *rdev; |
| 3193 | int err; |
| 3194 | |
| 3195 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3196 | return -EINVAL; |
| 3197 | minor = simple_strtoul(e+1, &e, 10); |
| 3198 | if (*e && *e != '\n') |
| 3199 | return -EINVAL; |
| 3200 | dev = MKDEV(major, minor); |
| 3201 | if (major != MAJOR(dev) || |
| 3202 | minor != MINOR(dev)) |
| 3203 | return -EOVERFLOW; |
| 3204 | |
| 3205 | |
| 3206 | if (mddev->persistent) { |
| 3207 | rdev = md_import_device(dev, mddev->major_version, |
| 3208 | mddev->minor_version); |
| 3209 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3210 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3211 | mdk_rdev_t, same_set); |
| 3212 | err = super_types[mddev->major_version] |
| 3213 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3214 | if (err < 0) |
| 3215 | goto out; |
| 3216 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3217 | } else if (mddev->external) |
| 3218 | rdev = md_import_device(dev, -2, -1); |
| 3219 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3220 | rdev = md_import_device(dev, -1, -1); |
| 3221 | |
| 3222 | if (IS_ERR(rdev)) |
| 3223 | return PTR_ERR(rdev); |
| 3224 | err = bind_rdev_to_array(rdev, mddev); |
| 3225 | out: |
| 3226 | if (err) |
| 3227 | export_rdev(rdev); |
| 3228 | return err ? err : len; |
| 3229 | } |
| 3230 | |
| 3231 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3232 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3233 | |
| 3234 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3235 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3236 | { |
| 3237 | char *end; |
| 3238 | unsigned long chunk, end_chunk; |
| 3239 | |
| 3240 | if (!mddev->bitmap) |
| 3241 | goto out; |
| 3242 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3243 | while (*buf) { |
| 3244 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3245 | if (buf == end) break; |
| 3246 | if (*end == '-') { /* range */ |
| 3247 | buf = end + 1; |
| 3248 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3249 | if (buf == end) break; |
| 3250 | } |
| 3251 | if (*end && !isspace(*end)) break; |
| 3252 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3253 | buf = end; |
| 3254 | while (isspace(*buf)) buf++; |
| 3255 | } |
| 3256 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3257 | out: |
| 3258 | return len; |
| 3259 | } |
| 3260 | |
| 3261 | static struct md_sysfs_entry md_bitmap = |
| 3262 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3263 | |
| 3264 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3265 | size_show(mddev_t *mddev, char *page) |
| 3266 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3267 | return sprintf(page, "%llu\n", |
| 3268 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3269 | } |
| 3270 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3271 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3272 | |
| 3273 | static ssize_t |
| 3274 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3275 | { |
| 3276 | /* If array is inactive, we can reduce the component size, but |
| 3277 | * not increase it (except from 0). |
| 3278 | * If array is active, we can try an on-line resize |
| 3279 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3280 | sector_t sectors; |
| 3281 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3282 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3283 | if (err < 0) |
| 3284 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3285 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3286 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3287 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3288 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3289 | if (mddev->dev_sectors == 0 || |
| 3290 | mddev->dev_sectors > sectors) |
| 3291 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3292 | else |
| 3293 | err = -ENOSPC; |
| 3294 | } |
| 3295 | return err ? err : len; |
| 3296 | } |
| 3297 | |
| 3298 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3299 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3300 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3301 | |
| 3302 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3303 | * This is one of |
| 3304 | * 'none' for arrays with no metadata (good luck...) |
| 3305 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3306 | * or N.M for internally known formats |
| 3307 | */ |
| 3308 | static ssize_t |
| 3309 | metadata_show(mddev_t *mddev, char *page) |
| 3310 | { |
| 3311 | if (mddev->persistent) |
| 3312 | return sprintf(page, "%d.%d\n", |
| 3313 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3314 | else if (mddev->external) |
| 3315 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3316 | else |
| 3317 | return sprintf(page, "none\n"); |
| 3318 | } |
| 3319 | |
| 3320 | static ssize_t |
| 3321 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3322 | { |
| 3323 | int major, minor; |
| 3324 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3325 | /* Changing the details of 'external' metadata is |
| 3326 | * always permitted. Otherwise there must be |
| 3327 | * no devices attached to the array. |
| 3328 | */ |
| 3329 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3330 | ; |
| 3331 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3332 | return -EBUSY; |
| 3333 | |
| 3334 | if (cmd_match(buf, "none")) { |
| 3335 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3336 | mddev->external = 0; |
| 3337 | mddev->major_version = 0; |
| 3338 | mddev->minor_version = 90; |
| 3339 | return len; |
| 3340 | } |
| 3341 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3342 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3343 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3344 | namelen = sizeof(mddev->metadata_type)-1; |
| 3345 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3346 | mddev->metadata_type[namelen] = 0; |
| 3347 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3348 | mddev->metadata_type[--namelen] = 0; |
| 3349 | mddev->persistent = 0; |
| 3350 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3351 | mddev->major_version = 0; |
| 3352 | mddev->minor_version = 90; |
| 3353 | return len; |
| 3354 | } |
| 3355 | major = simple_strtoul(buf, &e, 10); |
| 3356 | if (e==buf || *e != '.') |
| 3357 | return -EINVAL; |
| 3358 | buf = e+1; |
| 3359 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3360 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3361 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3362 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3363 | return -ENOENT; |
| 3364 | mddev->major_version = major; |
| 3365 | mddev->minor_version = minor; |
| 3366 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3367 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3368 | return len; |
| 3369 | } |
| 3370 | |
| 3371 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3372 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3373 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3374 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3375 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3376 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3377 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3378 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3379 | type = "frozen"; |
| 3380 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3381 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3382 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3383 | type = "reshape"; |
| 3384 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3385 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3386 | type = "resync"; |
| 3387 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3388 | type = "check"; |
| 3389 | else |
| 3390 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3391 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3392 | type = "recover"; |
| 3393 | } |
| 3394 | return sprintf(page, "%s\n", type); |
| 3395 | } |
| 3396 | |
| 3397 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3398 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3399 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3400 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3401 | return -EINVAL; |
| 3402 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3403 | if (cmd_match(page, "frozen")) |
| 3404 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3405 | else |
| 3406 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3407 | |
| 3408 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3409 | if (mddev->sync_thread) { |
| 3410 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3411 | md_unregister_thread(mddev->sync_thread); |
| 3412 | mddev->sync_thread = NULL; |
| 3413 | mddev->recovery = 0; |
| 3414 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3415 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3416 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3417 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3418 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3419 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3420 | else if (cmd_match(page, "recover")) { |
| 3421 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3422 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3423 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3424 | int err; |
| 3425 | if (mddev->pers->start_reshape == NULL) |
| 3426 | return -EINVAL; |
| 3427 | err = mddev->pers->start_reshape(mddev); |
| 3428 | if (err) |
| 3429 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3430 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3431 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3432 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3433 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3434 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3435 | return -EINVAL; |
| 3436 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3437 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3438 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3439 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3440 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3441 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3442 | return len; |
| 3443 | } |
| 3444 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3445 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3446 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3447 | { |
| 3448 | return sprintf(page, "%llu\n", |
| 3449 | (unsigned long long) mddev->resync_mismatches); |
| 3450 | } |
| 3451 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3452 | static struct md_sysfs_entry md_scan_mode = |
| 3453 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3454 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3455 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3456 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3457 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3458 | static ssize_t |
| 3459 | sync_min_show(mddev_t *mddev, char *page) |
| 3460 | { |
| 3461 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3462 | mddev->sync_speed_min ? "local": "system"); |
| 3463 | } |
| 3464 | |
| 3465 | static ssize_t |
| 3466 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3467 | { |
| 3468 | int min; |
| 3469 | char *e; |
| 3470 | if (strncmp(buf, "system", 6)==0) { |
| 3471 | mddev->sync_speed_min = 0; |
| 3472 | return len; |
| 3473 | } |
| 3474 | min = simple_strtoul(buf, &e, 10); |
| 3475 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3476 | return -EINVAL; |
| 3477 | mddev->sync_speed_min = min; |
| 3478 | return len; |
| 3479 | } |
| 3480 | |
| 3481 | static struct md_sysfs_entry md_sync_min = |
| 3482 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3483 | |
| 3484 | static ssize_t |
| 3485 | sync_max_show(mddev_t *mddev, char *page) |
| 3486 | { |
| 3487 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3488 | mddev->sync_speed_max ? "local": "system"); |
| 3489 | } |
| 3490 | |
| 3491 | static ssize_t |
| 3492 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3493 | { |
| 3494 | int max; |
| 3495 | char *e; |
| 3496 | if (strncmp(buf, "system", 6)==0) { |
| 3497 | mddev->sync_speed_max = 0; |
| 3498 | return len; |
| 3499 | } |
| 3500 | max = simple_strtoul(buf, &e, 10); |
| 3501 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3502 | return -EINVAL; |
| 3503 | mddev->sync_speed_max = max; |
| 3504 | return len; |
| 3505 | } |
| 3506 | |
| 3507 | static struct md_sysfs_entry md_sync_max = |
| 3508 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3509 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3510 | static ssize_t |
| 3511 | degraded_show(mddev_t *mddev, char *page) |
| 3512 | { |
| 3513 | return sprintf(page, "%d\n", mddev->degraded); |
| 3514 | } |
| 3515 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3516 | |
| 3517 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3518 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3519 | { |
| 3520 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3521 | } |
| 3522 | |
| 3523 | static ssize_t |
| 3524 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3525 | { |
| 3526 | long n; |
| 3527 | |
| 3528 | if (strict_strtol(buf, 10, &n)) |
| 3529 | return -EINVAL; |
| 3530 | |
| 3531 | if (n != 0 && n != 1) |
| 3532 | return -EINVAL; |
| 3533 | |
| 3534 | mddev->parallel_resync = n; |
| 3535 | |
| 3536 | if (mddev->sync_thread) |
| 3537 | wake_up(&resync_wait); |
| 3538 | |
| 3539 | return len; |
| 3540 | } |
| 3541 | |
| 3542 | /* force parallel resync, even with shared block devices */ |
| 3543 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3544 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3545 | sync_force_parallel_show, sync_force_parallel_store); |
| 3546 | |
| 3547 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3548 | sync_speed_show(mddev_t *mddev, char *page) |
| 3549 | { |
| 3550 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3551 | if (mddev->curr_resync == 0) |
| 3552 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3553 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3554 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3555 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3556 | db = resync - mddev->resync_mark_cnt; |
| 3557 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3558 | } |
| 3559 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3560 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3561 | |
| 3562 | static ssize_t |
| 3563 | sync_completed_show(mddev_t *mddev, char *page) |
| 3564 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3565 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3566 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3567 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3568 | return sprintf(page, "none\n"); |
| 3569 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3570 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3571 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3572 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3573 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3574 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3575 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3576 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3577 | } |
| 3578 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3579 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3580 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3581 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3582 | min_sync_show(mddev_t *mddev, char *page) |
| 3583 | { |
| 3584 | return sprintf(page, "%llu\n", |
| 3585 | (unsigned long long)mddev->resync_min); |
| 3586 | } |
| 3587 | static ssize_t |
| 3588 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3589 | { |
| 3590 | unsigned long long min; |
| 3591 | if (strict_strtoull(buf, 10, &min)) |
| 3592 | return -EINVAL; |
| 3593 | if (min > mddev->resync_max) |
| 3594 | return -EINVAL; |
| 3595 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3596 | return -EBUSY; |
| 3597 | |
| 3598 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3599 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3600 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3601 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3602 | return -EINVAL; |
| 3603 | } |
| 3604 | mddev->resync_min = min; |
| 3605 | |
| 3606 | return len; |
| 3607 | } |
| 3608 | |
| 3609 | static struct md_sysfs_entry md_min_sync = |
| 3610 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3611 | |
| 3612 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3613 | max_sync_show(mddev_t *mddev, char *page) |
| 3614 | { |
| 3615 | if (mddev->resync_max == MaxSector) |
| 3616 | return sprintf(page, "max\n"); |
| 3617 | else |
| 3618 | return sprintf(page, "%llu\n", |
| 3619 | (unsigned long long)mddev->resync_max); |
| 3620 | } |
| 3621 | static ssize_t |
| 3622 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3623 | { |
| 3624 | if (strncmp(buf, "max", 3) == 0) |
| 3625 | mddev->resync_max = MaxSector; |
| 3626 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3627 | unsigned long long max; |
| 3628 | if (strict_strtoull(buf, 10, &max)) |
| 3629 | return -EINVAL; |
| 3630 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3631 | return -EINVAL; |
| 3632 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3633 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3634 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3635 | return -EBUSY; |
| 3636 | |
| 3637 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3638 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3639 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3640 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3641 | return -EINVAL; |
| 3642 | } |
| 3643 | mddev->resync_max = max; |
| 3644 | } |
| 3645 | wake_up(&mddev->recovery_wait); |
| 3646 | return len; |
| 3647 | } |
| 3648 | |
| 3649 | static struct md_sysfs_entry md_max_sync = |
| 3650 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3651 | |
| 3652 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3653 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3654 | { |
| 3655 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3656 | } |
| 3657 | |
| 3658 | static ssize_t |
| 3659 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3660 | { |
| 3661 | char *e; |
| 3662 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3663 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3664 | if (mddev->pers == NULL || |
| 3665 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3666 | return -EINVAL; |
| 3667 | if (buf == e || (*e && *e != '\n')) |
| 3668 | return -EINVAL; |
| 3669 | if (new >= mddev->suspend_hi || |
| 3670 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3671 | mddev->suspend_lo = new; |
| 3672 | mddev->pers->quiesce(mddev, 2); |
| 3673 | return len; |
| 3674 | } else |
| 3675 | return -EINVAL; |
| 3676 | } |
| 3677 | static struct md_sysfs_entry md_suspend_lo = |
| 3678 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3679 | |
| 3680 | |
| 3681 | static ssize_t |
| 3682 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3683 | { |
| 3684 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3685 | } |
| 3686 | |
| 3687 | static ssize_t |
| 3688 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3689 | { |
| 3690 | char *e; |
| 3691 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3692 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3693 | if (mddev->pers == NULL || |
| 3694 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3695 | return -EINVAL; |
| 3696 | if (buf == e || (*e && *e != '\n')) |
| 3697 | return -EINVAL; |
| 3698 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3699 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3700 | mddev->suspend_hi = new; |
| 3701 | mddev->pers->quiesce(mddev, 1); |
| 3702 | mddev->pers->quiesce(mddev, 0); |
| 3703 | return len; |
| 3704 | } else |
| 3705 | return -EINVAL; |
| 3706 | } |
| 3707 | static struct md_sysfs_entry md_suspend_hi = |
| 3708 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3709 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3710 | static ssize_t |
| 3711 | reshape_position_show(mddev_t *mddev, char *page) |
| 3712 | { |
| 3713 | if (mddev->reshape_position != MaxSector) |
| 3714 | return sprintf(page, "%llu\n", |
| 3715 | (unsigned long long)mddev->reshape_position); |
| 3716 | strcpy(page, "none\n"); |
| 3717 | return 5; |
| 3718 | } |
| 3719 | |
| 3720 | static ssize_t |
| 3721 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3722 | { |
| 3723 | char *e; |
| 3724 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3725 | if (mddev->pers) |
| 3726 | return -EBUSY; |
| 3727 | if (buf == e || (*e && *e != '\n')) |
| 3728 | return -EINVAL; |
| 3729 | mddev->reshape_position = new; |
| 3730 | mddev->delta_disks = 0; |
| 3731 | mddev->new_level = mddev->level; |
| 3732 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3733 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3734 | return len; |
| 3735 | } |
| 3736 | |
| 3737 | static struct md_sysfs_entry md_reshape_position = |
| 3738 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3739 | reshape_position_store); |
| 3740 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3741 | static ssize_t |
| 3742 | array_size_show(mddev_t *mddev, char *page) |
| 3743 | { |
| 3744 | if (mddev->external_size) |
| 3745 | return sprintf(page, "%llu\n", |
| 3746 | (unsigned long long)mddev->array_sectors/2); |
| 3747 | else |
| 3748 | return sprintf(page, "default\n"); |
| 3749 | } |
| 3750 | |
| 3751 | static ssize_t |
| 3752 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3753 | { |
| 3754 | sector_t sectors; |
| 3755 | |
| 3756 | if (strncmp(buf, "default", 7) == 0) { |
| 3757 | if (mddev->pers) |
| 3758 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3759 | else |
| 3760 | sectors = mddev->array_sectors; |
| 3761 | |
| 3762 | mddev->external_size = 0; |
| 3763 | } else { |
| 3764 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3765 | return -EINVAL; |
| 3766 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3767 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3768 | |
| 3769 | mddev->external_size = 1; |
| 3770 | } |
| 3771 | |
| 3772 | mddev->array_sectors = sectors; |
| 3773 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3774 | if (mddev->pers) |
| 3775 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3776 | |
| 3777 | return len; |
| 3778 | } |
| 3779 | |
| 3780 | static struct md_sysfs_entry md_array_size = |
| 3781 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3782 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3783 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3784 | static struct attribute *md_default_attrs[] = { |
| 3785 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3786 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3787 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3788 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3789 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3790 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3791 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3792 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3793 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3794 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3795 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3796 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3797 | NULL, |
| 3798 | }; |
| 3799 | |
| 3800 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3801 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3802 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3803 | &md_sync_min.attr, |
| 3804 | &md_sync_max.attr, |
| 3805 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3806 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3807 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3808 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3809 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3810 | &md_suspend_lo.attr, |
| 3811 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3812 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3813 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3814 | NULL, |
| 3815 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3816 | static struct attribute_group md_redundancy_group = { |
| 3817 | .name = NULL, |
| 3818 | .attrs = md_redundancy_attrs, |
| 3819 | }; |
| 3820 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3821 | |
| 3822 | static ssize_t |
| 3823 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3824 | { |
| 3825 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3826 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3827 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3828 | |
| 3829 | if (!entry->show) |
| 3830 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3831 | rv = mddev_lock(mddev); |
| 3832 | if (!rv) { |
| 3833 | rv = entry->show(mddev, page); |
| 3834 | mddev_unlock(mddev); |
| 3835 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3836 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | static ssize_t |
| 3840 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3841 | const char *page, size_t length) |
| 3842 | { |
| 3843 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3844 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3845 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3846 | |
| 3847 | if (!entry->store) |
| 3848 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3849 | if (!capable(CAP_SYS_ADMIN)) |
| 3850 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3851 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3852 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3853 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3854 | if (!rv) { |
| 3855 | rv = entry->store(mddev, page, length); |
| 3856 | mddev_unlock(mddev); |
| 3857 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3858 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3859 | } |
| 3860 | |
| 3861 | static void md_free(struct kobject *ko) |
| 3862 | { |
| 3863 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3864 | |
| 3865 | if (mddev->sysfs_state) |
| 3866 | sysfs_put(mddev->sysfs_state); |
| 3867 | |
| 3868 | if (mddev->gendisk) { |
| 3869 | del_gendisk(mddev->gendisk); |
| 3870 | put_disk(mddev->gendisk); |
| 3871 | } |
| 3872 | if (mddev->queue) |
| 3873 | blk_cleanup_queue(mddev->queue); |
| 3874 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3875 | kfree(mddev); |
| 3876 | } |
| 3877 | |
| 3878 | static struct sysfs_ops md_sysfs_ops = { |
| 3879 | .show = md_attr_show, |
| 3880 | .store = md_attr_store, |
| 3881 | }; |
| 3882 | static struct kobj_type md_ktype = { |
| 3883 | .release = md_free, |
| 3884 | .sysfs_ops = &md_sysfs_ops, |
| 3885 | .default_attrs = md_default_attrs, |
| 3886 | }; |
| 3887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | int mdp_major = 0; |
| 3889 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3890 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3891 | { |
| 3892 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3893 | |
| 3894 | if (mddev->private == &md_redundancy_group) { |
| 3895 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3896 | if (mddev->sysfs_action) |
| 3897 | sysfs_put(mddev->sysfs_action); |
| 3898 | mddev->sysfs_action = NULL; |
| 3899 | mddev->private = NULL; |
| 3900 | } |
| 3901 | kobject_del(&mddev->kobj); |
| 3902 | kobject_put(&mddev->kobj); |
| 3903 | } |
| 3904 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3905 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3907 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | mddev_t *mddev = mddev_find(dev); |
| 3909 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3910 | int partitioned; |
| 3911 | int shift; |
| 3912 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3913 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | |
| 3915 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3916 | return -ENODEV; |
| 3917 | |
| 3918 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3919 | shift = partitioned ? MdpMinorShift : 0; |
| 3920 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3921 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3922 | /* wait for any previous instance if this device |
| 3923 | * to be completed removed (mddev_delayed_delete). |
| 3924 | */ |
| 3925 | flush_scheduled_work(); |
| 3926 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3927 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3928 | error = -EEXIST; |
| 3929 | if (mddev->gendisk) |
| 3930 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3931 | |
| 3932 | if (name) { |
| 3933 | /* Need to ensure that 'name' is not a duplicate. |
| 3934 | */ |
| 3935 | mddev_t *mddev2; |
| 3936 | spin_lock(&all_mddevs_lock); |
| 3937 | |
| 3938 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3939 | if (mddev2->gendisk && |
| 3940 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3941 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3942 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3943 | } |
| 3944 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3946 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3947 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3948 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3949 | if (!mddev->queue) |
| 3950 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3951 | mddev->queue->queuedata = mddev; |
| 3952 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3953 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3954 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3955 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3956 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | disk = alloc_disk(1 << shift); |
| 3959 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3960 | blk_cleanup_queue(mddev->queue); |
| 3961 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3962 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3964 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3966 | if (name) |
| 3967 | strcpy(disk->disk_name, name); |
| 3968 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3970 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | disk->fops = &md_fops; |
| 3973 | disk->private_data = mddev; |
| 3974 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3975 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3976 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3977 | * remove it now. |
| 3978 | */ |
| 3979 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3980 | add_disk(disk); |
| 3981 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3982 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3983 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3984 | if (error) { |
| 3985 | /* This isn't possible, but as kobject_init_and_add is marked |
| 3986 | * __must_check, we must do something with the result |
| 3987 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3988 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3989 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3990 | error = 0; |
| 3991 | } |
| 3992 | abort: |
| 3993 | mutex_unlock(&disks_mutex); |
| 3994 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3995 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3996 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3997 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3998 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3999 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4003 | { |
| 4004 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | return NULL; |
| 4006 | } |
| 4007 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4008 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4009 | { |
| 4010 | /* val must be "md_*" where * is not all digits. |
| 4011 | * We allocate an array with a large free minor number, and |
| 4012 | * set the name to val. val must not already be an active name. |
| 4013 | */ |
| 4014 | int len = strlen(val); |
| 4015 | char buf[DISK_NAME_LEN]; |
| 4016 | |
| 4017 | while (len && val[len-1] == '\n') |
| 4018 | len--; |
| 4019 | if (len >= DISK_NAME_LEN) |
| 4020 | return -E2BIG; |
| 4021 | strlcpy(buf, val, len+1); |
| 4022 | if (strncmp(buf, "md_", 3) != 0) |
| 4023 | return -EINVAL; |
| 4024 | return md_alloc(0, buf); |
| 4025 | } |
| 4026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | static void md_safemode_timeout(unsigned long data) |
| 4028 | { |
| 4029 | mddev_t *mddev = (mddev_t *) data; |
| 4030 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4031 | if (!atomic_read(&mddev->writes_pending)) { |
| 4032 | mddev->safemode = 1; |
| 4033 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4034 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4036 | md_wakeup_thread(mddev->thread); |
| 4037 | } |
| 4038 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4039 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | |
| 4041 | static int do_md_run(mddev_t * mddev) |
| 4042 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4043 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | mdk_rdev_t *rdev; |
| 4045 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4046 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4048 | if (list_empty(&mddev->disks)) |
| 4049 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | |
| 4052 | if (mddev->pers) |
| 4053 | return -EBUSY; |
| 4054 | |
| 4055 | /* |
| 4056 | * Analyze all RAID superblock(s) |
| 4057 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4058 | if (!mddev->raid_disks) { |
| 4059 | if (!mddev->persistent) |
| 4060 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4061 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4062 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4064 | if (mddev->level != LEVEL_NONE) |
| 4065 | request_module("md-level-%d", mddev->level); |
| 4066 | else if (mddev->clevel[0]) |
| 4067 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | |
| 4069 | /* |
| 4070 | * Drop all container device buffers, from now on |
| 4071 | * the only valid external interface is through the md |
| 4072 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4073 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4074 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4075 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | continue; |
| 4077 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4078 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4079 | |
| 4080 | /* perform some consistency tests on the device. |
| 4081 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4082 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4083 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4084 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4085 | if (mddev->dev_sectors && |
| 4086 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4087 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4088 | printk("md: %s: data overlaps metadata\n", |
| 4089 | mdname(mddev)); |
| 4090 | return -EINVAL; |
| 4091 | } |
| 4092 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4093 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4094 | > rdev->data_offset) { |
| 4095 | printk("md: %s: metadata overlaps data\n", |
| 4096 | mdname(mddev)); |
| 4097 | return -EINVAL; |
| 4098 | } |
| 4099 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4100 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | md_probe(mddev->unit, NULL, NULL); |
| 4104 | disk = mddev->gendisk; |
| 4105 | if (!disk) |
| 4106 | return -ENOMEM; |
| 4107 | |
| 4108 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4109 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4110 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4112 | if (mddev->level != LEVEL_NONE) |
| 4113 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4114 | mddev->level); |
| 4115 | else |
| 4116 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4117 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4118 | return -EINVAL; |
| 4119 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4120 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4122 | if (mddev->level != pers->level) { |
| 4123 | mddev->level = pers->level; |
| 4124 | mddev->new_level = pers->level; |
| 4125 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4126 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4128 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4129 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4130 | /* This personality cannot handle reshaping... */ |
| 4131 | mddev->pers = NULL; |
| 4132 | module_put(pers->owner); |
| 4133 | return -EINVAL; |
| 4134 | } |
| 4135 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4136 | if (pers->sync_request) { |
| 4137 | /* Warn if this is a potentially silly |
| 4138 | * configuration. |
| 4139 | */ |
| 4140 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4141 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4142 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4143 | |
| 4144 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4145 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4146 | if (rdev < rdev2 && |
| 4147 | rdev->bdev->bd_contains == |
| 4148 | rdev2->bdev->bd_contains) { |
| 4149 | printk(KERN_WARNING |
| 4150 | "%s: WARNING: %s appears to be" |
| 4151 | " on the same physical disk as" |
| 4152 | " %s.\n", |
| 4153 | mdname(mddev), |
| 4154 | bdevname(rdev->bdev,b), |
| 4155 | bdevname(rdev2->bdev,b2)); |
| 4156 | warned = 1; |
| 4157 | } |
| 4158 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4159 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4160 | if (warned) |
| 4161 | printk(KERN_WARNING |
| 4162 | "True protection against single-disk" |
| 4163 | " failure might be compromised.\n"); |
| 4164 | } |
| 4165 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4166 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4167 | /* may be over-ridden by personality */ |
| 4168 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4169 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4170 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4171 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4173 | if (start_readonly) |
| 4174 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4175 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4176 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4177 | if (err) |
| 4178 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4179 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4180 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4181 | " but 'external_size' not in effect?\n", __func__); |
| 4182 | printk(KERN_ERR |
| 4183 | "md: invalid array_size %llu > default size %llu\n", |
| 4184 | (unsigned long long)mddev->array_sectors / 2, |
| 4185 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4186 | err = -EINVAL; |
| 4187 | mddev->pers->stop(mddev); |
| 4188 | } |
| 4189 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4190 | err = bitmap_create(mddev); |
| 4191 | if (err) { |
| 4192 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4193 | mdname(mddev), err); |
| 4194 | mddev->pers->stop(mddev); |
| 4195 | } |
| 4196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | module_put(mddev->pers->owner); |
| 4199 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4200 | bitmap_destroy(mddev); |
| 4201 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4203 | if (mddev->pers->sync_request) { |
| 4204 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4205 | printk(KERN_WARNING |
| 4206 | "md: cannot register extra attributes for %s\n", |
| 4207 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4208 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4209 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4210 | mddev->ro = 0; |
| 4211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | atomic_set(&mddev->writes_pending,0); |
| 4213 | mddev->safemode = 0; |
| 4214 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4215 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4216 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4218 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4219 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4220 | if (rdev->raid_disk >= 0) { |
| 4221 | char nm[20]; |
| 4222 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4223 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4224 | printk("md: cannot register %s for %s\n", |
| 4225 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | |
| 4228 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4229 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4230 | if (mddev->flags) |
| 4231 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4233 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4235 | /* If there is a partially-recovered drive we need to |
| 4236 | * start recovery here. If we leave it to md_check_recovery, |
| 4237 | * it will remove the drives and not do the right thing |
| 4238 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4239 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4240 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4241 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4242 | if (rdev->raid_disk >= 0 && |
| 4243 | !test_bit(In_sync, &rdev->flags) && |
| 4244 | !test_bit(Faulty, &rdev->flags)) |
| 4245 | /* complete an interrupted recovery */ |
| 4246 | spares++; |
| 4247 | if (spares && mddev->pers->sync_request) { |
| 4248 | mddev->recovery = 0; |
| 4249 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4250 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4251 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4252 | "resync"); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4253 | if (!mddev->sync_thread) { |
| 4254 | printk(KERN_ERR "%s: could not start resync" |
| 4255 | " thread...\n", |
| 4256 | mdname(mddev)); |
| 4257 | /* leave the spares where they are, it shouldn't hurt */ |
| 4258 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4259 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4260 | } |
| 4261 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4262 | md_wakeup_thread(mddev->thread); |
| 4263 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4264 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4265 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4266 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4267 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4268 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4269 | if (mddev->sysfs_action) |
| 4270 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4271 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4272 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | return 0; |
| 4274 | } |
| 4275 | |
| 4276 | static int restart_array(mddev_t *mddev) |
| 4277 | { |
| 4278 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4280 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4282 | return -ENXIO; |
| 4283 | if (!mddev->pers) |
| 4284 | return -EINVAL; |
| 4285 | if (!mddev->ro) |
| 4286 | return -EBUSY; |
| 4287 | mddev->safemode = 0; |
| 4288 | mddev->ro = 0; |
| 4289 | set_disk_ro(disk, 0); |
| 4290 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4291 | mdname(mddev)); |
| 4292 | /* Kick recovery or resync if necessary */ |
| 4293 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4294 | md_wakeup_thread(mddev->thread); |
| 4295 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4296 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4297 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | } |
| 4299 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4300 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4301 | * to the file ourselves */ |
| 4302 | static int deny_bitmap_write_access(struct file * file) |
| 4303 | { |
| 4304 | struct inode *inode = file->f_mapping->host; |
| 4305 | |
| 4306 | spin_lock(&inode->i_lock); |
| 4307 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4308 | spin_unlock(&inode->i_lock); |
| 4309 | return -ETXTBSY; |
| 4310 | } |
| 4311 | atomic_set(&inode->i_writecount, -1); |
| 4312 | spin_unlock(&inode->i_lock); |
| 4313 | |
| 4314 | return 0; |
| 4315 | } |
| 4316 | |
| 4317 | static void restore_bitmap_write_access(struct file *file) |
| 4318 | { |
| 4319 | struct inode *inode = file->f_mapping->host; |
| 4320 | |
| 4321 | spin_lock(&inode->i_lock); |
| 4322 | atomic_set(&inode->i_writecount, 1); |
| 4323 | spin_unlock(&inode->i_lock); |
| 4324 | } |
| 4325 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4326 | /* mode: |
| 4327 | * 0 - completely stop and dis-assemble array |
| 4328 | * 1 - switch to readonly |
| 4329 | * 2 - stop but do not disassemble array |
| 4330 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4331 | 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] | 4332 | { |
| 4333 | int err = 0; |
| 4334 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4335 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4337 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4338 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4339 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4340 | err = -EBUSY; |
| 4341 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | |
| 4343 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4344 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4346 | md_unregister_thread(mddev->sync_thread); |
| 4347 | mddev->sync_thread = NULL; |
| 4348 | } |
| 4349 | |
| 4350 | del_timer_sync(&mddev->safemode_timer); |
| 4351 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4352 | switch(mode) { |
| 4353 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4355 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | goto out; |
| 4357 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4358 | break; |
| 4359 | case 0: /* disassemble */ |
| 4360 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4361 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4362 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | if (mddev->ro) |
| 4364 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4367 | mddev->queue->merge_bvec_fn = NULL; |
| 4368 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4369 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4371 | if (mddev->pers->sync_request) |
| 4372 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4374 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4375 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4376 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4377 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4378 | if (rdev->raid_disk >= 0) { |
| 4379 | char nm[20]; |
| 4380 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4381 | sysfs_remove_link(&mddev->kobj, nm); |
| 4382 | } |
| 4383 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4384 | set_capacity(disk, 0); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4385 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | if (mddev->ro) |
| 4388 | mddev->ro = 0; |
| 4389 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4390 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4391 | /* mark array as shutdown cleanly */ |
| 4392 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4393 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4395 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4397 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4398 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4400 | out: |
| 4401 | mutex_unlock(&mddev->open_mutex); |
| 4402 | if (err) |
| 4403 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | /* |
| 4405 | * Free resources if final stop |
| 4406 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4407 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4410 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4411 | bitmap_destroy(mddev); |
| 4412 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4413 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4414 | fput(mddev->bitmap_file); |
| 4415 | mddev->bitmap_file = NULL; |
| 4416 | } |
| 4417 | mddev->bitmap_offset = 0; |
| 4418 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4419 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4420 | flush_scheduled_work(); |
| 4421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | export_array(mddev); |
| 4423 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4424 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4425 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4426 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4427 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4428 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4429 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4430 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4431 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4432 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4433 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4434 | mddev->level = LEVEL_NONE; |
| 4435 | mddev->clevel[0] = 0; |
| 4436 | mddev->flags = 0; |
| 4437 | mddev->ro = 0; |
| 4438 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4439 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4440 | mddev->ctime = mddev->utime = 0; |
| 4441 | mddev->layout = 0; |
| 4442 | mddev->max_disks = 0; |
| 4443 | mddev->events = 0; |
| 4444 | mddev->delta_disks = 0; |
| 4445 | mddev->new_level = LEVEL_NONE; |
| 4446 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4447 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4448 | mddev->curr_resync = 0; |
| 4449 | mddev->resync_mismatches = 0; |
| 4450 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4451 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4452 | mddev->recovery = 0; |
| 4453 | mddev->in_sync = 0; |
| 4454 | mddev->changed = 0; |
| 4455 | mddev->degraded = 0; |
| 4456 | mddev->barriers_work = 0; |
| 4457 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4458 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4459 | if (mddev->hold_active == UNTIL_STOP) |
| 4460 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4461 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4462 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4464 | mdname(mddev)); |
| 4465 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4466 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4467 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4468 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4469 | return err; |
| 4470 | } |
| 4471 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4472 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | static void autorun_array(mddev_t *mddev) |
| 4474 | { |
| 4475 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | int err; |
| 4477 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4478 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | |
| 4481 | printk(KERN_INFO "md: running: "); |
| 4482 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4483 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | char b[BDEVNAME_SIZE]; |
| 4485 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4486 | } |
| 4487 | printk("\n"); |
| 4488 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4489 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | if (err) { |
| 4491 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4492 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | } |
| 4494 | } |
| 4495 | |
| 4496 | /* |
| 4497 | * lets try to run arrays based on all disks that have arrived |
| 4498 | * until now. (those are in pending_raid_disks) |
| 4499 | * |
| 4500 | * the method: pick the first pending disk, collect all disks with |
| 4501 | * the same UUID, remove all from the pending list and put them into |
| 4502 | * the 'same_array' list. Then order this list based on superblock |
| 4503 | * update time (freshest comes first), kick out 'old' disks and |
| 4504 | * compare superblocks. If everything's fine then run it. |
| 4505 | * |
| 4506 | * If "unit" is allocated, then bump its reference count |
| 4507 | */ |
| 4508 | static void autorun_devices(int part) |
| 4509 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4510 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4511 | mddev_t *mddev; |
| 4512 | char b[BDEVNAME_SIZE]; |
| 4513 | |
| 4514 | printk(KERN_INFO "md: autorun ...\n"); |
| 4515 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4516 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4518 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | rdev0 = list_entry(pending_raid_disks.next, |
| 4520 | mdk_rdev_t, same_set); |
| 4521 | |
| 4522 | printk(KERN_INFO "md: considering %s ...\n", |
| 4523 | bdevname(rdev0->bdev,b)); |
| 4524 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4525 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4527 | printk(KERN_INFO "md: adding %s ...\n", |
| 4528 | bdevname(rdev->bdev,b)); |
| 4529 | list_move(&rdev->same_set, &candidates); |
| 4530 | } |
| 4531 | /* |
| 4532 | * now we have a set of devices, with all of them having |
| 4533 | * mostly sane superblocks. It's time to allocate the |
| 4534 | * mddev. |
| 4535 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4536 | if (part) { |
| 4537 | dev = MKDEV(mdp_major, |
| 4538 | rdev0->preferred_minor << MdpMinorShift); |
| 4539 | unit = MINOR(dev) >> MdpMinorShift; |
| 4540 | } else { |
| 4541 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4542 | unit = MINOR(dev); |
| 4543 | } |
| 4544 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4545 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4546 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4547 | break; |
| 4548 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | |
| 4550 | md_probe(dev, NULL, NULL); |
| 4551 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4552 | if (!mddev || !mddev->gendisk) { |
| 4553 | if (mddev) |
| 4554 | mddev_put(mddev); |
| 4555 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | "md: cannot allocate memory for md drive.\n"); |
| 4557 | break; |
| 4558 | } |
| 4559 | if (mddev_lock(mddev)) |
| 4560 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4561 | mdname(mddev)); |
| 4562 | else if (mddev->raid_disks || mddev->major_version |
| 4563 | || !list_empty(&mddev->disks)) { |
| 4564 | printk(KERN_WARNING |
| 4565 | "md: %s already running, cannot run %s\n", |
| 4566 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4567 | mddev_unlock(mddev); |
| 4568 | } else { |
| 4569 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4570 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4571 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | list_del_init(&rdev->same_set); |
| 4573 | if (bind_rdev_to_array(rdev, mddev)) |
| 4574 | export_rdev(rdev); |
| 4575 | } |
| 4576 | autorun_array(mddev); |
| 4577 | mddev_unlock(mddev); |
| 4578 | } |
| 4579 | /* on success, candidates will be empty, on error |
| 4580 | * it won't... |
| 4581 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4582 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4583 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | mddev_put(mddev); |
| 4587 | } |
| 4588 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4589 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4590 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | static int get_version(void __user * arg) |
| 4593 | { |
| 4594 | mdu_version_t ver; |
| 4595 | |
| 4596 | ver.major = MD_MAJOR_VERSION; |
| 4597 | ver.minor = MD_MINOR_VERSION; |
| 4598 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4599 | |
| 4600 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4601 | return -EFAULT; |
| 4602 | |
| 4603 | return 0; |
| 4604 | } |
| 4605 | |
| 4606 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4607 | { |
| 4608 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4609 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4612 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4613 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4615 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | failed++; |
| 4617 | else { |
| 4618 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4619 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4620 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | else |
| 4622 | spare++; |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | info.major_version = mddev->major_version; |
| 4627 | info.minor_version = mddev->minor_version; |
| 4628 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4629 | info.ctime = mddev->ctime; |
| 4630 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4631 | info.size = mddev->dev_sectors / 2; |
| 4632 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4633 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | info.nr_disks = nr; |
| 4635 | info.raid_disks = mddev->raid_disks; |
| 4636 | info.md_minor = mddev->md_minor; |
| 4637 | info.not_persistent= !mddev->persistent; |
| 4638 | |
| 4639 | info.utime = mddev->utime; |
| 4640 | info.state = 0; |
| 4641 | if (mddev->in_sync) |
| 4642 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4643 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4644 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4645 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | info.working_disks = working; |
| 4647 | info.failed_disks = failed; |
| 4648 | info.spare_disks = spare; |
| 4649 | |
| 4650 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4651 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | |
| 4653 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4654 | return -EFAULT; |
| 4655 | |
| 4656 | return 0; |
| 4657 | } |
| 4658 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4659 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4660 | { |
| 4661 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4662 | char *ptr, *buf = NULL; |
| 4663 | int err = -ENOMEM; |
| 4664 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4665 | if (md_allow_write(mddev)) |
| 4666 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4667 | else |
| 4668 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4669 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4670 | if (!file) |
| 4671 | goto out; |
| 4672 | |
| 4673 | /* bitmap disabled, zero the first byte and copy out */ |
| 4674 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4675 | file->pathname[0] = '\0'; |
| 4676 | goto copy_out; |
| 4677 | } |
| 4678 | |
| 4679 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4680 | if (!buf) |
| 4681 | goto out; |
| 4682 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4683 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4684 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4685 | goto out; |
| 4686 | |
| 4687 | strcpy(file->pathname, ptr); |
| 4688 | |
| 4689 | copy_out: |
| 4690 | err = 0; |
| 4691 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4692 | err = -EFAULT; |
| 4693 | out: |
| 4694 | kfree(buf); |
| 4695 | kfree(file); |
| 4696 | return err; |
| 4697 | } |
| 4698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4700 | { |
| 4701 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | mdk_rdev_t *rdev; |
| 4703 | |
| 4704 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4705 | return -EFAULT; |
| 4706 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4707 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | if (rdev) { |
| 4709 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4710 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4711 | info.raid_disk = rdev->raid_disk; |
| 4712 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4713 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4715 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4717 | info.state |= (1<<MD_DISK_SYNC); |
| 4718 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4719 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4720 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | } else { |
| 4722 | info.major = info.minor = 0; |
| 4723 | info.raid_disk = -1; |
| 4724 | info.state = (1<<MD_DISK_REMOVED); |
| 4725 | } |
| 4726 | |
| 4727 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4728 | return -EFAULT; |
| 4729 | |
| 4730 | return 0; |
| 4731 | } |
| 4732 | |
| 4733 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4734 | { |
| 4735 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4736 | mdk_rdev_t *rdev; |
| 4737 | dev_t dev = MKDEV(info->major,info->minor); |
| 4738 | |
| 4739 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4740 | return -EOVERFLOW; |
| 4741 | |
| 4742 | if (!mddev->raid_disks) { |
| 4743 | int err; |
| 4744 | /* expecting a device which has a superblock */ |
| 4745 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4746 | if (IS_ERR(rdev)) { |
| 4747 | printk(KERN_WARNING |
| 4748 | "md: md_import_device returned %ld\n", |
| 4749 | PTR_ERR(rdev)); |
| 4750 | return PTR_ERR(rdev); |
| 4751 | } |
| 4752 | if (!list_empty(&mddev->disks)) { |
| 4753 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4754 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4755 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4757 | if (err < 0) { |
| 4758 | printk(KERN_WARNING |
| 4759 | "md: %s has different UUID to %s\n", |
| 4760 | bdevname(rdev->bdev,b), |
| 4761 | bdevname(rdev0->bdev,b2)); |
| 4762 | export_rdev(rdev); |
| 4763 | return -EINVAL; |
| 4764 | } |
| 4765 | } |
| 4766 | err = bind_rdev_to_array(rdev, mddev); |
| 4767 | if (err) |
| 4768 | export_rdev(rdev); |
| 4769 | return err; |
| 4770 | } |
| 4771 | |
| 4772 | /* |
| 4773 | * add_new_disk can be used once the array is assembled |
| 4774 | * to add "hot spares". They must already have a superblock |
| 4775 | * written |
| 4776 | */ |
| 4777 | if (mddev->pers) { |
| 4778 | int err; |
| 4779 | if (!mddev->pers->hot_add_disk) { |
| 4780 | printk(KERN_WARNING |
| 4781 | "%s: personality does not support diskops!\n", |
| 4782 | mdname(mddev)); |
| 4783 | return -EINVAL; |
| 4784 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4785 | if (mddev->persistent) |
| 4786 | rdev = md_import_device(dev, mddev->major_version, |
| 4787 | mddev->minor_version); |
| 4788 | else |
| 4789 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | if (IS_ERR(rdev)) { |
| 4791 | printk(KERN_WARNING |
| 4792 | "md: md_import_device returned %ld\n", |
| 4793 | PTR_ERR(rdev)); |
| 4794 | return PTR_ERR(rdev); |
| 4795 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4796 | /* set save_raid_disk if appropriate */ |
| 4797 | if (!mddev->persistent) { |
| 4798 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4799 | info->raid_disk < mddev->raid_disks) |
| 4800 | rdev->raid_disk = info->raid_disk; |
| 4801 | else |
| 4802 | rdev->raid_disk = -1; |
| 4803 | } else |
| 4804 | super_types[mddev->major_version]. |
| 4805 | validate_super(mddev, rdev); |
| 4806 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4807 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4808 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4809 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4810 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4811 | else |
| 4812 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | rdev->raid_disk = -1; |
| 4815 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4816 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4817 | /* If there is hot_add_disk but no hot_remove_disk |
| 4818 | * then added disks for geometry changes, |
| 4819 | * and should be added immediately. |
| 4820 | */ |
| 4821 | super_types[mddev->major_version]. |
| 4822 | validate_super(mddev, rdev); |
| 4823 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4824 | if (err) |
| 4825 | unbind_rdev_from_array(rdev); |
| 4826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | if (err) |
| 4828 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4829 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4830 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4831 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4832 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4833 | if (mddev->degraded) |
| 4834 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4835 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4836 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | return err; |
| 4838 | } |
| 4839 | |
| 4840 | /* otherwise, add_new_disk is only allowed |
| 4841 | * for major_version==0 superblocks |
| 4842 | */ |
| 4843 | if (mddev->major_version != 0) { |
| 4844 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4845 | mdname(mddev)); |
| 4846 | return -EINVAL; |
| 4847 | } |
| 4848 | |
| 4849 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4850 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4851 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | if (IS_ERR(rdev)) { |
| 4853 | printk(KERN_WARNING |
| 4854 | "md: error, md_import_device() returned %ld\n", |
| 4855 | PTR_ERR(rdev)); |
| 4856 | return PTR_ERR(rdev); |
| 4857 | } |
| 4858 | rdev->desc_nr = info->number; |
| 4859 | if (info->raid_disk < mddev->raid_disks) |
| 4860 | rdev->raid_disk = info->raid_disk; |
| 4861 | else |
| 4862 | rdev->raid_disk = -1; |
| 4863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4865 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4866 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4868 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4869 | set_bit(WriteMostly, &rdev->flags); |
| 4870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | if (!mddev->persistent) { |
| 4872 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4873 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4875 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4876 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4878 | err = bind_rdev_to_array(rdev, mddev); |
| 4879 | if (err) { |
| 4880 | export_rdev(rdev); |
| 4881 | return err; |
| 4882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | return 0; |
| 4886 | } |
| 4887 | |
| 4888 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4889 | { |
| 4890 | char b[BDEVNAME_SIZE]; |
| 4891 | mdk_rdev_t *rdev; |
| 4892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | rdev = find_rdev(mddev, dev); |
| 4894 | if (!rdev) |
| 4895 | return -ENXIO; |
| 4896 | |
| 4897 | if (rdev->raid_disk >= 0) |
| 4898 | goto busy; |
| 4899 | |
| 4900 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4901 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4902 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | |
| 4904 | return 0; |
| 4905 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4906 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4908 | return -EBUSY; |
| 4909 | } |
| 4910 | |
| 4911 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4912 | { |
| 4913 | char b[BDEVNAME_SIZE]; |
| 4914 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | mdk_rdev_t *rdev; |
| 4916 | |
| 4917 | if (!mddev->pers) |
| 4918 | return -ENODEV; |
| 4919 | |
| 4920 | if (mddev->major_version != 0) { |
| 4921 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4922 | " version-0 superblocks.\n", |
| 4923 | mdname(mddev)); |
| 4924 | return -EINVAL; |
| 4925 | } |
| 4926 | if (!mddev->pers->hot_add_disk) { |
| 4927 | printk(KERN_WARNING |
| 4928 | "%s: personality does not support diskops!\n", |
| 4929 | mdname(mddev)); |
| 4930 | return -EINVAL; |
| 4931 | } |
| 4932 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4933 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4934 | if (IS_ERR(rdev)) { |
| 4935 | printk(KERN_WARNING |
| 4936 | "md: error, md_import_device() returned %ld\n", |
| 4937 | PTR_ERR(rdev)); |
| 4938 | return -EINVAL; |
| 4939 | } |
| 4940 | |
| 4941 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4942 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4944 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4946 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4947 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4948 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | printk(KERN_WARNING |
| 4950 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4951 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4952 | err = -EINVAL; |
| 4953 | goto abort_export; |
| 4954 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4955 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4957 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4958 | err = bind_rdev_to_array(rdev, mddev); |
| 4959 | if (err) |
| 4960 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | |
| 4962 | /* |
| 4963 | * The rest should better be atomic, we can have disk failures |
| 4964 | * noticed in interrupt contexts ... |
| 4965 | */ |
| 4966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | rdev->raid_disk = -1; |
| 4968 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4969 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | |
| 4971 | /* |
| 4972 | * Kick recovery, maybe this spare has to be added to the |
| 4973 | * array immediately. |
| 4974 | */ |
| 4975 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4976 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4977 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4978 | return 0; |
| 4979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | abort_export: |
| 4981 | export_rdev(rdev); |
| 4982 | return err; |
| 4983 | } |
| 4984 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4985 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4986 | { |
| 4987 | int err; |
| 4988 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4989 | if (mddev->pers) { |
| 4990 | if (!mddev->pers->quiesce) |
| 4991 | return -EBUSY; |
| 4992 | if (mddev->recovery || mddev->sync_thread) |
| 4993 | return -EBUSY; |
| 4994 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4995 | } |
| 4996 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4997 | |
| 4998 | if (fd >= 0) { |
| 4999 | if (mddev->bitmap) |
| 5000 | return -EEXIST; /* cannot add when bitmap is present */ |
| 5001 | mddev->bitmap_file = fget(fd); |
| 5002 | |
| 5003 | if (mddev->bitmap_file == NULL) { |
| 5004 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5005 | mdname(mddev)); |
| 5006 | return -EBADF; |
| 5007 | } |
| 5008 | |
| 5009 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 5010 | if (err) { |
| 5011 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5012 | mdname(mddev)); |
| 5013 | fput(mddev->bitmap_file); |
| 5014 | mddev->bitmap_file = NULL; |
| 5015 | return err; |
| 5016 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 5017 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5018 | } else if (mddev->bitmap == NULL) |
| 5019 | return -ENOENT; /* cannot remove what isn't there */ |
| 5020 | err = 0; |
| 5021 | if (mddev->pers) { |
| 5022 | mddev->pers->quiesce(mddev, 1); |
| 5023 | if (fd >= 0) |
| 5024 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5025 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5026 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5027 | fd = -1; /* make sure to put the file */ |
| 5028 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5029 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5030 | } |
| 5031 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5032 | if (mddev->bitmap_file) { |
| 5033 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5034 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5035 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5036 | mddev->bitmap_file = NULL; |
| 5037 | } |
| 5038 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5039 | return err; |
| 5040 | } |
| 5041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5042 | /* |
| 5043 | * set_array_info is used two different ways |
| 5044 | * The original usage is when creating a new array. |
| 5045 | * In this usage, raid_disks is > 0 and it together with |
| 5046 | * level, size, not_persistent,layout,chunksize determine the |
| 5047 | * shape of the array. |
| 5048 | * This will always create an array with a type-0.90.0 superblock. |
| 5049 | * The newer usage is when assembling an array. |
| 5050 | * In this case raid_disks will be 0, and the major_version field is |
| 5051 | * use to determine which style super-blocks are to be found on the devices. |
| 5052 | * The minor and patch _version numbers are also kept incase the |
| 5053 | * super_block handler wishes to interpret them. |
| 5054 | */ |
| 5055 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5056 | { |
| 5057 | |
| 5058 | if (info->raid_disks == 0) { |
| 5059 | /* just setting version number for superblock loading */ |
| 5060 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5061 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | super_types[info->major_version].name == NULL) { |
| 5063 | /* maybe try to auto-load a module? */ |
| 5064 | printk(KERN_INFO |
| 5065 | "md: superblock version %d not known\n", |
| 5066 | info->major_version); |
| 5067 | return -EINVAL; |
| 5068 | } |
| 5069 | mddev->major_version = info->major_version; |
| 5070 | mddev->minor_version = info->minor_version; |
| 5071 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5072 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | return 0; |
| 5074 | } |
| 5075 | mddev->major_version = MD_MAJOR_VERSION; |
| 5076 | mddev->minor_version = MD_MINOR_VERSION; |
| 5077 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5078 | mddev->ctime = get_seconds(); |
| 5079 | |
| 5080 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5081 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5082 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | mddev->raid_disks = info->raid_disks; |
| 5084 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5085 | * openned |
| 5086 | */ |
| 5087 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5088 | mddev->recovery_cp = MaxSector; |
| 5089 | else |
| 5090 | mddev->recovery_cp = 0; |
| 5091 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5092 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | |
| 5094 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5095 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | |
| 5097 | mddev->max_disks = MD_SB_DISKS; |
| 5098 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5099 | if (mddev->persistent) |
| 5100 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5101 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5103 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 5104 | mddev->bitmap_offset = 0; |
| 5105 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5106 | mddev->reshape_position = MaxSector; |
| 5107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | /* |
| 5109 | * Generate a 128 bit UUID |
| 5110 | */ |
| 5111 | get_random_bytes(mddev->uuid, 16); |
| 5112 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5113 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5114 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5115 | mddev->new_layout = mddev->layout; |
| 5116 | mddev->delta_disks = 0; |
| 5117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | return 0; |
| 5119 | } |
| 5120 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5121 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5122 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5123 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5124 | |
| 5125 | if (mddev->external_size) |
| 5126 | return; |
| 5127 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5128 | mddev->array_sectors = array_sectors; |
| 5129 | } |
| 5130 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5131 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5132 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5133 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5134 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5135 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5136 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5137 | |
| 5138 | if (mddev->pers->resize == NULL) |
| 5139 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5140 | /* The "num_sectors" is the number of sectors of each device that |
| 5141 | * is used. This can only make sense for arrays with redundancy. |
| 5142 | * linear and raid0 always use whatever space is available. We can only |
| 5143 | * consider changing this number if no resync or reconstruction is |
| 5144 | * 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] | 5145 | * 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] | 5146 | * of each device. If num_sectors is zero, we find the largest size |
| 5147 | * that fits. |
| 5148 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5149 | */ |
| 5150 | if (mddev->sync_thread) |
| 5151 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5152 | if (mddev->bitmap) |
| 5153 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5154 | * grow, and re-add. |
| 5155 | */ |
| 5156 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5157 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5158 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5159 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5160 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5161 | num_sectors = avail; |
| 5162 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5163 | return -ENOSPC; |
| 5164 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5165 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5166 | if (!rv) |
| 5167 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5168 | return rv; |
| 5169 | } |
| 5170 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5171 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5172 | { |
| 5173 | int rv; |
| 5174 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5175 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5176 | return -EINVAL; |
| 5177 | if (raid_disks <= 0 || |
| 5178 | raid_disks >= mddev->max_disks) |
| 5179 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5180 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5181 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5182 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5183 | |
| 5184 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5185 | return rv; |
| 5186 | } |
| 5187 | |
| 5188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | /* |
| 5190 | * update_array_info is used to change the configuration of an |
| 5191 | * on-line array. |
| 5192 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5193 | * fields in the info are checked against the array. |
| 5194 | * Any differences that cannot be handled will cause an error. |
| 5195 | * Normally, only one change can be managed at a time. |
| 5196 | */ |
| 5197 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5198 | { |
| 5199 | int rv = 0; |
| 5200 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5201 | int state = 0; |
| 5202 | |
| 5203 | /* calculate expected state,ignoring low bits */ |
| 5204 | if (mddev->bitmap && mddev->bitmap_offset) |
| 5205 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5206 | |
| 5207 | if (mddev->major_version != info->major_version || |
| 5208 | mddev->minor_version != info->minor_version || |
| 5209 | /* mddev->patch_version != info->patch_version || */ |
| 5210 | mddev->ctime != info->ctime || |
| 5211 | mddev->level != info->level || |
| 5212 | /* mddev->layout != info->layout || */ |
| 5213 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5214 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5215 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5216 | ((state^info->state) & 0xfffffe00) |
| 5217 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | return -EINVAL; |
| 5219 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5220 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5221 | cnt++; |
| 5222 | if (mddev->raid_disks != info->raid_disks) |
| 5223 | cnt++; |
| 5224 | if (mddev->layout != info->layout) |
| 5225 | cnt++; |
| 5226 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5227 | cnt++; |
| 5228 | if (cnt == 0) |
| 5229 | return 0; |
| 5230 | if (cnt > 1) |
| 5231 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5232 | |
| 5233 | if (mddev->layout != info->layout) { |
| 5234 | /* Change layout |
| 5235 | * we don't need to do anything at the md level, the |
| 5236 | * personality will take care of it all. |
| 5237 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5238 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5240 | else { |
| 5241 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5242 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5243 | if (rv) |
| 5244 | mddev->new_layout = mddev->layout; |
| 5245 | return rv; |
| 5246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5248 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5249 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5251 | if (mddev->raid_disks != info->raid_disks) |
| 5252 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5254 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5255 | if (mddev->pers->quiesce == NULL) |
| 5256 | return -EINVAL; |
| 5257 | if (mddev->recovery || mddev->sync_thread) |
| 5258 | return -EBUSY; |
| 5259 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5260 | /* add the bitmap */ |
| 5261 | if (mddev->bitmap) |
| 5262 | return -EEXIST; |
| 5263 | if (mddev->default_bitmap_offset == 0) |
| 5264 | return -EINVAL; |
| 5265 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 5266 | mddev->pers->quiesce(mddev, 1); |
| 5267 | rv = bitmap_create(mddev); |
| 5268 | if (rv) |
| 5269 | bitmap_destroy(mddev); |
| 5270 | mddev->pers->quiesce(mddev, 0); |
| 5271 | } else { |
| 5272 | /* remove the bitmap */ |
| 5273 | if (!mddev->bitmap) |
| 5274 | return -ENOENT; |
| 5275 | if (mddev->bitmap->file) |
| 5276 | return -EINVAL; |
| 5277 | mddev->pers->quiesce(mddev, 1); |
| 5278 | bitmap_destroy(mddev); |
| 5279 | mddev->pers->quiesce(mddev, 0); |
| 5280 | mddev->bitmap_offset = 0; |
| 5281 | } |
| 5282 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5283 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5284 | return rv; |
| 5285 | } |
| 5286 | |
| 5287 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5288 | { |
| 5289 | mdk_rdev_t *rdev; |
| 5290 | |
| 5291 | if (mddev->pers == NULL) |
| 5292 | return -ENODEV; |
| 5293 | |
| 5294 | rdev = find_rdev(mddev, dev); |
| 5295 | if (!rdev) |
| 5296 | return -ENODEV; |
| 5297 | |
| 5298 | md_error(mddev, rdev); |
| 5299 | return 0; |
| 5300 | } |
| 5301 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5302 | /* |
| 5303 | * We have a problem here : there is no easy way to give a CHS |
| 5304 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5305 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5306 | * dosfs just mad... ;-) |
| 5307 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5308 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5309 | { |
| 5310 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5311 | |
| 5312 | geo->heads = 2; |
| 5313 | geo->sectors = 4; |
| 5314 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5318 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | unsigned int cmd, unsigned long arg) |
| 5320 | { |
| 5321 | int err = 0; |
| 5322 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | mddev_t *mddev = NULL; |
| 5324 | |
| 5325 | if (!capable(CAP_SYS_ADMIN)) |
| 5326 | return -EACCES; |
| 5327 | |
| 5328 | /* |
| 5329 | * Commands dealing with the RAID driver but not any |
| 5330 | * particular array: |
| 5331 | */ |
| 5332 | switch (cmd) |
| 5333 | { |
| 5334 | case RAID_VERSION: |
| 5335 | err = get_version(argp); |
| 5336 | goto done; |
| 5337 | |
| 5338 | case PRINT_RAID_DEBUG: |
| 5339 | err = 0; |
| 5340 | md_print_devices(); |
| 5341 | goto done; |
| 5342 | |
| 5343 | #ifndef MODULE |
| 5344 | case RAID_AUTORUN: |
| 5345 | err = 0; |
| 5346 | autostart_arrays(arg); |
| 5347 | goto done; |
| 5348 | #endif |
| 5349 | default:; |
| 5350 | } |
| 5351 | |
| 5352 | /* |
| 5353 | * Commands creating/starting a new array: |
| 5354 | */ |
| 5355 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5356 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | |
| 5358 | if (!mddev) { |
| 5359 | BUG(); |
| 5360 | goto abort; |
| 5361 | } |
| 5362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | err = mddev_lock(mddev); |
| 5364 | if (err) { |
| 5365 | printk(KERN_INFO |
| 5366 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5367 | err, cmd); |
| 5368 | goto abort; |
| 5369 | } |
| 5370 | |
| 5371 | switch (cmd) |
| 5372 | { |
| 5373 | case SET_ARRAY_INFO: |
| 5374 | { |
| 5375 | mdu_array_info_t info; |
| 5376 | if (!arg) |
| 5377 | memset(&info, 0, sizeof(info)); |
| 5378 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5379 | err = -EFAULT; |
| 5380 | goto abort_unlock; |
| 5381 | } |
| 5382 | if (mddev->pers) { |
| 5383 | err = update_array_info(mddev, &info); |
| 5384 | if (err) { |
| 5385 | printk(KERN_WARNING "md: couldn't update" |
| 5386 | " array info. %d\n", err); |
| 5387 | goto abort_unlock; |
| 5388 | } |
| 5389 | goto done_unlock; |
| 5390 | } |
| 5391 | if (!list_empty(&mddev->disks)) { |
| 5392 | printk(KERN_WARNING |
| 5393 | "md: array %s already has disks!\n", |
| 5394 | mdname(mddev)); |
| 5395 | err = -EBUSY; |
| 5396 | goto abort_unlock; |
| 5397 | } |
| 5398 | if (mddev->raid_disks) { |
| 5399 | printk(KERN_WARNING |
| 5400 | "md: array %s already initialised!\n", |
| 5401 | mdname(mddev)); |
| 5402 | err = -EBUSY; |
| 5403 | goto abort_unlock; |
| 5404 | } |
| 5405 | err = set_array_info(mddev, &info); |
| 5406 | if (err) { |
| 5407 | printk(KERN_WARNING "md: couldn't set" |
| 5408 | " array info. %d\n", err); |
| 5409 | goto abort_unlock; |
| 5410 | } |
| 5411 | } |
| 5412 | goto done_unlock; |
| 5413 | |
| 5414 | default:; |
| 5415 | } |
| 5416 | |
| 5417 | /* |
| 5418 | * Commands querying/configuring an existing array: |
| 5419 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5420 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5421 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5422 | if ((!mddev->raid_disks && !mddev->external) |
| 5423 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5424 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5425 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | err = -ENODEV; |
| 5427 | goto abort_unlock; |
| 5428 | } |
| 5429 | |
| 5430 | /* |
| 5431 | * Commands even a read-only array can execute: |
| 5432 | */ |
| 5433 | switch (cmd) |
| 5434 | { |
| 5435 | case GET_ARRAY_INFO: |
| 5436 | err = get_array_info(mddev, argp); |
| 5437 | goto done_unlock; |
| 5438 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5439 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5440 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5441 | goto done_unlock; |
| 5442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | case GET_DISK_INFO: |
| 5444 | err = get_disk_info(mddev, argp); |
| 5445 | goto done_unlock; |
| 5446 | |
| 5447 | case RESTART_ARRAY_RW: |
| 5448 | err = restart_array(mddev); |
| 5449 | goto done_unlock; |
| 5450 | |
| 5451 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5452 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | goto done_unlock; |
| 5454 | |
| 5455 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5456 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | goto done_unlock; |
| 5458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | /* |
| 5462 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5463 | * superblock, so we do not allow them on read-only arrays. |
| 5464 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5465 | * here and hit the 'default' below, so only disallow |
| 5466 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5468 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5469 | if (mddev->ro == 2) { |
| 5470 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5471 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5472 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5473 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5474 | } else { |
| 5475 | err = -EROFS; |
| 5476 | goto abort_unlock; |
| 5477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | switch (cmd) |
| 5481 | { |
| 5482 | case ADD_NEW_DISK: |
| 5483 | { |
| 5484 | mdu_disk_info_t info; |
| 5485 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5486 | err = -EFAULT; |
| 5487 | else |
| 5488 | err = add_new_disk(mddev, &info); |
| 5489 | goto done_unlock; |
| 5490 | } |
| 5491 | |
| 5492 | case HOT_REMOVE_DISK: |
| 5493 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5494 | goto done_unlock; |
| 5495 | |
| 5496 | case HOT_ADD_DISK: |
| 5497 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5498 | goto done_unlock; |
| 5499 | |
| 5500 | case SET_DISK_FAULTY: |
| 5501 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5502 | goto done_unlock; |
| 5503 | |
| 5504 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5505 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5506 | goto done_unlock; |
| 5507 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5508 | case SET_BITMAP_FILE: |
| 5509 | err = set_bitmap_file(mddev, (int)arg); |
| 5510 | goto done_unlock; |
| 5511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5512 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | err = -EINVAL; |
| 5514 | goto abort_unlock; |
| 5515 | } |
| 5516 | |
| 5517 | done_unlock: |
| 5518 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5519 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5520 | err != -EINVAL) |
| 5521 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5522 | mddev_unlock(mddev); |
| 5523 | |
| 5524 | return err; |
| 5525 | done: |
| 5526 | if (err) |
| 5527 | MD_BUG(); |
| 5528 | abort: |
| 5529 | return err; |
| 5530 | } |
| 5531 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5532 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5533 | { |
| 5534 | /* |
| 5535 | * Succeed if we can lock the mddev, which confirms that |
| 5536 | * it isn't being stopped right now. |
| 5537 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5538 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | int err; |
| 5540 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5541 | if (mddev->gendisk != bdev->bd_disk) { |
| 5542 | /* we are racing with mddev_put which is discarding this |
| 5543 | * bd_disk. |
| 5544 | */ |
| 5545 | mddev_put(mddev); |
| 5546 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5547 | flush_scheduled_work(); |
| 5548 | /* Then retry the open from the top */ |
| 5549 | return -ERESTARTSYS; |
| 5550 | } |
| 5551 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5552 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5553 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5554 | goto out; |
| 5555 | |
| 5556 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5557 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5558 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5559 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5560 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | out: |
| 5562 | return err; |
| 5563 | } |
| 5564 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5565 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5567 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5568 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5569 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5570 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | mddev_put(mddev); |
| 5572 | |
| 5573 | return 0; |
| 5574 | } |
| 5575 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5576 | static int md_media_changed(struct gendisk *disk) |
| 5577 | { |
| 5578 | mddev_t *mddev = disk->private_data; |
| 5579 | |
| 5580 | return mddev->changed; |
| 5581 | } |
| 5582 | |
| 5583 | static int md_revalidate(struct gendisk *disk) |
| 5584 | { |
| 5585 | mddev_t *mddev = disk->private_data; |
| 5586 | |
| 5587 | mddev->changed = 0; |
| 5588 | return 0; |
| 5589 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5590 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | { |
| 5592 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5593 | .open = md_open, |
| 5594 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5595 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5596 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5597 | .media_changed = md_media_changed, |
| 5598 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | }; |
| 5600 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5601 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | { |
| 5603 | mdk_thread_t *thread = arg; |
| 5604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | /* |
| 5606 | * md_thread is a 'system-thread', it's priority should be very |
| 5607 | * high. We avoid resource deadlocks individually in each |
| 5608 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5609 | * the very same RT priority as kswapd, thus we will never get |
| 5610 | * into a priority inversion deadlock. |
| 5611 | * |
| 5612 | * we definitely have to have equal or higher priority than |
| 5613 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5614 | * many dirty RAID5 blocks. |
| 5615 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5617 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5618 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5620 | /* We need to wait INTERRUPTIBLE so that |
| 5621 | * we don't add to the load-average. |
| 5622 | * That means we need to be sure no signals are |
| 5623 | * pending |
| 5624 | */ |
| 5625 | if (signal_pending(current)) |
| 5626 | flush_signals(current); |
| 5627 | |
| 5628 | wait_event_interruptible_timeout |
| 5629 | (thread->wqueue, |
| 5630 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5631 | || kthread_should_stop(), |
| 5632 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | |
| 5634 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5635 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5636 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5637 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | return 0; |
| 5640 | } |
| 5641 | |
| 5642 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5643 | { |
| 5644 | if (thread) { |
| 5645 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5646 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5647 | wake_up(&thread->wqueue); |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5652 | const char *name) |
| 5653 | { |
| 5654 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5655 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5656 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | if (!thread) |
| 5658 | return NULL; |
| 5659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | init_waitqueue_head(&thread->wqueue); |
| 5661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5662 | thread->run = run; |
| 5663 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5664 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5665 | thread->tsk = kthread_run(md_thread, thread, |
| 5666 | "%s_%s", |
| 5667 | mdname(thread->mddev), |
| 5668 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5669 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5670 | kfree(thread); |
| 5671 | return NULL; |
| 5672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | return thread; |
| 5674 | } |
| 5675 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | void md_unregister_thread(mdk_thread_t *thread) |
| 5677 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5678 | if (!thread) |
| 5679 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5680 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5681 | |
| 5682 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | kfree(thread); |
| 5684 | } |
| 5685 | |
| 5686 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5687 | { |
| 5688 | if (!mddev) { |
| 5689 | MD_BUG(); |
| 5690 | return; |
| 5691 | } |
| 5692 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5693 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5695 | |
| 5696 | if (mddev->external) |
| 5697 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5698 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5700 | mdname(mddev), |
| 5701 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5702 | __builtin_return_address(0),__builtin_return_address(1), |
| 5703 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5704 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5705 | if (!mddev->pers) |
| 5706 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | if (!mddev->pers->error_handler) |
| 5708 | return; |
| 5709 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5710 | if (mddev->degraded) |
| 5711 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5712 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5714 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5715 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5716 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | } |
| 5718 | |
| 5719 | /* seq_file implementation /proc/mdstat */ |
| 5720 | |
| 5721 | static void status_unused(struct seq_file *seq) |
| 5722 | { |
| 5723 | int i = 0; |
| 5724 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | |
| 5726 | seq_printf(seq, "unused devices: "); |
| 5727 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5728 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | char b[BDEVNAME_SIZE]; |
| 5730 | i++; |
| 5731 | seq_printf(seq, "%s ", |
| 5732 | bdevname(rdev->bdev,b)); |
| 5733 | } |
| 5734 | if (!i) |
| 5735 | seq_printf(seq, "<none>"); |
| 5736 | |
| 5737 | seq_printf(seq, "\n"); |
| 5738 | } |
| 5739 | |
| 5740 | |
| 5741 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5742 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5743 | sector_t max_sectors, resync, res; |
| 5744 | unsigned long dt, db; |
| 5745 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5746 | int scale; |
| 5747 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5749 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | |
| 5751 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5752 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5754 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | |
| 5756 | /* |
| 5757 | * Should not happen. |
| 5758 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5759 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | MD_BUG(); |
| 5761 | return; |
| 5762 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5763 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5764 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5765 | * u32, as those are the requirements for sector_div. |
| 5766 | * Thus 'scale' must be at least 10 |
| 5767 | */ |
| 5768 | scale = 10; |
| 5769 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5770 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5771 | scale++; |
| 5772 | } |
| 5773 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5774 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5775 | |
| 5776 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5778 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | seq_printf(seq, "["); |
| 5780 | for (i = 0; i < x; i++) |
| 5781 | seq_printf(seq, "="); |
| 5782 | seq_printf(seq, ">"); |
| 5783 | for (i = 0; i < y; i++) |
| 5784 | seq_printf(seq, "."); |
| 5785 | seq_printf(seq, "] "); |
| 5786 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5787 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5788 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5789 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5790 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5791 | "check" : |
| 5792 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5793 | "resync" : "recovery"))), |
| 5794 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5795 | (unsigned long long) resync/2, |
| 5796 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | |
| 5798 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | * dt: time from mark until now |
| 5800 | * db: blocks written from mark until now |
| 5801 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5802 | * |
| 5803 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5804 | * So we divide before multiply in case it is 32bit and close |
| 5805 | * to the limit. |
| 5806 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5807 | * near the end of resync when the number of remaining sectors |
| 5808 | * is close to 'db'. |
| 5809 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5810 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | */ |
| 5812 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5813 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5814 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5815 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5816 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5817 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5818 | sector_div(rt, db/32+1); |
| 5819 | rt *= dt; |
| 5820 | rt >>= 5; |
| 5821 | |
| 5822 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5823 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5825 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5829 | { |
| 5830 | struct list_head *tmp; |
| 5831 | loff_t l = *pos; |
| 5832 | mddev_t *mddev; |
| 5833 | |
| 5834 | if (l >= 0x10000) |
| 5835 | return NULL; |
| 5836 | if (!l--) |
| 5837 | /* header */ |
| 5838 | return (void*)1; |
| 5839 | |
| 5840 | spin_lock(&all_mddevs_lock); |
| 5841 | list_for_each(tmp,&all_mddevs) |
| 5842 | if (!l--) { |
| 5843 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5844 | mddev_get(mddev); |
| 5845 | spin_unlock(&all_mddevs_lock); |
| 5846 | return mddev; |
| 5847 | } |
| 5848 | spin_unlock(&all_mddevs_lock); |
| 5849 | if (!l--) |
| 5850 | return (void*)2;/* tail */ |
| 5851 | return NULL; |
| 5852 | } |
| 5853 | |
| 5854 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5855 | { |
| 5856 | struct list_head *tmp; |
| 5857 | mddev_t *next_mddev, *mddev = v; |
| 5858 | |
| 5859 | ++*pos; |
| 5860 | if (v == (void*)2) |
| 5861 | return NULL; |
| 5862 | |
| 5863 | spin_lock(&all_mddevs_lock); |
| 5864 | if (v == (void*)1) |
| 5865 | tmp = all_mddevs.next; |
| 5866 | else |
| 5867 | tmp = mddev->all_mddevs.next; |
| 5868 | if (tmp != &all_mddevs) |
| 5869 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5870 | else { |
| 5871 | next_mddev = (void*)2; |
| 5872 | *pos = 0x10000; |
| 5873 | } |
| 5874 | spin_unlock(&all_mddevs_lock); |
| 5875 | |
| 5876 | if (v != (void*)1) |
| 5877 | mddev_put(mddev); |
| 5878 | return next_mddev; |
| 5879 | |
| 5880 | } |
| 5881 | |
| 5882 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5883 | { |
| 5884 | mddev_t *mddev = v; |
| 5885 | |
| 5886 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5887 | mddev_put(mddev); |
| 5888 | } |
| 5889 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5890 | struct mdstat_info { |
| 5891 | int event; |
| 5892 | }; |
| 5893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5895 | { |
| 5896 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5897 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5899 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5900 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | |
| 5902 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5903 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | seq_printf(seq, "Personalities : "); |
| 5905 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5906 | list_for_each_entry(pers, &pers_list, list) |
| 5907 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | |
| 5909 | spin_unlock(&pers_lock); |
| 5910 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5911 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | return 0; |
| 5913 | } |
| 5914 | if (v == (void*)2) { |
| 5915 | status_unused(seq); |
| 5916 | return 0; |
| 5917 | } |
| 5918 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5919 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5923 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5924 | mddev->pers ? "" : "in"); |
| 5925 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5926 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5928 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5929 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5930 | seq_printf(seq, " %s", mddev->pers->name); |
| 5931 | } |
| 5932 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5933 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5934 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | char b[BDEVNAME_SIZE]; |
| 5936 | seq_printf(seq, " %s[%d]", |
| 5937 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5938 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5939 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5940 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | seq_printf(seq, "(F)"); |
| 5942 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5943 | } else if (rdev->raid_disk < 0) |
| 5944 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5945 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | if (!list_empty(&mddev->disks)) { |
| 5949 | if (mddev->pers) |
| 5950 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5951 | (unsigned long long) |
| 5952 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | else |
| 5954 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5955 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5957 | if (mddev->persistent) { |
| 5958 | if (mddev->major_version != 0 || |
| 5959 | mddev->minor_version != 90) { |
| 5960 | seq_printf(seq," super %d.%d", |
| 5961 | mddev->major_version, |
| 5962 | mddev->minor_version); |
| 5963 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5964 | } else if (mddev->external) |
| 5965 | seq_printf(seq, " super external:%s", |
| 5966 | mddev->metadata_type); |
| 5967 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5968 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5969 | |
| 5970 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5971 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5973 | if (mddev->pers->sync_request) { |
| 5974 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5975 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5976 | seq_printf(seq, "\n "); |
| 5977 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5978 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5979 | else if (mddev->recovery_cp < MaxSector) |
| 5980 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5981 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5982 | } else |
| 5983 | seq_printf(seq, "\n "); |
| 5984 | |
| 5985 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5986 | unsigned long chunk_kb; |
| 5987 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5988 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5989 | chunk_kb = bitmap->chunksize >> 10; |
| 5990 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5991 | "%lu%s chunk", |
| 5992 | bitmap->pages - bitmap->missing_pages, |
| 5993 | bitmap->pages, |
| 5994 | (bitmap->pages - bitmap->missing_pages) |
| 5995 | << (PAGE_SHIFT - 10), |
| 5996 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5997 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5998 | if (bitmap->file) { |
| 5999 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6000 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6001 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6002 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6003 | seq_printf(seq, "\n"); |
| 6004 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | seq_printf(seq, "\n"); |
| 6008 | } |
| 6009 | mddev_unlock(mddev); |
| 6010 | |
| 6011 | return 0; |
| 6012 | } |
| 6013 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6014 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | .start = md_seq_start, |
| 6016 | .next = md_seq_next, |
| 6017 | .stop = md_seq_stop, |
| 6018 | .show = md_seq_show, |
| 6019 | }; |
| 6020 | |
| 6021 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6022 | { |
| 6023 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6024 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6025 | if (mi == NULL) |
| 6026 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6027 | |
| 6028 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6029 | if (error) |
| 6030 | kfree(mi); |
| 6031 | else { |
| 6032 | struct seq_file *p = file->private_data; |
| 6033 | p->private = mi; |
| 6034 | mi->event = atomic_read(&md_event_count); |
| 6035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6036 | return error; |
| 6037 | } |
| 6038 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6039 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6040 | { |
| 6041 | struct seq_file *m = filp->private_data; |
| 6042 | struct mdstat_info *mi = m->private; |
| 6043 | int mask; |
| 6044 | |
| 6045 | poll_wait(filp, &md_event_waiters, wait); |
| 6046 | |
| 6047 | /* always allow read */ |
| 6048 | mask = POLLIN | POLLRDNORM; |
| 6049 | |
| 6050 | if (mi->event != atomic_read(&md_event_count)) |
| 6051 | mask |= POLLERR | POLLPRI; |
| 6052 | return mask; |
| 6053 | } |
| 6054 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6055 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6056 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | .open = md_seq_open, |
| 6058 | .read = seq_read, |
| 6059 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6060 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6061 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | }; |
| 6063 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6064 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6067 | list_add_tail(&p->list, &pers_list); |
| 6068 | 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] | 6069 | spin_unlock(&pers_lock); |
| 6070 | return 0; |
| 6071 | } |
| 6072 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6073 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6075 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6076 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6077 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | spin_unlock(&pers_lock); |
| 6079 | return 0; |
| 6080 | } |
| 6081 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6082 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | { |
| 6084 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6086 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6087 | |
| 6088 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6089 | rcu_read_lock(); |
| 6090 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6092 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6093 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6094 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6095 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6096 | * as sync_io is counted when a request starts, and |
| 6097 | * disk_stats is counted when it completes. |
| 6098 | * So resync activity will cause curr_events to be smaller than |
| 6099 | * when there was no such activity. |
| 6100 | * non-sync IO will cause disk_stat to increase without |
| 6101 | * increasing sync_io so curr_events will (eventually) |
| 6102 | * be larger than it was before. Once it becomes |
| 6103 | * substantially larger, the test below will cause |
| 6104 | * the array to appear non-idle, and resync will slow |
| 6105 | * down. |
| 6106 | * If there is a lot of outstanding resync activity when |
| 6107 | * we set last_event to curr_events, then all that activity |
| 6108 | * completing might cause the array to appear non-idle |
| 6109 | * and resync will be slowed down even though there might |
| 6110 | * not have been non-resync activity. This will only |
| 6111 | * happen once though. 'last_events' will soon reflect |
| 6112 | * the state where there is little or no outstanding |
| 6113 | * resync requests, and further resync activity will |
| 6114 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6115 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6117 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6118 | rdev->last_events = curr_events; |
| 6119 | idle = 0; |
| 6120 | } |
| 6121 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6122 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | return idle; |
| 6124 | } |
| 6125 | |
| 6126 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6127 | { |
| 6128 | /* another "blocks" (512byte) blocks have been synced */ |
| 6129 | atomic_sub(blocks, &mddev->recovery_active); |
| 6130 | wake_up(&mddev->recovery_wait); |
| 6131 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6132 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | md_wakeup_thread(mddev->thread); |
| 6134 | // stop recovery, signal do_sync .... |
| 6135 | } |
| 6136 | } |
| 6137 | |
| 6138 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6139 | /* md_write_start(mddev, bi) |
| 6140 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6141 | * in superblock) before writing, schedule a superblock update |
| 6142 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6143 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6144 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6146 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6147 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6148 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6149 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6150 | BUG_ON(mddev->ro == 1); |
| 6151 | if (mddev->ro == 2) { |
| 6152 | /* need to switch to read/write */ |
| 6153 | mddev->ro = 0; |
| 6154 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6155 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6156 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6157 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6158 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6159 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6160 | if (mddev->safemode == 1) |
| 6161 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6162 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6163 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6164 | if (mddev->in_sync) { |
| 6165 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6166 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6167 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6168 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6169 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6170 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6171 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6172 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6173 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6174 | wait_event(mddev->sb_wait, |
| 6175 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6176 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6177 | } |
| 6178 | |
| 6179 | void md_write_end(mddev_t *mddev) |
| 6180 | { |
| 6181 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6182 | if (mddev->safemode == 2) |
| 6183 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6184 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6186 | } |
| 6187 | } |
| 6188 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6189 | /* md_allow_write(mddev) |
| 6190 | * Calling this ensures that the array is marked 'active' so that writes |
| 6191 | * may proceed without blocking. It is important to call this before |
| 6192 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6193 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6194 | * |
| 6195 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6196 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6197 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6198 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6199 | { |
| 6200 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6201 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6202 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6203 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6204 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6205 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6206 | |
| 6207 | spin_lock_irq(&mddev->write_lock); |
| 6208 | if (mddev->in_sync) { |
| 6209 | mddev->in_sync = 0; |
| 6210 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6211 | if (mddev->safemode_delay && |
| 6212 | mddev->safemode == 0) |
| 6213 | mddev->safemode = 1; |
| 6214 | spin_unlock_irq(&mddev->write_lock); |
| 6215 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6216 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6217 | } else |
| 6218 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6219 | |
| 6220 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6221 | return -EAGAIN; |
| 6222 | else |
| 6223 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6224 | } |
| 6225 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | #define SYNC_MARKS 10 |
| 6228 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6229 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | { |
| 6231 | mddev_t *mddev2; |
| 6232 | unsigned int currspeed = 0, |
| 6233 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6234 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | unsigned long mark[SYNC_MARKS]; |
| 6236 | sector_t mark_cnt[SYNC_MARKS]; |
| 6237 | int last_mark,m; |
| 6238 | struct list_head *tmp; |
| 6239 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6240 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6241 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6242 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | |
| 6244 | /* just incase thread restarts... */ |
| 6245 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6246 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6247 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6248 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6250 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6251 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6252 | desc = "data-check"; |
| 6253 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6254 | desc = "requested-resync"; |
| 6255 | else |
| 6256 | desc = "resync"; |
| 6257 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6258 | desc = "reshape"; |
| 6259 | else |
| 6260 | desc = "recovery"; |
| 6261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6262 | /* we overload curr_resync somewhat here. |
| 6263 | * 0 == not engaged in resync at all |
| 6264 | * 2 == checking that there is no conflict with another sync |
| 6265 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6266 | * commense |
| 6267 | * other == active in resync - this many blocks |
| 6268 | * |
| 6269 | * Before starting a resync we must have set curr_resync to |
| 6270 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6271 | * less than ours. When we find one that is the same or higher |
| 6272 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6273 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6274 | * This will mean we have to start checking from the beginning again. |
| 6275 | * |
| 6276 | */ |
| 6277 | |
| 6278 | do { |
| 6279 | mddev->curr_resync = 2; |
| 6280 | |
| 6281 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6282 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6283 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6284 | goto skip; |
| 6285 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6286 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6287 | if (mddev2 == mddev) |
| 6288 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6289 | if (!mddev->parallel_resync |
| 6290 | && mddev2->curr_resync |
| 6291 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | DEFINE_WAIT(wq); |
| 6293 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6294 | /* arbitrarily yield */ |
| 6295 | mddev->curr_resync = 1; |
| 6296 | wake_up(&resync_wait); |
| 6297 | } |
| 6298 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6299 | /* no need to wait here, we can wait the next |
| 6300 | * time 'round when curr_resync == 2 |
| 6301 | */ |
| 6302 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6303 | /* We need to wait 'interruptible' so as not to |
| 6304 | * contribute to the load average, and not to |
| 6305 | * be caught by 'softlockup' |
| 6306 | */ |
| 6307 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6308 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6309 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6310 | printk(KERN_INFO "md: delaying %s of %s" |
| 6311 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6312 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6313 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6315 | if (signal_pending(current)) |
| 6316 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | schedule(); |
| 6318 | finish_wait(&resync_wait, &wq); |
| 6319 | goto try_again; |
| 6320 | } |
| 6321 | finish_wait(&resync_wait, &wq); |
| 6322 | } |
| 6323 | } |
| 6324 | } while (mddev->curr_resync < 2); |
| 6325 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6326 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6327 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6329 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | */ |
| 6331 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6332 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6333 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6334 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6335 | j = mddev->resync_min; |
| 6336 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6337 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6338 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6339 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6340 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6341 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6343 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6344 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6345 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6346 | if (rdev->raid_disk >= 0 && |
| 6347 | !test_bit(Faulty, &rdev->flags) && |
| 6348 | !test_bit(In_sync, &rdev->flags) && |
| 6349 | rdev->recovery_offset < j) |
| 6350 | j = rdev->recovery_offset; |
| 6351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6353 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6354 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6355 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6356 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6357 | "(but not more than %d KB/sec) for %s.\n", |
| 6358 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6359 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6360 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6361 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6362 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6364 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6365 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | } |
| 6367 | last_mark = 0; |
| 6368 | mddev->resync_mark = mark[last_mark]; |
| 6369 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6370 | |
| 6371 | /* |
| 6372 | * Tune reconstruction: |
| 6373 | */ |
| 6374 | window = 32*(PAGE_SIZE/512); |
| 6375 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6376 | window/2,(unsigned long long) max_sectors/2); |
| 6377 | |
| 6378 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6379 | last_check = 0; |
| 6380 | |
| 6381 | if (j>2) { |
| 6382 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6383 | "md: resuming %s of %s from checkpoint.\n", |
| 6384 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | mddev->curr_resync = j; |
| 6386 | } |
| 6387 | |
| 6388 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6389 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6391 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6392 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6393 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6394 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6395 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6396 | > (max_sectors >> 4)) || |
| 6397 | (j - mddev->curr_resync_completed)*2 |
| 6398 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6399 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6400 | /* time to update curr_resync_completed */ |
| 6401 | blk_unplug(mddev->queue); |
| 6402 | wait_event(mddev->recovery_wait, |
| 6403 | atomic_read(&mddev->recovery_active) == 0); |
| 6404 | mddev->curr_resync_completed = |
| 6405 | mddev->curr_resync; |
| 6406 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6407 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6408 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6409 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6410 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6411 | /* As this condition is controlled by user-space, |
| 6412 | * we can block indefinitely, so use '_interruptible' |
| 6413 | * to avoid triggering warnings. |
| 6414 | */ |
| 6415 | flush_signals(current); /* just in case */ |
| 6416 | wait_event_interruptible(mddev->recovery_wait, |
| 6417 | mddev->resync_max > j |
| 6418 | || kthread_should_stop()); |
| 6419 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6420 | |
| 6421 | if (kthread_should_stop()) |
| 6422 | goto interrupted; |
| 6423 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6424 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6425 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6426 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6427 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | goto out; |
| 6429 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6430 | |
| 6431 | if (!skipped) { /* actual IO requested */ |
| 6432 | io_sectors += sectors; |
| 6433 | atomic_add(sectors, &mddev->recovery_active); |
| 6434 | } |
| 6435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | j += sectors; |
| 6437 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6438 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6439 | if (last_check == 0) |
| 6440 | /* this is the earliers that rebuilt will be |
| 6441 | * visible in /proc/mdstat |
| 6442 | */ |
| 6443 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6444 | |
| 6445 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | continue; |
| 6447 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6448 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6449 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6450 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | break; |
| 6452 | |
| 6453 | repeat: |
| 6454 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6455 | /* step marks */ |
| 6456 | int next = (last_mark+1) % SYNC_MARKS; |
| 6457 | |
| 6458 | mddev->resync_mark = mark[next]; |
| 6459 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6460 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6461 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | last_mark = next; |
| 6463 | } |
| 6464 | |
| 6465 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6466 | if (kthread_should_stop()) |
| 6467 | goto interrupted; |
| 6468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | |
| 6470 | /* |
| 6471 | * this loop exits only if either when we are slower than |
| 6472 | * the 'hard' speed limit, or the system was IO-idle for |
| 6473 | * a jiffy. |
| 6474 | * the system might be non-idle CPU-wise, but we only care |
| 6475 | * about not overloading the IO subsystem. (things like an |
| 6476 | * e2fsck being done on the RAID array should execute fast) |
| 6477 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6478 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6479 | cond_resched(); |
| 6480 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6481 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6482 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6483 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6484 | if (currspeed > speed_min(mddev)) { |
| 6485 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6486 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6487 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | goto repeat; |
| 6489 | } |
| 6490 | } |
| 6491 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6492 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | /* |
| 6494 | * this also signals 'finished resyncing' to md_stop |
| 6495 | */ |
| 6496 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6497 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | |
| 6499 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6500 | |
| 6501 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6502 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6504 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6505 | mddev->curr_resync > 2) { |
| 6506 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6507 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6508 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6509 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6510 | "md: checkpointing %s of %s.\n", |
| 6511 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6512 | mddev->recovery_cp = mddev->curr_resync; |
| 6513 | } |
| 6514 | } else |
| 6515 | mddev->recovery_cp = MaxSector; |
| 6516 | } else { |
| 6517 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6518 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6519 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6520 | if (rdev->raid_disk >= 0 && |
| 6521 | !test_bit(Faulty, &rdev->flags) && |
| 6522 | !test_bit(In_sync, &rdev->flags) && |
| 6523 | rdev->recovery_offset < mddev->curr_resync) |
| 6524 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6527 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | skip: |
| 6530 | mddev->curr_resync = 0; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6531 | mddev->curr_resync_completed = 0; |
NeilBrown | 24395a8 | 2009-11-06 14:59:27 +1100 | [diff] [blame] | 6532 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6533 | /* We completed so max setting can be forgotten. */ |
| 6534 | mddev->resync_max = MaxSector; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6535 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | wake_up(&resync_wait); |
| 6537 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6538 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6539 | return; |
| 6540 | |
| 6541 | interrupted: |
| 6542 | /* |
| 6543 | * got a signal, exit. |
| 6544 | */ |
| 6545 | printk(KERN_INFO |
| 6546 | "md: md_do_sync() got signal ... exiting\n"); |
| 6547 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6548 | goto out; |
| 6549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6551 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | |
| 6553 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6554 | static int remove_and_add_spares(mddev_t *mddev) |
| 6555 | { |
| 6556 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6557 | int spares = 0; |
| 6558 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6559 | mddev->curr_resync_completed = 0; |
| 6560 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6561 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6562 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6563 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6564 | (test_bit(Faulty, &rdev->flags) || |
| 6565 | ! test_bit(In_sync, &rdev->flags)) && |
| 6566 | atomic_read(&rdev->nr_pending)==0) { |
| 6567 | if (mddev->pers->hot_remove_disk( |
| 6568 | mddev, rdev->raid_disk)==0) { |
| 6569 | char nm[20]; |
| 6570 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6571 | sysfs_remove_link(&mddev->kobj, nm); |
| 6572 | rdev->raid_disk = -1; |
| 6573 | } |
| 6574 | } |
| 6575 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6576 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6577 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6578 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6579 | !test_bit(In_sync, &rdev->flags) && |
| 6580 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6581 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6582 | if (rdev->raid_disk < 0 |
| 6583 | && !test_bit(Faulty, &rdev->flags)) { |
| 6584 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6585 | if (mddev->pers-> |
| 6586 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6587 | char nm[20]; |
| 6588 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6589 | if (sysfs_create_link(&mddev->kobj, |
| 6590 | &rdev->kobj, nm)) |
| 6591 | printk(KERN_WARNING |
| 6592 | "md: cannot register " |
| 6593 | "%s for %s\n", |
| 6594 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6595 | spares++; |
| 6596 | md_new_event(mddev); |
| 6597 | } else |
| 6598 | break; |
| 6599 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6600 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6601 | } |
| 6602 | return spares; |
| 6603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | /* |
| 6605 | * This routine is regularly called by all per-raid-array threads to |
| 6606 | * deal with generic issues like resync and super-block update. |
| 6607 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6608 | * need this as they never do any recovery or update the superblock. |
| 6609 | * |
| 6610 | * It does not do any resync itself, but rather "forks" off other threads |
| 6611 | * to do that as needed. |
| 6612 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6613 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6614 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | * and wakeups up this thread which will reap the thread and finish up. |
| 6616 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6617 | * |
| 6618 | * The overall approach is: |
| 6619 | * 1/ if the superblock needs updating, update it. |
| 6620 | * 2/ If a recovery thread is running, don't do anything else. |
| 6621 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6622 | * 4/ If there are any faulty devices, remove them. |
| 6623 | * 5/ If array is degraded, try to add spares devices |
| 6624 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6625 | */ |
| 6626 | void md_check_recovery(mddev_t *mddev) |
| 6627 | { |
| 6628 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | |
| 6630 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6631 | if (mddev->bitmap) |
| 6632 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | |
| 6634 | if (mddev->ro) |
| 6635 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6636 | |
| 6637 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6638 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6639 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6640 | mdname(mddev)); |
| 6641 | mddev->safemode = 2; |
| 6642 | } |
| 6643 | flush_signals(current); |
| 6644 | } |
| 6645 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6646 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6647 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6649 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6651 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6652 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6653 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6654 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | )) |
| 6656 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6657 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6658 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6659 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6660 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6661 | if (mddev->ro) { |
| 6662 | /* Only thing we do on a ro array is remove |
| 6663 | * failed devices. |
| 6664 | */ |
| 6665 | remove_and_add_spares(mddev); |
| 6666 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6667 | goto unlock; |
| 6668 | } |
| 6669 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6670 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6671 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6672 | spin_lock_irq(&mddev->write_lock); |
| 6673 | if (mddev->safemode && |
| 6674 | !atomic_read(&mddev->writes_pending) && |
| 6675 | !mddev->in_sync && |
| 6676 | mddev->recovery_cp == MaxSector) { |
| 6677 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6678 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6679 | if (mddev->persistent) |
| 6680 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6681 | } |
| 6682 | if (mddev->safemode == 1) |
| 6683 | mddev->safemode = 0; |
| 6684 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6685 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6686 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6687 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6688 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6689 | if (mddev->flags) |
| 6690 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6691 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6692 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6693 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6694 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6695 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6698 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6699 | /* resync/recovery still happening */ |
| 6700 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6701 | goto unlock; |
| 6702 | } |
| 6703 | if (mddev->sync_thread) { |
| 6704 | /* resync has finished, collect result */ |
| 6705 | md_unregister_thread(mddev->sync_thread); |
| 6706 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6707 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6708 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | /* success...*/ |
| 6710 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6711 | if (mddev->pers->spare_active(mddev)) |
| 6712 | sysfs_notify(&mddev->kobj, NULL, |
| 6713 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6715 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6716 | mddev->pers->finish_reshape) |
| 6717 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6718 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6719 | |
| 6720 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6721 | * information must be scrapped |
| 6722 | */ |
| 6723 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6724 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6725 | rdev->saved_raid_disk = -1; |
| 6726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | mddev->recovery = 0; |
| 6728 | /* flag recovery needed just to double check */ |
| 6729 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6730 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6731 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | goto unlock; |
| 6733 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6734 | /* Set RUNNING before clearing NEEDED to avoid |
| 6735 | * any transients in the value of "sync_action". |
| 6736 | */ |
| 6737 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6738 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6739 | /* Clear some bits that don't mean anything, but |
| 6740 | * might be left set |
| 6741 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6742 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6743 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6745 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6746 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | /* no recovery is running. |
| 6748 | * remove any failed drives, then |
| 6749 | * add spares if possible. |
| 6750 | * Spare are also removed and re-added, to allow |
| 6751 | * the personality to fail the re-add. |
| 6752 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6754 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6755 | if (mddev->pers->check_reshape == NULL || |
| 6756 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6757 | /* Cannot proceed */ |
| 6758 | goto unlock; |
| 6759 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6760 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6761 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6762 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6763 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6764 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6765 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6766 | } else if (mddev->recovery_cp < MaxSector) { |
| 6767 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6768 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6769 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6770 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6774 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6775 | /* We are adding a device or devices to an array |
| 6776 | * which has the bitmap stored on all devices. |
| 6777 | * So make sure all bitmap pages get written |
| 6778 | */ |
| 6779 | bitmap_write_all(mddev->bitmap); |
| 6780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6782 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6783 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | if (!mddev->sync_thread) { |
| 6785 | printk(KERN_ERR "%s: could not start resync" |
| 6786 | " thread...\n", |
| 6787 | mdname(mddev)); |
| 6788 | /* leave the spares where they are, it shouldn't hurt */ |
| 6789 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6790 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6791 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6792 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6793 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | } |
| 6795 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6796 | if (!mddev->sync_thread) { |
| 6797 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6798 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6799 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6800 | if (mddev->sysfs_action) |
| 6801 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6803 | mddev_unlock(mddev); |
| 6804 | } |
| 6805 | } |
| 6806 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6807 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6808 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6809 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6810 | wait_event_timeout(rdev->blocked_wait, |
| 6811 | !test_bit(Blocked, &rdev->flags), |
| 6812 | msecs_to_jiffies(5000)); |
| 6813 | rdev_dec_pending(rdev, mddev); |
| 6814 | } |
| 6815 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6816 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6817 | static int md_notify_reboot(struct notifier_block *this, |
| 6818 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | { |
| 6820 | struct list_head *tmp; |
| 6821 | mddev_t *mddev; |
| 6822 | |
| 6823 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6824 | |
| 6825 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6826 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6827 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6828 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6829 | /* Force a switch to readonly even array |
| 6830 | * appears to still be in use. Hence |
| 6831 | * the '100'. |
| 6832 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6833 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6834 | mddev_unlock(mddev); |
| 6835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | /* |
| 6837 | * certain more exotic SCSI devices are known to be |
| 6838 | * volatile wrt too early system reboots. While the |
| 6839 | * right place to handle this issue is the given |
| 6840 | * driver, we do want to have a safe RAID driver ... |
| 6841 | */ |
| 6842 | mdelay(1000*1); |
| 6843 | } |
| 6844 | return NOTIFY_DONE; |
| 6845 | } |
| 6846 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6847 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | .notifier_call = md_notify_reboot, |
| 6849 | .next = NULL, |
| 6850 | .priority = INT_MAX, /* before any real devices */ |
| 6851 | }; |
| 6852 | |
| 6853 | static void md_geninit(void) |
| 6854 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6856 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6857 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | } |
| 6859 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6860 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6862 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6863 | return -1; |
| 6864 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6865 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | return -1; |
| 6867 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6868 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6869 | md_probe, NULL, NULL); |
| 6870 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | md_probe, NULL, NULL); |
| 6872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6873 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6874 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | |
| 6876 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6877 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | } |
| 6879 | |
| 6880 | |
| 6881 | #ifndef MODULE |
| 6882 | |
| 6883 | /* |
| 6884 | * Searches all registered partitions for autorun RAID arrays |
| 6885 | * at boot time. |
| 6886 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6887 | |
| 6888 | static LIST_HEAD(all_detected_devices); |
| 6889 | struct detected_devices_node { |
| 6890 | struct list_head list; |
| 6891 | dev_t dev; |
| 6892 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | |
| 6894 | void md_autodetect_dev(dev_t dev) |
| 6895 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6896 | struct detected_devices_node *node_detected_dev; |
| 6897 | |
| 6898 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6899 | if (node_detected_dev) { |
| 6900 | node_detected_dev->dev = dev; |
| 6901 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6902 | } else { |
| 6903 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6904 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6906 | } |
| 6907 | |
| 6908 | |
| 6909 | static void autostart_arrays(int part) |
| 6910 | { |
| 6911 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6912 | struct detected_devices_node *node_detected_dev; |
| 6913 | dev_t dev; |
| 6914 | int i_scanned, i_passed; |
| 6915 | |
| 6916 | i_scanned = 0; |
| 6917 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | |
| 6919 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6920 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6921 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6922 | i_scanned++; |
| 6923 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6924 | struct detected_devices_node, list); |
| 6925 | list_del(&node_detected_dev->list); |
| 6926 | dev = node_detected_dev->dev; |
| 6927 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6928 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | if (IS_ERR(rdev)) |
| 6930 | continue; |
| 6931 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6932 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | MD_BUG(); |
| 6934 | continue; |
| 6935 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6936 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6938 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6940 | |
| 6941 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6942 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | |
| 6944 | autorun_devices(part); |
| 6945 | } |
| 6946 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6947 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6948 | |
| 6949 | static __exit void md_exit(void) |
| 6950 | { |
| 6951 | mddev_t *mddev; |
| 6952 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6953 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6954 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6955 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6957 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6958 | unregister_blkdev(mdp_major, "mdp"); |
| 6959 | unregister_reboot_notifier(&md_notifier); |
| 6960 | unregister_sysctl_table(raid_table_header); |
| 6961 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6962 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6964 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | } |
| 6966 | } |
| 6967 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6968 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6969 | module_exit(md_exit) |
| 6970 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6971 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6972 | { |
| 6973 | return sprintf(buffer, "%d", start_readonly); |
| 6974 | } |
| 6975 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6976 | { |
| 6977 | char *e; |
| 6978 | int num = simple_strtoul(val, &e, 10); |
| 6979 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6980 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6981 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6982 | } |
| 6983 | return -EINVAL; |
| 6984 | } |
| 6985 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6986 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6987 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6988 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6989 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6991 | EXPORT_SYMBOL(register_md_personality); |
| 6992 | EXPORT_SYMBOL(unregister_md_personality); |
| 6993 | EXPORT_SYMBOL(md_error); |
| 6994 | EXPORT_SYMBOL(md_done_sync); |
| 6995 | EXPORT_SYMBOL(md_write_start); |
| 6996 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | EXPORT_SYMBOL(md_register_thread); |
| 6998 | EXPORT_SYMBOL(md_unregister_thread); |
| 6999 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | EXPORT_SYMBOL(md_check_recovery); |
| 7001 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7002 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7003 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |