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; |
| 947 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 948 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 949 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 950 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 951 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * sync_super for 0.90.0 |
| 957 | */ |
| 958 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 959 | { |
| 960 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | mdk_rdev_t *rdev2; |
| 962 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | /* make rdev->sb match mddev data.. |
| 966 | * |
| 967 | * 1/ zero out disks |
| 968 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 969 | * 3/ any empty disks < next_spare become removed |
| 970 | * |
| 971 | * disks[0] gets initialised to REMOVED because |
| 972 | * we cannot be sure from other fields if it has |
| 973 | * been initialised or not. |
| 974 | */ |
| 975 | int i; |
| 976 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 977 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 978 | rdev->sb_size = MD_SB_BYTES; |
| 979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 981 | |
| 982 | memset(sb, 0, sizeof(*sb)); |
| 983 | |
| 984 | sb->md_magic = MD_SB_MAGIC; |
| 985 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | sb->patch_version = mddev->patch_version; |
| 987 | sb->gvalid_words = 0; /* ignored */ |
| 988 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 989 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 990 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 991 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 992 | |
| 993 | sb->ctime = mddev->ctime; |
| 994 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 995 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | sb->raid_disks = mddev->raid_disks; |
| 997 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 998 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | sb->utime = mddev->utime; |
| 1000 | sb->state = 0; |
| 1001 | sb->events_hi = (mddev->events>>32); |
| 1002 | sb->events_lo = (u32)mddev->events; |
| 1003 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1004 | if (mddev->reshape_position == MaxSector) |
| 1005 | sb->minor_version = 90; |
| 1006 | else { |
| 1007 | sb->minor_version = 91; |
| 1008 | sb->reshape_position = mddev->reshape_position; |
| 1009 | sb->new_level = mddev->new_level; |
| 1010 | sb->delta_disks = mddev->delta_disks; |
| 1011 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1012 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1013 | } |
| 1014 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (mddev->in_sync) |
| 1016 | { |
| 1017 | sb->recovery_cp = mddev->recovery_cp; |
| 1018 | sb->cp_events_hi = (mddev->events>>32); |
| 1019 | sb->cp_events_lo = (u32)mddev->events; |
| 1020 | if (mddev->recovery_cp == MaxSector) |
| 1021 | sb->state = (1<< MD_SB_CLEAN); |
| 1022 | } else |
| 1023 | sb->recovery_cp = 0; |
| 1024 | |
| 1025 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1026 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1028 | if (mddev->bitmap && mddev->bitmap_file == NULL) |
| 1029 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1032 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1034 | int desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1035 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 1036 | && !test_bit(Faulty, &rdev2->flags)) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1037 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1039 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1040 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | d = &sb->disks[rdev2->desc_nr]; |
| 1042 | nr_disks++; |
| 1043 | d->number = rdev2->desc_nr; |
| 1044 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1045 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1046 | if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags) |
| 1047 | && !test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | d->raid_disk = rdev2->raid_disk; |
| 1049 | else |
| 1050 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1051 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1053 | else if (test_bit(In_sync, &rdev2->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | d->state = (1<<MD_DISK_ACTIVE); |
| 1055 | d->state |= (1<<MD_DISK_SYNC); |
| 1056 | active++; |
| 1057 | working++; |
| 1058 | } else { |
| 1059 | d->state = 0; |
| 1060 | spare++; |
| 1061 | working++; |
| 1062 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1063 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1064 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1067 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1068 | mdp_disk_t *d = &sb->disks[i]; |
| 1069 | if (d->state == 0 && d->number == 0) { |
| 1070 | d->number = i; |
| 1071 | d->raid_disk = i; |
| 1072 | d->state = (1<<MD_DISK_REMOVED); |
| 1073 | d->state |= (1<<MD_DISK_FAULTY); |
| 1074 | failed++; |
| 1075 | } |
| 1076 | } |
| 1077 | sb->nr_disks = nr_disks; |
| 1078 | sb->active_disks = active; |
| 1079 | sb->working_disks = working; |
| 1080 | sb->failed_disks = failed; |
| 1081 | sb->spare_disks = spare; |
| 1082 | |
| 1083 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1084 | sb->sb_csum = calc_sb_csum(sb); |
| 1085 | } |
| 1086 | |
| 1087 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1088 | * rdev_size_change for 0.90.0 |
| 1089 | */ |
| 1090 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1091 | 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] | 1092 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1093 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1094 | return 0; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1095 | if (rdev->mddev->bitmap_offset) |
| 1096 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1097 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1098 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1099 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1100 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1101 | rdev->sb_page); |
| 1102 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1103 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | * version 1 superblock |
| 1109 | */ |
| 1110 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1111 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1113 | __le32 disk_csum; |
| 1114 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | unsigned long long newcsum; |
| 1116 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1117 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | int i; |
| 1119 | |
| 1120 | disk_csum = sb->sb_csum; |
| 1121 | sb->sb_csum = 0; |
| 1122 | newcsum = 0; |
| 1123 | for (i=0; size>=4; size -= 4 ) |
| 1124 | newcsum += le32_to_cpu(*isuper++); |
| 1125 | |
| 1126 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1127 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
| 1129 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1130 | sb->sb_csum = disk_csum; |
| 1131 | return cpu_to_le32(csum); |
| 1132 | } |
| 1133 | |
| 1134 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1135 | { |
| 1136 | struct mdp_superblock_1 *sb; |
| 1137 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1138 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1140 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
| 1142 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1143 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | * It is always aligned to a 4K boundary and |
| 1145 | * depeding on minor_version, it can be: |
| 1146 | * 0: At least 8K, but less than 12K, from end of device |
| 1147 | * 1: At start of device |
| 1148 | * 2: 4K from start of device. |
| 1149 | */ |
| 1150 | switch(minor_version) { |
| 1151 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1152 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1153 | sb_start -= 8*2; |
| 1154 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | break; |
| 1156 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1157 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | break; |
| 1159 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1160 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | break; |
| 1162 | default: |
| 1163 | return -EINVAL; |
| 1164 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1165 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1167 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1168 | * and it is safe to read 4k, so we do that |
| 1169 | */ |
| 1170 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | if (ret) return ret; |
| 1172 | |
| 1173 | |
| 1174 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1175 | |
| 1176 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1177 | sb->major_version != cpu_to_le32(1) || |
| 1178 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1179 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1180 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | return -EINVAL; |
| 1182 | |
| 1183 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1184 | printk("md: invalid superblock checksum on %s\n", |
| 1185 | bdevname(rdev->bdev,b)); |
| 1186 | return -EINVAL; |
| 1187 | } |
| 1188 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1189 | printk("md: data_size too small on %s\n", |
| 1190 | bdevname(rdev->bdev,b)); |
| 1191 | return -EINVAL; |
| 1192 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | rdev->preferred_minor = 0xffff; |
| 1195 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1196 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1198 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1199 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1200 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1201 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1202 | |
| 1203 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1204 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1205 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1206 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1207 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1208 | rdev->desc_nr = -1; |
| 1209 | else |
| 1210 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1211 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1212 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1213 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1214 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | __u64 ev1, ev2; |
| 1216 | struct mdp_superblock_1 *refsb = |
| 1217 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1218 | |
| 1219 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1220 | sb->level != refsb->level || |
| 1221 | sb->layout != refsb->layout || |
| 1222 | sb->chunksize != refsb->chunksize) { |
| 1223 | printk(KERN_WARNING "md: %s has strangely different" |
| 1224 | " superblock to %s\n", |
| 1225 | bdevname(rdev->bdev,b), |
| 1226 | bdevname(refdev->bdev,b2)); |
| 1227 | return -EINVAL; |
| 1228 | } |
| 1229 | ev1 = le64_to_cpu(sb->events); |
| 1230 | ev2 = le64_to_cpu(refsb->events); |
| 1231 | |
| 1232 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1233 | ret = 1; |
| 1234 | else |
| 1235 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1237 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1238 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1239 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1241 | rdev->sectors = rdev->sb_start; |
| 1242 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1244 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1245 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1246 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1247 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1251 | { |
| 1252 | 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] | 1253 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1255 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1256 | clear_bit(Faulty, &rdev->flags); |
| 1257 | clear_bit(In_sync, &rdev->flags); |
| 1258 | clear_bit(WriteMostly, &rdev->flags); |
| 1259 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | if (mddev->raid_disks == 0) { |
| 1262 | mddev->major_version = 1; |
| 1263 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1264 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1265 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1267 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1268 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1269 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | mddev->layout = le32_to_cpu(sb->layout); |
| 1271 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1272 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1273 | mddev->events = ev1; |
NeilBrown | 9223214 | 2005-08-18 11:24:16 -0700 | [diff] [blame] | 1274 | mddev->bitmap_offset = 0; |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1275 | mddev->default_bitmap_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | |
| 1277 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1278 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1279 | |
| 1280 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1281 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1282 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1283 | mddev->bitmap_file == NULL ) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1284 | mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1285 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1286 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1287 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1288 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1289 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1290 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1291 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1292 | } else { |
| 1293 | mddev->reshape_position = MaxSector; |
| 1294 | mddev->delta_disks = 0; |
| 1295 | mddev->new_level = mddev->level; |
| 1296 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1297 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1298 | } |
| 1299 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1300 | } else if (mddev->pers == NULL) { |
| 1301 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | ++ev1; |
| 1303 | if (ev1 < mddev->events) |
| 1304 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1305 | } else if (mddev->bitmap) { |
| 1306 | /* If adding to array with a bitmap, then we can accept an |
| 1307 | * older device, but not too old. |
| 1308 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1309 | if (ev1 < mddev->bitmap->events_cleared) |
| 1310 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1311 | } else { |
| 1312 | if (ev1 < mddev->events) |
| 1313 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1314 | return 0; |
| 1315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1317 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1318 | if (rdev->desc_nr < 0 || |
| 1319 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1320 | role = 0xffff; |
| 1321 | rdev->desc_nr = -1; |
| 1322 | } else |
| 1323 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | switch(role) { |
| 1325 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | break; |
| 1327 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1328 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | break; |
| 1330 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1331 | if ((le32_to_cpu(sb->feature_map) & |
| 1332 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1333 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1334 | else |
| 1335 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | rdev->raid_disk = role; |
| 1337 | break; |
| 1338 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1339 | if (sb->devflags & WriteMostly1) |
| 1340 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1341 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1342 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1348 | { |
| 1349 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | mdk_rdev_t *rdev2; |
| 1351 | int max_dev, i; |
| 1352 | /* make rdev->sb match mddev and rdev data. */ |
| 1353 | |
| 1354 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1355 | |
| 1356 | sb->feature_map = 0; |
| 1357 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1358 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1360 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1361 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1362 | |
| 1363 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1364 | sb->events = cpu_to_le64(mddev->events); |
| 1365 | if (mddev->in_sync) |
| 1366 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1367 | else |
| 1368 | sb->resync_offset = cpu_to_le64(0); |
| 1369 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1370 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1371 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1372 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1373 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1374 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1375 | sb->level = cpu_to_le32(mddev->level); |
| 1376 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1377 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1378 | if (mddev->bitmap && mddev->bitmap_file == NULL) { |
| 1379 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1380 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1381 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1382 | |
| 1383 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1384 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1385 | if (rdev->recovery_offset > 0) { |
| 1386 | sb->feature_map |= |
| 1387 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1388 | sb->recovery_offset = |
| 1389 | cpu_to_le64(rdev->recovery_offset); |
| 1390 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1393 | if (mddev->reshape_position != MaxSector) { |
| 1394 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1395 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1396 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1397 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1398 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1399 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1400 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1403 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | if (rdev2->desc_nr+1 > max_dev) |
| 1405 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1406 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1407 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1408 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1409 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1410 | rdev->sb_size = max_dev * 2 + 256; |
| 1411 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1412 | if (rdev->sb_size & bmask) |
| 1413 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | for (i=0; i<max_dev;i++) |
| 1416 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1417 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1418 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1420 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1422 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1424 | else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0) |
| 1425 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | else |
| 1427 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1428 | } |
| 1429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1431 | } |
| 1432 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1433 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1434 | 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] | 1435 | { |
| 1436 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1437 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1438 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1439 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1440 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1441 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1442 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1443 | max_sectors -= rdev->data_offset; |
| 1444 | if (!num_sectors || num_sectors > max_sectors) |
| 1445 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1446 | } else if (rdev->mddev->bitmap_offset) { |
| 1447 | /* minor version 0 with bitmap we can't move */ |
| 1448 | return 0; |
| 1449 | } else { |
| 1450 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1451 | sector_t sb_start; |
| 1452 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1453 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1454 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1455 | if (!num_sectors || num_sectors > max_sectors) |
| 1456 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1457 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1458 | } |
| 1459 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1460 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1461 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1462 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1463 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1464 | rdev->sb_page); |
| 1465 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1466 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1469 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | [0] = { |
| 1471 | .name = "0.90.0", |
| 1472 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1473 | .load_super = super_90_load, |
| 1474 | .validate_super = super_90_validate, |
| 1475 | .sync_super = super_90_sync, |
| 1476 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | }, |
| 1478 | [1] = { |
| 1479 | .name = "md-1", |
| 1480 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1481 | .load_super = super_1_load, |
| 1482 | .validate_super = super_1_validate, |
| 1483 | .sync_super = super_1_sync, |
| 1484 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | }, |
| 1486 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
| 1488 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1489 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1490 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1492 | rcu_read_lock(); |
| 1493 | rdev_for_each_rcu(rdev, mddev1) |
| 1494 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1495 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1496 | rdev2->bdev->bd_contains) { |
| 1497 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1498 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1499 | } |
| 1500 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | return 0; |
| 1502 | } |
| 1503 | |
| 1504 | static LIST_HEAD(pending_raid_disks); |
| 1505 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1506 | /* |
| 1507 | * Try to register data integrity profile for an mddev |
| 1508 | * |
| 1509 | * This is called when an array is started and after a disk has been kicked |
| 1510 | * from the array. It only succeeds if all working and active component devices |
| 1511 | * are integrity capable with matching profiles. |
| 1512 | */ |
| 1513 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1514 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1515 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1516 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1517 | if (list_empty(&mddev->disks)) |
| 1518 | return 0; /* nothing to do */ |
| 1519 | if (blk_get_integrity(mddev->gendisk)) |
| 1520 | return 0; /* already registered */ |
| 1521 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1522 | /* skip spares and non-functional disks */ |
| 1523 | if (test_bit(Faulty, &rdev->flags)) |
| 1524 | continue; |
| 1525 | if (rdev->raid_disk < 0) |
| 1526 | continue; |
| 1527 | /* |
| 1528 | * If at least one rdev is not integrity capable, we can not |
| 1529 | * enable data integrity for the md device. |
| 1530 | */ |
| 1531 | if (!bdev_get_integrity(rdev->bdev)) |
| 1532 | return -EINVAL; |
| 1533 | if (!reference) { |
| 1534 | /* Use the first rdev as the reference */ |
| 1535 | reference = rdev; |
| 1536 | continue; |
| 1537 | } |
| 1538 | /* does this rdev's profile match the reference profile? */ |
| 1539 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1540 | rdev->bdev->bd_disk) < 0) |
| 1541 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1542 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1543 | /* |
| 1544 | * All component devices are integrity capable and have matching |
| 1545 | * profiles, register the common profile for the md device. |
| 1546 | */ |
| 1547 | if (blk_integrity_register(mddev->gendisk, |
| 1548 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1549 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1550 | mdname(mddev)); |
| 1551 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1552 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1553 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1554 | mdname(mddev)); |
| 1555 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1556 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1557 | EXPORT_SYMBOL(md_integrity_register); |
| 1558 | |
| 1559 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1560 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1561 | { |
| 1562 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1563 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1564 | |
| 1565 | if (!bi_mddev) /* nothing to do */ |
| 1566 | return; |
| 1567 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1568 | return; |
| 1569 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1570 | rdev->bdev->bd_disk) >= 0) |
| 1571 | return; |
| 1572 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1573 | blk_integrity_unregister(mddev->gendisk); |
| 1574 | } |
| 1575 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1578 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1579 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1580 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1581 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1582 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
| 1584 | if (rdev->mddev) { |
| 1585 | MD_BUG(); |
| 1586 | return -EINVAL; |
| 1587 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1588 | |
| 1589 | /* prevent duplicates */ |
| 1590 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1591 | return -EEXIST; |
| 1592 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1593 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1594 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1595 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1596 | if (mddev->pers) { |
| 1597 | /* Cannot change size, so fail |
| 1598 | * If mddev->level <= 0, then we don't care |
| 1599 | * about aligning sizes (e.g. linear) |
| 1600 | */ |
| 1601 | if (mddev->level > 0) |
| 1602 | return -ENOSPC; |
| 1603 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1604 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | |
| 1607 | /* Verify rdev->desc_nr is unique. |
| 1608 | * If it is -1, assign a free number, else |
| 1609 | * check number is not in use |
| 1610 | */ |
| 1611 | if (rdev->desc_nr < 0) { |
| 1612 | int choice = 0; |
| 1613 | if (mddev->pers) choice = mddev->raid_disks; |
| 1614 | while (find_rdev_nr(mddev, choice)) |
| 1615 | choice++; |
| 1616 | rdev->desc_nr = choice; |
| 1617 | } else { |
| 1618 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1619 | return -EBUSY; |
| 1620 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1621 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1622 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1623 | mdname(mddev), mddev->max_disks); |
| 1624 | return -EBUSY; |
| 1625 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1626 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1627 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1628 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1631 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1632 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1633 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1634 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1635 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1636 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1637 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1638 | kobject_del(&rdev->kobj); |
| 1639 | goto fail; |
| 1640 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1641 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1642 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1643 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1644 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1645 | |
| 1646 | /* May as well allow recovery to be retried once */ |
| 1647 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1650 | |
| 1651 | fail: |
| 1652 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1653 | b, mdname(mddev)); |
| 1654 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1657 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1658 | { |
| 1659 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1660 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1661 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1665 | { |
| 1666 | char b[BDEVNAME_SIZE]; |
| 1667 | if (!rdev->mddev) { |
| 1668 | MD_BUG(); |
| 1669 | return; |
| 1670 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1671 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1672 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1674 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1675 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1676 | sysfs_put(rdev->sysfs_state); |
| 1677 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1678 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1679 | * writing to 'remove' to "dev/state". We also need |
| 1680 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1681 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1682 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1683 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1684 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1685 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * prevent the device from being mounted, repartitioned or |
| 1690 | * otherwise reused by a RAID array (or any other kernel |
| 1691 | * subsystem), by bd_claiming the device. |
| 1692 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1693 | 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] | 1694 | { |
| 1695 | int err = 0; |
| 1696 | struct block_device *bdev; |
| 1697 | char b[BDEVNAME_SIZE]; |
| 1698 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1699 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | if (IS_ERR(bdev)) { |
| 1701 | printk(KERN_ERR "md: could not open %s.\n", |
| 1702 | __bdevname(dev, b)); |
| 1703 | return PTR_ERR(bdev); |
| 1704 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1705 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | if (err) { |
| 1707 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1708 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1709 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | return err; |
| 1711 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1712 | if (!shared) |
| 1713 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | rdev->bdev = bdev; |
| 1715 | return err; |
| 1716 | } |
| 1717 | |
| 1718 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1719 | { |
| 1720 | struct block_device *bdev = rdev->bdev; |
| 1721 | rdev->bdev = NULL; |
| 1722 | if (!bdev) |
| 1723 | MD_BUG(); |
| 1724 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1725 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | void md_autodetect_dev(dev_t dev); |
| 1729 | |
| 1730 | static void export_rdev(mdk_rdev_t * rdev) |
| 1731 | { |
| 1732 | char b[BDEVNAME_SIZE]; |
| 1733 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1734 | bdevname(rdev->bdev,b)); |
| 1735 | if (rdev->mddev) |
| 1736 | MD_BUG(); |
| 1737 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1739 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1740 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | #endif |
| 1742 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1743 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1747 | { |
| 1748 | unbind_rdev_from_array(rdev); |
| 1749 | export_rdev(rdev); |
| 1750 | } |
| 1751 | |
| 1752 | static void export_array(mddev_t *mddev) |
| 1753 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1754 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1756 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | if (!rdev->mddev) { |
| 1758 | MD_BUG(); |
| 1759 | continue; |
| 1760 | } |
| 1761 | kick_rdev_from_array(rdev); |
| 1762 | } |
| 1763 | if (!list_empty(&mddev->disks)) |
| 1764 | MD_BUG(); |
| 1765 | mddev->raid_disks = 0; |
| 1766 | mddev->major_version = 0; |
| 1767 | } |
| 1768 | |
| 1769 | static void print_desc(mdp_disk_t *desc) |
| 1770 | { |
| 1771 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1772 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1773 | } |
| 1774 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1775 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | { |
| 1777 | int i; |
| 1778 | |
| 1779 | printk(KERN_INFO |
| 1780 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1781 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1782 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1783 | sb->ctime); |
| 1784 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1785 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1786 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1787 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1788 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1789 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1790 | sb->failed_disks, sb->spare_disks, |
| 1791 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1792 | |
| 1793 | printk(KERN_INFO); |
| 1794 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1795 | mdp_disk_t *desc; |
| 1796 | |
| 1797 | desc = sb->disks + i; |
| 1798 | if (desc->number || desc->major || desc->minor || |
| 1799 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1800 | printk(" D %2d: ", i); |
| 1801 | print_desc(desc); |
| 1802 | } |
| 1803 | } |
| 1804 | printk(KERN_INFO "md: THIS: "); |
| 1805 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1808 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1809 | { |
| 1810 | __u8 *uuid; |
| 1811 | |
| 1812 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1813 | printk(KERN_INFO |
| 1814 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x" |
| 1815 | ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n" |
| 1816 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1817 | le32_to_cpu(sb->major_version), |
| 1818 | le32_to_cpu(sb->feature_map), |
| 1819 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1820 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1821 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1822 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1823 | sb->set_name, |
| 1824 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1825 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1826 | |
| 1827 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1828 | printk(KERN_INFO |
| 1829 | "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] | 1830 | " RO:%llu\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1831 | "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x" |
| 1832 | ":%02x%02x%02x%02x%02x%02x\n" |
| 1833 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1834 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1835 | le32_to_cpu(sb->level), |
| 1836 | (unsigned long long)le64_to_cpu(sb->size), |
| 1837 | le32_to_cpu(sb->raid_disks), |
| 1838 | le32_to_cpu(sb->layout), |
| 1839 | le32_to_cpu(sb->chunksize), |
| 1840 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 1841 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 1842 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 1843 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 1844 | le32_to_cpu(sb->dev_number), |
| 1845 | uuid[0], uuid[1], uuid[2], uuid[3], |
| 1846 | uuid[4], uuid[5], uuid[6], uuid[7], |
| 1847 | uuid[8], uuid[9], uuid[10], uuid[11], |
| 1848 | uuid[12], uuid[13], uuid[14], uuid[15], |
| 1849 | sb->devflags, |
| 1850 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 1851 | (unsigned long long)le64_to_cpu(sb->events), |
| 1852 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 1853 | le32_to_cpu(sb->sb_csum), |
| 1854 | le32_to_cpu(sb->max_dev) |
| 1855 | ); |
| 1856 | } |
| 1857 | |
| 1858 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | { |
| 1860 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1861 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 1862 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1863 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 1864 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1866 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 1867 | switch (major_version) { |
| 1868 | case 0: |
| 1869 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 1870 | break; |
| 1871 | case 1: |
| 1872 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 1873 | break; |
| 1874 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | } else |
| 1876 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 1877 | } |
| 1878 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 1879 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1881 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | mdk_rdev_t *rdev; |
| 1883 | mddev_t *mddev; |
| 1884 | char b[BDEVNAME_SIZE]; |
| 1885 | |
| 1886 | printk("\n"); |
| 1887 | printk("md: **********************************\n"); |
| 1888 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 1889 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 1890 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 1892 | if (mddev->bitmap) |
| 1893 | bitmap_print_sb(mddev->bitmap); |
| 1894 | else |
| 1895 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1896 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 1898 | printk("\n"); |
| 1899 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1900 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1901 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1902 | } |
| 1903 | printk("md: **********************************\n"); |
| 1904 | printk("\n"); |
| 1905 | } |
| 1906 | |
| 1907 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1908 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1910 | /* Update each superblock (in-memory image), but |
| 1911 | * if we are allowed to, skip spares which already |
| 1912 | * have the right event counter, or have one earlier |
| 1913 | * (which would mean they aren't being marked as dirty |
| 1914 | * with the rest of the array) |
| 1915 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame^] | 1918 | /* First make sure individual recovery_offsets are correct */ |
| 1919 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1920 | if (rdev->raid_disk >= 0 && |
| 1921 | !test_bit(In_sync, &rdev->flags) && |
| 1922 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 1923 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 1924 | |
| 1925 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1926 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1927 | if (rdev->sb_events == mddev->events || |
| 1928 | (nospares && |
| 1929 | rdev->raid_disk < 0 && |
| 1930 | (rdev->sb_events&1)==0 && |
| 1931 | rdev->sb_events+1 == mddev->events)) { |
| 1932 | /* Don't update this superblock */ |
| 1933 | rdev->sb_loaded = 2; |
| 1934 | } else { |
| 1935 | super_types[mddev->major_version]. |
| 1936 | sync_super(mddev, rdev); |
| 1937 | rdev->sb_loaded = 1; |
| 1938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1942 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1945 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1946 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 1948 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 1949 | if (mddev->external) |
| 1950 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1952 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1953 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1954 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 1955 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 1956 | force_change = 1; |
| 1957 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 1958 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 1959 | * though if events isn't the right even/odd, we will have to do |
| 1960 | * spares after all |
| 1961 | */ |
| 1962 | nospares = 1; |
| 1963 | if (force_change) |
| 1964 | nospares = 0; |
| 1965 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1966 | /* If the array is degraded, then skipping spares is both |
| 1967 | * dangerous and fairly pointless. |
| 1968 | * Dangerous because a device that was removed from the array |
| 1969 | * might have a event_count that still looks up-to-date, |
| 1970 | * so it can be re-added without a resync. |
| 1971 | * Pointless because if there are any spares to skip, |
| 1972 | * then a recovery will happen and soon that array won't |
| 1973 | * be degraded any more and the spare can go back to sleep then. |
| 1974 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1975 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 1976 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1977 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1978 | |
| 1979 | /* If this is just a dirty<->clean transition, and the array is clean |
| 1980 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1981 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1982 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 1983 | && (mddev->events & 1) |
| 1984 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1985 | mddev->events--; |
| 1986 | else { |
| 1987 | /* otherwise we have to go forward and ... */ |
| 1988 | mddev->events ++; |
| 1989 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 1990 | /* .. if the array isn't clean, an 'even' event must also go |
| 1991 | * to spares. */ |
| 1992 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1993 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1994 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 1995 | /* otherwise an 'odd' event must go to spares */ |
| 1996 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1997 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 1998 | } |
| 1999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | |
| 2001 | if (!mddev->events) { |
| 2002 | /* |
| 2003 | * oops, this 64-bit counter should never wrap. |
| 2004 | * Either we are in around ~1 trillion A.C., assuming |
| 2005 | * 1 reboot per second, or we have a bug: |
| 2006 | */ |
| 2007 | MD_BUG(); |
| 2008 | mddev->events --; |
| 2009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
| 2011 | /* |
| 2012 | * do not write anything to disk if using |
| 2013 | * nonpersistent superblocks |
| 2014 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2015 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2016 | if (!mddev->external) |
| 2017 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2018 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2019 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2020 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2022 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2023 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2024 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
| 2026 | dprintk(KERN_INFO |
| 2027 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2028 | mdname(mddev),mddev->in_sync); |
| 2029 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2030 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2031 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | char b[BDEVNAME_SIZE]; |
| 2033 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2034 | if (rdev->sb_loaded != 1) |
| 2035 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2036 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | dprintk("(skipping faulty "); |
| 2038 | |
| 2039 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2040 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2041 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2042 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2043 | rdev->sb_page); |
| 2044 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2045 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2046 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2047 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | } else |
| 2050 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2051 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | /* only need to write one superblock... */ |
| 2053 | break; |
| 2054 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2055 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2056 | /* 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] | 2057 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2058 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2059 | if (mddev->in_sync != sync_req || |
| 2060 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2061 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2062 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2063 | goto repeat; |
| 2064 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2065 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2066 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2067 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2068 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2069 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | } |
| 2072 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2073 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2074 | * We want to accept with case. For this we use cmd_match. |
| 2075 | */ |
| 2076 | static int cmd_match(const char *cmd, const char *str) |
| 2077 | { |
| 2078 | /* See if cmd, written into a sysfs file, matches |
| 2079 | * str. They must either be the same, or cmd can |
| 2080 | * have a trailing newline |
| 2081 | */ |
| 2082 | while (*cmd && *str && *cmd == *str) { |
| 2083 | cmd++; |
| 2084 | str++; |
| 2085 | } |
| 2086 | if (*cmd == '\n') |
| 2087 | cmd++; |
| 2088 | if (*str || *cmd) |
| 2089 | return 0; |
| 2090 | return 1; |
| 2091 | } |
| 2092 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2093 | struct rdev_sysfs_entry { |
| 2094 | struct attribute attr; |
| 2095 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2096 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2097 | }; |
| 2098 | |
| 2099 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2100 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2101 | { |
| 2102 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2103 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2104 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2105 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2106 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2107 | sep = ","; |
| 2108 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2109 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2110 | len += sprintf(page+len, "%sin_sync",sep); |
| 2111 | sep = ","; |
| 2112 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2113 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2114 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2115 | sep = ","; |
| 2116 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2117 | if (test_bit(Blocked, &rdev->flags)) { |
| 2118 | len += sprintf(page+len, "%sblocked", sep); |
| 2119 | sep = ","; |
| 2120 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2121 | if (!test_bit(Faulty, &rdev->flags) && |
| 2122 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2123 | len += sprintf(page+len, "%sspare", sep); |
| 2124 | sep = ","; |
| 2125 | } |
| 2126 | return len+sprintf(page+len, "\n"); |
| 2127 | } |
| 2128 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2129 | static ssize_t |
| 2130 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2131 | { |
| 2132 | /* can write |
| 2133 | * faulty - simulates and error |
| 2134 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2135 | * writemostly - sets write_mostly |
| 2136 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2137 | * blocked - sets the Blocked flag |
| 2138 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2139 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2140 | */ |
| 2141 | int err = -EINVAL; |
| 2142 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2143 | md_error(rdev->mddev, rdev); |
| 2144 | err = 0; |
| 2145 | } else if (cmd_match(buf, "remove")) { |
| 2146 | if (rdev->raid_disk >= 0) |
| 2147 | err = -EBUSY; |
| 2148 | else { |
| 2149 | mddev_t *mddev = rdev->mddev; |
| 2150 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2151 | if (mddev->pers) |
| 2152 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2153 | md_new_event(mddev); |
| 2154 | err = 0; |
| 2155 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2156 | } else if (cmd_match(buf, "writemostly")) { |
| 2157 | set_bit(WriteMostly, &rdev->flags); |
| 2158 | err = 0; |
| 2159 | } else if (cmd_match(buf, "-writemostly")) { |
| 2160 | clear_bit(WriteMostly, &rdev->flags); |
| 2161 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2162 | } else if (cmd_match(buf, "blocked")) { |
| 2163 | set_bit(Blocked, &rdev->flags); |
| 2164 | err = 0; |
| 2165 | } else if (cmd_match(buf, "-blocked")) { |
| 2166 | clear_bit(Blocked, &rdev->flags); |
| 2167 | wake_up(&rdev->blocked_wait); |
| 2168 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2169 | md_wakeup_thread(rdev->mddev->thread); |
| 2170 | |
| 2171 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2172 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2173 | set_bit(In_sync, &rdev->flags); |
| 2174 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2175 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2176 | if (!err && rdev->sysfs_state) |
| 2177 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2178 | return err ? err : len; |
| 2179 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2180 | static struct rdev_sysfs_entry rdev_state = |
| 2181 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2182 | |
| 2183 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2184 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2185 | { |
| 2186 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2187 | } |
| 2188 | |
| 2189 | static ssize_t |
| 2190 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2191 | { |
| 2192 | char *e; |
| 2193 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2194 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2195 | atomic_set(&rdev->corrected_errors, n); |
| 2196 | return len; |
| 2197 | } |
| 2198 | return -EINVAL; |
| 2199 | } |
| 2200 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2201 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2202 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2203 | static ssize_t |
| 2204 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2205 | { |
| 2206 | if (rdev->raid_disk < 0) |
| 2207 | return sprintf(page, "none\n"); |
| 2208 | else |
| 2209 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2210 | } |
| 2211 | |
| 2212 | static ssize_t |
| 2213 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2214 | { |
| 2215 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2216 | int err; |
| 2217 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2218 | int slot = simple_strtoul(buf, &e, 10); |
| 2219 | if (strncmp(buf, "none", 4)==0) |
| 2220 | slot = -1; |
| 2221 | else if (e==buf || (*e && *e!= '\n')) |
| 2222 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2223 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2224 | /* Setting 'slot' on an active array requires also |
| 2225 | * updating the 'rd%d' link, and communicating |
| 2226 | * with the personality with ->hot_*_disk. |
| 2227 | * For now we only support removing |
| 2228 | * failed/spare devices. This normally happens automatically, |
| 2229 | * but not when the metadata is externally managed. |
| 2230 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2231 | if (rdev->raid_disk == -1) |
| 2232 | return -EEXIST; |
| 2233 | /* personality does all needed checks */ |
| 2234 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2235 | return -EINVAL; |
| 2236 | err = rdev->mddev->pers-> |
| 2237 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2238 | if (err) |
| 2239 | return err; |
| 2240 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2241 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
| 2242 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2243 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2244 | } else if (rdev->mddev->pers) { |
| 2245 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2246 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2247 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2248 | */ |
| 2249 | |
| 2250 | if (rdev->raid_disk != -1) |
| 2251 | return -EBUSY; |
| 2252 | |
| 2253 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2254 | return -EINVAL; |
| 2255 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2256 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2257 | if (rdev2->raid_disk == slot) |
| 2258 | return -EEXIST; |
| 2259 | |
| 2260 | rdev->raid_disk = slot; |
| 2261 | if (test_bit(In_sync, &rdev->flags)) |
| 2262 | rdev->saved_raid_disk = slot; |
| 2263 | else |
| 2264 | rdev->saved_raid_disk = -1; |
| 2265 | err = rdev->mddev->pers-> |
| 2266 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2267 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2268 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2269 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2270 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2271 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2272 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2273 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2274 | printk(KERN_WARNING |
| 2275 | "md: cannot register " |
| 2276 | "%s for %s\n", |
| 2277 | nm, mdname(rdev->mddev)); |
| 2278 | |
| 2279 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2280 | } else { |
| 2281 | if (slot >= rdev->mddev->raid_disks) |
| 2282 | return -ENOSPC; |
| 2283 | rdev->raid_disk = slot; |
| 2284 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2285 | clear_bit(Faulty, &rdev->flags); |
| 2286 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2287 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2288 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2289 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2290 | return len; |
| 2291 | } |
| 2292 | |
| 2293 | |
| 2294 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2295 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2296 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2297 | static ssize_t |
| 2298 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2299 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2300 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | static ssize_t |
| 2304 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2305 | { |
| 2306 | char *e; |
| 2307 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2308 | if (e==buf || (*e && *e != '\n')) |
| 2309 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2310 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2311 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2312 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2313 | /* Must set offset before size, so overlap checks |
| 2314 | * can be sane */ |
| 2315 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2316 | rdev->data_offset = offset; |
| 2317 | return len; |
| 2318 | } |
| 2319 | |
| 2320 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2321 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2322 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2323 | static ssize_t |
| 2324 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2325 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2326 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2329 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2330 | { |
| 2331 | /* check if two start/length pairs overlap */ |
| 2332 | if (s1+l1 <= s2) |
| 2333 | return 0; |
| 2334 | if (s2+l2 <= s1) |
| 2335 | return 0; |
| 2336 | return 1; |
| 2337 | } |
| 2338 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2339 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2340 | { |
| 2341 | unsigned long long blocks; |
| 2342 | sector_t new; |
| 2343 | |
| 2344 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2345 | return -EINVAL; |
| 2346 | |
| 2347 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2348 | return -EINVAL; /* sector conversion overflow */ |
| 2349 | |
| 2350 | new = blocks * 2; |
| 2351 | if (new != blocks * 2) |
| 2352 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2353 | |
| 2354 | *sectors = new; |
| 2355 | return 0; |
| 2356 | } |
| 2357 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2358 | static ssize_t |
| 2359 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2360 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2361 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2362 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2363 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2364 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2365 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2366 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2367 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2368 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2369 | sectors = super_types[my_mddev->major_version]. |
| 2370 | rdev_size_change(rdev, sectors); |
| 2371 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2372 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2373 | } else if (!sectors) |
| 2374 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2375 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2376 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2377 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2378 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2379 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2380 | rdev->sectors = sectors; |
| 2381 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2382 | /* need to check that all other rdevs with the same ->bdev |
| 2383 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2384 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2385 | * we have to change it back, we will have the lock again. |
| 2386 | */ |
| 2387 | mddev_t *mddev; |
| 2388 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2389 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2390 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2391 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2392 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2393 | mdk_rdev_t *rdev2; |
| 2394 | |
| 2395 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2396 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2397 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2398 | (rdev->bdev == rdev2->bdev && |
| 2399 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2400 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2401 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2402 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2403 | overlap = 1; |
| 2404 | break; |
| 2405 | } |
| 2406 | mddev_unlock(mddev); |
| 2407 | if (overlap) { |
| 2408 | mddev_put(mddev); |
| 2409 | break; |
| 2410 | } |
| 2411 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2412 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2413 | if (overlap) { |
| 2414 | /* Someone else could have slipped in a size |
| 2415 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2416 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2417 | * safe, and trust userspace not to race with |
| 2418 | * itself |
| 2419 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2420 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2421 | return -EBUSY; |
| 2422 | } |
| 2423 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2424 | return len; |
| 2425 | } |
| 2426 | |
| 2427 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2428 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2429 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2430 | static struct attribute *rdev_default_attrs[] = { |
| 2431 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2432 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2433 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2434 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2435 | &rdev_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2436 | NULL, |
| 2437 | }; |
| 2438 | static ssize_t |
| 2439 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2440 | { |
| 2441 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2442 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2443 | mddev_t *mddev = rdev->mddev; |
| 2444 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2445 | |
| 2446 | if (!entry->show) |
| 2447 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2448 | |
| 2449 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2450 | if (!rv) { |
| 2451 | if (rdev->mddev == NULL) |
| 2452 | rv = -EBUSY; |
| 2453 | else |
| 2454 | rv = entry->show(rdev, page); |
| 2455 | mddev_unlock(mddev); |
| 2456 | } |
| 2457 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | static ssize_t |
| 2461 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2462 | const char *page, size_t length) |
| 2463 | { |
| 2464 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2465 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2466 | ssize_t rv; |
| 2467 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2468 | |
| 2469 | if (!entry->store) |
| 2470 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2471 | if (!capable(CAP_SYS_ADMIN)) |
| 2472 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2473 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2474 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2475 | if (rdev->mddev == NULL) |
| 2476 | rv = -EBUSY; |
| 2477 | else |
| 2478 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2479 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2480 | } |
| 2481 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | static void rdev_free(struct kobject *ko) |
| 2485 | { |
| 2486 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2487 | kfree(rdev); |
| 2488 | } |
| 2489 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2490 | .show = rdev_attr_show, |
| 2491 | .store = rdev_attr_store, |
| 2492 | }; |
| 2493 | static struct kobj_type rdev_ktype = { |
| 2494 | .release = rdev_free, |
| 2495 | .sysfs_ops = &rdev_sysfs_ops, |
| 2496 | .default_attrs = rdev_default_attrs, |
| 2497 | }; |
| 2498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | /* |
| 2500 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2501 | * |
| 2502 | * mark the device faulty if: |
| 2503 | * |
| 2504 | * - the device is nonexistent (zero size) |
| 2505 | * - the device has no valid superblock |
| 2506 | * |
| 2507 | * a faulty rdev _never_ has rdev->sb set. |
| 2508 | */ |
| 2509 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2510 | { |
| 2511 | char b[BDEVNAME_SIZE]; |
| 2512 | int err; |
| 2513 | mdk_rdev_t *rdev; |
| 2514 | sector_t size; |
| 2515 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2516 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | if (!rdev) { |
| 2518 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2519 | return ERR_PTR(-ENOMEM); |
| 2520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | |
| 2522 | if ((err = alloc_disk_sb(rdev))) |
| 2523 | goto abort_free; |
| 2524 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2525 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | if (err) |
| 2527 | goto abort_free; |
| 2528 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2529 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2532 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2533 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2534 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2536 | rdev->sb_events = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2538 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2539 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | |
| 2541 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2542 | if (!size) { |
| 2543 | printk(KERN_WARNING |
| 2544 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2545 | bdevname(rdev->bdev,b)); |
| 2546 | err = -EINVAL; |
| 2547 | goto abort_free; |
| 2548 | } |
| 2549 | |
| 2550 | if (super_format >= 0) { |
| 2551 | err = super_types[super_format]. |
| 2552 | load_super(rdev, NULL, super_minor); |
| 2553 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2554 | printk(KERN_WARNING |
| 2555 | "md: %s does not have a valid v%d.%d " |
| 2556 | "superblock, not importing!\n", |
| 2557 | bdevname(rdev->bdev,b), |
| 2558 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | goto abort_free; |
| 2560 | } |
| 2561 | if (err < 0) { |
| 2562 | printk(KERN_WARNING |
| 2563 | "md: could not read %s's sb, not importing!\n", |
| 2564 | bdevname(rdev->bdev,b)); |
| 2565 | goto abort_free; |
| 2566 | } |
| 2567 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2570 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | |
| 2572 | return rdev; |
| 2573 | |
| 2574 | abort_free: |
| 2575 | if (rdev->sb_page) { |
| 2576 | if (rdev->bdev) |
| 2577 | unlock_rdev(rdev); |
| 2578 | free_disk_sb(rdev); |
| 2579 | } |
| 2580 | kfree(rdev); |
| 2581 | return ERR_PTR(err); |
| 2582 | } |
| 2583 | |
| 2584 | /* |
| 2585 | * Check a full RAID array for plausibility |
| 2586 | */ |
| 2587 | |
| 2588 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2589 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | { |
| 2591 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2592 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | char b[BDEVNAME_SIZE]; |
| 2594 | |
| 2595 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2596 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | switch (super_types[mddev->major_version]. |
| 2598 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2599 | case 1: |
| 2600 | freshest = rdev; |
| 2601 | break; |
| 2602 | case 0: |
| 2603 | break; |
| 2604 | default: |
| 2605 | printk( KERN_ERR \ |
| 2606 | "md: fatal superblock inconsistency in %s" |
| 2607 | " -- removing from array\n", |
| 2608 | bdevname(rdev->bdev,b)); |
| 2609 | kick_rdev_from_array(rdev); |
| 2610 | } |
| 2611 | |
| 2612 | |
| 2613 | super_types[mddev->major_version]. |
| 2614 | validate_super(mddev, freshest); |
| 2615 | |
| 2616 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2617 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2618 | if (rdev->desc_nr >= mddev->max_disks || |
| 2619 | i > mddev->max_disks) { |
| 2620 | printk(KERN_WARNING |
| 2621 | "md: %s: %s: only %d devices permitted\n", |
| 2622 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2623 | mddev->max_disks); |
| 2624 | kick_rdev_from_array(rdev); |
| 2625 | continue; |
| 2626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | if (rdev != freshest) |
| 2628 | if (super_types[mddev->major_version]. |
| 2629 | validate_super(mddev, rdev)) { |
| 2630 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2631 | " from array!\n", |
| 2632 | bdevname(rdev->bdev,b)); |
| 2633 | kick_rdev_from_array(rdev); |
| 2634 | continue; |
| 2635 | } |
| 2636 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2637 | rdev->desc_nr = i++; |
| 2638 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2639 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2640 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2641 | rdev->raid_disk = -1; |
| 2642 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | } |
| 2644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2647 | static void md_safemode_timeout(unsigned long data); |
| 2648 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2649 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2650 | safe_delay_show(mddev_t *mddev, char *page) |
| 2651 | { |
| 2652 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2653 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2654 | } |
| 2655 | static ssize_t |
| 2656 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2657 | { |
| 2658 | int scale=1; |
| 2659 | int dot=0; |
| 2660 | int i; |
| 2661 | unsigned long msec; |
| 2662 | char buf[30]; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2663 | |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2664 | /* remove a period, and count digits after it */ |
| 2665 | if (len >= sizeof(buf)) |
| 2666 | return -EINVAL; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2667 | strlcpy(buf, cbuf, sizeof(buf)); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2668 | for (i=0; i<len; i++) { |
| 2669 | if (dot) { |
| 2670 | if (isdigit(buf[i])) { |
| 2671 | buf[i-1] = buf[i]; |
| 2672 | scale *= 10; |
| 2673 | } |
| 2674 | buf[i] = 0; |
| 2675 | } else if (buf[i] == '.') { |
| 2676 | dot=1; |
| 2677 | buf[i] = 0; |
| 2678 | } |
| 2679 | } |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2680 | if (strict_strtoul(buf, 10, &msec) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2681 | return -EINVAL; |
| 2682 | msec = (msec * 1000) / scale; |
| 2683 | if (msec == 0) |
| 2684 | mddev->safemode_delay = 0; |
| 2685 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2686 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2687 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2688 | if (mddev->safemode_delay == 0) |
| 2689 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2690 | if (mddev->safemode_delay < old_delay) |
| 2691 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2692 | } |
| 2693 | return len; |
| 2694 | } |
| 2695 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2696 | __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] | 2697 | |
| 2698 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2699 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2700 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2701 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2702 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2703 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2704 | else if (mddev->clevel[0]) |
| 2705 | return sprintf(page, "%s\n", mddev->clevel); |
| 2706 | else if (mddev->level != LEVEL_NONE) |
| 2707 | return sprintf(page, "%d\n", mddev->level); |
| 2708 | else |
| 2709 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2710 | } |
| 2711 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2712 | static ssize_t |
| 2713 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2714 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2715 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2716 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2717 | struct mdk_personality *pers; |
| 2718 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2719 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2720 | |
| 2721 | if (mddev->pers == NULL) { |
| 2722 | if (len == 0) |
| 2723 | return 0; |
| 2724 | if (len >= sizeof(mddev->clevel)) |
| 2725 | return -ENOSPC; |
| 2726 | strncpy(mddev->clevel, buf, len); |
| 2727 | if (mddev->clevel[len-1] == '\n') |
| 2728 | len--; |
| 2729 | mddev->clevel[len] = 0; |
| 2730 | mddev->level = LEVEL_NONE; |
| 2731 | return rv; |
| 2732 | } |
| 2733 | |
| 2734 | /* request to change the personality. Need to ensure: |
| 2735 | * - array is not engaged in resync/recovery/reshape |
| 2736 | * - old personality can be suspended |
| 2737 | * - new personality will access other array. |
| 2738 | */ |
| 2739 | |
| 2740 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2741 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2742 | |
| 2743 | if (!mddev->pers->quiesce) { |
| 2744 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2745 | mdname(mddev), mddev->pers->name); |
| 2746 | return -EINVAL; |
| 2747 | } |
| 2748 | |
| 2749 | /* Now find the new personality */ |
| 2750 | if (len == 0 || len >= sizeof(level)) |
| 2751 | return -EINVAL; |
| 2752 | strncpy(level, buf, len); |
| 2753 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2754 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2755 | level[len] = 0; |
| 2756 | |
| 2757 | request_module("md-%s", level); |
| 2758 | spin_lock(&pers_lock); |
| 2759 | pers = find_pers(LEVEL_NONE, level); |
| 2760 | if (!pers || !try_module_get(pers->owner)) { |
| 2761 | spin_unlock(&pers_lock); |
| 2762 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2763 | return -EINVAL; |
| 2764 | } |
| 2765 | spin_unlock(&pers_lock); |
| 2766 | |
| 2767 | if (pers == mddev->pers) { |
| 2768 | /* Nothing to do! */ |
| 2769 | module_put(pers->owner); |
| 2770 | return rv; |
| 2771 | } |
| 2772 | if (!pers->takeover) { |
| 2773 | module_put(pers->owner); |
| 2774 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2775 | mdname(mddev), level); |
| 2776 | return -EINVAL; |
| 2777 | } |
| 2778 | |
| 2779 | /* ->takeover must set new_* and/or delta_disks |
| 2780 | * if it succeeds, and may set them when it fails. |
| 2781 | */ |
| 2782 | priv = pers->takeover(mddev); |
| 2783 | if (IS_ERR(priv)) { |
| 2784 | mddev->new_level = mddev->level; |
| 2785 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2786 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2787 | mddev->raid_disks -= mddev->delta_disks; |
| 2788 | mddev->delta_disks = 0; |
| 2789 | module_put(pers->owner); |
| 2790 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 2791 | mdname(mddev), level); |
| 2792 | return PTR_ERR(priv); |
| 2793 | } |
| 2794 | |
| 2795 | /* Looks like we have a winner */ |
| 2796 | mddev_suspend(mddev); |
| 2797 | mddev->pers->stop(mddev); |
| 2798 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2799 | /* Invalidate devices that are now superfluous */ |
| 2800 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2801 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 2802 | rdev->raid_disk = -1; |
| 2803 | clear_bit(In_sync, &rdev->flags); |
| 2804 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2805 | mddev->pers = pers; |
| 2806 | mddev->private = priv; |
| 2807 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 2808 | mddev->level = mddev->new_level; |
| 2809 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2810 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2811 | mddev->delta_disks = 0; |
| 2812 | pers->run(mddev); |
| 2813 | mddev_resume(mddev); |
| 2814 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2815 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2816 | md_wakeup_thread(mddev->thread); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2817 | return rv; |
| 2818 | } |
| 2819 | |
| 2820 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2821 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2822 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2823 | |
| 2824 | static ssize_t |
| 2825 | layout_show(mddev_t *mddev, char *page) |
| 2826 | { |
| 2827 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2828 | if (mddev->reshape_position != MaxSector && |
| 2829 | mddev->layout != mddev->new_layout) |
| 2830 | return sprintf(page, "%d (%d)\n", |
| 2831 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2832 | return sprintf(page, "%d\n", mddev->layout); |
| 2833 | } |
| 2834 | |
| 2835 | static ssize_t |
| 2836 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 2837 | { |
| 2838 | char *e; |
| 2839 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2840 | |
| 2841 | if (!*buf || (*e && *e != '\n')) |
| 2842 | return -EINVAL; |
| 2843 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2844 | if (mddev->pers) { |
| 2845 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2846 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2847 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2848 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2849 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2850 | if (err) { |
| 2851 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2852 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2853 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2854 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2855 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2856 | if (mddev->reshape_position == MaxSector) |
| 2857 | mddev->layout = n; |
| 2858 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2859 | return len; |
| 2860 | } |
| 2861 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2862 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 2863 | |
| 2864 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2865 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2866 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2867 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 2868 | if (mddev->raid_disks == 0) |
| 2869 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2870 | if (mddev->reshape_position != MaxSector && |
| 2871 | mddev->delta_disks != 0) |
| 2872 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 2873 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2874 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 2875 | } |
| 2876 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2877 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 2878 | |
| 2879 | static ssize_t |
| 2880 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 2881 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2882 | char *e; |
| 2883 | int rv = 0; |
| 2884 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2885 | |
| 2886 | if (!*buf || (*e && *e != '\n')) |
| 2887 | return -EINVAL; |
| 2888 | |
| 2889 | if (mddev->pers) |
| 2890 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2891 | else if (mddev->reshape_position != MaxSector) { |
| 2892 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 2893 | mddev->delta_disks = n - olddisks; |
| 2894 | mddev->raid_disks = n; |
| 2895 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 2896 | mddev->raid_disks = n; |
| 2897 | return rv ? rv : len; |
| 2898 | } |
| 2899 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2900 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2901 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 2902 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2903 | chunk_size_show(mddev_t *mddev, char *page) |
| 2904 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 2905 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2906 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 2907 | return sprintf(page, "%d (%d)\n", |
| 2908 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2909 | mddev->chunk_sectors << 9); |
| 2910 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | static ssize_t |
| 2914 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 2915 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2916 | char *e; |
| 2917 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2918 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2919 | if (!*buf || (*e && *e != '\n')) |
| 2920 | return -EINVAL; |
| 2921 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2922 | if (mddev->pers) { |
| 2923 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2924 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2925 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2926 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 2927 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2928 | if (err) { |
| 2929 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2930 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 2931 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2932 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2933 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2934 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2935 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 2936 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2937 | return len; |
| 2938 | } |
| 2939 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2940 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 2941 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2942 | static ssize_t |
| 2943 | resync_start_show(mddev_t *mddev, char *page) |
| 2944 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 2945 | if (mddev->recovery_cp == MaxSector) |
| 2946 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2947 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 2948 | } |
| 2949 | |
| 2950 | static ssize_t |
| 2951 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 2952 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2953 | char *e; |
| 2954 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 2955 | |
| 2956 | if (mddev->pers) |
| 2957 | return -EBUSY; |
| 2958 | if (!*buf || (*e && *e != '\n')) |
| 2959 | return -EINVAL; |
| 2960 | |
| 2961 | mddev->recovery_cp = n; |
| 2962 | return len; |
| 2963 | } |
| 2964 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2965 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 2966 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2967 | /* |
| 2968 | * The array state can be: |
| 2969 | * |
| 2970 | * clear |
| 2971 | * No devices, no size, no level |
| 2972 | * Equivalent to STOP_ARRAY ioctl |
| 2973 | * inactive |
| 2974 | * May have some settings, but array is not active |
| 2975 | * all IO results in error |
| 2976 | * When written, doesn't tear down array, but just stops it |
| 2977 | * suspended (not supported yet) |
| 2978 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 2979 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2980 | * readonly |
| 2981 | * no resync can happen. no superblocks get written. |
| 2982 | * write requests fail |
| 2983 | * read-auto |
| 2984 | * like readonly, but behaves like 'clean' on a write request. |
| 2985 | * |
| 2986 | * clean - no pending writes, but otherwise active. |
| 2987 | * When written to inactive array, starts without resync |
| 2988 | * If a write request arrives then |
| 2989 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 2990 | * if not known, block and switch to write-pending |
| 2991 | * If written to an active array that has pending writes, then fails. |
| 2992 | * active |
| 2993 | * fully active: IO and resync can be happening. |
| 2994 | * When written to inactive array, starts with resync |
| 2995 | * |
| 2996 | * write-pending |
| 2997 | * clean, but writes are blocked waiting for 'active' to be written. |
| 2998 | * |
| 2999 | * active-idle |
| 3000 | * like active, but no writes have been seen for a while (100msec). |
| 3001 | * |
| 3002 | */ |
| 3003 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3004 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3005 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3006 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3007 | "write-pending", "active-idle", NULL }; |
| 3008 | |
| 3009 | static int match_word(const char *word, char **list) |
| 3010 | { |
| 3011 | int n; |
| 3012 | for (n=0; list[n]; n++) |
| 3013 | if (cmd_match(word, list[n])) |
| 3014 | break; |
| 3015 | return n; |
| 3016 | } |
| 3017 | |
| 3018 | static ssize_t |
| 3019 | array_state_show(mddev_t *mddev, char *page) |
| 3020 | { |
| 3021 | enum array_state st = inactive; |
| 3022 | |
| 3023 | if (mddev->pers) |
| 3024 | switch(mddev->ro) { |
| 3025 | case 1: |
| 3026 | st = readonly; |
| 3027 | break; |
| 3028 | case 2: |
| 3029 | st = read_auto; |
| 3030 | break; |
| 3031 | case 0: |
| 3032 | if (mddev->in_sync) |
| 3033 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3034 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3035 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3036 | else if (mddev->safemode) |
| 3037 | st = active_idle; |
| 3038 | else |
| 3039 | st = active; |
| 3040 | } |
| 3041 | else { |
| 3042 | if (list_empty(&mddev->disks) && |
| 3043 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3044 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3045 | st = clear; |
| 3046 | else |
| 3047 | st = inactive; |
| 3048 | } |
| 3049 | return sprintf(page, "%s\n", array_states[st]); |
| 3050 | } |
| 3051 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3052 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3053 | static int do_md_run(mddev_t * mddev); |
| 3054 | static int restart_array(mddev_t *mddev); |
| 3055 | |
| 3056 | static ssize_t |
| 3057 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3058 | { |
| 3059 | int err = -EINVAL; |
| 3060 | enum array_state st = match_word(buf, array_states); |
| 3061 | switch(st) { |
| 3062 | case bad_word: |
| 3063 | break; |
| 3064 | case clear: |
| 3065 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3066 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3067 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3068 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3069 | break; |
| 3070 | case inactive: |
| 3071 | /* stopping an active array */ |
| 3072 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3073 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3074 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3075 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3076 | } else |
| 3077 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3078 | break; |
| 3079 | case suspended: |
| 3080 | break; /* not supported yet */ |
| 3081 | case readonly: |
| 3082 | if (mddev->pers) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3083 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3084 | else { |
| 3085 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3086 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3087 | err = do_md_run(mddev); |
| 3088 | } |
| 3089 | break; |
| 3090 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3091 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3092 | if (mddev->ro == 0) |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3093 | err = do_md_stop(mddev, 1, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3094 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3095 | err = restart_array(mddev); |
| 3096 | if (err == 0) { |
| 3097 | mddev->ro = 2; |
| 3098 | set_disk_ro(mddev->gendisk, 0); |
| 3099 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3100 | } else { |
| 3101 | mddev->ro = 2; |
| 3102 | err = do_md_run(mddev); |
| 3103 | } |
| 3104 | break; |
| 3105 | case clean: |
| 3106 | if (mddev->pers) { |
| 3107 | restart_array(mddev); |
| 3108 | spin_lock_irq(&mddev->write_lock); |
| 3109 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3110 | if (mddev->in_sync == 0) { |
| 3111 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3112 | if (mddev->safemode == 1) |
| 3113 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3114 | if (mddev->persistent) |
| 3115 | set_bit(MD_CHANGE_CLEAN, |
| 3116 | &mddev->flags); |
| 3117 | } |
| 3118 | err = 0; |
| 3119 | } else |
| 3120 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3121 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3122 | } else |
| 3123 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3124 | break; |
| 3125 | case active: |
| 3126 | if (mddev->pers) { |
| 3127 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3128 | if (mddev->external) |
| 3129 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3130 | wake_up(&mddev->sb_wait); |
| 3131 | err = 0; |
| 3132 | } else { |
| 3133 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3134 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3135 | err = do_md_run(mddev); |
| 3136 | } |
| 3137 | break; |
| 3138 | case write_pending: |
| 3139 | case active_idle: |
| 3140 | /* these cannot be set */ |
| 3141 | break; |
| 3142 | } |
| 3143 | if (err) |
| 3144 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3145 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3146 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3147 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3148 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3149 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3150 | static struct md_sysfs_entry md_array_state = |
| 3151 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3152 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3153 | static ssize_t |
| 3154 | null_show(mddev_t *mddev, char *page) |
| 3155 | { |
| 3156 | return -EINVAL; |
| 3157 | } |
| 3158 | |
| 3159 | static ssize_t |
| 3160 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3161 | { |
| 3162 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3163 | /* The new device is added to the array. |
| 3164 | * If the array has a persistent superblock, we read the |
| 3165 | * superblock to initialise info and check validity. |
| 3166 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3167 | * which mainly checks size. |
| 3168 | */ |
| 3169 | char *e; |
| 3170 | int major = simple_strtoul(buf, &e, 10); |
| 3171 | int minor; |
| 3172 | dev_t dev; |
| 3173 | mdk_rdev_t *rdev; |
| 3174 | int err; |
| 3175 | |
| 3176 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3177 | return -EINVAL; |
| 3178 | minor = simple_strtoul(e+1, &e, 10); |
| 3179 | if (*e && *e != '\n') |
| 3180 | return -EINVAL; |
| 3181 | dev = MKDEV(major, minor); |
| 3182 | if (major != MAJOR(dev) || |
| 3183 | minor != MINOR(dev)) |
| 3184 | return -EOVERFLOW; |
| 3185 | |
| 3186 | |
| 3187 | if (mddev->persistent) { |
| 3188 | rdev = md_import_device(dev, mddev->major_version, |
| 3189 | mddev->minor_version); |
| 3190 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3191 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3192 | mdk_rdev_t, same_set); |
| 3193 | err = super_types[mddev->major_version] |
| 3194 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3195 | if (err < 0) |
| 3196 | goto out; |
| 3197 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3198 | } else if (mddev->external) |
| 3199 | rdev = md_import_device(dev, -2, -1); |
| 3200 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3201 | rdev = md_import_device(dev, -1, -1); |
| 3202 | |
| 3203 | if (IS_ERR(rdev)) |
| 3204 | return PTR_ERR(rdev); |
| 3205 | err = bind_rdev_to_array(rdev, mddev); |
| 3206 | out: |
| 3207 | if (err) |
| 3208 | export_rdev(rdev); |
| 3209 | return err ? err : len; |
| 3210 | } |
| 3211 | |
| 3212 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3213 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3214 | |
| 3215 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3216 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3217 | { |
| 3218 | char *end; |
| 3219 | unsigned long chunk, end_chunk; |
| 3220 | |
| 3221 | if (!mddev->bitmap) |
| 3222 | goto out; |
| 3223 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3224 | while (*buf) { |
| 3225 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3226 | if (buf == end) break; |
| 3227 | if (*end == '-') { /* range */ |
| 3228 | buf = end + 1; |
| 3229 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3230 | if (buf == end) break; |
| 3231 | } |
| 3232 | if (*end && !isspace(*end)) break; |
| 3233 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
| 3234 | buf = end; |
| 3235 | while (isspace(*buf)) buf++; |
| 3236 | } |
| 3237 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3238 | out: |
| 3239 | return len; |
| 3240 | } |
| 3241 | |
| 3242 | static struct md_sysfs_entry md_bitmap = |
| 3243 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3244 | |
| 3245 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3246 | size_show(mddev_t *mddev, char *page) |
| 3247 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3248 | return sprintf(page, "%llu\n", |
| 3249 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3250 | } |
| 3251 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3252 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3253 | |
| 3254 | static ssize_t |
| 3255 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3256 | { |
| 3257 | /* If array is inactive, we can reduce the component size, but |
| 3258 | * not increase it (except from 0). |
| 3259 | * If array is active, we can try an on-line resize |
| 3260 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3261 | sector_t sectors; |
| 3262 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3263 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3264 | if (err < 0) |
| 3265 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3266 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3267 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3268 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3269 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3270 | if (mddev->dev_sectors == 0 || |
| 3271 | mddev->dev_sectors > sectors) |
| 3272 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3273 | else |
| 3274 | err = -ENOSPC; |
| 3275 | } |
| 3276 | return err ? err : len; |
| 3277 | } |
| 3278 | |
| 3279 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3280 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3281 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3282 | |
| 3283 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3284 | * This is one of |
| 3285 | * 'none' for arrays with no metadata (good luck...) |
| 3286 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3287 | * or N.M for internally known formats |
| 3288 | */ |
| 3289 | static ssize_t |
| 3290 | metadata_show(mddev_t *mddev, char *page) |
| 3291 | { |
| 3292 | if (mddev->persistent) |
| 3293 | return sprintf(page, "%d.%d\n", |
| 3294 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3295 | else if (mddev->external) |
| 3296 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3297 | else |
| 3298 | return sprintf(page, "none\n"); |
| 3299 | } |
| 3300 | |
| 3301 | static ssize_t |
| 3302 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3303 | { |
| 3304 | int major, minor; |
| 3305 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3306 | /* Changing the details of 'external' metadata is |
| 3307 | * always permitted. Otherwise there must be |
| 3308 | * no devices attached to the array. |
| 3309 | */ |
| 3310 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3311 | ; |
| 3312 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3313 | return -EBUSY; |
| 3314 | |
| 3315 | if (cmd_match(buf, "none")) { |
| 3316 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3317 | mddev->external = 0; |
| 3318 | mddev->major_version = 0; |
| 3319 | mddev->minor_version = 90; |
| 3320 | return len; |
| 3321 | } |
| 3322 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3323 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3324 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3325 | namelen = sizeof(mddev->metadata_type)-1; |
| 3326 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3327 | mddev->metadata_type[namelen] = 0; |
| 3328 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3329 | mddev->metadata_type[--namelen] = 0; |
| 3330 | mddev->persistent = 0; |
| 3331 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3332 | mddev->major_version = 0; |
| 3333 | mddev->minor_version = 90; |
| 3334 | return len; |
| 3335 | } |
| 3336 | major = simple_strtoul(buf, &e, 10); |
| 3337 | if (e==buf || *e != '.') |
| 3338 | return -EINVAL; |
| 3339 | buf = e+1; |
| 3340 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3341 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3342 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3343 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3344 | return -ENOENT; |
| 3345 | mddev->major_version = major; |
| 3346 | mddev->minor_version = minor; |
| 3347 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3348 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3349 | return len; |
| 3350 | } |
| 3351 | |
| 3352 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3353 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3354 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3355 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3356 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3357 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3358 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3359 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3360 | type = "frozen"; |
| 3361 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3362 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3363 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3364 | type = "reshape"; |
| 3365 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3366 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3367 | type = "resync"; |
| 3368 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3369 | type = "check"; |
| 3370 | else |
| 3371 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3372 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3373 | type = "recover"; |
| 3374 | } |
| 3375 | return sprintf(page, "%s\n", type); |
| 3376 | } |
| 3377 | |
| 3378 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3379 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3380 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3381 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3382 | return -EINVAL; |
| 3383 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3384 | if (cmd_match(page, "frozen")) |
| 3385 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3386 | else |
| 3387 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3388 | |
| 3389 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3390 | if (mddev->sync_thread) { |
| 3391 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3392 | md_unregister_thread(mddev->sync_thread); |
| 3393 | mddev->sync_thread = NULL; |
| 3394 | mddev->recovery = 0; |
| 3395 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3396 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3397 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3398 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3399 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3400 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3401 | else if (cmd_match(page, "recover")) { |
| 3402 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3403 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3404 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3405 | int err; |
| 3406 | if (mddev->pers->start_reshape == NULL) |
| 3407 | return -EINVAL; |
| 3408 | err = mddev->pers->start_reshape(mddev); |
| 3409 | if (err) |
| 3410 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3411 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3412 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3413 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3414 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3415 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3416 | return -EINVAL; |
| 3417 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3418 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3419 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3420 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3421 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3422 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3423 | return len; |
| 3424 | } |
| 3425 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3426 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3427 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3428 | { |
| 3429 | return sprintf(page, "%llu\n", |
| 3430 | (unsigned long long) mddev->resync_mismatches); |
| 3431 | } |
| 3432 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3433 | static struct md_sysfs_entry md_scan_mode = |
| 3434 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3435 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3436 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3437 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3438 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3439 | static ssize_t |
| 3440 | sync_min_show(mddev_t *mddev, char *page) |
| 3441 | { |
| 3442 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3443 | mddev->sync_speed_min ? "local": "system"); |
| 3444 | } |
| 3445 | |
| 3446 | static ssize_t |
| 3447 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3448 | { |
| 3449 | int min; |
| 3450 | char *e; |
| 3451 | if (strncmp(buf, "system", 6)==0) { |
| 3452 | mddev->sync_speed_min = 0; |
| 3453 | return len; |
| 3454 | } |
| 3455 | min = simple_strtoul(buf, &e, 10); |
| 3456 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3457 | return -EINVAL; |
| 3458 | mddev->sync_speed_min = min; |
| 3459 | return len; |
| 3460 | } |
| 3461 | |
| 3462 | static struct md_sysfs_entry md_sync_min = |
| 3463 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3464 | |
| 3465 | static ssize_t |
| 3466 | sync_max_show(mddev_t *mddev, char *page) |
| 3467 | { |
| 3468 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3469 | mddev->sync_speed_max ? "local": "system"); |
| 3470 | } |
| 3471 | |
| 3472 | static ssize_t |
| 3473 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3474 | { |
| 3475 | int max; |
| 3476 | char *e; |
| 3477 | if (strncmp(buf, "system", 6)==0) { |
| 3478 | mddev->sync_speed_max = 0; |
| 3479 | return len; |
| 3480 | } |
| 3481 | max = simple_strtoul(buf, &e, 10); |
| 3482 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3483 | return -EINVAL; |
| 3484 | mddev->sync_speed_max = max; |
| 3485 | return len; |
| 3486 | } |
| 3487 | |
| 3488 | static struct md_sysfs_entry md_sync_max = |
| 3489 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3490 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3491 | static ssize_t |
| 3492 | degraded_show(mddev_t *mddev, char *page) |
| 3493 | { |
| 3494 | return sprintf(page, "%d\n", mddev->degraded); |
| 3495 | } |
| 3496 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3497 | |
| 3498 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3499 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3500 | { |
| 3501 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3502 | } |
| 3503 | |
| 3504 | static ssize_t |
| 3505 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3506 | { |
| 3507 | long n; |
| 3508 | |
| 3509 | if (strict_strtol(buf, 10, &n)) |
| 3510 | return -EINVAL; |
| 3511 | |
| 3512 | if (n != 0 && n != 1) |
| 3513 | return -EINVAL; |
| 3514 | |
| 3515 | mddev->parallel_resync = n; |
| 3516 | |
| 3517 | if (mddev->sync_thread) |
| 3518 | wake_up(&resync_wait); |
| 3519 | |
| 3520 | return len; |
| 3521 | } |
| 3522 | |
| 3523 | /* force parallel resync, even with shared block devices */ |
| 3524 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3525 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3526 | sync_force_parallel_show, sync_force_parallel_store); |
| 3527 | |
| 3528 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3529 | sync_speed_show(mddev_t *mddev, char *page) |
| 3530 | { |
| 3531 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3532 | if (mddev->curr_resync == 0) |
| 3533 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3534 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3535 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3536 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3537 | db = resync - mddev->resync_mark_cnt; |
| 3538 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3539 | } |
| 3540 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3541 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3542 | |
| 3543 | static ssize_t |
| 3544 | sync_completed_show(mddev_t *mddev, char *page) |
| 3545 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3546 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3547 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3548 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3549 | return sprintf(page, "none\n"); |
| 3550 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3551 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3552 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3553 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3554 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3555 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3556 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3557 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
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_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3561 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3562 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3563 | min_sync_show(mddev_t *mddev, char *page) |
| 3564 | { |
| 3565 | return sprintf(page, "%llu\n", |
| 3566 | (unsigned long long)mddev->resync_min); |
| 3567 | } |
| 3568 | static ssize_t |
| 3569 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3570 | { |
| 3571 | unsigned long long min; |
| 3572 | if (strict_strtoull(buf, 10, &min)) |
| 3573 | return -EINVAL; |
| 3574 | if (min > mddev->resync_max) |
| 3575 | return -EINVAL; |
| 3576 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3577 | return -EBUSY; |
| 3578 | |
| 3579 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3580 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3581 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3582 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3583 | return -EINVAL; |
| 3584 | } |
| 3585 | mddev->resync_min = min; |
| 3586 | |
| 3587 | return len; |
| 3588 | } |
| 3589 | |
| 3590 | static struct md_sysfs_entry md_min_sync = |
| 3591 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3592 | |
| 3593 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3594 | max_sync_show(mddev_t *mddev, char *page) |
| 3595 | { |
| 3596 | if (mddev->resync_max == MaxSector) |
| 3597 | return sprintf(page, "max\n"); |
| 3598 | else |
| 3599 | return sprintf(page, "%llu\n", |
| 3600 | (unsigned long long)mddev->resync_max); |
| 3601 | } |
| 3602 | static ssize_t |
| 3603 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3604 | { |
| 3605 | if (strncmp(buf, "max", 3) == 0) |
| 3606 | mddev->resync_max = MaxSector; |
| 3607 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3608 | unsigned long long max; |
| 3609 | if (strict_strtoull(buf, 10, &max)) |
| 3610 | return -EINVAL; |
| 3611 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3612 | return -EINVAL; |
| 3613 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3614 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3615 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3616 | return -EBUSY; |
| 3617 | |
| 3618 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3619 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3620 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3621 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3622 | return -EINVAL; |
| 3623 | } |
| 3624 | mddev->resync_max = max; |
| 3625 | } |
| 3626 | wake_up(&mddev->recovery_wait); |
| 3627 | return len; |
| 3628 | } |
| 3629 | |
| 3630 | static struct md_sysfs_entry md_max_sync = |
| 3631 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3632 | |
| 3633 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3634 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3635 | { |
| 3636 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3637 | } |
| 3638 | |
| 3639 | static ssize_t |
| 3640 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3641 | { |
| 3642 | char *e; |
| 3643 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3644 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3645 | if (mddev->pers == NULL || |
| 3646 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3647 | return -EINVAL; |
| 3648 | if (buf == e || (*e && *e != '\n')) |
| 3649 | return -EINVAL; |
| 3650 | if (new >= mddev->suspend_hi || |
| 3651 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3652 | mddev->suspend_lo = new; |
| 3653 | mddev->pers->quiesce(mddev, 2); |
| 3654 | return len; |
| 3655 | } else |
| 3656 | return -EINVAL; |
| 3657 | } |
| 3658 | static struct md_sysfs_entry md_suspend_lo = |
| 3659 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3660 | |
| 3661 | |
| 3662 | static ssize_t |
| 3663 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3664 | { |
| 3665 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3666 | } |
| 3667 | |
| 3668 | static ssize_t |
| 3669 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3670 | { |
| 3671 | char *e; |
| 3672 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3673 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3674 | if (mddev->pers == NULL || |
| 3675 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3676 | return -EINVAL; |
| 3677 | if (buf == e || (*e && *e != '\n')) |
| 3678 | return -EINVAL; |
| 3679 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3680 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3681 | mddev->suspend_hi = new; |
| 3682 | mddev->pers->quiesce(mddev, 1); |
| 3683 | mddev->pers->quiesce(mddev, 0); |
| 3684 | return len; |
| 3685 | } else |
| 3686 | return -EINVAL; |
| 3687 | } |
| 3688 | static struct md_sysfs_entry md_suspend_hi = |
| 3689 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3690 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3691 | static ssize_t |
| 3692 | reshape_position_show(mddev_t *mddev, char *page) |
| 3693 | { |
| 3694 | if (mddev->reshape_position != MaxSector) |
| 3695 | return sprintf(page, "%llu\n", |
| 3696 | (unsigned long long)mddev->reshape_position); |
| 3697 | strcpy(page, "none\n"); |
| 3698 | return 5; |
| 3699 | } |
| 3700 | |
| 3701 | static ssize_t |
| 3702 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3703 | { |
| 3704 | char *e; |
| 3705 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3706 | if (mddev->pers) |
| 3707 | return -EBUSY; |
| 3708 | if (buf == e || (*e && *e != '\n')) |
| 3709 | return -EINVAL; |
| 3710 | mddev->reshape_position = new; |
| 3711 | mddev->delta_disks = 0; |
| 3712 | mddev->new_level = mddev->level; |
| 3713 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3714 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3715 | return len; |
| 3716 | } |
| 3717 | |
| 3718 | static struct md_sysfs_entry md_reshape_position = |
| 3719 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 3720 | reshape_position_store); |
| 3721 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3722 | static ssize_t |
| 3723 | array_size_show(mddev_t *mddev, char *page) |
| 3724 | { |
| 3725 | if (mddev->external_size) |
| 3726 | return sprintf(page, "%llu\n", |
| 3727 | (unsigned long long)mddev->array_sectors/2); |
| 3728 | else |
| 3729 | return sprintf(page, "default\n"); |
| 3730 | } |
| 3731 | |
| 3732 | static ssize_t |
| 3733 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3734 | { |
| 3735 | sector_t sectors; |
| 3736 | |
| 3737 | if (strncmp(buf, "default", 7) == 0) { |
| 3738 | if (mddev->pers) |
| 3739 | sectors = mddev->pers->size(mddev, 0, 0); |
| 3740 | else |
| 3741 | sectors = mddev->array_sectors; |
| 3742 | |
| 3743 | mddev->external_size = 0; |
| 3744 | } else { |
| 3745 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 3746 | return -EINVAL; |
| 3747 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3748 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3749 | |
| 3750 | mddev->external_size = 1; |
| 3751 | } |
| 3752 | |
| 3753 | mddev->array_sectors = sectors; |
| 3754 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 3755 | if (mddev->pers) |
| 3756 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3757 | |
| 3758 | return len; |
| 3759 | } |
| 3760 | |
| 3761 | static struct md_sysfs_entry md_array_size = |
| 3762 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 3763 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3764 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3765 | static struct attribute *md_default_attrs[] = { |
| 3766 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3767 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3768 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3769 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3770 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3771 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3772 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3773 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3774 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3775 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3776 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3777 | &md_array_size.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3778 | NULL, |
| 3779 | }; |
| 3780 | |
| 3781 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3782 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3783 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3784 | &md_sync_min.attr, |
| 3785 | &md_sync_max.attr, |
| 3786 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3787 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3788 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3789 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3790 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3791 | &md_suspend_lo.attr, |
| 3792 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3793 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3794 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3795 | NULL, |
| 3796 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 3797 | static struct attribute_group md_redundancy_group = { |
| 3798 | .name = NULL, |
| 3799 | .attrs = md_redundancy_attrs, |
| 3800 | }; |
| 3801 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3802 | |
| 3803 | static ssize_t |
| 3804 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3805 | { |
| 3806 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3807 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3808 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3809 | |
| 3810 | if (!entry->show) |
| 3811 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3812 | rv = mddev_lock(mddev); |
| 3813 | if (!rv) { |
| 3814 | rv = entry->show(mddev, page); |
| 3815 | mddev_unlock(mddev); |
| 3816 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3817 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | static ssize_t |
| 3821 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3822 | const char *page, size_t length) |
| 3823 | { |
| 3824 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 3825 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3826 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3827 | |
| 3828 | if (!entry->store) |
| 3829 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3830 | if (!capable(CAP_SYS_ADMIN)) |
| 3831 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3832 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3833 | if (mddev->hold_active == UNTIL_IOCTL) |
| 3834 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 3835 | if (!rv) { |
| 3836 | rv = entry->store(mddev, page, length); |
| 3837 | mddev_unlock(mddev); |
| 3838 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3839 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | static void md_free(struct kobject *ko) |
| 3843 | { |
| 3844 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 3845 | |
| 3846 | if (mddev->sysfs_state) |
| 3847 | sysfs_put(mddev->sysfs_state); |
| 3848 | |
| 3849 | if (mddev->gendisk) { |
| 3850 | del_gendisk(mddev->gendisk); |
| 3851 | put_disk(mddev->gendisk); |
| 3852 | } |
| 3853 | if (mddev->queue) |
| 3854 | blk_cleanup_queue(mddev->queue); |
| 3855 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3856 | kfree(mddev); |
| 3857 | } |
| 3858 | |
| 3859 | static struct sysfs_ops md_sysfs_ops = { |
| 3860 | .show = md_attr_show, |
| 3861 | .store = md_attr_store, |
| 3862 | }; |
| 3863 | static struct kobj_type md_ktype = { |
| 3864 | .release = md_free, |
| 3865 | .sysfs_ops = &md_sysfs_ops, |
| 3866 | .default_attrs = md_default_attrs, |
| 3867 | }; |
| 3868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | int mdp_major = 0; |
| 3870 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 3871 | static void mddev_delayed_delete(struct work_struct *ws) |
| 3872 | { |
| 3873 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 3874 | |
| 3875 | if (mddev->private == &md_redundancy_group) { |
| 3876 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 3877 | if (mddev->sysfs_action) |
| 3878 | sysfs_put(mddev->sysfs_action); |
| 3879 | mddev->sysfs_action = NULL; |
| 3880 | mddev->private = NULL; |
| 3881 | } |
| 3882 | kobject_del(&mddev->kobj); |
| 3883 | kobject_put(&mddev->kobj); |
| 3884 | } |
| 3885 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3886 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3887 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3888 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | mddev_t *mddev = mddev_find(dev); |
| 3890 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3891 | int partitioned; |
| 3892 | int shift; |
| 3893 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3894 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | |
| 3896 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3897 | return -ENODEV; |
| 3898 | |
| 3899 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 3900 | shift = partitioned ? MdpMinorShift : 0; |
| 3901 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3903 | /* wait for any previous instance if this device |
| 3904 | * to be completed removed (mddev_delayed_delete). |
| 3905 | */ |
| 3906 | flush_scheduled_work(); |
| 3907 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 3908 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3909 | error = -EEXIST; |
| 3910 | if (mddev->gendisk) |
| 3911 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3912 | |
| 3913 | if (name) { |
| 3914 | /* Need to ensure that 'name' is not a duplicate. |
| 3915 | */ |
| 3916 | mddev_t *mddev2; |
| 3917 | spin_lock(&all_mddevs_lock); |
| 3918 | |
| 3919 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 3920 | if (mddev2->gendisk && |
| 3921 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 3922 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3923 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3924 | } |
| 3925 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3927 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3928 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3929 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3930 | if (!mddev->queue) |
| 3931 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3932 | mddev->queue->queuedata = mddev; |
| 3933 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3934 | /* Can be unlocked because the queue is new: no concurrency */ |
| 3935 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 3936 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3937 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | disk = alloc_disk(1 << shift); |
| 3940 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3941 | blk_cleanup_queue(mddev->queue); |
| 3942 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3943 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3945 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3947 | if (name) |
| 3948 | strcpy(disk->disk_name, name); |
| 3949 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3951 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | disk->fops = &md_fops; |
| 3954 | disk->private_data = mddev; |
| 3955 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3956 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3957 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 3958 | * remove it now. |
| 3959 | */ |
| 3960 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | add_disk(disk); |
| 3962 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 3963 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 3964 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3965 | if (error) { |
| 3966 | /* This isn't possible, but as kobject_init_and_add is marked |
| 3967 | * __must_check, we must do something with the result |
| 3968 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 3969 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 3970 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3971 | error = 0; |
| 3972 | } |
| 3973 | abort: |
| 3974 | mutex_unlock(&disks_mutex); |
| 3975 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 3976 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3977 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 3978 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3979 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 3980 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3981 | } |
| 3982 | |
| 3983 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 3984 | { |
| 3985 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | return NULL; |
| 3987 | } |
| 3988 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 3989 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 3990 | { |
| 3991 | /* val must be "md_*" where * is not all digits. |
| 3992 | * We allocate an array with a large free minor number, and |
| 3993 | * set the name to val. val must not already be an active name. |
| 3994 | */ |
| 3995 | int len = strlen(val); |
| 3996 | char buf[DISK_NAME_LEN]; |
| 3997 | |
| 3998 | while (len && val[len-1] == '\n') |
| 3999 | len--; |
| 4000 | if (len >= DISK_NAME_LEN) |
| 4001 | return -E2BIG; |
| 4002 | strlcpy(buf, val, len+1); |
| 4003 | if (strncmp(buf, "md_", 3) != 0) |
| 4004 | return -EINVAL; |
| 4005 | return md_alloc(0, buf); |
| 4006 | } |
| 4007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | static void md_safemode_timeout(unsigned long data) |
| 4009 | { |
| 4010 | mddev_t *mddev = (mddev_t *) data; |
| 4011 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4012 | if (!atomic_read(&mddev->writes_pending)) { |
| 4013 | mddev->safemode = 1; |
| 4014 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4015 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4016 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | md_wakeup_thread(mddev->thread); |
| 4018 | } |
| 4019 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4020 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4021 | |
| 4022 | static int do_md_run(mddev_t * mddev) |
| 4023 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4024 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | mdk_rdev_t *rdev; |
| 4026 | struct gendisk *disk; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4027 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4029 | if (list_empty(&mddev->disks)) |
| 4030 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | |
| 4033 | if (mddev->pers) |
| 4034 | return -EBUSY; |
| 4035 | |
| 4036 | /* |
| 4037 | * Analyze all RAID superblock(s) |
| 4038 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4039 | if (!mddev->raid_disks) { |
| 4040 | if (!mddev->persistent) |
| 4041 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4042 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4045 | if (mddev->level != LEVEL_NONE) |
| 4046 | request_module("md-level-%d", mddev->level); |
| 4047 | else if (mddev->clevel[0]) |
| 4048 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | |
| 4050 | /* |
| 4051 | * Drop all container device buffers, from now on |
| 4052 | * the only valid external interface is through the md |
| 4053 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4055 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4056 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4057 | continue; |
| 4058 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4059 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4060 | |
| 4061 | /* perform some consistency tests on the device. |
| 4062 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4063 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4064 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4065 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4066 | if (mddev->dev_sectors && |
| 4067 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4068 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4069 | printk("md: %s: data overlaps metadata\n", |
| 4070 | mdname(mddev)); |
| 4071 | return -EINVAL; |
| 4072 | } |
| 4073 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4074 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4075 | > rdev->data_offset) { |
| 4076 | printk("md: %s: metadata overlaps data\n", |
| 4077 | mdname(mddev)); |
| 4078 | return -EINVAL; |
| 4079 | } |
| 4080 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4081 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | md_probe(mddev->unit, NULL, NULL); |
| 4085 | disk = mddev->gendisk; |
| 4086 | if (!disk) |
| 4087 | return -ENOMEM; |
| 4088 | |
| 4089 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4090 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4091 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4093 | if (mddev->level != LEVEL_NONE) |
| 4094 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4095 | mddev->level); |
| 4096 | else |
| 4097 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4098 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | return -EINVAL; |
| 4100 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4101 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4103 | if (mddev->level != pers->level) { |
| 4104 | mddev->level = pers->level; |
| 4105 | mddev->new_level = pers->level; |
| 4106 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4107 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4109 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4110 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4111 | /* This personality cannot handle reshaping... */ |
| 4112 | mddev->pers = NULL; |
| 4113 | module_put(pers->owner); |
| 4114 | return -EINVAL; |
| 4115 | } |
| 4116 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4117 | if (pers->sync_request) { |
| 4118 | /* Warn if this is a potentially silly |
| 4119 | * configuration. |
| 4120 | */ |
| 4121 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4122 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4123 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4124 | |
| 4125 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4126 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4127 | if (rdev < rdev2 && |
| 4128 | rdev->bdev->bd_contains == |
| 4129 | rdev2->bdev->bd_contains) { |
| 4130 | printk(KERN_WARNING |
| 4131 | "%s: WARNING: %s appears to be" |
| 4132 | " on the same physical disk as" |
| 4133 | " %s.\n", |
| 4134 | mdname(mddev), |
| 4135 | bdevname(rdev->bdev,b), |
| 4136 | bdevname(rdev2->bdev,b2)); |
| 4137 | warned = 1; |
| 4138 | } |
| 4139 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4140 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4141 | if (warned) |
| 4142 | printk(KERN_WARNING |
| 4143 | "True protection against single-disk" |
| 4144 | " failure might be compromised.\n"); |
| 4145 | } |
| 4146 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4147 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4148 | /* may be over-ridden by personality */ |
| 4149 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4150 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4151 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4152 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4154 | if (start_readonly) |
| 4155 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4156 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4157 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4158 | if (err) |
| 4159 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4160 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4161 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4162 | " but 'external_size' not in effect?\n", __func__); |
| 4163 | printk(KERN_ERR |
| 4164 | "md: invalid array_size %llu > default size %llu\n", |
| 4165 | (unsigned long long)mddev->array_sectors / 2, |
| 4166 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4167 | err = -EINVAL; |
| 4168 | mddev->pers->stop(mddev); |
| 4169 | } |
| 4170 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4171 | err = bitmap_create(mddev); |
| 4172 | if (err) { |
| 4173 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4174 | mdname(mddev), err); |
| 4175 | mddev->pers->stop(mddev); |
| 4176 | } |
| 4177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | module_put(mddev->pers->owner); |
| 4180 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4181 | bitmap_destroy(mddev); |
| 4182 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4184 | if (mddev->pers->sync_request) { |
| 4185 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4186 | printk(KERN_WARNING |
| 4187 | "md: cannot register extra attributes for %s\n", |
| 4188 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4189 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4190 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4191 | mddev->ro = 0; |
| 4192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | atomic_set(&mddev->writes_pending,0); |
| 4194 | mddev->safemode = 0; |
| 4195 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4196 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4197 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4199 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4200 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4201 | if (rdev->raid_disk >= 0) { |
| 4202 | char nm[20]; |
| 4203 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4204 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4205 | printk("md: cannot register %s for %s\n", |
| 4206 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | |
| 4209 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4210 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4211 | if (mddev->flags) |
| 4212 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4214 | set_capacity(disk, mddev->array_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4216 | /* If there is a partially-recovered drive we need to |
| 4217 | * start recovery here. If we leave it to md_check_recovery, |
| 4218 | * it will remove the drives and not do the right thing |
| 4219 | */ |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4220 | if (mddev->degraded && !mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4221 | int spares = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4222 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4223 | if (rdev->raid_disk >= 0 && |
| 4224 | !test_bit(In_sync, &rdev->flags) && |
| 4225 | !test_bit(Faulty, &rdev->flags)) |
| 4226 | /* complete an interrupted recovery */ |
| 4227 | spares++; |
| 4228 | if (spares && mddev->pers->sync_request) { |
| 4229 | mddev->recovery = 0; |
| 4230 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4231 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 4232 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 4233 | "resync"); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4234 | if (!mddev->sync_thread) { |
| 4235 | printk(KERN_ERR "%s: could not start resync" |
| 4236 | " thread...\n", |
| 4237 | mdname(mddev)); |
| 4238 | /* leave the spares where they are, it shouldn't hurt */ |
| 4239 | mddev->recovery = 0; |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4240 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4241 | } |
| 4242 | } |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4243 | md_wakeup_thread(mddev->thread); |
| 4244 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4245 | |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4246 | revalidate_disk(mddev->gendisk); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4247 | mddev->changed = 1; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4248 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4249 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4250 | if (mddev->sysfs_action) |
| 4251 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4252 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4253 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | return 0; |
| 4255 | } |
| 4256 | |
| 4257 | static int restart_array(mddev_t *mddev) |
| 4258 | { |
| 4259 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4261 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4263 | return -ENXIO; |
| 4264 | if (!mddev->pers) |
| 4265 | return -EINVAL; |
| 4266 | if (!mddev->ro) |
| 4267 | return -EBUSY; |
| 4268 | mddev->safemode = 0; |
| 4269 | mddev->ro = 0; |
| 4270 | set_disk_ro(disk, 0); |
| 4271 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4272 | mdname(mddev)); |
| 4273 | /* Kick recovery or resync if necessary */ |
| 4274 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4275 | md_wakeup_thread(mddev->thread); |
| 4276 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4277 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4278 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | } |
| 4280 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4281 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4282 | * to the file ourselves */ |
| 4283 | static int deny_bitmap_write_access(struct file * file) |
| 4284 | { |
| 4285 | struct inode *inode = file->f_mapping->host; |
| 4286 | |
| 4287 | spin_lock(&inode->i_lock); |
| 4288 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4289 | spin_unlock(&inode->i_lock); |
| 4290 | return -ETXTBSY; |
| 4291 | } |
| 4292 | atomic_set(&inode->i_writecount, -1); |
| 4293 | spin_unlock(&inode->i_lock); |
| 4294 | |
| 4295 | return 0; |
| 4296 | } |
| 4297 | |
| 4298 | static void restore_bitmap_write_access(struct file *file) |
| 4299 | { |
| 4300 | struct inode *inode = file->f_mapping->host; |
| 4301 | |
| 4302 | spin_lock(&inode->i_lock); |
| 4303 | atomic_set(&inode->i_writecount, 1); |
| 4304 | spin_unlock(&inode->i_lock); |
| 4305 | } |
| 4306 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4307 | /* mode: |
| 4308 | * 0 - completely stop and dis-assemble array |
| 4309 | * 1 - switch to readonly |
| 4310 | * 2 - stop but do not disassemble array |
| 4311 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4312 | 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] | 4313 | { |
| 4314 | int err = 0; |
| 4315 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4316 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4318 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4319 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4320 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4321 | err = -EBUSY; |
| 4322 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
| 4324 | if (mddev->sync_thread) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4325 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4327 | md_unregister_thread(mddev->sync_thread); |
| 4328 | mddev->sync_thread = NULL; |
| 4329 | } |
| 4330 | |
| 4331 | del_timer_sync(&mddev->safemode_timer); |
| 4332 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4333 | switch(mode) { |
| 4334 | case 1: /* readonly */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | err = -ENXIO; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4336 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | goto out; |
| 4338 | mddev->ro = 1; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4339 | break; |
| 4340 | case 0: /* disassemble */ |
| 4341 | case 2: /* stop */ |
NeilBrown | 6b8b3e8 | 2005-08-04 12:53:35 -0700 | [diff] [blame] | 4342 | bitmap_flush(mddev); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4343 | md_super_wait(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | if (mddev->ro) |
| 4345 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | mddev->pers->stop(mddev); |
NeilBrown | d1b5380 | 2007-02-28 20:11:42 -0800 | [diff] [blame] | 4348 | mddev->queue->merge_bvec_fn = NULL; |
| 4349 | mddev->queue->unplug_fn = NULL; |
NeilBrown | 041ae52 | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4350 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | module_put(mddev->pers->owner); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4352 | if (mddev->pers->sync_request) |
| 4353 | mddev->private = &md_redundancy_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | mddev->pers = NULL; |
Dan Williams | 4f54b0e | 2008-05-23 13:04:37 -0700 | [diff] [blame] | 4355 | /* tell userspace to handle 'inactive' */ |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4356 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4357 | |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4358 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4359 | if (rdev->raid_disk >= 0) { |
| 4360 | char nm[20]; |
| 4361 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4362 | sysfs_remove_link(&mddev->kobj, nm); |
| 4363 | } |
| 4364 | |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4365 | set_capacity(disk, 0); |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 4366 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4368 | if (mddev->ro) |
| 4369 | mddev->ro = 0; |
| 4370 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4371 | if (!mddev->in_sync || mddev->flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | /* mark array as shutdown cleanly */ |
| 4373 | mddev->in_sync = 1; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4374 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4376 | if (mode == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | set_disk_ro(disk, 1); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4378 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4379 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4381 | out: |
| 4382 | mutex_unlock(&mddev->open_mutex); |
| 4383 | if (err) |
| 4384 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | /* |
| 4386 | * Free resources if final stop |
| 4387 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4388 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4391 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4392 | bitmap_destroy(mddev); |
| 4393 | if (mddev->bitmap_file) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4394 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4395 | fput(mddev->bitmap_file); |
| 4396 | mddev->bitmap_file = NULL; |
| 4397 | } |
| 4398 | mddev->bitmap_offset = 0; |
| 4399 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 4400 | /* make sure all md_delayed_delete calls have finished */ |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 4401 | flush_scheduled_work(); |
| 4402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | export_array(mddev); |
| 4404 | |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 4405 | mddev->array_sectors = 0; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4406 | mddev->external_size = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4407 | mddev->dev_sectors = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4408 | mddev->raid_disks = 0; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4409 | mddev->recovery_cp = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4410 | mddev->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4411 | mddev->resync_max = MaxSector; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4412 | mddev->reshape_position = MaxSector; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4413 | mddev->external = 0; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4414 | mddev->persistent = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4415 | mddev->level = LEVEL_NONE; |
| 4416 | mddev->clevel[0] = 0; |
| 4417 | mddev->flags = 0; |
| 4418 | mddev->ro = 0; |
| 4419 | mddev->metadata_type[0] = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4420 | mddev->chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4421 | mddev->ctime = mddev->utime = 0; |
| 4422 | mddev->layout = 0; |
| 4423 | mddev->max_disks = 0; |
| 4424 | mddev->events = 0; |
| 4425 | mddev->delta_disks = 0; |
| 4426 | mddev->new_level = LEVEL_NONE; |
| 4427 | mddev->new_layout = 0; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4428 | mddev->new_chunk_sectors = 0; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 4429 | mddev->curr_resync = 0; |
| 4430 | mddev->resync_mismatches = 0; |
| 4431 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4432 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4433 | mddev->recovery = 0; |
| 4434 | mddev->in_sync = 0; |
| 4435 | mddev->changed = 0; |
| 4436 | mddev->degraded = 0; |
| 4437 | mddev->barriers_work = 0; |
| 4438 | mddev->safemode = 0; |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4439 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4440 | if (mddev->hold_active == UNTIL_STOP) |
| 4441 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4442 | |
NeilBrown | a8a55c3 | 2006-06-26 00:27:39 -0700 | [diff] [blame] | 4443 | } else if (mddev->pers) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | printk(KERN_INFO "md: %s switched to read-only mode.\n", |
| 4445 | mdname(mddev)); |
| 4446 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4447 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4448 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4449 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | return err; |
| 4451 | } |
| 4452 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4453 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | static void autorun_array(mddev_t *mddev) |
| 4455 | { |
| 4456 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4457 | int err; |
| 4458 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4459 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4460 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
| 4462 | printk(KERN_INFO "md: running: "); |
| 4463 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4464 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | char b[BDEVNAME_SIZE]; |
| 4466 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4467 | } |
| 4468 | printk("\n"); |
| 4469 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4470 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | if (err) { |
| 4472 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4473 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | /* |
| 4478 | * lets try to run arrays based on all disks that have arrived |
| 4479 | * until now. (those are in pending_raid_disks) |
| 4480 | * |
| 4481 | * the method: pick the first pending disk, collect all disks with |
| 4482 | * the same UUID, remove all from the pending list and put them into |
| 4483 | * the 'same_array' list. Then order this list based on superblock |
| 4484 | * update time (freshest comes first), kick out 'old' disks and |
| 4485 | * compare superblocks. If everything's fine then run it. |
| 4486 | * |
| 4487 | * If "unit" is allocated, then bump its reference count |
| 4488 | */ |
| 4489 | static void autorun_devices(int part) |
| 4490 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4491 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | mddev_t *mddev; |
| 4493 | char b[BDEVNAME_SIZE]; |
| 4494 | |
| 4495 | printk(KERN_INFO "md: autorun ...\n"); |
| 4496 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4497 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4499 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | rdev0 = list_entry(pending_raid_disks.next, |
| 4501 | mdk_rdev_t, same_set); |
| 4502 | |
| 4503 | printk(KERN_INFO "md: considering %s ...\n", |
| 4504 | bdevname(rdev0->bdev,b)); |
| 4505 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4506 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4508 | printk(KERN_INFO "md: adding %s ...\n", |
| 4509 | bdevname(rdev->bdev,b)); |
| 4510 | list_move(&rdev->same_set, &candidates); |
| 4511 | } |
| 4512 | /* |
| 4513 | * now we have a set of devices, with all of them having |
| 4514 | * mostly sane superblocks. It's time to allocate the |
| 4515 | * mddev. |
| 4516 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4517 | if (part) { |
| 4518 | dev = MKDEV(mdp_major, |
| 4519 | rdev0->preferred_minor << MdpMinorShift); |
| 4520 | unit = MINOR(dev) >> MdpMinorShift; |
| 4521 | } else { |
| 4522 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4523 | unit = MINOR(dev); |
| 4524 | } |
| 4525 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4527 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4528 | break; |
| 4529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
| 4531 | md_probe(dev, NULL, NULL); |
| 4532 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4533 | if (!mddev || !mddev->gendisk) { |
| 4534 | if (mddev) |
| 4535 | mddev_put(mddev); |
| 4536 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | "md: cannot allocate memory for md drive.\n"); |
| 4538 | break; |
| 4539 | } |
| 4540 | if (mddev_lock(mddev)) |
| 4541 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4542 | mdname(mddev)); |
| 4543 | else if (mddev->raid_disks || mddev->major_version |
| 4544 | || !list_empty(&mddev->disks)) { |
| 4545 | printk(KERN_WARNING |
| 4546 | "md: %s already running, cannot run %s\n", |
| 4547 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4548 | mddev_unlock(mddev); |
| 4549 | } else { |
| 4550 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4551 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4552 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | list_del_init(&rdev->same_set); |
| 4554 | if (bind_rdev_to_array(rdev, mddev)) |
| 4555 | export_rdev(rdev); |
| 4556 | } |
| 4557 | autorun_array(mddev); |
| 4558 | mddev_unlock(mddev); |
| 4559 | } |
| 4560 | /* on success, candidates will be empty, on error |
| 4561 | * it won't... |
| 4562 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4563 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4564 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4567 | mddev_put(mddev); |
| 4568 | } |
| 4569 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4570 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4571 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | static int get_version(void __user * arg) |
| 4574 | { |
| 4575 | mdu_version_t ver; |
| 4576 | |
| 4577 | ver.major = MD_MAJOR_VERSION; |
| 4578 | ver.minor = MD_MINOR_VERSION; |
| 4579 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4580 | |
| 4581 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4582 | return -EFAULT; |
| 4583 | |
| 4584 | return 0; |
| 4585 | } |
| 4586 | |
| 4587 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4588 | { |
| 4589 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4590 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4593 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4594 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4596 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | failed++; |
| 4598 | else { |
| 4599 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4600 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4601 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | else |
| 4603 | spare++; |
| 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | info.major_version = mddev->major_version; |
| 4608 | info.minor_version = mddev->minor_version; |
| 4609 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4610 | info.ctime = mddev->ctime; |
| 4611 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4612 | info.size = mddev->dev_sectors / 2; |
| 4613 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4614 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | info.nr_disks = nr; |
| 4616 | info.raid_disks = mddev->raid_disks; |
| 4617 | info.md_minor = mddev->md_minor; |
| 4618 | info.not_persistent= !mddev->persistent; |
| 4619 | |
| 4620 | info.utime = mddev->utime; |
| 4621 | info.state = 0; |
| 4622 | if (mddev->in_sync) |
| 4623 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4624 | if (mddev->bitmap && mddev->bitmap_offset) |
| 4625 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4626 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | info.working_disks = working; |
| 4628 | info.failed_disks = failed; |
| 4629 | info.spare_disks = spare; |
| 4630 | |
| 4631 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4632 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | |
| 4634 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4635 | return -EFAULT; |
| 4636 | |
| 4637 | return 0; |
| 4638 | } |
| 4639 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4640 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4641 | { |
| 4642 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4643 | char *ptr, *buf = NULL; |
| 4644 | int err = -ENOMEM; |
| 4645 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4646 | if (md_allow_write(mddev)) |
| 4647 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4648 | else |
| 4649 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4650 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4651 | if (!file) |
| 4652 | goto out; |
| 4653 | |
| 4654 | /* bitmap disabled, zero the first byte and copy out */ |
| 4655 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4656 | file->pathname[0] = '\0'; |
| 4657 | goto copy_out; |
| 4658 | } |
| 4659 | |
| 4660 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4661 | if (!buf) |
| 4662 | goto out; |
| 4663 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4664 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4665 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4666 | goto out; |
| 4667 | |
| 4668 | strcpy(file->pathname, ptr); |
| 4669 | |
| 4670 | copy_out: |
| 4671 | err = 0; |
| 4672 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4673 | err = -EFAULT; |
| 4674 | out: |
| 4675 | kfree(buf); |
| 4676 | kfree(file); |
| 4677 | return err; |
| 4678 | } |
| 4679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4681 | { |
| 4682 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | mdk_rdev_t *rdev; |
| 4684 | |
| 4685 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4686 | return -EFAULT; |
| 4687 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4688 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | if (rdev) { |
| 4690 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4691 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4692 | info.raid_disk = rdev->raid_disk; |
| 4693 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4694 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4696 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4698 | info.state |= (1<<MD_DISK_SYNC); |
| 4699 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4700 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4701 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | } else { |
| 4703 | info.major = info.minor = 0; |
| 4704 | info.raid_disk = -1; |
| 4705 | info.state = (1<<MD_DISK_REMOVED); |
| 4706 | } |
| 4707 | |
| 4708 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4709 | return -EFAULT; |
| 4710 | |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
| 4714 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 4715 | { |
| 4716 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4717 | mdk_rdev_t *rdev; |
| 4718 | dev_t dev = MKDEV(info->major,info->minor); |
| 4719 | |
| 4720 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 4721 | return -EOVERFLOW; |
| 4722 | |
| 4723 | if (!mddev->raid_disks) { |
| 4724 | int err; |
| 4725 | /* expecting a device which has a superblock */ |
| 4726 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 4727 | if (IS_ERR(rdev)) { |
| 4728 | printk(KERN_WARNING |
| 4729 | "md: md_import_device returned %ld\n", |
| 4730 | PTR_ERR(rdev)); |
| 4731 | return PTR_ERR(rdev); |
| 4732 | } |
| 4733 | if (!list_empty(&mddev->disks)) { |
| 4734 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 4735 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4736 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4738 | if (err < 0) { |
| 4739 | printk(KERN_WARNING |
| 4740 | "md: %s has different UUID to %s\n", |
| 4741 | bdevname(rdev->bdev,b), |
| 4742 | bdevname(rdev0->bdev,b2)); |
| 4743 | export_rdev(rdev); |
| 4744 | return -EINVAL; |
| 4745 | } |
| 4746 | } |
| 4747 | err = bind_rdev_to_array(rdev, mddev); |
| 4748 | if (err) |
| 4749 | export_rdev(rdev); |
| 4750 | return err; |
| 4751 | } |
| 4752 | |
| 4753 | /* |
| 4754 | * add_new_disk can be used once the array is assembled |
| 4755 | * to add "hot spares". They must already have a superblock |
| 4756 | * written |
| 4757 | */ |
| 4758 | if (mddev->pers) { |
| 4759 | int err; |
| 4760 | if (!mddev->pers->hot_add_disk) { |
| 4761 | printk(KERN_WARNING |
| 4762 | "%s: personality does not support diskops!\n", |
| 4763 | mdname(mddev)); |
| 4764 | return -EINVAL; |
| 4765 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 4766 | if (mddev->persistent) |
| 4767 | rdev = md_import_device(dev, mddev->major_version, |
| 4768 | mddev->minor_version); |
| 4769 | else |
| 4770 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | if (IS_ERR(rdev)) { |
| 4772 | printk(KERN_WARNING |
| 4773 | "md: md_import_device returned %ld\n", |
| 4774 | PTR_ERR(rdev)); |
| 4775 | return PTR_ERR(rdev); |
| 4776 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 4777 | /* set save_raid_disk if appropriate */ |
| 4778 | if (!mddev->persistent) { |
| 4779 | if (info->state & (1<<MD_DISK_SYNC) && |
| 4780 | info->raid_disk < mddev->raid_disks) |
| 4781 | rdev->raid_disk = info->raid_disk; |
| 4782 | else |
| 4783 | rdev->raid_disk = -1; |
| 4784 | } else |
| 4785 | super_types[mddev->major_version]. |
| 4786 | validate_super(mddev, rdev); |
| 4787 | rdev->saved_raid_disk = rdev->raid_disk; |
| 4788 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4789 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4790 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4791 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4792 | else |
| 4793 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4795 | rdev->raid_disk = -1; |
| 4796 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 4797 | if (!err && !mddev->pers->hot_remove_disk) { |
| 4798 | /* If there is hot_add_disk but no hot_remove_disk |
| 4799 | * then added disks for geometry changes, |
| 4800 | * and should be added immediately. |
| 4801 | */ |
| 4802 | super_types[mddev->major_version]. |
| 4803 | validate_super(mddev, rdev); |
| 4804 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 4805 | if (err) |
| 4806 | unbind_rdev_from_array(rdev); |
| 4807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | if (err) |
| 4809 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 4810 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4811 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4812 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 4813 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4814 | if (mddev->degraded) |
| 4815 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 4816 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 4817 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | return err; |
| 4819 | } |
| 4820 | |
| 4821 | /* otherwise, add_new_disk is only allowed |
| 4822 | * for major_version==0 superblocks |
| 4823 | */ |
| 4824 | if (mddev->major_version != 0) { |
| 4825 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 4826 | mdname(mddev)); |
| 4827 | return -EINVAL; |
| 4828 | } |
| 4829 | |
| 4830 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 4831 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4832 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | if (IS_ERR(rdev)) { |
| 4834 | printk(KERN_WARNING |
| 4835 | "md: error, md_import_device() returned %ld\n", |
| 4836 | PTR_ERR(rdev)); |
| 4837 | return PTR_ERR(rdev); |
| 4838 | } |
| 4839 | rdev->desc_nr = info->number; |
| 4840 | if (info->raid_disk < mddev->raid_disks) |
| 4841 | rdev->raid_disk = info->raid_disk; |
| 4842 | else |
| 4843 | rdev->raid_disk = -1; |
| 4844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4846 | if (info->state & (1<<MD_DISK_SYNC)) |
| 4847 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4849 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 4850 | set_bit(WriteMostly, &rdev->flags); |
| 4851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | if (!mddev->persistent) { |
| 4853 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4854 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4856 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4857 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4859 | err = bind_rdev_to_array(rdev, mddev); |
| 4860 | if (err) { |
| 4861 | export_rdev(rdev); |
| 4862 | return err; |
| 4863 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | return 0; |
| 4867 | } |
| 4868 | |
| 4869 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 4870 | { |
| 4871 | char b[BDEVNAME_SIZE]; |
| 4872 | mdk_rdev_t *rdev; |
| 4873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | rdev = find_rdev(mddev, dev); |
| 4875 | if (!rdev) |
| 4876 | return -ENXIO; |
| 4877 | |
| 4878 | if (rdev->raid_disk >= 0) |
| 4879 | goto busy; |
| 4880 | |
| 4881 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4882 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4883 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | |
| 4885 | return 0; |
| 4886 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 4887 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4889 | return -EBUSY; |
| 4890 | } |
| 4891 | |
| 4892 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 4893 | { |
| 4894 | char b[BDEVNAME_SIZE]; |
| 4895 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | mdk_rdev_t *rdev; |
| 4897 | |
| 4898 | if (!mddev->pers) |
| 4899 | return -ENODEV; |
| 4900 | |
| 4901 | if (mddev->major_version != 0) { |
| 4902 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 4903 | " version-0 superblocks.\n", |
| 4904 | mdname(mddev)); |
| 4905 | return -EINVAL; |
| 4906 | } |
| 4907 | if (!mddev->pers->hot_add_disk) { |
| 4908 | printk(KERN_WARNING |
| 4909 | "%s: personality does not support diskops!\n", |
| 4910 | mdname(mddev)); |
| 4911 | return -EINVAL; |
| 4912 | } |
| 4913 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4914 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | if (IS_ERR(rdev)) { |
| 4916 | printk(KERN_WARNING |
| 4917 | "md: error, md_import_device() returned %ld\n", |
| 4918 | PTR_ERR(rdev)); |
| 4919 | return -EINVAL; |
| 4920 | } |
| 4921 | |
| 4922 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4923 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4925 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 4927 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4929 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | printk(KERN_WARNING |
| 4931 | "md: can not hot-add faulty %s disk to %s!\n", |
| 4932 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 4933 | err = -EINVAL; |
| 4934 | goto abort_export; |
| 4935 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4936 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 4938 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 4939 | err = bind_rdev_to_array(rdev, mddev); |
| 4940 | if (err) |
| 4941 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | |
| 4943 | /* |
| 4944 | * The rest should better be atomic, we can have disk failures |
| 4945 | * noticed in interrupt contexts ... |
| 4946 | */ |
| 4947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4948 | rdev->raid_disk = -1; |
| 4949 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4950 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | |
| 4952 | /* |
| 4953 | * Kick recovery, maybe this spare has to be added to the |
| 4954 | * array immediately. |
| 4955 | */ |
| 4956 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4957 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4958 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4959 | return 0; |
| 4960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | abort_export: |
| 4962 | export_rdev(rdev); |
| 4963 | return err; |
| 4964 | } |
| 4965 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4966 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 4967 | { |
| 4968 | int err; |
| 4969 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4970 | if (mddev->pers) { |
| 4971 | if (!mddev->pers->quiesce) |
| 4972 | return -EBUSY; |
| 4973 | if (mddev->recovery || mddev->sync_thread) |
| 4974 | return -EBUSY; |
| 4975 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4976 | } |
| 4977 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4978 | |
| 4979 | if (fd >= 0) { |
| 4980 | if (mddev->bitmap) |
| 4981 | return -EEXIST; /* cannot add when bitmap is present */ |
| 4982 | mddev->bitmap_file = fget(fd); |
| 4983 | |
| 4984 | if (mddev->bitmap_file == NULL) { |
| 4985 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 4986 | mdname(mddev)); |
| 4987 | return -EBADF; |
| 4988 | } |
| 4989 | |
| 4990 | err = deny_bitmap_write_access(mddev->bitmap_file); |
| 4991 | if (err) { |
| 4992 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 4993 | mdname(mddev)); |
| 4994 | fput(mddev->bitmap_file); |
| 4995 | mddev->bitmap_file = NULL; |
| 4996 | return err; |
| 4997 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 4998 | mddev->bitmap_offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4999 | } else if (mddev->bitmap == NULL) |
| 5000 | return -ENOENT; /* cannot remove what isn't there */ |
| 5001 | err = 0; |
| 5002 | if (mddev->pers) { |
| 5003 | mddev->pers->quiesce(mddev, 1); |
| 5004 | if (fd >= 0) |
| 5005 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5006 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5007 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5008 | fd = -1; /* make sure to put the file */ |
| 5009 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5010 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5011 | } |
| 5012 | if (fd < 0) { |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5013 | if (mddev->bitmap_file) { |
| 5014 | restore_bitmap_write_access(mddev->bitmap_file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5015 | fput(mddev->bitmap_file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5016 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5017 | mddev->bitmap_file = NULL; |
| 5018 | } |
| 5019 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5020 | return err; |
| 5021 | } |
| 5022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | /* |
| 5024 | * set_array_info is used two different ways |
| 5025 | * The original usage is when creating a new array. |
| 5026 | * In this usage, raid_disks is > 0 and it together with |
| 5027 | * level, size, not_persistent,layout,chunksize determine the |
| 5028 | * shape of the array. |
| 5029 | * This will always create an array with a type-0.90.0 superblock. |
| 5030 | * The newer usage is when assembling an array. |
| 5031 | * In this case raid_disks will be 0, and the major_version field is |
| 5032 | * use to determine which style super-blocks are to be found on the devices. |
| 5033 | * The minor and patch _version numbers are also kept incase the |
| 5034 | * super_block handler wishes to interpret them. |
| 5035 | */ |
| 5036 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5037 | { |
| 5038 | |
| 5039 | if (info->raid_disks == 0) { |
| 5040 | /* just setting version number for superblock loading */ |
| 5041 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5042 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | super_types[info->major_version].name == NULL) { |
| 5044 | /* maybe try to auto-load a module? */ |
| 5045 | printk(KERN_INFO |
| 5046 | "md: superblock version %d not known\n", |
| 5047 | info->major_version); |
| 5048 | return -EINVAL; |
| 5049 | } |
| 5050 | mddev->major_version = info->major_version; |
| 5051 | mddev->minor_version = info->minor_version; |
| 5052 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5053 | mddev->persistent = !info->not_persistent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | return 0; |
| 5055 | } |
| 5056 | mddev->major_version = MD_MAJOR_VERSION; |
| 5057 | mddev->minor_version = MD_MINOR_VERSION; |
| 5058 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5059 | mddev->ctime = get_seconds(); |
| 5060 | |
| 5061 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5062 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5063 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | mddev->raid_disks = info->raid_disks; |
| 5065 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5066 | * openned |
| 5067 | */ |
| 5068 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5069 | mddev->recovery_cp = MaxSector; |
| 5070 | else |
| 5071 | mddev->recovery_cp = 0; |
| 5072 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5073 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | |
| 5075 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5076 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5077 | |
| 5078 | mddev->max_disks = MD_SB_DISKS; |
| 5079 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5080 | if (mddev->persistent) |
| 5081 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5082 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5084 | mddev->default_bitmap_offset = MD_SB_BYTES >> 9; |
| 5085 | mddev->bitmap_offset = 0; |
| 5086 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5087 | mddev->reshape_position = MaxSector; |
| 5088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | /* |
| 5090 | * Generate a 128 bit UUID |
| 5091 | */ |
| 5092 | get_random_bytes(mddev->uuid, 16); |
| 5093 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5094 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5095 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5096 | mddev->new_layout = mddev->layout; |
| 5097 | mddev->delta_disks = 0; |
| 5098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | return 0; |
| 5100 | } |
| 5101 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5102 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5103 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5104 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5105 | |
| 5106 | if (mddev->external_size) |
| 5107 | return; |
| 5108 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5109 | mddev->array_sectors = array_sectors; |
| 5110 | } |
| 5111 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5112 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5113 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5114 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5115 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5116 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5117 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5118 | |
| 5119 | if (mddev->pers->resize == NULL) |
| 5120 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5121 | /* The "num_sectors" is the number of sectors of each device that |
| 5122 | * is used. This can only make sense for arrays with redundancy. |
| 5123 | * linear and raid0 always use whatever space is available. We can only |
| 5124 | * consider changing this number if no resync or reconstruction is |
| 5125 | * 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] | 5126 | * 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] | 5127 | * of each device. If num_sectors is zero, we find the largest size |
| 5128 | * that fits. |
| 5129 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5130 | */ |
| 5131 | if (mddev->sync_thread) |
| 5132 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5133 | if (mddev->bitmap) |
| 5134 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5135 | * grow, and re-add. |
| 5136 | */ |
| 5137 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5138 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5139 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5140 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5141 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5142 | num_sectors = avail; |
| 5143 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5144 | return -ENOSPC; |
| 5145 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5146 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5147 | if (!rv) |
| 5148 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5149 | return rv; |
| 5150 | } |
| 5151 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5152 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5153 | { |
| 5154 | int rv; |
| 5155 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5156 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5157 | return -EINVAL; |
| 5158 | if (raid_disks <= 0 || |
| 5159 | raid_disks >= mddev->max_disks) |
| 5160 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5161 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5162 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5163 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5164 | |
| 5165 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5166 | return rv; |
| 5167 | } |
| 5168 | |
| 5169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | /* |
| 5171 | * update_array_info is used to change the configuration of an |
| 5172 | * on-line array. |
| 5173 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5174 | * fields in the info are checked against the array. |
| 5175 | * Any differences that cannot be handled will cause an error. |
| 5176 | * Normally, only one change can be managed at a time. |
| 5177 | */ |
| 5178 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5179 | { |
| 5180 | int rv = 0; |
| 5181 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5182 | int state = 0; |
| 5183 | |
| 5184 | /* calculate expected state,ignoring low bits */ |
| 5185 | if (mddev->bitmap && mddev->bitmap_offset) |
| 5186 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | |
| 5188 | if (mddev->major_version != info->major_version || |
| 5189 | mddev->minor_version != info->minor_version || |
| 5190 | /* mddev->patch_version != info->patch_version || */ |
| 5191 | mddev->ctime != info->ctime || |
| 5192 | mddev->level != info->level || |
| 5193 | /* mddev->layout != info->layout || */ |
| 5194 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5195 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5196 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5197 | ((state^info->state) & 0xfffffe00) |
| 5198 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | return -EINVAL; |
| 5200 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5201 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5202 | cnt++; |
| 5203 | if (mddev->raid_disks != info->raid_disks) |
| 5204 | cnt++; |
| 5205 | if (mddev->layout != info->layout) |
| 5206 | cnt++; |
| 5207 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5208 | cnt++; |
| 5209 | if (cnt == 0) |
| 5210 | return 0; |
| 5211 | if (cnt > 1) |
| 5212 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | |
| 5214 | if (mddev->layout != info->layout) { |
| 5215 | /* Change layout |
| 5216 | * we don't need to do anything at the md level, the |
| 5217 | * personality will take care of it all. |
| 5218 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5219 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5221 | else { |
| 5222 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5223 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5224 | if (rv) |
| 5225 | mddev->new_layout = mddev->layout; |
| 5226 | return rv; |
| 5227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5229 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5230 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5231 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5232 | if (mddev->raid_disks != info->raid_disks) |
| 5233 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5235 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5236 | if (mddev->pers->quiesce == NULL) |
| 5237 | return -EINVAL; |
| 5238 | if (mddev->recovery || mddev->sync_thread) |
| 5239 | return -EBUSY; |
| 5240 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5241 | /* add the bitmap */ |
| 5242 | if (mddev->bitmap) |
| 5243 | return -EEXIST; |
| 5244 | if (mddev->default_bitmap_offset == 0) |
| 5245 | return -EINVAL; |
| 5246 | mddev->bitmap_offset = mddev->default_bitmap_offset; |
| 5247 | mddev->pers->quiesce(mddev, 1); |
| 5248 | rv = bitmap_create(mddev); |
| 5249 | if (rv) |
| 5250 | bitmap_destroy(mddev); |
| 5251 | mddev->pers->quiesce(mddev, 0); |
| 5252 | } else { |
| 5253 | /* remove the bitmap */ |
| 5254 | if (!mddev->bitmap) |
| 5255 | return -ENOENT; |
| 5256 | if (mddev->bitmap->file) |
| 5257 | return -EINVAL; |
| 5258 | mddev->pers->quiesce(mddev, 1); |
| 5259 | bitmap_destroy(mddev); |
| 5260 | mddev->pers->quiesce(mddev, 0); |
| 5261 | mddev->bitmap_offset = 0; |
| 5262 | } |
| 5263 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5264 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | return rv; |
| 5266 | } |
| 5267 | |
| 5268 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5269 | { |
| 5270 | mdk_rdev_t *rdev; |
| 5271 | |
| 5272 | if (mddev->pers == NULL) |
| 5273 | return -ENODEV; |
| 5274 | |
| 5275 | rdev = find_rdev(mddev, dev); |
| 5276 | if (!rdev) |
| 5277 | return -ENODEV; |
| 5278 | |
| 5279 | md_error(mddev, rdev); |
| 5280 | return 0; |
| 5281 | } |
| 5282 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5283 | /* |
| 5284 | * We have a problem here : there is no easy way to give a CHS |
| 5285 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5286 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5287 | * dosfs just mad... ;-) |
| 5288 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5289 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5290 | { |
| 5291 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5292 | |
| 5293 | geo->heads = 2; |
| 5294 | geo->sectors = 4; |
| 5295 | geo->cylinders = get_capacity(mddev->gendisk) / 8; |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5299 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | unsigned int cmd, unsigned long arg) |
| 5301 | { |
| 5302 | int err = 0; |
| 5303 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5304 | mddev_t *mddev = NULL; |
| 5305 | |
| 5306 | if (!capable(CAP_SYS_ADMIN)) |
| 5307 | return -EACCES; |
| 5308 | |
| 5309 | /* |
| 5310 | * Commands dealing with the RAID driver but not any |
| 5311 | * particular array: |
| 5312 | */ |
| 5313 | switch (cmd) |
| 5314 | { |
| 5315 | case RAID_VERSION: |
| 5316 | err = get_version(argp); |
| 5317 | goto done; |
| 5318 | |
| 5319 | case PRINT_RAID_DEBUG: |
| 5320 | err = 0; |
| 5321 | md_print_devices(); |
| 5322 | goto done; |
| 5323 | |
| 5324 | #ifndef MODULE |
| 5325 | case RAID_AUTORUN: |
| 5326 | err = 0; |
| 5327 | autostart_arrays(arg); |
| 5328 | goto done; |
| 5329 | #endif |
| 5330 | default:; |
| 5331 | } |
| 5332 | |
| 5333 | /* |
| 5334 | * Commands creating/starting a new array: |
| 5335 | */ |
| 5336 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5337 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5338 | |
| 5339 | if (!mddev) { |
| 5340 | BUG(); |
| 5341 | goto abort; |
| 5342 | } |
| 5343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | err = mddev_lock(mddev); |
| 5345 | if (err) { |
| 5346 | printk(KERN_INFO |
| 5347 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5348 | err, cmd); |
| 5349 | goto abort; |
| 5350 | } |
| 5351 | |
| 5352 | switch (cmd) |
| 5353 | { |
| 5354 | case SET_ARRAY_INFO: |
| 5355 | { |
| 5356 | mdu_array_info_t info; |
| 5357 | if (!arg) |
| 5358 | memset(&info, 0, sizeof(info)); |
| 5359 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5360 | err = -EFAULT; |
| 5361 | goto abort_unlock; |
| 5362 | } |
| 5363 | if (mddev->pers) { |
| 5364 | err = update_array_info(mddev, &info); |
| 5365 | if (err) { |
| 5366 | printk(KERN_WARNING "md: couldn't update" |
| 5367 | " array info. %d\n", err); |
| 5368 | goto abort_unlock; |
| 5369 | } |
| 5370 | goto done_unlock; |
| 5371 | } |
| 5372 | if (!list_empty(&mddev->disks)) { |
| 5373 | printk(KERN_WARNING |
| 5374 | "md: array %s already has disks!\n", |
| 5375 | mdname(mddev)); |
| 5376 | err = -EBUSY; |
| 5377 | goto abort_unlock; |
| 5378 | } |
| 5379 | if (mddev->raid_disks) { |
| 5380 | printk(KERN_WARNING |
| 5381 | "md: array %s already initialised!\n", |
| 5382 | mdname(mddev)); |
| 5383 | err = -EBUSY; |
| 5384 | goto abort_unlock; |
| 5385 | } |
| 5386 | err = set_array_info(mddev, &info); |
| 5387 | if (err) { |
| 5388 | printk(KERN_WARNING "md: couldn't set" |
| 5389 | " array info. %d\n", err); |
| 5390 | goto abort_unlock; |
| 5391 | } |
| 5392 | } |
| 5393 | goto done_unlock; |
| 5394 | |
| 5395 | default:; |
| 5396 | } |
| 5397 | |
| 5398 | /* |
| 5399 | * Commands querying/configuring an existing array: |
| 5400 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5401 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5402 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5403 | if ((!mddev->raid_disks && !mddev->external) |
| 5404 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5405 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5406 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | err = -ENODEV; |
| 5408 | goto abort_unlock; |
| 5409 | } |
| 5410 | |
| 5411 | /* |
| 5412 | * Commands even a read-only array can execute: |
| 5413 | */ |
| 5414 | switch (cmd) |
| 5415 | { |
| 5416 | case GET_ARRAY_INFO: |
| 5417 | err = get_array_info(mddev, argp); |
| 5418 | goto done_unlock; |
| 5419 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5420 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5421 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5422 | goto done_unlock; |
| 5423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | case GET_DISK_INFO: |
| 5425 | err = get_disk_info(mddev, argp); |
| 5426 | goto done_unlock; |
| 5427 | |
| 5428 | case RESTART_ARRAY_RW: |
| 5429 | err = restart_array(mddev); |
| 5430 | goto done_unlock; |
| 5431 | |
| 5432 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5433 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | goto done_unlock; |
| 5435 | |
| 5436 | case STOP_ARRAY_RO: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5437 | err = do_md_stop(mddev, 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | goto done_unlock; |
| 5439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | /* |
| 5443 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5444 | * superblock, so we do not allow them on read-only arrays. |
| 5445 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5446 | * here and hit the 'default' below, so only disallow |
| 5447 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5449 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5450 | if (mddev->ro == 2) { |
| 5451 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5452 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5453 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5454 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5455 | } else { |
| 5456 | err = -EROFS; |
| 5457 | goto abort_unlock; |
| 5458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | } |
| 5460 | |
| 5461 | switch (cmd) |
| 5462 | { |
| 5463 | case ADD_NEW_DISK: |
| 5464 | { |
| 5465 | mdu_disk_info_t info; |
| 5466 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5467 | err = -EFAULT; |
| 5468 | else |
| 5469 | err = add_new_disk(mddev, &info); |
| 5470 | goto done_unlock; |
| 5471 | } |
| 5472 | |
| 5473 | case HOT_REMOVE_DISK: |
| 5474 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5475 | goto done_unlock; |
| 5476 | |
| 5477 | case HOT_ADD_DISK: |
| 5478 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5479 | goto done_unlock; |
| 5480 | |
| 5481 | case SET_DISK_FAULTY: |
| 5482 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5483 | goto done_unlock; |
| 5484 | |
| 5485 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5486 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | goto done_unlock; |
| 5488 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5489 | case SET_BITMAP_FILE: |
| 5490 | err = set_bitmap_file(mddev, (int)arg); |
| 5491 | goto done_unlock; |
| 5492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5494 | err = -EINVAL; |
| 5495 | goto abort_unlock; |
| 5496 | } |
| 5497 | |
| 5498 | done_unlock: |
| 5499 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5500 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5501 | err != -EINVAL) |
| 5502 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | mddev_unlock(mddev); |
| 5504 | |
| 5505 | return err; |
| 5506 | done: |
| 5507 | if (err) |
| 5508 | MD_BUG(); |
| 5509 | abort: |
| 5510 | return err; |
| 5511 | } |
| 5512 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5513 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | { |
| 5515 | /* |
| 5516 | * Succeed if we can lock the mddev, which confirms that |
| 5517 | * it isn't being stopped right now. |
| 5518 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5519 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | int err; |
| 5521 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5522 | if (mddev->gendisk != bdev->bd_disk) { |
| 5523 | /* we are racing with mddev_put which is discarding this |
| 5524 | * bd_disk. |
| 5525 | */ |
| 5526 | mddev_put(mddev); |
| 5527 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5528 | flush_scheduled_work(); |
| 5529 | /* Then retry the open from the top */ |
| 5530 | return -ERESTARTSYS; |
| 5531 | } |
| 5532 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5533 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5534 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | goto out; |
| 5536 | |
| 5537 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5538 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5539 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5540 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5541 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | out: |
| 5543 | return err; |
| 5544 | } |
| 5545 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5546 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5548 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5549 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5550 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5551 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | mddev_put(mddev); |
| 5553 | |
| 5554 | return 0; |
| 5555 | } |
| 5556 | |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5557 | static int md_media_changed(struct gendisk *disk) |
| 5558 | { |
| 5559 | mddev_t *mddev = disk->private_data; |
| 5560 | |
| 5561 | return mddev->changed; |
| 5562 | } |
| 5563 | |
| 5564 | static int md_revalidate(struct gendisk *disk) |
| 5565 | { |
| 5566 | mddev_t *mddev = disk->private_data; |
| 5567 | |
| 5568 | mddev->changed = 0; |
| 5569 | return 0; |
| 5570 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5571 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5572 | { |
| 5573 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5574 | .open = md_open, |
| 5575 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5576 | .ioctl = md_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5577 | .getgeo = md_getgeo, |
Linus Torvalds | 44ce629 | 2007-05-09 18:51:36 -0700 | [diff] [blame] | 5578 | .media_changed = md_media_changed, |
| 5579 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | }; |
| 5581 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5582 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | { |
| 5584 | mdk_thread_t *thread = arg; |
| 5585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | /* |
| 5587 | * md_thread is a 'system-thread', it's priority should be very |
| 5588 | * high. We avoid resource deadlocks individually in each |
| 5589 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5590 | * the very same RT priority as kswapd, thus we will never get |
| 5591 | * into a priority inversion deadlock. |
| 5592 | * |
| 5593 | * we definitely have to have equal or higher priority than |
| 5594 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5595 | * many dirty RAID5 blocks. |
| 5596 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5598 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5599 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5601 | /* We need to wait INTERRUPTIBLE so that |
| 5602 | * we don't add to the load-average. |
| 5603 | * That means we need to be sure no signals are |
| 5604 | * pending |
| 5605 | */ |
| 5606 | if (signal_pending(current)) |
| 5607 | flush_signals(current); |
| 5608 | |
| 5609 | wait_event_interruptible_timeout |
| 5610 | (thread->wqueue, |
| 5611 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5612 | || kthread_should_stop(), |
| 5613 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5614 | |
| 5615 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5616 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5617 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | return 0; |
| 5621 | } |
| 5622 | |
| 5623 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5624 | { |
| 5625 | if (thread) { |
| 5626 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5627 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5628 | wake_up(&thread->wqueue); |
| 5629 | } |
| 5630 | } |
| 5631 | |
| 5632 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5633 | const char *name) |
| 5634 | { |
| 5635 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5637 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | if (!thread) |
| 5639 | return NULL; |
| 5640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | init_waitqueue_head(&thread->wqueue); |
| 5642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | thread->run = run; |
| 5644 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5645 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5646 | thread->tsk = kthread_run(md_thread, thread, |
| 5647 | "%s_%s", |
| 5648 | mdname(thread->mddev), |
| 5649 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5650 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | kfree(thread); |
| 5652 | return NULL; |
| 5653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | return thread; |
| 5655 | } |
| 5656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5657 | void md_unregister_thread(mdk_thread_t *thread) |
| 5658 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5659 | if (!thread) |
| 5660 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5661 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5662 | |
| 5663 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | kfree(thread); |
| 5665 | } |
| 5666 | |
| 5667 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 5668 | { |
| 5669 | if (!mddev) { |
| 5670 | MD_BUG(); |
| 5671 | return; |
| 5672 | } |
| 5673 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5674 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5676 | |
| 5677 | if (mddev->external) |
| 5678 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5679 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 5681 | mdname(mddev), |
| 5682 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 5683 | __builtin_return_address(0),__builtin_return_address(1), |
| 5684 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5685 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 5686 | if (!mddev->pers) |
| 5687 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | if (!mddev->pers->error_handler) |
| 5689 | return; |
| 5690 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5691 | if (mddev->degraded) |
| 5692 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5693 | set_bit(StateChanged, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 5695 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5696 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 5697 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | /* seq_file implementation /proc/mdstat */ |
| 5701 | |
| 5702 | static void status_unused(struct seq_file *seq) |
| 5703 | { |
| 5704 | int i = 0; |
| 5705 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | |
| 5707 | seq_printf(seq, "unused devices: "); |
| 5708 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5709 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | char b[BDEVNAME_SIZE]; |
| 5711 | i++; |
| 5712 | seq_printf(seq, "%s ", |
| 5713 | bdevname(rdev->bdev,b)); |
| 5714 | } |
| 5715 | if (!i) |
| 5716 | seq_printf(seq, "<none>"); |
| 5717 | |
| 5718 | seq_printf(seq, "\n"); |
| 5719 | } |
| 5720 | |
| 5721 | |
| 5722 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 5723 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5724 | sector_t max_sectors, resync, res; |
| 5725 | unsigned long dt, db; |
| 5726 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5727 | int scale; |
| 5728 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5730 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
| 5732 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5733 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5735 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | |
| 5737 | /* |
| 5738 | * Should not happen. |
| 5739 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5740 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | MD_BUG(); |
| 5742 | return; |
| 5743 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5744 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5745 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5746 | * u32, as those are the requirements for sector_div. |
| 5747 | * Thus 'scale' must be at least 10 |
| 5748 | */ |
| 5749 | scale = 10; |
| 5750 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5751 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5752 | scale++; |
| 5753 | } |
| 5754 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5755 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5756 | |
| 5757 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5759 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5760 | seq_printf(seq, "["); |
| 5761 | for (i = 0; i < x; i++) |
| 5762 | seq_printf(seq, "="); |
| 5763 | seq_printf(seq, ">"); |
| 5764 | for (i = 0; i < y; i++) |
| 5765 | seq_printf(seq, "."); |
| 5766 | seq_printf(seq, "] "); |
| 5767 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 5768 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5769 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 5770 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 5771 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 5772 | "check" : |
| 5773 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 5774 | "resync" : "recovery"))), |
| 5775 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5776 | (unsigned long long) resync/2, |
| 5777 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | |
| 5779 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | * dt: time from mark until now |
| 5781 | * db: blocks written from mark until now |
| 5782 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5783 | * |
| 5784 | * rt is a sector_t, so could be 32bit or 64bit. |
| 5785 | * So we divide before multiply in case it is 32bit and close |
| 5786 | * to the limit. |
| 5787 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 5788 | * near the end of resync when the number of remaining sectors |
| 5789 | * is close to 'db'. |
| 5790 | * We then divide rt by 32 after multiplying by db to compensate. |
| 5791 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | */ |
| 5793 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 5794 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5795 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 5796 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 5798 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 5799 | sector_div(rt, db/32+1); |
| 5800 | rt *= dt; |
| 5801 | rt >>= 5; |
| 5802 | |
| 5803 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 5804 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 5806 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 5810 | { |
| 5811 | struct list_head *tmp; |
| 5812 | loff_t l = *pos; |
| 5813 | mddev_t *mddev; |
| 5814 | |
| 5815 | if (l >= 0x10000) |
| 5816 | return NULL; |
| 5817 | if (!l--) |
| 5818 | /* header */ |
| 5819 | return (void*)1; |
| 5820 | |
| 5821 | spin_lock(&all_mddevs_lock); |
| 5822 | list_for_each(tmp,&all_mddevs) |
| 5823 | if (!l--) { |
| 5824 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 5825 | mddev_get(mddev); |
| 5826 | spin_unlock(&all_mddevs_lock); |
| 5827 | return mddev; |
| 5828 | } |
| 5829 | spin_unlock(&all_mddevs_lock); |
| 5830 | if (!l--) |
| 5831 | return (void*)2;/* tail */ |
| 5832 | return NULL; |
| 5833 | } |
| 5834 | |
| 5835 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 5836 | { |
| 5837 | struct list_head *tmp; |
| 5838 | mddev_t *next_mddev, *mddev = v; |
| 5839 | |
| 5840 | ++*pos; |
| 5841 | if (v == (void*)2) |
| 5842 | return NULL; |
| 5843 | |
| 5844 | spin_lock(&all_mddevs_lock); |
| 5845 | if (v == (void*)1) |
| 5846 | tmp = all_mddevs.next; |
| 5847 | else |
| 5848 | tmp = mddev->all_mddevs.next; |
| 5849 | if (tmp != &all_mddevs) |
| 5850 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 5851 | else { |
| 5852 | next_mddev = (void*)2; |
| 5853 | *pos = 0x10000; |
| 5854 | } |
| 5855 | spin_unlock(&all_mddevs_lock); |
| 5856 | |
| 5857 | if (v != (void*)1) |
| 5858 | mddev_put(mddev); |
| 5859 | return next_mddev; |
| 5860 | |
| 5861 | } |
| 5862 | |
| 5863 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 5864 | { |
| 5865 | mddev_t *mddev = v; |
| 5866 | |
| 5867 | if (mddev && v != (void*)1 && v != (void*)2) |
| 5868 | mddev_put(mddev); |
| 5869 | } |
| 5870 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5871 | struct mdstat_info { |
| 5872 | int event; |
| 5873 | }; |
| 5874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | static int md_seq_show(struct seq_file *seq, void *v) |
| 5876 | { |
| 5877 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5878 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5880 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5881 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | |
| 5883 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5884 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | seq_printf(seq, "Personalities : "); |
| 5886 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5887 | list_for_each_entry(pers, &pers_list, list) |
| 5888 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | |
| 5890 | spin_unlock(&pers_lock); |
| 5891 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5892 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | return 0; |
| 5894 | } |
| 5895 | if (v == (void*)2) { |
| 5896 | status_unused(seq); |
| 5897 | return 0; |
| 5898 | } |
| 5899 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5900 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 5902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 5904 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 5905 | mddev->pers ? "" : "in"); |
| 5906 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5907 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5909 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 5910 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | seq_printf(seq, " %s", mddev->pers->name); |
| 5912 | } |
| 5913 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5914 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5915 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | char b[BDEVNAME_SIZE]; |
| 5917 | seq_printf(seq, " %s[%d]", |
| 5918 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5919 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5920 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5921 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | seq_printf(seq, "(F)"); |
| 5923 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5924 | } else if (rdev->raid_disk < 0) |
| 5925 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5926 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | if (!list_empty(&mddev->disks)) { |
| 5930 | if (mddev->pers) |
| 5931 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5932 | (unsigned long long) |
| 5933 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | else |
| 5935 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5936 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5938 | if (mddev->persistent) { |
| 5939 | if (mddev->major_version != 0 || |
| 5940 | mddev->minor_version != 90) { |
| 5941 | seq_printf(seq," super %d.%d", |
| 5942 | mddev->major_version, |
| 5943 | mddev->minor_version); |
| 5944 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5945 | } else if (mddev->external) |
| 5946 | seq_printf(seq, " super external:%s", |
| 5947 | mddev->metadata_type); |
| 5948 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 5949 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | |
| 5951 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5952 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5954 | if (mddev->pers->sync_request) { |
| 5955 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5956 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 5957 | seq_printf(seq, "\n "); |
| 5958 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 5959 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 5960 | else if (mddev->recovery_cp < MaxSector) |
| 5961 | seq_printf(seq, "\tresync=PENDING\n "); |
| 5962 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5963 | } else |
| 5964 | seq_printf(seq, "\n "); |
| 5965 | |
| 5966 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5967 | unsigned long chunk_kb; |
| 5968 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5969 | spin_lock_irqsave(&bitmap->lock, flags); |
| 5970 | chunk_kb = bitmap->chunksize >> 10; |
| 5971 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 5972 | "%lu%s chunk", |
| 5973 | bitmap->pages - bitmap->missing_pages, |
| 5974 | bitmap->pages, |
| 5975 | (bitmap->pages - bitmap->missing_pages) |
| 5976 | << (PAGE_SHIFT - 10), |
| 5977 | chunk_kb ? chunk_kb : bitmap->chunksize, |
| 5978 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5979 | if (bitmap->file) { |
| 5980 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 5981 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5982 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 5983 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5984 | seq_printf(seq, "\n"); |
| 5985 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | } |
| 5987 | |
| 5988 | seq_printf(seq, "\n"); |
| 5989 | } |
| 5990 | mddev_unlock(mddev); |
| 5991 | |
| 5992 | return 0; |
| 5993 | } |
| 5994 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 5995 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | .start = md_seq_start, |
| 5997 | .next = md_seq_next, |
| 5998 | .stop = md_seq_stop, |
| 5999 | .show = md_seq_show, |
| 6000 | }; |
| 6001 | |
| 6002 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6003 | { |
| 6004 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6005 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6006 | if (mi == NULL) |
| 6007 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6008 | |
| 6009 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6010 | if (error) |
| 6011 | kfree(mi); |
| 6012 | else { |
| 6013 | struct seq_file *p = file->private_data; |
| 6014 | p->private = mi; |
| 6015 | mi->event = atomic_read(&md_event_count); |
| 6016 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | return error; |
| 6018 | } |
| 6019 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6020 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6021 | { |
| 6022 | struct seq_file *m = filp->private_data; |
| 6023 | struct mdstat_info *mi = m->private; |
| 6024 | int mask; |
| 6025 | |
| 6026 | poll_wait(filp, &md_event_waiters, wait); |
| 6027 | |
| 6028 | /* always allow read */ |
| 6029 | mask = POLLIN | POLLRDNORM; |
| 6030 | |
| 6031 | if (mi->event != atomic_read(&md_event_count)) |
| 6032 | mask |= POLLERR | POLLPRI; |
| 6033 | return mask; |
| 6034 | } |
| 6035 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6036 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6037 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6038 | .open = md_seq_open, |
| 6039 | .read = seq_read, |
| 6040 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6041 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6042 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | }; |
| 6044 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6045 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6046 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6048 | list_add_tail(&p->list, &pers_list); |
| 6049 | 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] | 6050 | spin_unlock(&pers_lock); |
| 6051 | return 0; |
| 6052 | } |
| 6053 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6054 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6056 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6058 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | spin_unlock(&pers_lock); |
| 6060 | return 0; |
| 6061 | } |
| 6062 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6063 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6064 | { |
| 6065 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6067 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | |
| 6069 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6070 | rcu_read_lock(); |
| 6071 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6073 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6074 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6075 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6076 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6077 | * as sync_io is counted when a request starts, and |
| 6078 | * disk_stats is counted when it completes. |
| 6079 | * So resync activity will cause curr_events to be smaller than |
| 6080 | * when there was no such activity. |
| 6081 | * non-sync IO will cause disk_stat to increase without |
| 6082 | * increasing sync_io so curr_events will (eventually) |
| 6083 | * be larger than it was before. Once it becomes |
| 6084 | * substantially larger, the test below will cause |
| 6085 | * the array to appear non-idle, and resync will slow |
| 6086 | * down. |
| 6087 | * If there is a lot of outstanding resync activity when |
| 6088 | * we set last_event to curr_events, then all that activity |
| 6089 | * completing might cause the array to appear non-idle |
| 6090 | * and resync will be slowed down even though there might |
| 6091 | * not have been non-resync activity. This will only |
| 6092 | * happen once though. 'last_events' will soon reflect |
| 6093 | * the state where there is little or no outstanding |
| 6094 | * resync requests, and further resync activity will |
| 6095 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6096 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6098 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | rdev->last_events = curr_events; |
| 6100 | idle = 0; |
| 6101 | } |
| 6102 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6103 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | return idle; |
| 6105 | } |
| 6106 | |
| 6107 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6108 | { |
| 6109 | /* another "blocks" (512byte) blocks have been synced */ |
| 6110 | atomic_sub(blocks, &mddev->recovery_active); |
| 6111 | wake_up(&mddev->recovery_wait); |
| 6112 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6113 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | md_wakeup_thread(mddev->thread); |
| 6115 | // stop recovery, signal do_sync .... |
| 6116 | } |
| 6117 | } |
| 6118 | |
| 6119 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6120 | /* md_write_start(mddev, bi) |
| 6121 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6122 | * in superblock) before writing, schedule a superblock update |
| 6123 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6124 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6125 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6127 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6128 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6129 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6130 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6131 | BUG_ON(mddev->ro == 1); |
| 6132 | if (mddev->ro == 2) { |
| 6133 | /* need to switch to read/write */ |
| 6134 | mddev->ro = 0; |
| 6135 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6136 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6137 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6138 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6139 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6140 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6141 | if (mddev->safemode == 1) |
| 6142 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6143 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6144 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6145 | if (mddev->in_sync) { |
| 6146 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6147 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6148 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6149 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6150 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6151 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6152 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6153 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6154 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6155 | wait_event(mddev->sb_wait, |
| 6156 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6157 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | } |
| 6159 | |
| 6160 | void md_write_end(mddev_t *mddev) |
| 6161 | { |
| 6162 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6163 | if (mddev->safemode == 2) |
| 6164 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6165 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6167 | } |
| 6168 | } |
| 6169 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6170 | /* md_allow_write(mddev) |
| 6171 | * Calling this ensures that the array is marked 'active' so that writes |
| 6172 | * may proceed without blocking. It is important to call this before |
| 6173 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6174 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6175 | * |
| 6176 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6177 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6178 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6179 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6180 | { |
| 6181 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6182 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6183 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6184 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6185 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6186 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6187 | |
| 6188 | spin_lock_irq(&mddev->write_lock); |
| 6189 | if (mddev->in_sync) { |
| 6190 | mddev->in_sync = 0; |
| 6191 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6192 | if (mddev->safemode_delay && |
| 6193 | mddev->safemode == 0) |
| 6194 | mddev->safemode = 1; |
| 6195 | spin_unlock_irq(&mddev->write_lock); |
| 6196 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6197 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6198 | } else |
| 6199 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6200 | |
| 6201 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6202 | return -EAGAIN; |
| 6203 | else |
| 6204 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6205 | } |
| 6206 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | #define SYNC_MARKS 10 |
| 6209 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6210 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | { |
| 6212 | mddev_t *mddev2; |
| 6213 | unsigned int currspeed = 0, |
| 6214 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6215 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | unsigned long mark[SYNC_MARKS]; |
| 6217 | sector_t mark_cnt[SYNC_MARKS]; |
| 6218 | int last_mark,m; |
| 6219 | struct list_head *tmp; |
| 6220 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6221 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6222 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6223 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | |
| 6225 | /* just incase thread restarts... */ |
| 6226 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6227 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6228 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6229 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6231 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6232 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6233 | desc = "data-check"; |
| 6234 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6235 | desc = "requested-resync"; |
| 6236 | else |
| 6237 | desc = "resync"; |
| 6238 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6239 | desc = "reshape"; |
| 6240 | else |
| 6241 | desc = "recovery"; |
| 6242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | /* we overload curr_resync somewhat here. |
| 6244 | * 0 == not engaged in resync at all |
| 6245 | * 2 == checking that there is no conflict with another sync |
| 6246 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6247 | * commense |
| 6248 | * other == active in resync - this many blocks |
| 6249 | * |
| 6250 | * Before starting a resync we must have set curr_resync to |
| 6251 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6252 | * less than ours. When we find one that is the same or higher |
| 6253 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6254 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6255 | * This will mean we have to start checking from the beginning again. |
| 6256 | * |
| 6257 | */ |
| 6258 | |
| 6259 | do { |
| 6260 | mddev->curr_resync = 2; |
| 6261 | |
| 6262 | try_again: |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6263 | if (kthread_should_stop()) { |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6264 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6265 | goto skip; |
| 6266 | } |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6267 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | if (mddev2 == mddev) |
| 6269 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6270 | if (!mddev->parallel_resync |
| 6271 | && mddev2->curr_resync |
| 6272 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6273 | DEFINE_WAIT(wq); |
| 6274 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6275 | /* arbitrarily yield */ |
| 6276 | mddev->curr_resync = 1; |
| 6277 | wake_up(&resync_wait); |
| 6278 | } |
| 6279 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6280 | /* no need to wait here, we can wait the next |
| 6281 | * time 'round when curr_resync == 2 |
| 6282 | */ |
| 6283 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6284 | /* We need to wait 'interruptible' so as not to |
| 6285 | * contribute to the load average, and not to |
| 6286 | * be caught by 'softlockup' |
| 6287 | */ |
| 6288 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6289 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6290 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6291 | printk(KERN_INFO "md: delaying %s of %s" |
| 6292 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6294 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6296 | if (signal_pending(current)) |
| 6297 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6298 | schedule(); |
| 6299 | finish_wait(&resync_wait, &wq); |
| 6300 | goto try_again; |
| 6301 | } |
| 6302 | finish_wait(&resync_wait, &wq); |
| 6303 | } |
| 6304 | } |
| 6305 | } while (mddev->curr_resync < 2); |
| 6306 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6307 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6308 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6310 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | */ |
| 6312 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6313 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6314 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6315 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6316 | j = mddev->resync_min; |
| 6317 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6318 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6319 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6320 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6321 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6322 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6324 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6325 | j = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6326 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6327 | if (rdev->raid_disk >= 0 && |
| 6328 | !test_bit(Faulty, &rdev->flags) && |
| 6329 | !test_bit(In_sync, &rdev->flags) && |
| 6330 | rdev->recovery_offset < j) |
| 6331 | j = rdev->recovery_offset; |
| 6332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6334 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6335 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6336 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6337 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6338 | "(but not more than %d KB/sec) for %s.\n", |
| 6339 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6341 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6342 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6343 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6344 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6345 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6346 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6347 | } |
| 6348 | last_mark = 0; |
| 6349 | mddev->resync_mark = mark[last_mark]; |
| 6350 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6351 | |
| 6352 | /* |
| 6353 | * Tune reconstruction: |
| 6354 | */ |
| 6355 | window = 32*(PAGE_SIZE/512); |
| 6356 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6357 | window/2,(unsigned long long) max_sectors/2); |
| 6358 | |
| 6359 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | last_check = 0; |
| 6361 | |
| 6362 | if (j>2) { |
| 6363 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6364 | "md: resuming %s of %s from checkpoint.\n", |
| 6365 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6366 | mddev->curr_resync = j; |
| 6367 | } |
| 6368 | |
| 6369 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6370 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6372 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6373 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6374 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6375 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6376 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6377 | > (max_sectors >> 4)) || |
| 6378 | (j - mddev->curr_resync_completed)*2 |
| 6379 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6380 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6381 | /* time to update curr_resync_completed */ |
| 6382 | blk_unplug(mddev->queue); |
| 6383 | wait_event(mddev->recovery_wait, |
| 6384 | atomic_read(&mddev->recovery_active) == 0); |
| 6385 | mddev->curr_resync_completed = |
| 6386 | mddev->curr_resync; |
| 6387 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6388 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6389 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6390 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6391 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6392 | /* As this condition is controlled by user-space, |
| 6393 | * we can block indefinitely, so use '_interruptible' |
| 6394 | * to avoid triggering warnings. |
| 6395 | */ |
| 6396 | flush_signals(current); /* just in case */ |
| 6397 | wait_event_interruptible(mddev->recovery_wait, |
| 6398 | mddev->resync_max > j |
| 6399 | || kthread_should_stop()); |
| 6400 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6401 | |
| 6402 | if (kthread_should_stop()) |
| 6403 | goto interrupted; |
| 6404 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6405 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6406 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6407 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6408 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | goto out; |
| 6410 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6411 | |
| 6412 | if (!skipped) { /* actual IO requested */ |
| 6413 | io_sectors += sectors; |
| 6414 | atomic_add(sectors, &mddev->recovery_active); |
| 6415 | } |
| 6416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | j += sectors; |
| 6418 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6419 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6420 | if (last_check == 0) |
| 6421 | /* this is the earliers that rebuilt will be |
| 6422 | * visible in /proc/mdstat |
| 6423 | */ |
| 6424 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6425 | |
| 6426 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | continue; |
| 6428 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6429 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6431 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | break; |
| 6433 | |
| 6434 | repeat: |
| 6435 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6436 | /* step marks */ |
| 6437 | int next = (last_mark+1) % SYNC_MARKS; |
| 6438 | |
| 6439 | mddev->resync_mark = mark[next]; |
| 6440 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6441 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6442 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6443 | last_mark = next; |
| 6444 | } |
| 6445 | |
| 6446 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6447 | if (kthread_should_stop()) |
| 6448 | goto interrupted; |
| 6449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | |
| 6451 | /* |
| 6452 | * this loop exits only if either when we are slower than |
| 6453 | * the 'hard' speed limit, or the system was IO-idle for |
| 6454 | * a jiffy. |
| 6455 | * the system might be non-idle CPU-wise, but we only care |
| 6456 | * about not overloading the IO subsystem. (things like an |
| 6457 | * e2fsck being done on the RAID array should execute fast) |
| 6458 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6459 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | cond_resched(); |
| 6461 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6462 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6463 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6465 | if (currspeed > speed_min(mddev)) { |
| 6466 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6467 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6468 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | goto repeat; |
| 6470 | } |
| 6471 | } |
| 6472 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6473 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6474 | /* |
| 6475 | * this also signals 'finished resyncing' to md_stop |
| 6476 | */ |
| 6477 | out: |
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 | |
| 6480 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6481 | |
| 6482 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6483 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6484 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6485 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6486 | mddev->curr_resync > 2) { |
| 6487 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6488 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6489 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6490 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6491 | "md: checkpointing %s of %s.\n", |
| 6492 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6493 | mddev->recovery_cp = mddev->curr_resync; |
| 6494 | } |
| 6495 | } else |
| 6496 | mddev->recovery_cp = MaxSector; |
| 6497 | } else { |
| 6498 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6499 | mddev->curr_resync = MaxSector; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6500 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6501 | if (rdev->raid_disk >= 0 && |
| 6502 | !test_bit(Faulty, &rdev->flags) && |
| 6503 | !test_bit(In_sync, &rdev->flags) && |
| 6504 | rdev->recovery_offset < mddev->curr_resync) |
| 6505 | rdev->recovery_offset = mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6506 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6507 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6508 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | skip: |
| 6511 | mddev->curr_resync = 0; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6512 | mddev->curr_resync_completed = 0; |
NeilBrown | 24395a8 | 2009-11-06 14:59:27 +1100 | [diff] [blame] | 6513 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6514 | /* We completed so max setting can be forgotten. */ |
| 6515 | mddev->resync_max = MaxSector; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6516 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6517 | wake_up(&resync_wait); |
| 6518 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6519 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6520 | return; |
| 6521 | |
| 6522 | interrupted: |
| 6523 | /* |
| 6524 | * got a signal, exit. |
| 6525 | */ |
| 6526 | printk(KERN_INFO |
| 6527 | "md: md_do_sync() got signal ... exiting\n"); |
| 6528 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6529 | goto out; |
| 6530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6532 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | |
| 6534 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6535 | static int remove_and_add_spares(mddev_t *mddev) |
| 6536 | { |
| 6537 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6538 | int spares = 0; |
| 6539 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6540 | mddev->curr_resync_completed = 0; |
| 6541 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6542 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6543 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6544 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6545 | (test_bit(Faulty, &rdev->flags) || |
| 6546 | ! test_bit(In_sync, &rdev->flags)) && |
| 6547 | atomic_read(&rdev->nr_pending)==0) { |
| 6548 | if (mddev->pers->hot_remove_disk( |
| 6549 | mddev, rdev->raid_disk)==0) { |
| 6550 | char nm[20]; |
| 6551 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6552 | sysfs_remove_link(&mddev->kobj, nm); |
| 6553 | rdev->raid_disk = -1; |
| 6554 | } |
| 6555 | } |
| 6556 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6557 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6558 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6559 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6560 | !test_bit(In_sync, &rdev->flags) && |
| 6561 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6562 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6563 | if (rdev->raid_disk < 0 |
| 6564 | && !test_bit(Faulty, &rdev->flags)) { |
| 6565 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6566 | if (mddev->pers-> |
| 6567 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6568 | char nm[20]; |
| 6569 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6570 | if (sysfs_create_link(&mddev->kobj, |
| 6571 | &rdev->kobj, nm)) |
| 6572 | printk(KERN_WARNING |
| 6573 | "md: cannot register " |
| 6574 | "%s for %s\n", |
| 6575 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6576 | spares++; |
| 6577 | md_new_event(mddev); |
| 6578 | } else |
| 6579 | break; |
| 6580 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6581 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6582 | } |
| 6583 | return spares; |
| 6584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6585 | /* |
| 6586 | * This routine is regularly called by all per-raid-array threads to |
| 6587 | * deal with generic issues like resync and super-block update. |
| 6588 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6589 | * need this as they never do any recovery or update the superblock. |
| 6590 | * |
| 6591 | * It does not do any resync itself, but rather "forks" off other threads |
| 6592 | * to do that as needed. |
| 6593 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6594 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6595 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | * and wakeups up this thread which will reap the thread and finish up. |
| 6597 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6598 | * |
| 6599 | * The overall approach is: |
| 6600 | * 1/ if the superblock needs updating, update it. |
| 6601 | * 2/ If a recovery thread is running, don't do anything else. |
| 6602 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6603 | * 4/ If there are any faulty devices, remove them. |
| 6604 | * 5/ If array is degraded, try to add spares devices |
| 6605 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6606 | */ |
| 6607 | void md_check_recovery(mddev_t *mddev) |
| 6608 | { |
| 6609 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | |
| 6611 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6612 | if (mddev->bitmap) |
| 6613 | bitmap_daemon_work(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | |
| 6615 | if (mddev->ro) |
| 6616 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6617 | |
| 6618 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6619 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6620 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6621 | mdname(mddev)); |
| 6622 | mddev->safemode = 2; |
| 6623 | } |
| 6624 | flush_signals(current); |
| 6625 | } |
| 6626 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6627 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6628 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6629 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6630 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6632 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6633 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6634 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6635 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | )) |
| 6637 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6638 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6639 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6640 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6641 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6642 | if (mddev->ro) { |
| 6643 | /* Only thing we do on a ro array is remove |
| 6644 | * failed devices. |
| 6645 | */ |
| 6646 | remove_and_add_spares(mddev); |
| 6647 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6648 | goto unlock; |
| 6649 | } |
| 6650 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6651 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6652 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6653 | spin_lock_irq(&mddev->write_lock); |
| 6654 | if (mddev->safemode && |
| 6655 | !atomic_read(&mddev->writes_pending) && |
| 6656 | !mddev->in_sync && |
| 6657 | mddev->recovery_cp == MaxSector) { |
| 6658 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6659 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6660 | if (mddev->persistent) |
| 6661 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6662 | } |
| 6663 | if (mddev->safemode == 1) |
| 6664 | mddev->safemode = 0; |
| 6665 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6666 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6667 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6668 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6669 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6670 | if (mddev->flags) |
| 6671 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6672 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6673 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6674 | if (test_and_clear_bit(StateChanged, &rdev->flags)) |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6675 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 6676 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 6679 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 6680 | /* resync/recovery still happening */ |
| 6681 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6682 | goto unlock; |
| 6683 | } |
| 6684 | if (mddev->sync_thread) { |
| 6685 | /* resync has finished, collect result */ |
| 6686 | md_unregister_thread(mddev->sync_thread); |
| 6687 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6688 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 6689 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | /* success...*/ |
| 6691 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 6692 | if (mddev->pers->spare_active(mddev)) |
| 6693 | sysfs_notify(&mddev->kobj, NULL, |
| 6694 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6695 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6696 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6697 | mddev->pers->finish_reshape) |
| 6698 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6699 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6700 | |
| 6701 | /* if array is no-longer degraded, then any saved_raid_disk |
| 6702 | * information must be scrapped |
| 6703 | */ |
| 6704 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6705 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6706 | rdev->saved_raid_disk = -1; |
| 6707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | mddev->recovery = 0; |
| 6709 | /* flag recovery needed just to double check */ |
| 6710 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6711 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6712 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | goto unlock; |
| 6714 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6715 | /* Set RUNNING before clearing NEEDED to avoid |
| 6716 | * any transients in the value of "sync_action". |
| 6717 | */ |
| 6718 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6719 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6720 | /* Clear some bits that don't mean anything, but |
| 6721 | * might be left set |
| 6722 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6723 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6724 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6726 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 6727 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | /* no recovery is running. |
| 6729 | * remove any failed drives, then |
| 6730 | * add spares if possible. |
| 6731 | * Spare are also removed and re-added, to allow |
| 6732 | * the personality to fail the re-add. |
| 6733 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6734 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6735 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6736 | if (mddev->pers->check_reshape == NULL || |
| 6737 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6738 | /* Cannot proceed */ |
| 6739 | goto unlock; |
| 6740 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6741 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6742 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6743 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6744 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 6745 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6746 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6747 | } else if (mddev->recovery_cp < MaxSector) { |
| 6748 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6749 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6750 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 6751 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6754 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 6755 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 6756 | /* We are adding a device or devices to an array |
| 6757 | * which has the bitmap stored on all devices. |
| 6758 | * So make sure all bitmap pages get written |
| 6759 | */ |
| 6760 | bitmap_write_all(mddev->bitmap); |
| 6761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 6763 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6764 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | if (!mddev->sync_thread) { |
| 6766 | printk(KERN_ERR "%s: could not start resync" |
| 6767 | " thread...\n", |
| 6768 | mdname(mddev)); |
| 6769 | /* leave the spares where they are, it shouldn't hurt */ |
| 6770 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6771 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6773 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6774 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6775 | } |
| 6776 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6777 | if (!mddev->sync_thread) { |
| 6778 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6779 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 6780 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 6781 | if (mddev->sysfs_action) |
| 6782 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | mddev_unlock(mddev); |
| 6785 | } |
| 6786 | } |
| 6787 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6788 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 6789 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 6790 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6791 | wait_event_timeout(rdev->blocked_wait, |
| 6792 | !test_bit(Blocked, &rdev->flags), |
| 6793 | msecs_to_jiffies(5000)); |
| 6794 | rdev_dec_pending(rdev, mddev); |
| 6795 | } |
| 6796 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 6797 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6798 | static int md_notify_reboot(struct notifier_block *this, |
| 6799 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6800 | { |
| 6801 | struct list_head *tmp; |
| 6802 | mddev_t *mddev; |
| 6803 | |
| 6804 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 6805 | |
| 6806 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 6807 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6808 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6809 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6810 | /* Force a switch to readonly even array |
| 6811 | * appears to still be in use. Hence |
| 6812 | * the '100'. |
| 6813 | */ |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6814 | do_md_stop(mddev, 1, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 6815 | mddev_unlock(mddev); |
| 6816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | /* |
| 6818 | * certain more exotic SCSI devices are known to be |
| 6819 | * volatile wrt too early system reboots. While the |
| 6820 | * right place to handle this issue is the given |
| 6821 | * driver, we do want to have a safe RAID driver ... |
| 6822 | */ |
| 6823 | mdelay(1000*1); |
| 6824 | } |
| 6825 | return NOTIFY_DONE; |
| 6826 | } |
| 6827 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6828 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6829 | .notifier_call = md_notify_reboot, |
| 6830 | .next = NULL, |
| 6831 | .priority = INT_MAX, /* before any real devices */ |
| 6832 | }; |
| 6833 | |
| 6834 | static void md_geninit(void) |
| 6835 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 6837 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 6838 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6839 | } |
| 6840 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6841 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6843 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | return -1; |
| 6845 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6846 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | return -1; |
| 6848 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6849 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6850 | md_probe, NULL, NULL); |
| 6851 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | md_probe, NULL, NULL); |
| 6853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 6855 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | |
| 6857 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6858 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6859 | } |
| 6860 | |
| 6861 | |
| 6862 | #ifndef MODULE |
| 6863 | |
| 6864 | /* |
| 6865 | * Searches all registered partitions for autorun RAID arrays |
| 6866 | * at boot time. |
| 6867 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6868 | |
| 6869 | static LIST_HEAD(all_detected_devices); |
| 6870 | struct detected_devices_node { |
| 6871 | struct list_head list; |
| 6872 | dev_t dev; |
| 6873 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | |
| 6875 | void md_autodetect_dev(dev_t dev) |
| 6876 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6877 | struct detected_devices_node *node_detected_dev; |
| 6878 | |
| 6879 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 6880 | if (node_detected_dev) { |
| 6881 | node_detected_dev->dev = dev; |
| 6882 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 6883 | } else { |
| 6884 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 6885 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 6886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6887 | } |
| 6888 | |
| 6889 | |
| 6890 | static void autostart_arrays(int part) |
| 6891 | { |
| 6892 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6893 | struct detected_devices_node *node_detected_dev; |
| 6894 | dev_t dev; |
| 6895 | int i_scanned, i_passed; |
| 6896 | |
| 6897 | i_scanned = 0; |
| 6898 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | |
| 6900 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 6901 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6902 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 6903 | i_scanned++; |
| 6904 | node_detected_dev = list_entry(all_detected_devices.next, |
| 6905 | struct detected_devices_node, list); |
| 6906 | list_del(&node_detected_dev->list); |
| 6907 | dev = node_detected_dev->dev; |
| 6908 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 6909 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6910 | if (IS_ERR(rdev)) |
| 6911 | continue; |
| 6912 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6913 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | MD_BUG(); |
| 6915 | continue; |
| 6916 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 6917 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6919 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 6921 | |
| 6922 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 6923 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | |
| 6925 | autorun_devices(part); |
| 6926 | } |
| 6927 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6928 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | |
| 6930 | static __exit void md_exit(void) |
| 6931 | { |
| 6932 | mddev_t *mddev; |
| 6933 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 6934 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6935 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6936 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6938 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | unregister_blkdev(mdp_major, "mdp"); |
| 6940 | unregister_reboot_notifier(&md_notifier); |
| 6941 | unregister_sysctl_table(raid_table_header); |
| 6942 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6943 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6944 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6945 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | } |
| 6947 | } |
| 6948 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 6949 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | module_exit(md_exit) |
| 6951 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6952 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 6953 | { |
| 6954 | return sprintf(buffer, "%d", start_readonly); |
| 6955 | } |
| 6956 | static int set_ro(const char *val, struct kernel_param *kp) |
| 6957 | { |
| 6958 | char *e; |
| 6959 | int num = simple_strtoul(val, &e, 10); |
| 6960 | if (*val && (*e == '\0' || *e == '\n')) { |
| 6961 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 6962 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6963 | } |
| 6964 | return -EINVAL; |
| 6965 | } |
| 6966 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 6967 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 6968 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6969 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6970 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6972 | EXPORT_SYMBOL(register_md_personality); |
| 6973 | EXPORT_SYMBOL(unregister_md_personality); |
| 6974 | EXPORT_SYMBOL(md_error); |
| 6975 | EXPORT_SYMBOL(md_done_sync); |
| 6976 | EXPORT_SYMBOL(md_write_start); |
| 6977 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | EXPORT_SYMBOL(md_register_thread); |
| 6979 | EXPORT_SYMBOL(md_unregister_thread); |
| 6980 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | EXPORT_SYMBOL(md_check_recovery); |
| 6982 | MODULE_LICENSE("GPL"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 6983 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 6984 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |